From 01ba004059921d486c13ef6ecb8336f9994db955 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Thu, 2 Mar 2006 14:18:59 +0000 Subject: [PATCH] Rewrite the double-error detection in hamming32. --- hamming32.c | 24 ++---------------------- 1 file changed, 2 insertions(+), 22 deletions(-) diff --git a/hamming32.c b/hamming32.c index 9b8576b..6cbd153 100644 --- a/hamming32.c +++ b/hamming32.c @@ -52,28 +52,8 @@ int has_error(unsigned code) int has_double_error(unsigned code) { - 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 */ -- 2.39.2