#define CODE_BITS (DATA_BITS + PARITY_BITS)
#define NUM_DATA_WORDS (1 << DATA_BITS)
-unsigned char hamming_parity_lookup[256];
-
/*
* Needed since we store all the parity at the end of the word, not at the expected
* power-of-two bit positions.
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
};
-/* FIXME: check if the lookup table actually helps us any here */
unsigned find_parity_32(unsigned data)
{
- return
- hamming_parity_lookup[ data & 0xff] ^
- hamming_parity_lookup[(data >> 8) & 0xff] ^
- hamming_parity_lookup[(data >> 16) & 0xff] ^
- hamming_parity_lookup[ data >> 24 ];
+ data = (data >> 16) ^ data;
+ data = (data >> 8) ^ data;
+ data = (data >> 4) ^ data;
+ data = (data >> 2) ^ data;
+ data = (data >> 1) ^ data;
+ return (data & 1);
}
unsigned generate_parity(unsigned data)
return (data << PARITY_BITS) | generate_parity(data);
}
-void generate_lookup()
-{
- unsigned i;
-
- printf("Generating lookup table.\n");
-
- for (i = 0; i < 256; ++i) {
- unsigned parity = (i >> 4) ^ i;
- parity = (parity >> 2) ^ parity;
- parity = (parity >> 1) ^ parity;
- hamming_parity_lookup[i] = parity & 1;
- }
-}
-
/* can detect all single or double bit errors */
int has_error(unsigned code)
{
int main()
{
- generate_lookup();
check_zero_bit_detection();
check_single_bit_detection();
check_double_bit_detection();