1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
|
(* Copyright (C) 2013-2014 Matthew Fluet.
* Copyright (C) 1999-2007 Henry Cejtin, Matthew Fluet, Suresh
* Jagannathan, and Stephen Weeks.
* Copyright (C) 1997-2000 NEC Research Institute.
*
* MLton is released under a HPND-style license.
* See the file MLton-LICENSE for details.
*)
structure IntInf: INT_INF_EXTRA =
struct
open Primitive.IntInf
type t = int
structure BigWord = C_MPLimb
structure SmallInt = ObjptrInt
structure W = ObjptrWord
structure I = ObjptrInt
structure MPLimb = C_MPLimb
val precision: Int.int option = NONE
fun sign (arg: int): Int.int =
case compare (arg, zero) of
LESS => ~1
| EQUAL => 0
| GREATER => 1
fun sameSign (x, y) = sign x = sign y
local
val maxShift32 = Word32.<< (0wx1, 0w30)
val maxShift = Word32.toWord maxShift32
fun make f (arg, shift) =
let
fun body loop (arg, shift) =
if Word.<= (shift, maxShift)
then f (arg, Word32.fromWord shift)
else loop (f (arg, maxShift32),
Word.- (shift, maxShift))
fun loop (arg, shift) = body loop (arg, shift)
in
body loop (arg, shift)
end
in
val << = make <<
val ~>> = make ~>>
end
val fromInt = schckFromInt
val toInt = schckToInt
val fromLarge = schckFromLargeInt
val toLarge = schckToLargeInt
local
open StringCvt
val binCvt = mkCvt {base = 2, smallCvt = I.fmt BIN}
val octCvt = mkCvt {base = 8, smallCvt = I.fmt OCT}
val decCvt = mkCvt {base = 10, smallCvt = I.fmt DEC}
val hexCvt = mkCvt {base = 16, smallCvt = I.fmt HEX}
in
fun fmt radix =
case radix of
BIN => binCvt
| OCT => octCvt
| DEC => decCvt
| HEX => hexCvt
val toString = fmt DEC
end
local
open StringCvt
(*
* Given a char, if it is a digit in the appropriate base,
* convert it to a word. Otherwise, return NONE.
* Note, both a-f and A-F are accepted as hexadecimal digits.
*)
fun binDig (ch: char): W.word option =
case ch of
#"0" => SOME 0w0
| #"1" => SOME 0w1
| _ => NONE
local
val op <= = Char.<=
in
fun octDig (ch: char): W.word option =
if #"0" <= ch andalso ch <= #"7"
then SOME (W.fromInt (Int.- (Char.ord ch,
Char.ord #"0")))
else NONE
fun decDig (ch: char): W.word option =
if #"0" <= ch andalso ch <= #"9"
then SOME (W.fromInt (Int.- (Char.ord ch,
Char.ord #"0")))
else NONE
fun hexDig (ch: char): W.word option =
if #"0" <= ch andalso ch <= #"9"
then SOME (W.fromInt (Int.- (Char.ord ch,
Char.ord #"0")))
else if #"a" <= ch andalso ch <= #"f"
then SOME (W.fromInt (Int.- (Char.ord ch,
Int.- (Char.ord #"a", 0xa))))
else if #"A" <= ch andalso ch <= #"F"
then SOME (W.fromInt (Int.- (Char.ord ch,
Int.- (Char.ord #"A", 0xA))))
else NONE
end
(*
* Given a digit converter and a char reader, return a digit
* reader.
*)
fun toDigR (charToDig: char -> W.word option,
cread: (char, 'a) reader)
(s: 'a)
: (W.word * 'a) option =
case cread s of
NONE => NONE
| SOME (ch, s') =>
case charToDig ch of
NONE => NONE
| SOME dig => SOME (dig, s')
(*
* A chunk represents the result of processing some digits.
* more is a bool indicating if there might be more digits.
* shift is base raised to the number-of-digits-seen power.
* chunk is the value of the digits seen.
*)
type chunk = {more: bool,
shift: W.word,
chunk: W.word}
(*
* Given the base and a digit reader,
* return a chunk reader.
*)
fun toChunkR (base: W.word,
dread: (W.word, 'a) reader)
: (chunk, 'a) reader =
let
fun loop {left: Int32.int,
shift: W.word,
chunk: W.word,
s: 'a}
: chunk * 'a =
if Int32.<= (left, 0)
then ({more = true,
shift = shift,
chunk = chunk},
s)
else
case dread s of
NONE => ({more = false,
shift = shift,
chunk = chunk},
s)
| SOME (dig, s') =>
loop {left = Int32.- (left, 1),
shift = W.* (base, shift),
chunk = W.+ (W.* (base, chunk), dig),
s = s'}
(* digitsPerChunk = floor((W.wordSize - 3) / (log2 base)) *)
val digitsPerChunk : Int32.t =
case (W.wordSize, base) of
(64, 0w16) => 15
| (64, 0w10) => 18
| (64, 0w8) => 20
| (64, 0w2) => 61
| (32, 0w16) => 7
| (32, 0w10) => 8
| (32, 0w8) => 9
| (32, 0w2) => 29
| _ => raise (Fail "IntInf.scan:digitsPerChunk")
fun reader (s: 'a): (chunk * 'a) option =
case dread s of
NONE => NONE
| SOME (dig, next) =>
SOME (loop {left = Int32.- (digitsPerChunk, 1),
shift = base,
chunk = dig,
s = next})
in
reader
end
(*
* Given a chunk reader, return an unsigned reader.
*)
fun toUnsR (ckread: (chunk, 'a) reader): (int, 'a) reader =
let
fun loop (more: bool, acc: int, s: 'a) =
if more
then case ckread s of
NONE => (acc, s)
| SOME ({more, shift, chunk}, s') =>
loop (more,
((W.toLargeInt shift) * acc)
+ (W.toLargeInt chunk),
s')
else (acc, s)
fun reader (s: 'a): (int * 'a) option =
case ckread s of
NONE => NONE
| SOME ({more, chunk, ...}, s') =>
SOME (loop (more,
W.toLargeInt chunk,
s'))
in
reader
end
(*
* Given a char reader and an unsigned reader, return an unsigned
* reader that includes skipping the option hex '0x'.
*)
fun toHexR (cread: (char, 'a) reader, uread: (int, 'a) reader) s =
case cread s of
NONE => NONE
| SOME (c1, s1) =>
if c1 = #"0" then
case cread s1 of
NONE => SOME (zero, s1)
| SOME (c2, s2) =>
if c2 = #"x" orelse c2 = #"X" then
case uread s2 of
NONE => SOME (zero, s1)
| SOME x => SOME x
else uread s
else uread s
(*
* Given a char reader and an unsigned reader, return a signed
* reader. This includes skipping any initial white space.
*)
fun toSign (cread: (char, 'a) reader, uread: (int, 'a) reader)
: (int, 'a) reader =
let
fun reader (s: 'a): (int * 'a) option =
let val s = StringCvt.skipWS cread s in
case cread s of
NONE => NONE
| SOME (ch, s') =>
let
val (isNeg, s'') =
case ch of
#"+" => (false, s')
| #"-" => (true, s')
| #"~" => (true, s')
| _ => (false, s)
in
if isNeg
then case uread s'' of
NONE => NONE
| SOME (abs, s''') => SOME (~ abs, s''')
else uread s''
end
end
in
reader
end
(*
* Base-specific conversions from char readers to
* int readers.
*)
local
fun reader (base, dig)
(cread: (char, 'a) reader)
: (int, 'a) reader =
let
val dread = toDigR (dig, cread)
val ckread = toChunkR (base, dread)
val uread = toUnsR ckread
val hread = if base = 0w16 then toHexR (cread, uread) else uread
val reader = toSign (cread, hread)
in
reader
end
in
fun binReader z = reader (0w2, binDig) z
fun octReader z = reader (0w8, octDig) z
fun decReader z = reader (0w10, decDig) z
fun hexReader z = reader (0w16, hexDig) z
end
in
fun scan radix =
case radix of
BIN => binReader
| OCT => octReader
| DEC => decReader
| HEX => hexReader
end
val fromString = StringCvt.scanString (scan StringCvt.DEC)
local
fun isEven (n: Int.int) = Int.andb (n, 0x1) = 0
in
fun pow (i: int, j: Int.int): int =
if Int.< (j, 0) then
if i = zero then
raise Div
else
if i = one then one
else if i = negOne then if isEven j then one else negOne
else zero
else
if j = 0 then one
else
let
fun square (n: int): int = n * n
(* pow (j) returns (i ^ j) *)
fun pow (j: Int.int): int =
if Int.<= (j, 0) then one
else if isEven j then evenPow j
else i * evenPow (Int.- (j, 1))
(* evenPow (j) returns (i ^ j), assuming j is even *)
and evenPow (j: Int.int): int =
square (pow (Int.div (j, 2)))
in
pow j
end
end
val log2 =
mkLog2 {fromSmall = fn {smallLog2} => Int32.toInt smallLog2,
fromLarge = fn {numLimbsMinusOne, mostSigLimbLog2} =>
Int.+ (Int.* (MPLimb.wordSize, SeqIndex.toInt numLimbsMinusOne),
Int32.toInt mostSigLimbLog2)}
end
|