5 #define EXTRA_BIT_POSITION (PARITY_BITS - 1)
6 #define CODE_BITS (DATA_BITS + PARITY_BITS)
7 #define NUM_DATA_WORDS (1 << DATA_BITS)
9 unsigned char hamming_parity_lookup[256];
12 * Needed since we store all the parity at the end of the word, not at the expected
13 * power-of-two bit positions. This is the inverse of the mapping
14 * (0..15) -> (0, 8, 4, 2, 1, the rest in ascending order)
16 unsigned char permutation_table[CODE_BITS] = {
17 0, 4, 3, 5, 2, 6, 7, 8, 1, 9, 10, 11, 12, 13, 14, 15
20 /* FIXME: check if the lookup table actually helps us any here */
21 unsigned find_parity_32(unsigned data)
24 hamming_parity_lookup[ data & 0xff] ^
25 hamming_parity_lookup[(data >> 8) & 0xff] ^
26 hamming_parity_lookup[(data >> 16) & 0xff] ^
27 hamming_parity_lookup[ data >> 24 ];
30 unsigned generate_parity(unsigned data)
32 unsigned parity1 = find_parity_32(data & 0x036ad555);
33 unsigned parity2 = find_parity_32(data & 0x02d9b333);
34 unsigned parity3 = find_parity_32(data & 0x01c78f0f);
35 unsigned parity4 = find_parity_32(data & 0x003f80ff);
36 unsigned parity5 = find_parity_32(data & 0x00007fff);
37 unsigned parity6 = find_parity_32(data & 0x03b4e986);
39 return parity6 | (parity5 << 1) | (parity4 << 2) | (parity3 << 3) | (parity2 << 4) | (parity1 << 5);
42 unsigned make_codeword(unsigned data)
44 return (data << PARITY_BITS) | generate_parity(data);
47 void generate_lookup()
51 printf("Generating lookup table.\n");
53 for (i = 0; i < 256; ++i) {
54 unsigned parity = (i >> 4) ^ i;
55 parity = (parity >> 2) ^ parity;
56 parity = (parity >> 1) ^ parity;
57 hamming_parity_lookup[i] = parity & 1;
61 /* can detect all single or double bit errors */
62 int has_error(unsigned code)
64 unsigned data = code >> PARITY_BITS;
65 unsigned parity = code & ((1 << PARITY_BITS) - 1);
67 return (generate_parity(data) != parity);
70 int has_double_error(unsigned code)
73 unsigned data = code >> PARITY_BITS;
74 unsigned parity = code & ((1 << PARITY_BITS) - 1);
75 unsigned gen_parity = generate_parity(data);
77 unsigned hamming_parity = parity >> 1;
78 unsigned gen_hamming_parity = gen_parity >> 1;
79 unsigned extra_parity = find_parity_32(code);
81 /* no errors at all (user should have used has_error() first; boo, hiss) */
82 if (hamming_parity == gen_hamming_parity && extra_parity == 1)
85 /* both hamming and simple parity errors; this is a single-bit error */
86 if (hamming_parity != gen_hamming_parity && extra_parity == 0)
89 /* hamming says OK, but simple parity indicates an error => simple parity error is wrong */
90 if (hamming_parity == gen_hamming_parity && extra_parity == 0)
93 /* hamming says error, simple parity says OK => DOUBLE ERROR */
98 /* Correct any single-bit error -- assumes there are no double-bit errors */
99 unsigned correct_single_bit_error(unsigned code)
101 unsigned bits[CODE_BITS];
102 unsigned parity[PARITY_BITS];
105 parity[EXTRA_BIT_POSITION] = 0;
107 for (i = 0; i < CODE_BITS; ++i) {
108 bits[i] = (code & (1 << i)) ? 1 : 0;
110 for (i = 1; i < CODE_BITS; ++i) {
111 parity[EXTRA_BIT_POSITION] ^= bits[i];
114 parity[0] = bits[PARITY_BITS+0] ^ bits[PARITY_BITS+1] ^ bits[PARITY_BITS+3] ^ bits[PARITY_BITS+4] ^ bits[PARITY_BITS+6] ^ bits[PARITY_BITS+8] ^ bits[PARITY_BITS+10];
115 parity[1] = bits[PARITY_BITS+0] ^ bits[PARITY_BITS+2] ^ bits[PARITY_BITS+3] ^ bits[PARITY_BITS+5] ^ bits[PARITY_BITS+6] ^ bits[PARITY_BITS+9] ^ bits[PARITY_BITS+10];
116 parity[2] = bits[PARITY_BITS+1] ^ bits[PARITY_BITS+2] ^ bits[PARITY_BITS+3] ^ bits[PARITY_BITS+7] ^ bits[PARITY_BITS+8] ^ bits[PARITY_BITS+9] ^ bits[PARITY_BITS+10];
117 parity[3] = bits[PARITY_BITS+4] ^ bits[PARITY_BITS+5] ^ bits[PARITY_BITS+6] ^ bits[PARITY_BITS+7] ^ bits[PARITY_BITS+8] ^ bits[PARITY_BITS+9] ^ bits[PARITY_BITS+10];
119 for (i = 0; i < PARITY_BITS - 1; ++i) {
120 if (parity[i] != bits[PARITY_BITS - 1 - i]) {
126 /* flip the wrong bit */
127 code ^= (1 << permutation_table[bp]);
128 parity[EXTRA_BIT_POSITION] ^= 1;
131 /* recompute the lower parity */
132 return (code & ~1) | parity[EXTRA_BIT_POSITION];
136 void check_zero_bit_detection()
139 printf("Checking zero bit detection.\n");
141 for (i = 0; i < NUM_DATA_WORDS; ++i) {
142 unsigned code = make_codeword(i);
143 if (has_error(code)) {
144 printf("ERROR: Failed zero-bit test 1 for %x\n", i);
146 if (has_double_error(code)) {
147 printf("ERROR: Failed zero-bit test 2 for %x\n", i);
152 void check_single_bit_detection()
155 printf("Checking single bit detection and correction.\n");
157 for (i = 0; i < NUM_DATA_WORDS; ++i) {
158 unsigned code = make_codeword(i);
159 for (j = 0; j < CODE_BITS; ++j) {
160 unsigned corrupted_code = code ^ (1 << j);
162 if (!has_error(corrupted_code)) {
163 printf("ERROR: Failed single-bit test 1 for %x with bit %u flipped\n", i, j);
165 if (has_double_error(corrupted_code)) {
166 printf("ERROR: Failed single-bit test 2 for %x with bit %u flipped\n", i, j);
169 if (correct_single_bit_error(corrupted_code) != code) {
170 printf("ERROR: Failed single-bit correction test for %x with bit %u flipped\n", i, j);
177 void check_double_bit_detection()
180 printf("Checking double bit detection.\n");
182 for (i = 0; i < NUM_DATA_WORDS; ++i) {
183 unsigned code = make_codeword(i);
184 for (j = 0; j < CODE_BITS; ++j) {
185 for (k = 0; k < CODE_BITS; ++k) {
186 unsigned corrupted_code = code ^ (1 << j) ^ (1 << k);
190 if (!has_error(corrupted_code)) {
191 printf("ERROR: Failed double-bit test 1 for %x with bit %u and %u flipped\n", i, j, k);
193 if (!has_double_error(corrupted_code)) {
194 printf("ERROR: Failed double-bit test 2 for %x with bit %u and %u flipped\n", i, j, k);
204 check_zero_bit_detection();
205 check_single_bit_detection();
206 check_double_bit_detection();