]> git.sesse.net Git - hamming/blob - hamming32.c
Drop the lookup table for hamming32; check_zero_bit_detection() seems to be ~25%...
[hamming] / hamming32.c
1 #include <stdio.h>
2
3 #define DATA_BITS    26
4 #define PARITY_BITS  6
5 #define EXTRA_BIT_POSITION (PARITY_BITS - 1)
6 #define CODE_BITS    (DATA_BITS + PARITY_BITS)
7 #define NUM_DATA_WORDS (1 << DATA_BITS)
8
9 /* 
10  * Needed since we store all the parity at the end of the word, not at the expected
11  * power-of-two bit positions.
12  */
13 unsigned char permutation_table[CODE_BITS] = {
14         0, 5, 4, 31, 3, 30, 29, 28, 2, 27, 26, 25, 24, 23, 22, 21, 1, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6
15 };
16
17 unsigned find_parity_32(unsigned data)
18 {
19         data = (data >> 16) ^ data;
20         data = (data >> 8) ^ data;
21         data = (data >> 4) ^ data;
22         data = (data >> 2) ^ data;
23         data = (data >> 1) ^ data;
24         return (data & 1);
25 }
26
27 unsigned generate_parity(unsigned data)
28 {
29         unsigned parity1 = find_parity_32(data & 0x036ad555);
30         unsigned parity2 = find_parity_32(data & 0x02d9b333);
31         unsigned parity3 = find_parity_32(data & 0x01c78f0f);
32         unsigned parity4 = find_parity_32(data & 0x003f80ff);
33         unsigned parity5 = find_parity_32(data & 0x00007fff);
34         unsigned parity6 = find_parity_32(data & 0x03b4e996);
35                 
36         return parity6 | (parity5 << 1) | (parity4 << 2) | (parity3 << 3) | (parity2 << 4) | (parity1 << 5);
37 }
38
39 unsigned make_codeword(unsigned data)
40 {
41         return (data << PARITY_BITS) | generate_parity(data);
42 }
43
44 /* can detect all single or double bit errors */
45 int has_error(unsigned code)
46 {
47         unsigned data = code >> PARITY_BITS;
48         unsigned parity = code & ((1 << PARITY_BITS) - 1);
49
50         return (generate_parity(data) != parity);
51 }
52
53 int has_double_error(unsigned code)
54 {
55         unsigned i;
56         unsigned data = code >> PARITY_BITS;
57         unsigned parity = code & ((1 << PARITY_BITS) - 1);
58         unsigned gen_parity = generate_parity(data);
59
60         unsigned hamming_parity = parity >> 1;
61         unsigned gen_hamming_parity = gen_parity >> 1;
62         unsigned extra_parity = find_parity_32(code);
63
64         /* no errors at all (user should have used has_error() first; boo, hiss) */
65         if (hamming_parity == gen_hamming_parity && extra_parity == 0)
66                 return 0;
67
68         /* both hamming and simple parity errors; this is a single-bit error */
69         if (hamming_parity != gen_hamming_parity && extra_parity == 1)
70                 return 0;
71
72         /* hamming says OK, but simple parity indicates an error => simple parity error is wrong */
73         if (hamming_parity == gen_hamming_parity && extra_parity == 1)
74                 return 0;
75
76         /* hamming says error, simple parity says OK => DOUBLE ERROR */
77         return 1;
78 }
79
80 /* Correct any single-bit error -- assumes there are no double-bit errors */
81 unsigned correct_single_bit_error(unsigned code)
82 {
83         unsigned parity_diff = generate_parity(code >> PARITY_BITS) ^ code;
84         unsigned bp = 0, i;
85
86         for (i = 0; i < PARITY_BITS - 1; ++i) {
87                 if (parity_diff & (1 << (PARITY_BITS - 1 - i))) {
88                         bp |= (1 << i);
89                 }
90         }
91         
92         if (bp != 0) {
93                 /* flip the wrong bit */
94                 code ^= (1 << permutation_table[bp]);
95         }
96
97         /* recompute the lower parity */
98         return (code & ~1) | find_parity_32(code & ~1);
99 }
100
101 void check_zero_bit_detection()
102 {
103         unsigned i;
104         printf("Checking zero bit detection.\n");
105
106         for (i = 0; i < NUM_DATA_WORDS; ++i) {
107                 unsigned code = make_codeword(i);
108                 if (has_error(code)) {
109                         printf("ERROR: Failed zero-bit test 1 for %x\n", i);
110                 }
111                 if (has_double_error(code)) {
112                         printf("ERROR: Failed zero-bit test 2 for %x\n", i);
113                 }
114         }
115 }
116
117 void check_single_bit_detection()
118 {
119         unsigned i, j;
120         printf("Checking single bit detection and correction.\n");
121
122         for (i = 0; i < NUM_DATA_WORDS; ++i) {
123                 unsigned code = make_codeword(i);
124                 for (j = 0; j < CODE_BITS; ++j) {
125                         unsigned corrupted_code = code ^ (1 << j);
126                         
127                         if (!has_error(corrupted_code)) {
128                                 printf("ERROR: Failed single-bit test 1 for %x with bit %u flipped\n", i, j);
129                         }
130                         if (has_double_error(corrupted_code)) {
131                                 printf("ERROR: Failed single-bit test 2 for %x with bit %u flipped\n", i, j);
132                         }
133                         if (correct_single_bit_error(corrupted_code) != code) {
134                                 printf("ERROR: Failed single-bit correction test for %x with bit %u flipped\n", i, j);
135                         }
136                 }
137         }
138 }
139
140 void check_double_bit_detection()
141 {
142         unsigned i, j, k;
143         printf("Checking double bit detection.\n");
144
145         for (i = 0; i < NUM_DATA_WORDS; ++i) {
146                 unsigned code = make_codeword(i);
147                 for (j = 0; j < CODE_BITS; ++j) {
148                         for (k = 0; k < CODE_BITS; ++k) {
149                                 unsigned corrupted_code = code ^ (1 << j) ^ (1 << k);
150                                 if (j == k)
151                                         continue;
152                                 
153                                 if (!has_error(corrupted_code)) {
154                                         printf("ERROR: Failed double-bit test 1 for %x with bit %u and %u flipped\n", i, j, k);
155                                 }
156                                 if (!has_double_error(corrupted_code)) {
157                                         printf("ERROR: Failed double-bit test 2 for %x with bit %u and %u flipped\n", i, j, k);
158                                 }
159                         }
160                 }
161         }
162 }
163
164 int main()
165 {
166         check_zero_bit_detection();
167         check_single_bit_detection();
168         check_double_bit_detection();
169
170         return 0;
171 }