]> git.sesse.net Git - hamming/blobdiff - hamming32.c
Be slighly more consistent about temporaries.
[hamming] / hamming32.c
index 2d8b882c7fdefeb8b6adb13ced7ce9918a02b150..fb53e3b2e9dfa48c0f908ba304092f56b44ccc9a 100644 (file)
@@ -44,37 +44,14 @@ unsigned make_codeword(unsigned data)
 /* can detect all single or double bit errors */
 int has_error(unsigned code)
 {
-       unsigned data = code >> PARITY_BITS;
        unsigned parity = code & ((1 << PARITY_BITS) - 1);
-
-       return (generate_parity(data) != parity);
+       return (generate_parity(code >> PARITY_BITS) != parity);
 }
 
 int has_double_error(unsigned code)
 {
-       unsigned i;
-       unsigned data = code >> PARITY_BITS;
-       unsigned parity = code & ((1 << PARITY_BITS) - 1);
-       unsigned gen_parity = generate_parity(data);
-
-       unsigned hamming_parity = parity >> 1;
-       unsigned gen_hamming_parity = gen_parity >> 1;
-       unsigned extra_parity = find_parity_32(code);
-
-       /* no errors at all (user should have used has_error() first; boo, hiss) */
-       if (hamming_parity == gen_hamming_parity && extra_parity == 0)
-               return 0;
-
-       /* both hamming and simple parity errors; this is a single-bit error */
-       if (hamming_parity != gen_hamming_parity && extra_parity == 1)
-               return 0;
-
-       /* hamming says OK, but simple parity indicates an error => simple parity error is wrong */
-       if (hamming_parity == gen_hamming_parity && extra_parity == 1)
-               return 0;
-
-       /* hamming says error, simple parity says OK => DOUBLE ERROR */
-       return 1;
+       unsigned parity_diff = generate_parity(code >> PARITY_BITS) ^ code;
+       return (parity_diff & ((1 << PARITY_BITS) - 1)) && !find_parity_32(code);
 }
 
 /* Correct any single-bit error -- assumes there are no double-bit errors */
@@ -101,10 +78,17 @@ unsigned correct_single_bit_error(unsigned code)
 void check_zero_bit_detection()
 {
        unsigned i;
-       printf("Checking zero bit detection.\n");
+       printf("Checking zero bit detection.");
+       fflush(stdout);
 
        for (i = 0; i < NUM_DATA_WORDS; ++i) {
                unsigned code = make_codeword(i);
+
+               if ((i & 0xfffff) == 0) {
+                       printf(".");
+                       fflush(stdout);
+               }
+               
                if (has_error(code)) {
                        printf("ERROR: Failed zero-bit test 1 for %x\n", i);
                }
@@ -112,15 +96,24 @@ void check_zero_bit_detection()
                        printf("ERROR: Failed zero-bit test 2 for %x\n", i);
                }
        }
+
+       printf("\n");
 }
 
 void check_single_bit_detection()
 {
        unsigned i, j;
-       printf("Checking single bit detection and correction.\n");
+       printf("Checking single bit detection and correction.");
+       fflush(stdout);
 
        for (i = 0; i < NUM_DATA_WORDS; ++i) {
                unsigned code = make_codeword(i);
+
+               if ((i & 0xfffff) == 0) {
+                       printf(".");
+                       fflush(stdout);
+               }
+               
                for (j = 0; j < CODE_BITS; ++j) {
                        unsigned corrupted_code = code ^ (1 << j);
                        
@@ -135,15 +128,24 @@ void check_single_bit_detection()
                        }
                }
        }
+
+       printf("\n");
 }
 
 void check_double_bit_detection()
 {
        unsigned i, j, k;
-       printf("Checking double bit detection.\n");
+       printf("Checking double bit detection.");
+       fflush(stdout);
 
        for (i = 0; i < NUM_DATA_WORDS; ++i) {
                unsigned code = make_codeword(i);
+               
+               if ((i & 0xfffff) == 0) {
+                       printf(".");
+                       fflush(stdout);
+               }
+               
                for (j = 0; j < CODE_BITS; ++j) {
                        for (k = 0; k < CODE_BITS; ++k) {
                                unsigned corrupted_code = code ^ (1 << j) ^ (1 << k);
@@ -159,6 +161,8 @@ void check_double_bit_detection()
                        }
                }
        }
+
+       printf("\n");
 }
 
 int main()