Debian Codegraph @

basis-library/integer/word.sml

raw
1 (* Copyright (C) 2013,2023 Matthew Fluet.
2 * Copyright (C) 1999-2007 Henry Cejtin, Matthew Fluet, Suresh
3 * Jagannathan, and Stephen Weeks.
4 * Copyright (C) 1997-2000 NEC Research Institute.
5 *
6 * MLton is released under a HPND-style license.
7 * See the file MLton-LICENSE for details.
8 *)
9
10 functor Word (W: PRIM_WORD) : WORD_EXTRA =
11 struct
12
13 open W
14 type t = word
15
16 val wordSize: Int.int = Primitive.Int32.zextdToInt sizeInBits
17 val sizeInBitsWord = Primitive.Word32.zextdToWord sizeInBitsWord
18
19 fun << (i, n) =
20 if Word.>= (n, sizeInBitsWord)
21 then zero
22 else W.<<? (i, Primitive.Word32.zextdFromWord n)
23 fun >> (i, n) =
24 if Word.>= (n, sizeInBitsWord)
25 then zero
26 else W.>>? (i, Primitive.Word32.zextdFromWord n)
27 fun ~>> (i, n) =
28 if Word.< (n, sizeInBitsWord)
29 then W.~>>? (i, Primitive.Word32.zextdFromWord n)
30 else W.~>>? (i, Primitive.Word32.- (W.sizeInBitsWord, 0w1))
31 fun rol (i, n) = W.rolUnsafe (i, Primitive.Word32.zextdFromWord n)
32 fun ror (i, n) = W.rorUnsafe (i, Primitive.Word32.zextdFromWord n)
33
34 local
35 fun st (w, msk, sft) =
36 let
37 val odd = andb (w, msk)
38 val evn = xorb (w, odd)
39 in
40 (xorb (W.<<? (odd, sft), W.>>? (evn, sft)),
41 xorb (msk, W.<<? (msk, Primitive.Word32.>>? (sft, 0w1))),
42 Primitive.Word32.>>? (sft, 0w1))
43 end
44 val (f, sft : Primitive.Word32.t) =
45 case W.sizeInBitsWord of
46 0w8 => (fn x => x, 0w4)
47 | 0w16 => (st, 0w8)
48 | 0w32 => (st o st, 0w16)
49 | 0w64 => (st o st o st, 0w32)
50 | _ => raise (Fail "Word.bswap")
51 in
52 fun bswap w = #1 (f (w, W.<<? (one, sft) - one, sft))
53 end
54
55 val fromInt = W.sextdFromInt
56 val toIntX = W.schckToInt
57 fun toInt w =
58 let
59 val i = W.zchckToInt w
60 in
61 if Primitive.Controls.detectOverflow
62 andalso (case Int.precision of
63 NONE => false
64 | SOME precision =>
65 Int32.<= (precision, W.sizeInBits))
66 andalso Int.< (i, 0)
67 then raise Overflow
68 else i
69 end
70 val fromLargeInt = W.sextdFromLargeInt
71 val toLargeIntX = W.schckToLargeInt
72 fun toLargeInt w =
73 let
74 val i = W.zchckToLargeInt w
75 in
76 if Primitive.Controls.detectOverflow
77 andalso (case LargeInt.precision of
78 NONE => false
79 | SOME precision =>
80 Int32.<= (precision, W.sizeInBits))
81 andalso LargeInt.< (i, 0)
82 then raise Overflow
83 else i
84 end
85
86 val fromLargeWord = W.zextdFromLargeWord
87 val fromLarge = fromLargeWord
88 val toLargeWordX = W.sextdToLargeWord
89 val toLargeX = toLargeWordX
90 val toLargeWord = W.zextdToLargeWord
91 val toLarge = toLargeWord
92
93 val fromWord = W.zextdFromWord
94 val toWordX = W.sextdToWord
95 val toWord = W.zextdToWord
96
97 local
98 (* Allocate a buffer large enough to hold any formatted word in any radix.
99 * The most that will be required is for maxWord in binary.
100 *)
101 val maxNumDigits = wordSize
102 val oneBuf = One.make (fn () => CharArray.array (maxNumDigits, #"\000"))
103 in
104 fun fmt radix (w: word): string =
105 One.use
106 (oneBuf, fn buf =>
107 let
108 val radix = fromInt (StringCvt.radixToInt radix)
109 fun loop (q, i: Int.int) =
110 let
111 val _ =
112 CharArray.update
113 (buf, i, StringCvt.digitToChar (toInt (q mod radix)))
114 val q = q div radix
115 in
116 if q = zero
117 then CharArraySlice.vector
118 (CharArraySlice.slice (buf, i, NONE))
119 else loop (q, Int.- (i, 1))
120 end
121 in
122 loop (w, Int.- (maxNumDigits, 1))
123 end)
124 end
125
126 (*
127 fun fmt radix (w: word): string =
128 let
129 val radix = fromInt (StringCvt.radixToInt radix)
130 fun loop (q, chars) =
131 let
132 val chars = StringCvt.digitToChar (toInt (q mod radix)) :: chars
133 val q = q div radix
134 in
135 if q = zero
136 then String.implode chars
137 else loop (q, chars)
138 end
139 in
140 loop (w, [])
141 end
142 *)
143
144 val toString = fmt StringCvt.HEX
145
146 fun scan radix reader state =
147 let
148 val state0 = StringCvt.skipWS reader state
149 val charToDigit = StringCvt.charToDigit radix
150 val radixWord = fromInt (StringCvt.radixToInt radix)
151 fun finishNum (state, n) =
152 case reader state of
153 NONE => SOME (n, state)
154 | SOME (c, state') =>
155 case charToDigit c of
156 NONE => SOME (n, state)
157 | SOME n' =>
158 let
159 val n'' = n * radixWord
160 in
161 if n'' div radixWord = n
162 then let
163 val n' = fromInt n'
164 val n''' = n'' + n'
165 in
166 if n''' >= n''
167 then finishNum (state', n''')
168 else raise Overflow
169 end
170 else raise Overflow
171 end
172 fun startNum state =
173 case reader state of
174 NONE => NONE
175 | SOME (c, state') =>
176 case charToDigit c of
177 NONE => NONE
178 | SOME n => finishNum (state', fromInt n)
179 fun startNumOr (state, res) =
180 case startNum state of
181 NONE => res
182 | SOME (w, s) => SOME (w, s)
183 fun startNumWith (c, state) =
184 case charToDigit c of
185 NONE => NONE
186 | SOME n => finishNum (state, fromInt n)
187 fun startNumWithOr ((c, state), res) =
188 case startNumWith (c, state) of
189 NONE => res
190 | SOME (w, s) => SOME (w, s)
191
192 fun num state = finishNum (state, zero)
193 in
194 case reader state0 of
195 NONE => NONE
196 | SOME (c, state1) =>
197 case c of
198 #"0" =>
199 let
200 val zero = SOME (zero, state1)
201 in
202 case reader state1 of
203 NONE => zero
204 | SOME (c, state2) =>
205 (case c of
206 #"w" => (case radix of
207 StringCvt.HEX =>
208 (case reader state2 of
209 NONE =>
210 (* the #"w" was not followed by
211 * an #"X" or #"x", therefore we
212 * return 0 *)
213 zero
214 | SOME (c, state3) =>
215 (case c of
216 #"x" => startNumOr (state3, zero)
217 | #"X" => startNumOr (state3, zero)
218 | _ =>
219 (* the #"w" was not followed by
220 * an #"X" or #"x", therefore we
221 * return 0 *)
222 zero))
223 | _ => startNumOr (state2, zero))
224 | #"x" => (case radix of
225 StringCvt.HEX => startNumOr (state2, zero)
226 | _ => zero)
227 | #"X" => (case radix of
228 StringCvt.HEX => startNumOr (state2, zero)
229 | _ => zero)
230 | _ => startNumWithOr ((c, state2), zero))
231 end
232 | _ => startNumWith (c, state1)
233 end
234
235 val fromString = StringCvt.scanString (scan StringCvt.HEX)
236
237 end
238
239 structure Word8 = Word (Primitive.Word8)
240 structure Word16 = Word (Primitive.Word16)
241 structure Word32 = Word (Primitive.Word32)
242 structure Word64 = Word (Primitive.Word64)