Import Upstream version 20180207
[hcoop/debian/mlton.git] / basis-library / integer / embed-word.sml
CommitLineData
7f918cf1
CE
1(* Copyright (C) 2004-2006 Henry Cejtin, Matthew Fluet, Suresh
2 * Jagannathan, and Stephen Weeks.
3 *
4 * MLton is released under a BSD-style license.
5 * See the file MLton-LICENSE for details.
6 *)
7
8signature EMBED_WORD =
9 sig
10 eqtype word
11 type big
12
13 val fromBigUnsafe: big -> word
14 val sizeInBits: Int32.int
15 val toBig: word -> big
16 end
17
18functor EmbedWord (structure Big: WORD
19 structure Small: EMBED_WORD where type big = Big.word): WORD =
20 struct
21 structure Small =
22 struct
23 open Small
24 val wordSize: Int.int = Int32.toInt sizeInBits
25 end
26
27 val () = if Int.< (Small.wordSize, Big.wordSize) then ()
28 else raise Fail "EmbedWord"
29
30 open Small
31
32 fun ones size =
33 Big.- (Big.<< (Big.fromLarge 0w1, Word.fromInt size),
34 Big.fromLarge 0w1)
35
36 val maxWord = ones wordSize
37
38 fun fromBig (w: Big.word): word =
39 fromBigUnsafe (Big.andb (w, maxWord))
40
41 fun fromBigOverflow (w: Big.word): word =
42 if Big.<= (w, maxWord)
43 then fromBigUnsafe w
44 else raise Overflow
45
46 fun highBitIsSet (w: Big.word): bool =
47 Big.> (w, ones (Int.- (wordSize, 1)))
48
49 fun toBigX (w: word): Big.word =
50 let
51 val w = toBig w
52 in
53 if highBitIsSet w
54 then Big.orb (w, Big.notb maxWord)
55 else w
56 end
57
58 local
59 val make: (Big.word * Big.word -> Big.word) -> (word * word -> word) =
60 fn f => fn (x, y) => fromBig (f (toBig x, toBig y))
61 in
62 val op * = make Big.*
63 val op + = make Big.+
64 val op - = make Big.-
65 val andb = make Big.andb
66 val op div = make Big.div
67 val op mod = make Big.mod
68 val orb = make Big.orb
69 val xorb = make Big.xorb
70 end
71
72 local
73 val make: ((Big.word * Word.word -> Big.word)
74 -> word * Word.word -> word) =
75 fn f => fn (w, w') => fromBig (f (toBig w, w'))
76 in
77 val >> = make Big.>>
78 val << = make Big.<<
79 end
80
81 fun ~>> (w, w') = fromBig (Big.~>> (toBigX w, w'))
82
83 local
84 val make: (Big.word * Big.word -> 'a) -> (word * word -> 'a) =
85 fn f => fn (x, y) => f (toBig x, toBig y)
86 in
87 val op < = make Big.<
88 val op <= = make Big.<=
89 val op > = make Big.>
90 val op >= = make Big.>=
91 val compare = make Big.compare
92 end
93
94 local
95 val make: (Big.word -> Big.word) -> word -> word =
96 fn f => fn w => fromBig (f (toBig w))
97 in
98 val notb = make Big.notb
99 end
100
101 local
102 val make: ('a -> Big.word) -> 'a -> word =
103 fn f => fn a => fromBig (f a)
104 in
105 val fromInt = make Big.fromInt
106 val fromLarge = make Big.fromLarge
107 val fromLargeInt = make Big.fromLargeInt
108 end
109
110 local
111 val make: (Big.word -> 'a) -> word -> 'a =
112 fn f => fn w => f (toBig w)
113 in
114 val toInt = make Big.toInt
115 val toLarge = make Big.toLarge
116 val toLargeInt = make Big.toLargeInt
117 val toString = make Big.toString
118 end
119
120 local
121 val make: (Big.word -> 'a) -> word -> 'a =
122 fn f => fn w => f (toBigX w)
123 in
124 val toIntX = make Big.toIntX
125 val toLargeIntX = make Big.toLargeIntX
126 val toLargeX = make Big.toLargeX
127 end
128
129 fun fmt r i = Big.fmt r (toBig i)
130
131 val fromLargeWord = fromLarge
132
133 fun fromString s = Option.map fromBigOverflow (Big.fromString s)
134
135 fun max (w, w') = if w >= w' then w else w'
136
137 fun min (w, w') = if w <= w' then w else w'
138
139 fun scan r reader state =
140 Option.map
141 (fn (w, state) => (fromBigOverflow w, state))
142 (Big.scan r reader state)
143
144 val toLargeWord = toLarge
145
146 val toLargeWordX = toLargeX
147
148 fun ~ w = fromLarge 0w0 - w
149 end
150
151functor EmbedWord8 (Small: EMBED_WORD where type big = Word8.word): WORD =
152 EmbedWord (structure Big = Word8
153 structure Small = Small)
154
155functor EmbedWord16 (Small: EMBED_WORD where type big = Word16.word): WORD =
156 EmbedWord (structure Big = Word16
157 structure Small = Small)
158
159functor EmbedWord32 (Small: EMBED_WORD where type big = Word32.word): WORD =
160 EmbedWord (structure Big = Word32
161 structure Small = Small)
162
163structure Word1 = EmbedWord8 (Primitive.Word1)
164structure Word2 = EmbedWord8 (Primitive.Word2)
165structure Word3 = EmbedWord8 (Primitive.Word3)
166structure Word4 = EmbedWord8 (Primitive.Word4)
167structure Word5 = EmbedWord8 (Primitive.Word5)
168structure Word6 = EmbedWord8 (Primitive.Word6)
169structure Word7 = EmbedWord8 (Primitive.Word7)
170structure Word9 = EmbedWord16 (Primitive.Word9)
171structure Word10 = EmbedWord16 (Primitive.Word10)
172structure Word11 = EmbedWord16 (Primitive.Word11)
173structure Word12 = EmbedWord16 (Primitive.Word12)
174structure Word13 = EmbedWord16 (Primitive.Word13)
175structure Word14 = EmbedWord16 (Primitive.Word14)
176structure Word15 = EmbedWord16 (Primitive.Word15)
177structure Word17 = EmbedWord32 (Primitive.Word17)
178structure Word18 = EmbedWord32 (Primitive.Word18)
179structure Word19 = EmbedWord32 (Primitive.Word19)
180structure Word20 = EmbedWord32 (Primitive.Word20)
181structure Word21 = EmbedWord32 (Primitive.Word21)
182structure Word22 = EmbedWord32 (Primitive.Word22)
183structure Word23 = EmbedWord32 (Primitive.Word23)
184structure Word24 = EmbedWord32 (Primitive.Word24)
185structure Word25 = EmbedWord32 (Primitive.Word25)
186structure Word26 = EmbedWord32 (Primitive.Word26)
187structure Word27 = EmbedWord32 (Primitive.Word27)
188structure Word28 = EmbedWord32 (Primitive.Word28)
189structure Word29 = EmbedWord32 (Primitive.Word29)
190structure Word30 = EmbedWord32 (Primitive.Word30)
191structure Word31 = EmbedWord32 (Primitive.Word31)