1 (* Copyright (C
) 1999-2007 Henry Cejtin
, Matthew Fluet
, Suresh
2 * Jagannathan
, and Stephen Weeks
.
3 * Copyright (C
) 1997-2000 NEC Research Institute
.
5 * MLton is released under a BSD
-style license
.
6 * See the file MLton
-LICENSE for details
.
11 structure Int: PRIM_INTEGER
12 structure Word: PRIM_WORD
13 val idFromIntToWord
: Int.int -> Word.word
14 val idFromWordToInt
: Word.word -> Int.int
16 signature PRIM_INTEGER
=
22 val maxInt
: int option
23 val minInt
: int option
29 val div: int * int -> int
30 val mod: int * int -> int
31 val quot
: int * int -> int
32 val rem
: int * int -> int
34 val ltu
: int * int -> bool
35 val leu
: int * int -> bool
36 val gtu
: int * int -> bool
37 val geu
: int * int -> bool
39 val andb
: int * int -> int
40 val <<?
: int * Primitive
.Word32
.word -> int
42 val orb
: int * int -> int
43 val rolUnsafe
: int * Primitive
.Word32
.word -> int
44 val rorUnsafe
: int * Primitive
.Word32
.word -> int
45 val ~
>>?
: int * Primitive
.Word32
.word -> int
46 val >>?
: int * Primitive
.Word32
.word -> int
47 val xorb
: int * int -> int
49 val power
: {base
:int, exp
: int} -> int
50 val log2
: int -> Primitive
.Int32
.int
51 val log2Word
: int -> Primitive
.Word32
.word
62 val div: word * word -> word
63 val mod: word * word -> word
65 val log2
: word -> Primitive
.Int32
.int
66 val log2Word
: word -> Primitive
.Word32
.word
69 functor MkNum0 (S
: MKNUM0_ARG
): sig
70 structure Int: PRIM_INTEGER
71 structure Word: PRIM_WORD
77 if Int.sizeInBits
<> Word.sizeInBits
78 orelse Int.sizeInBitsWord
<> Word.sizeInBitsWord
79 then raise Primitive
.Exn
.Fail8
"MkNum0: Int.sizeInBits <> Word.sizeInBits"
86 val zero
= zextdFromWord32
0w0
87 val one
= zextdFromWord32
0w1
89 val maxWord
' = notb zero
93 if Primitive
.Controls
.safe
andalso w
' = zero
97 val op div = make (op quotUnsafe
)
98 val op mod = make (op remUnsafe
)
103 fun loop (n
, s
, acc
) =
109 then (>>?
(n
, s
), Primitive
.Word32
.+ (acc
, s
))
112 loop (n
, Primitive
.Word32
.>>?
(s
, 0w1
), acc
)
115 if Primitive
.Controls
.safe
andalso w
= zero
117 else loop (w
, Primitive
.Word32
.>>?
(sizeInBitsWord
, 0w1
), 0w0
)
119 fun log2 w
= Primitive
.IntWordConv
.zextdFromWord32ToInt32 (log2Word w
)
126 val zero
= zextdFromInt32
0
127 val one
= zextdFromInt32
1
131 fn (x
: int, y
: int) =>
133 (f (idFromIntToWord x
, idFromIntToWord y
))
137 (f (idFromIntToWord x
))
139 fn (x
: int, w
: Primitive
.Word32
.word) =>
141 (f (idFromIntToWord x
, w
))
143 val andb
= makeBinop
Word.andb
144 val <<?
= makeShop
Word.<<?
145 val notb
= makeUnop
Word.notb
146 val orb
= makeBinop
Word.orb
147 val rolUnsafe
= makeShop
Word.rolUnsafe
148 val rorUnsafe
= makeShop
Word.rorUnsafe
149 val ~
>>?
= makeShop
Word.~
>>?
150 val >>?
= makeShop
Word.>>?
151 val xorb
= makeBinop
Word.xorb
153 fun log2 i
= Word.log2 (idFromIntToWord i
)
154 fun log2Word i
= Word.log2Word (idFromIntToWord i
)
156 val minInt
' = <<?
(one
, Primitive
.Word32
.- (sizeInBitsWord
, 0w1
))
157 val maxInt
' = >>?
(notb zero
, 0w1
)
158 val minInt
= SOME minInt
'
159 val maxInt
= SOME maxInt
'
161 fun abs (x
: int) = if x
< zero
then ~ x
else x
164 if Primitive
.Controls
.safe
167 else if (Primitive
.Controls
.detectOverflow
168 orelse Primitive
.Controls
.safe
)
169 andalso x
= minInt
' andalso y
= ~one
170 then if Primitive
.Controls
.detectOverflow
173 else quotUnsafe (x
, y
)
176 if Primitive
.Controls
.safe
179 else if x
= minInt
' andalso y
= ~one
181 else remUnsafe (x
, y
)
186 then quotUnsafe (x
, y
)
190 else quotUnsafe (x
-? one
, y
) -? one
193 then if (Primitive
.Controls
.detectOverflow
194 orelse Primitive
.Controls
.safe
)
195 andalso x
= minInt
' andalso y
= ~one
196 then if Primitive
.Controls
.detectOverflow
199 else quotUnsafe (x
, y
)
201 then quotUnsafe (x
+? one
, y
) -? one
207 then remUnsafe (x
, y
)
211 else remUnsafe (x
-? one
, y
) +?
(y
+ one
)
214 then if x
= minInt
' andalso y
= ~one
216 else remUnsafe (x
, y
)
218 then remUnsafe (x
+? one
, y
) +?
(y
-? one
)
223 UnsignedIntegralComparisons
225 type word = Word.word
226 val idFromIntToWord
= idFromIntToWord
232 fun power
{base
, exp
} =
233 if Primitive
.Controls
.safe
235 then raise Primitive
.Exn
.Fail8
"Int.power"
237 fun loop (exp
, accum
) =
240 else loop (exp
- one
, base
* accum
)
247 structure Primitive
= struct
252 MkNum0 (structure Int = Int8
253 structure Word = Word8
254 val idFromIntToWord
= IntWordConv
.idFromInt8ToWord8
255 val idFromWordToInt
= IntWordConv
.idFromWord8ToInt8
)
257 structure Int8
: PRIM_INTEGER
= S
.Int
258 structure Word8 : PRIM_WORD
= S
.Word
262 MkNum0 (structure Int = Int16
263 structure Word = Word16
264 val idFromIntToWord
= IntWordConv
.idFromInt16ToWord16
265 val idFromWordToInt
= IntWordConv
.idFromWord16ToInt16
)
267 structure Int16
: PRIM_INTEGER
= S
.Int
268 structure Word16
: PRIM_WORD
= S
.Word
272 MkNum0 (structure Int = Int32
273 structure Word = Word32
274 val idFromIntToWord
= IntWordConv
.idFromInt32ToWord32
275 val idFromWordToInt
= IntWordConv
.idFromWord32ToInt32
)
277 structure Int32
: PRIM_INTEGER
= S
.Int
278 structure Word32
: PRIM_WORD
= S
.Word
282 MkNum0 (structure Int = Int64
283 structure Word = Word64
284 val idFromIntToWord
= IntWordConv
.idFromInt64ToWord64
285 val idFromWordToInt
= IntWordConv
.idFromWord64ToInt64
)
287 structure Int64
: PRIM_INTEGER
= S
.Int
288 structure Word64
: PRIM_WORD
= S
.Word