-
Notifications
You must be signed in to change notification settings - Fork 39
/
hamming_exhaustive_012.c.txt
211 lines (164 loc) · 7.39 KB
/
hamming_exhaustive_012.c.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/* This is similar to hamming.c but the main program does an exhaustive
test, for all 2**32 possible information words, corrupting 0, 1, or 2
bits of either the information bits or the check bits. The number of
39-bit words with 0, 1, or 2 bits set is 39*40/2 + 1 = 781. Hence the
inner loop is done 781*2**32 = 3354*10**9 times). Took 24 hours on
linux61. */
/* This program generates check bits for a Hamming
SEC-DED code with 32 information bits, and simulates
error creation, detection, and correction for 0-, 1-, and
2- bit errors. The errors may be in the data or the check
bits (or both).
Max line length is 57, to fit in hacker.book. */
#include <stdio.h>
#include <time.h>
// ----------------------------- parity --------------------------------
unsigned int parity(unsigned int x) {
x = x ^ (x >> 1);
x = x ^ (x >> 2);
x = x ^ (x >> 4);
x = x ^ (x >> 8);
x = x ^ (x >> 16);
return x & 1;
}
// --------------------------- checkbits -------------------------------
unsigned int checkbits(unsigned int u) {
/* Computes the six parity check bits for the
"information" bits given in the 32-bit word u. The
check bits are p[5:0]. On sending, an overall parity
bit will be prepended to p (by another process).
Bit Checks these bits of u
p[0] 0, 1, 3, 5, ..., 31 (0 and the odd positions).
p[1] 0, 2-3, 6-7, ..., 30-31 (0 and positions xxx1x).
p[2] 0, 4-7, 12-15, 20-23, 28-31 (0 and posns xx1xx).
p[3] 0, 8-15, 24-31 (0 and positions x1xxx).
p[4] 0, 16-31 (0 and positions 1xxxx).
p[5] 1-31 */
unsigned int p0, p1, p2, p3, p4, p5, p6, p;
unsigned int t1, t2, t3;
// First calculate p[5:0] ignoring u[0].
p0 = u ^ (u >> 2);
p0 = p0 ^ (p0 >> 4);
p0 = p0 ^ (p0 >> 8);
p0 = p0 ^ (p0 >> 16); // p0 is in posn 1.
t1 = u ^ (u >> 1);
p1 = t1 ^ (t1 >> 4);
p1 = p1 ^ (p1 >> 8);
p1 = p1 ^ (p1 >> 16); // p1 is in posn 2.
t2 = t1 ^ (t1 >> 2);
p2 = t2 ^ (t2 >> 8);
p2 = p2 ^ (p2 >> 16); // p2 is in posn 4.
t3 = t2 ^ (t2 >> 4);
p3 = t3 ^ (t3 >> 16); // p3 is in posn 8.
p4 = t3 ^ (t3 >> 8); // p4 is in posn 16.
p5 = p4 ^ (p4 >> 16); // p5 is in posn 0.
p = ((p0>>1) & 1) | ((p1>>1) & 2) | ((p2>>2) & 4) |
((p3>>5) & 8) | ((p4>>12) & 16) | ((p5 & 1) << 5);
p = p ^ (-(u & 1) & 0x3F); // Now account for u[0].
return p;
}
// ---------------------------- perturb --------------------------------
int perturb(unsigned int *p, unsigned int *u) {
/* This generates all the possible 39-bit quantities with 0, 1, or 2
bits set, and alters the corresponding 0, 1, or 2 bits of p and u,
treating them as a concatenation of p and u (39 bits long).
The error bit words are generated in the order (illustrated for a
5-bit quantitity):
00011, 00101, 01001, 10001, 00001, 00110, 01010, 10010, 00010,
01100, 10100, 00100, 11000, 01000, 10000, 00000. */
static unsigned long long mask = (1LL << 39) - 1;
static unsigned long long x = 1, y = 2;
unsigned long long errorBits;
int num;
errorBits = x | y;
if (errorBits == 0) num = 0; // Set num = number
else if (x == 0 || y == 0) num = 1; // of 1-bits in
else num = 2; // errorBits.
*u = *u ^ (unsigned)errorBits; // Apply the
*p = *p ^ (errorBits >> 32); // error bits.
if (y != 0) y = (y << 1) & mask;
else if (x != 0) {x = (x << 1) & mask; y = (x << 1) & mask;}
else {x = 1; y = 2;} // If both = 0, start over.
return num;
}
// ---------------------------- correct --------------------------------
int correct(unsigned int pr, unsigned int *ur) {
/* This function looks at the received seven check
bits and 32 information bits (pr and ur), and
determines how many errors occurred (under the
presumption that it must be 0, 1, or 2). It returns
with 0, 1, or 2, meaning that no errors, one error, or
two errors occurred. It corrects the information word
received (ur) if there was one error in it. */
unsigned int po, p, syn, b;
po = parity(pr ^ *ur); // Compute overall parity
// of the received data.
p = checkbits(*ur); // Calculate check bits
// for the received info.
syn = p ^ (pr & 0x3F); // Syndrome (exclusive of
// overall parity bit).
if (po == 0) {
if (syn == 0) return 0; // If no errors, return 0.
else return 2; // Two errors, return 2.
}
// One error occurred.
if (((syn - 1) & syn) == 0) // If syn has zero or one
return 1; // bits set, then the
// error is in the check
// bits or the overall
// parity bit (no
// correction required).
// One error, and syn bits 5:0 tell where it is in ur.
b = syn - 31 - (syn >> 5); // Map syn to range 0 to 31.
// if (syn == 0x1f) b = 0; // (These two lines equiv.
// else b = syn & 0x1f; // to the one line above.)
*ur = *ur ^ (1 << b); // Correct the bit.
return 1;
}
// ------------------------------ main ---------------------------------
int main(int argc, char *argv[]) {
unsigned int us, ur, uc; // Information words, sent, received,
// and corrected.
unsigned int ps, pr; // Check bits sent and received.
int e, c; // Number of errors made and detected.
int i;
clock_t cloc;
time_t start_time;
cloc = clock();
start_time = time(NULL);
printf("----sent---- --received-- corrected\n");
printf("ps us pr ur e c uc\n");
us = 0xFFFFFFFF; // This loops through all
do { // 2**32 values of us
us = us + 1; // (information bits sent).
ps = checkbits(us); // Compute their 6 check bits
ps = ps | (parity(us ^ ps) << 6); // and prepend the overall
// parity bit.
for (i = 0; i < (39*40)/2 + 1; i++) {// For all 0, 1, or 2-bit errors:
/* Corrupt us and/or ps by flipping 0, 1, or 2 bits, producing
ur and pr (information and parity, resp., received). */
ur = us; // Set up the received data.
pr = ps;
e = perturb(&pr, &ur); // Alter 0, 1, or 2 bits of pr and ur,
// and set e = number of bits altered.
uc = ur;
c = correct(pr, &uc); // Correct ur if 1 error occurred.
if ((us & 0xFFFFFF) == 0) {
printf("%02x %08x %02x %08x ", ps, us, pr, ur); // Program
printf("%d %d %08x\n", e, c, uc); // trace.
}
if (e != c) {
printf("Program error, e = %d, c = %d\n", e, c);
return 1;
}
if (e <= 1 && uc != us) {
printf("Program error, e = %d, us = %08x, uc = %08x\n", e, us, uc);
return 1;
}
}
} while (us < 0xFFFFFFFF); // End of us loop.
printf("hamming_exhaustive_012 ended, no errors.\n");
printf("Process time = %4.2f secs\n", (float)(clock() - cloc)/CLOCKS_PER_SEC);
printf("Wall clock time = %d secs\n", (int)difftime(time(NULL), start_time));
return 0;
}