1 (* Copyright (C
) 2009 Matthew Fluet
.
2 * Copyright (C
) 1999-2006, 2008 Henry Cejtin
, Matthew Fluet
, Suresh
3 * Jagannathan
, and Stephen Weeks
.
5 * MLton is released under a BSD
-style license
.
6 * See the file MLton
-LICENSE for details
.
13 val addCheck
: t
* t
-> t (* may
raise Overflow
*)
14 val fromWord8
: Word8.t
-> t
15 (* fromWord8s f
. f
0 should return the least significant byte
16 * and f
3 should return the most significant
.
18 val fromWord8s
: (int -> Word8.t
) -> t
19 val log2
: t
-> t (* 2 ^
(log2 w
) <= w
< 2 ^
(1 + log2 w
) *)
20 val maxPow2ThatDivides
: t
-> word
21 val toWord8
: t
-> Word8.t
22 (* val rotateLeft
: t
* t
-> t
*)
23 val roundDownToPowerOfTwo
: t
-> t
24 val roundUpToPowerOfTwo
: t
-> t
27 structure Int = Pervasive
.Int
29 structure Z
= FixWord (Pervasive
.Word)
32 val equals
: t
* t
-> bool = op =
34 fun fromWord8s (f
: int -> Word8.t
): t
=
37 Pervasive
.Word.<< (Word8.toWord (f i
), shift
)
38 fun loop (w
, i
, shift
) =
39 if Int.>= (Int.* (i
, 8), Pervasive
.Word.wordSize
)
41 else loop (orb (w
, g (i
, shift
)),
48 val fromWord
= fn x
=> x
49 val toWord
= fn x
=> x
50 val toWordX
= fn x
=> x
52 val fromIntInf
= fromLargeInt
53 val toIntInf
= toLargeInt
54 val toIntInfX
= toLargeIntX
56 val fromWord8
= Word8.toWord
57 val toWord8
= Word8.fromWord
61 then Error
.bug
"Word.log2: 0"
64 fun loop (n
, s
, ac
): word =
71 then (>> (n
, s
), ac
+ s
)
74 loop (n
, >> (s
, 0w1
), ac
)
80 fun roundDownToPowerOfTwo (w
: t
) = << (0w1
, log2 w
)
82 fun roundUpToPowerOfTwo w
=
84 val w
' = roundDownToPowerOfTwo w
91 structure M
= MaxPow2ThatDivides (open Word
93 val equals
: t
* t
-> bool = op =
98 fun addCheck (w
, w
') =