1 (* Copyright (C
) 2004-2006 Henry Cejtin
, Matthew Fluet
, Suresh
2 * Jagannathan
, and Stephen Weeks
.
4 * MLton is released under a BSD
-style license
.
5 * See the file MLton
-LICENSE for details
.
13 val fromBigUnsafe
: big
-> int
14 val sizeInBits
: Int32
.int
18 functor EmbedInt (structure Big
: INTEGER_EXTRA
19 structure Small
: EMBED_INT
where type big
= Big
.int): INTEGER
=
24 val precision
': Int.int = Int32
.toInt sizeInBits
27 val () = if Int.< (Small
.precision
', Big
.precision
') then ()
28 else raise Fail
"EmbedWord"
32 val shift
= Word.fromInt (Int.- (Big
.precision
', precision
'))
34 val extend
: Big
.int -> Big
.int =
35 fn i
=> Big
.~
>> (Big
.<< (i
, shift
), shift
)
37 val toBig
: Small
.int -> Big
.int = extend
o Small
.toBig
39 val precision
= SOME precision
'
41 val maxIntBig
= Big
.>> (Big
.fromInt ~
1, Word.+ (shift
, 0w1
))
43 val minIntBig
= Big
.- (Big
.~ maxIntBig
, Big
.fromInt
1)
45 val mask
= Big
.>> (Big
.fromInt ~
1, shift
)
47 fun fromBig (i
: Big
.int): int =
49 val i
' = Big
.andb (i
, mask
)
56 val maxInt
= SOME (fromBig maxIntBig
)
58 val minInt
= SOME (fromBig minIntBig
)
61 val make
: (Big
.int * Big
.int -> Big
.int) -> (int * int -> int) =
62 fn f
=> fn (x
, y
) => fromBig (f (toBig x
, toBig y
))
67 val op div = make Big
.div
68 val op mod = make Big
.mod
69 val quot
= make Big
.quot
70 val rem
= make Big
.rem
74 val make
: (Big
.int * Big
.int -> 'a
) -> (int * int -> 'a
) =
75 fn f
=> fn (x
, y
) => f (toBig x
, toBig y
)
78 val op <= = make Big
.<=
80 val op >= = make Big
.>=
81 val compare
= make Big
.compare
84 val fromInt
= fromBig
o Big
.fromInt
86 val toInt
= Big
.toInt
o toBig
89 val make
: (Big
.int -> Big
.int) -> (int -> int) =
90 fn f
=> fn x
=> fromBig (f (toBig x
))
93 val abs
= make Big
.abs
96 fun fmt r i
= Big
.fmt
r (toBig i
)
98 val fromLarge
= fromBig
o Big
.fromLarge
100 fun fromString s
= Option
.map
fromBig (Big
.fromString s
)
102 fun max (i
, j
) = if i
>= j
then i
else j
104 fun min (i
, j
) = if i
<= j
then i
else j
106 fun scan r reader state
=
108 (fn (i
, state
) => (fromBig i
, state
))
109 (Big
.scan r reader state
)
111 val sign
= Big
.sign
o toBig
113 fun sameSign (x
, y
) = sign x
= sign y
115 val toLarge
= Big
.toLarge
o toBig
117 val toString
= Big
.toString
o toBig
120 functor Embed8 (Small
: EMBED_INT
where type big
= Int8
.int): INTEGER
=
121 EmbedInt (structure Big
= Int8
122 structure Small
= Small
)
124 functor Embed16 (Small
: EMBED_INT
where type big
= Int16
.int): INTEGER
=
125 EmbedInt (structure Big
= Int16
126 structure Small
= Small
)
128 functor Embed32 (Small
: EMBED_INT
where type big
= Int32
.int): INTEGER
=
129 EmbedInt (structure Big
= Int32
130 structure Small
= Small
)
132 structure Int1
= Embed8 (Primitive
.Int1
)
133 structure Int2
= Embed8 (Primitive
.Int2
)
134 structure Int3
= Embed8 (Primitive
.Int3
)
135 structure Int4
= Embed8 (Primitive
.Int4
)
136 structure Int5
= Embed8 (Primitive
.Int5
)
137 structure Int6
= Embed8 (Primitive
.Int6
)
138 structure Int7
= Embed8 (Primitive
.Int7
)
139 structure Int9
= Embed16 (Primitive
.Int9
)
140 structure Int10
= Embed16 (Primitive
.Int10
)
141 structure Int11
= Embed16 (Primitive
.Int11
)
142 structure Int12
= Embed16 (Primitive
.Int12
)
143 structure Int13
= Embed16 (Primitive
.Int13
)
144 structure Int14
= Embed16 (Primitive
.Int14
)
145 structure Int15
= Embed16 (Primitive
.Int15
)
146 structure Int17
= Embed32 (Primitive
.Int17
)
147 structure Int18
= Embed32 (Primitive
.Int18
)
148 structure Int19
= Embed32 (Primitive
.Int19
)
149 structure Int20
= Embed32 (Primitive
.Int20
)
150 structure Int21
= Embed32 (Primitive
.Int21
)
151 structure Int22
= Embed32 (Primitive
.Int22
)
152 structure Int23
= Embed32 (Primitive
.Int23
)
153 structure Int24
= Embed32 (Primitive
.Int24
)
154 structure Int25
= Embed32 (Primitive
.Int25
)
155 structure Int26
= Embed32 (Primitive
.Int26
)
156 structure Int27
= Embed32 (Primitive
.Int27
)
157 structure Int28
= Embed32 (Primitive
.Int28
)
158 structure Int29
= Embed32 (Primitive
.Int29
)
159 structure Int30
= Embed32 (Primitive
.Int30
)
160 structure Int31
= Embed32 (Primitive
.Int31
)