detect (but don't correct two-bit errors in a Word16
This commit is contained in:
parent
ed405e366e
commit
74b26de743
|
@ -64,8 +64,9 @@ instance Hamming Word16 where
|
|||
]
|
||||
|
||||
correctErrors c
|
||||
| isValid c = Just c
|
||||
| otherwise = let
|
||||
| isValid c = Just c
|
||||
| not (oddParity c) = Nothing
|
||||
| otherwise = let
|
||||
errorFlags = map (oddParity . (c .&.))
|
||||
[0xaaaa, 0xcccc, 0xf0f0, 0xff00]
|
||||
flipAddr = foldl
|
||||
|
|
|
@ -21,7 +21,7 @@ License along with this program. If not, see
|
|||
|
||||
module Hamming.Word16Spec (spec) where
|
||||
|
||||
import Data.Bits (shiftL, xor)
|
||||
import Data.Bits (shiftL, xor, (.|.))
|
||||
import Data.Word (Word16)
|
||||
|
||||
import Hamming
|
||||
|
@ -68,7 +68,7 @@ correctErrorsSpec = describe "correctErrors" $ mapM_
|
|||
in it ("should be " ++ show expected) $
|
||||
actual `shouldBe` expected
|
||||
) $ ("valid", withChkBits, Just withChkBits) :
|
||||
singleBitErrors
|
||||
singleBitErrors ++ doubleBitErrors
|
||||
|
||||
singleBitErrors :: [(String, Word16, Maybe Word16)]
|
||||
singleBitErrors = map
|
||||
|
@ -78,6 +78,22 @@ singleBitErrors = map
|
|||
in ("bad bit " ++ show bit, code, Just withChkBits)
|
||||
) [0..15]
|
||||
|
||||
doubleBitErrors :: [(String, Word16, Maybe Word16)]
|
||||
doubleBitErrors =
|
||||
[ ( \(bitA, bitB) -> let
|
||||
mask = (1 `shiftL` bitA) .|. (1 `shiftL` bitB)
|
||||
code = withChkBits `xor` mask
|
||||
in
|
||||
( "bad bits " ++ show bitA ++ " & " ++ show bitB
|
||||
, code
|
||||
, Nothing
|
||||
)
|
||||
) (a, b)
|
||||
| a <- [0..14]
|
||||
, b <- [1..15]
|
||||
, a < b
|
||||
]
|
||||
|
||||
noChkBits :: Word16
|
||||
noChkBits = 0x34c0
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user