This repository was archived by the owner on Nov 1, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathWord.has
129 lines (106 loc) · 3.95 KB
/
Word.has
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
module Word(Word, Short, Byte, wordToShorts, wordToBytes, bytesToString, Bits(..),
wordToInt, shortToInt, byteToInt) where
import LMLbitops
#define AND {-:"Pand":-}
#define OR {-:"Por":-}
#define XOR {-:"Pxor":-}
#define COMPL {-:"Pcompl":-}
#define LSH {-:"Plsh":-}
#define RSH {-:"Prsh":-}
infixl 8 `bitLsh`, `bitRsh`
infixl 7 `bitAnd`
infixl 6 `bitXor`
infixl 5 `bitOr`
class Bits a where
bitAnd, bitOr, bitXor :: a -> a -> a
bitCompl :: a -> a
bitRsh, bitLsh :: a -> Int -> a
bitSwap :: a -> a
bit0 :: a
bitSize :: a -> Int
data Word = Word Int {-# STRICT #-} deriving (Eq, Ord)
instance Bits Word where
bitAnd (Word x) (Word y) = Word (AND x y)
bitOr (Word x) (Word y) = Word (OR x y)
bitXor (Word x) (Word y) = Word (XOR x y)
bitCompl (Word x) = Word (COMPL x)
bitLsh (Word x) y = Word (LSH x y)
bitRsh (Word x) y = Word (RSH x y)
bitSwap (Word x) = Word (OR (LSH x 16) (AND (RSH x 16) 65535))
bit0 = Word 1
bitSize (Word _) = 32
instance Num Word where
Word x + Word y = Word (x+y)
Word x - Word y = Word (x-y)
Word x * Word y = Word (x*y)
negate (Word x) = Word (negate x)
fromInteger x = Word (fromInteger x)
fromInt x = Word x
instance Text Word where
showsPrec _ (Word w) =
let i = toInteger w + (if w < 0 then 2*(toInteger maxInt + 1) else 0)
in showString (conv 8 i)
showsType _ = showString "Word"
conv :: Int -> Integer -> String
conv 0 _ = ""
conv n i = conv (n-1) q ++ ["0123456789ABCDEF"!!r] where (q, r) = quotRem i 16
data Short = Short Int {-# STRICT #-} deriving (Eq, Ord)
#define SHORTMASK(x) (AND (x) 65535)
instance Bits Short where
bitAnd (Short x) (Short y) = Short (AND x y)
bitOr (Short x) (Short y) = Short (OR x y)
bitXor (Short x) (Short y) = Short (XOR x y)
bitCompl (Short x) = Short (SHORTMASK (COMPL x))
bitLsh (Short x) y = Short (SHORTMASK (LSH x y))
bitRsh (Short x) y = Short (RSH x y)
bitSwap (Short x) = Short (SHORTMASK(OR (LSH x 8) (AND (RSH x 8) 255)))
bit0 = Short 1
bitSize (Short _) = 16
instance Num Short where
Short x + Short y = Short (SHORTMASK(x+y))
Short x - Short y = Short (SHORTMASK(x-y))
Short x * Short y = Short (SHORTMASK(x*y))
negate (Short x) = Short (SHORTMASK(negate x))
fromInteger x = Short (SHORTMASK(fromInteger x))
fromInt x = Short (SHORTMASK(x))
instance Text Short where
showsPrec _ (Short w) =
let i = toInteger w
in showString (conv 4 i)
showsType _ = showString "Short"
data Byte = Byte Int {-# STRICT #-} deriving (Eq, Ord)
#define BYTEMASK(x) (AND (x) 255)
instance Bits Byte where
bitAnd (Byte x) (Byte y) = Byte (AND x y)
bitOr (Byte x) (Byte y) = Byte (OR x y)
bitXor (Byte x) (Byte y) = Byte (XOR x y)
bitCompl (Byte x) = Byte (BYTEMASK (COMPL x))
bitLsh (Byte x) y = Byte (BYTEMASK (LSH x y))
bitRsh (Byte x) y = Byte (RSH x y)
bitSwap (Byte x) = Byte (BYTEMASK(OR (LSH x 4) (AND (RSH x 8) 15)))
bit0 = Byte 1
bitSize (Byte _) = 8
instance Num Byte where
Byte x + Byte y = Byte (BYTEMASK(x+y))
Byte x - Byte y = Byte (BYTEMASK(x-y))
Byte x * Byte y = Byte (BYTEMASK(x*y))
negate (Byte x) = Byte (BYTEMASK(negate x))
fromInteger x = Byte (BYTEMASK(fromInteger x))
fromInt x = Byte (BYTEMASK(x))
instance Text Byte where
showsPrec _ (Byte w) =
let i = toInteger w
in showString (conv 2 i)
showsType _ = showString "Byte"
wordToShorts (Word w) = [Short (SHORTMASK(RSH w 16)), Short (SHORTMASK(w))]
wordToBytes (Word w) = [Byte (BYTEMASK(RSH w 24)), Byte (BYTEMASK(RSH w 16)), Byte (BYTEMASK(RSH w 8)), Byte (BYTEMASK(w))]
bytesToString :: [Byte] -> String
bytesToString bs = map (\(Byte b) -> chr b) bs
stringToBytes :: String -> [Byte]
stringToBytes cs = map (\c -> Byte (BYTEMASK(ord c))) cs
wordToInt :: Word -> Int
wordToInt (Word w) = w
shortToInt :: Short -> Int
shortToInt (Short w) = w
byteToInt :: Byte -> Int
byteToInt (Byte w) = w