-
Notifications
You must be signed in to change notification settings - Fork 6
/
check-logic.hs
55 lines (36 loc) · 1 KB
/
check-logic.hs
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
import Data.Bits
import Numeric
main :: IO ()
main = mapM_ printResult tests
type LogicTest = (Integer, Integer, String)
tests :: [ LogicTest ]
tests =
[ ( 0, 1, ".|." )
, ( 0, -1, ".|." )
, ( -1, 1, ".|." )
, ( -1, -1, ".|." )
, ( -1 * 0xa, 0xf, ".|." )
, ( 0xf, -1 * 0xa, ".|." )
, ( 0x3ff002, -1 * 0x3ff802, ".|." )
, ( -1, -2, ".|." )
, ( 0, 10, ".|." )
, ( 0, -10, ".|." )
, ( 3, 1, ".&." )
, ( 3, -1, ".&." )
, ( -1, 3, ".&." )
, ( 3, 1, ".^." )
, ( 3, -1, ".^." )
, ( -1, 3, ".^." )
]
printResult :: LogicTest -> IO ()
printResult (a, b, op) =
putStrLn $ hexShow a ++ " " ++ op ++ " " ++ hexShow b ++ " -> " ++ hexShow ((lookupOp op) a b)
hexShow :: Integer -> String
hexShow i
| i < 0 = "-0x" ++ showHex (abs i) ""
| otherwise = "+0x" ++ showHex i ""
lookupOp :: String -> Integer -> Integer -> Integer
lookupOp ".|." = (.|.)
lookupOp ".&." = (.&.)
lookupOp ".^." = xor
lookupOp x = error $ "lookupOp '" ++ x ++ "'."