| 1 |
(* Copyright (C) 2013-2014,2016-2017 Matthew Fluet. |
| 2 |
* Copyright (C) 1999-2008 Henry Cejtin, Matthew Fluet, Suresh |
| 3 |
* Jagannathan, and Stephen Weeks. |
| 4 |
* Copyright (C) 1997-2000 NEC Research Institute. |
| 5 |
* |
| 6 |
* MLton is released under a HPND-style license. |
| 7 |
* See the file MLton-LICENSE for details. |
| 8 |
*) |
| 9 |
|
| 10 |
signature PRIM_INT_INF = |
| 11 |
sig |
| 12 |
eqtype int |
| 13 |
type t = int |
| 14 |
|
| 15 |
val precision: Primitive.Int32.int option |
| 16 |
|
| 17 |
val maxInt: int option |
| 18 |
val minInt: int option |
| 19 |
|
| 20 |
val zero: int |
| 21 |
val one: int |
| 22 |
val negOne: int |
| 23 |
|
| 24 |
datatype rep = |
| 25 |
Big of C_MPLimb.word vector |
| 26 |
| Small of ObjptrInt.int |
| 27 |
val rep: int -> rep |
| 28 |
val fromRep: rep -> int option |
| 29 |
|
| 30 |
val isSmall: int -> bool |
| 31 |
val areSmall: int * int -> bool |
| 32 |
|
| 33 |
val abs: int -> int |
| 34 |
val +! : int * int -> int |
| 35 |
val + : int * int -> int |
| 36 |
val divMod: int * int -> int * int |
| 37 |
val div: int * int -> int |
| 38 |
val gcd: int * int -> int |
| 39 |
val mod: int * int -> int |
| 40 |
val *! : int * int -> int |
| 41 |
val * : int * int -> int |
| 42 |
val ~! : int -> int |
| 43 |
val ~ : int -> int |
| 44 |
val quotRem: int * int -> int * int |
| 45 |
val quot: int * int -> int |
| 46 |
val rem: int * int -> int |
| 47 |
val -! : int * int -> int |
| 48 |
val - : int * int -> int |
| 49 |
|
| 50 |
val < : int * int -> bool |
| 51 |
val <= : int * int -> bool |
| 52 |
val > : int * int -> bool |
| 53 |
val >= : int * int -> bool |
| 54 |
val compare: int * int -> Primitive.Order.order |
| 55 |
val min: int * int -> int |
| 56 |
val max: int * int -> int |
| 57 |
val ltu: int * int -> bool |
| 58 |
val leu: int * int -> bool |
| 59 |
val gtu: int * int -> bool |
| 60 |
val geu: int * int -> bool |
| 61 |
|
| 62 |
val andb: int * int -> int |
| 63 |
val <<? : int * Primitive.Word32.word -> int |
| 64 |
val << : int * Primitive.Word32.word -> int |
| 65 |
val notb: int -> int |
| 66 |
val orb: int * int -> int |
| 67 |
val ~>>? : int * Primitive.Word32.word -> int |
| 68 |
val ~>> : int * Primitive.Word32.word -> int |
| 69 |
val xorb: int * int -> int |
| 70 |
|
| 71 |
val mkCvt: ({base: Primitive.Int32.int, |
| 72 |
smallCvt: ObjptrInt.int -> Primitive.String8.string} |
| 73 |
-> int -> Primitive.String8.string) |
| 74 |
val mkLog2: ({fromSmall: {smallLog2: Primitive.Int32.int} -> 'a, |
| 75 |
fromLarge: {mostSigLimbLog2: Primitive.Int32.int, |
| 76 |
numLimbsMinusOne: SeqIndex.int} -> 'a} |
| 77 |
-> int -> 'a) |
| 78 |
|
| 79 |
val zextdFromInt8: Primitive.Int8.int -> int |
| 80 |
val zextdFromInt16: Primitive.Int16.int -> int |
| 81 |
val zextdFromInt32: Primitive.Int32.int -> int |
| 82 |
val zextdFromInt64: Primitive.Int64.int -> int |
| 83 |
val zextdFromIntInf: Primitive.IntInf.int -> int |
| 84 |
val zextdFromWord8: Primitive.Word8.word -> int |
| 85 |
val zextdFromWord16: Primitive.Word16.word -> int |
| 86 |
val zextdFromWord32: Primitive.Word32.word -> int |
| 87 |
val zextdFromWord64: Primitive.Word64.word -> int |
| 88 |
val zextdToInt8: int -> Primitive.Int8.int |
| 89 |
val zextdToInt16: int -> Primitive.Int16.int |
| 90 |
val zextdToInt32: int -> Primitive.Int32.int |
| 91 |
val zextdToInt64: int -> Primitive.Int64.int |
| 92 |
val zextdToIntInf: int -> Primitive.IntInf.int |
| 93 |
val zextdToWord8: int -> Primitive.Word8.word |
| 94 |
val zextdToWord16: int -> Primitive.Word16.word |
| 95 |
val zextdToWord32: int -> Primitive.Word32.word |
| 96 |
val zextdToWord64: int -> Primitive.Word64.word |
| 97 |
|
| 98 |
val sextdFromInt8: Primitive.Int8.int -> int |
| 99 |
val sextdFromInt16: Primitive.Int16.int -> int |
| 100 |
val sextdFromInt32: Primitive.Int32.int -> int |
| 101 |
val sextdFromInt64: Primitive.Int64.int -> int |
| 102 |
val sextdFromIntInf: Primitive.IntInf.int -> int |
| 103 |
val sextdFromWord8: Primitive.Word8.word -> int |
| 104 |
val sextdFromWord16: Primitive.Word16.word -> int |
| 105 |
val sextdFromWord32: Primitive.Word32.word -> int |
| 106 |
val sextdFromWord64: Primitive.Word64.word -> int |
| 107 |
val sextdToInt8: int -> Primitive.Int8.int |
| 108 |
val sextdToInt16: int -> Primitive.Int16.int |
| 109 |
val sextdToInt32: int -> Primitive.Int32.int |
| 110 |
val sextdToInt64: int -> Primitive.Int64.int |
| 111 |
val sextdToIntInf: int -> Primitive.IntInf.int |
| 112 |
val sextdToWord8: int -> Primitive.Word8.word |
| 113 |
val sextdToWord16: int -> Primitive.Word16.word |
| 114 |
val sextdToWord32: int -> Primitive.Word32.word |
| 115 |
val sextdToWord64: int -> Primitive.Word64.word |
| 116 |
|
| 117 |
val castFromInt8: Primitive.Int8.int -> int |
| 118 |
val castFromInt16: Primitive.Int16.int -> int |
| 119 |
val castFromInt32: Primitive.Int32.int -> int |
| 120 |
val castFromInt64: Primitive.Int64.int -> int |
| 121 |
val castFromIntInf: Primitive.IntInf.int -> int |
| 122 |
val castFromWord8: Primitive.Word8.word -> int |
| 123 |
val castFromWord16: Primitive.Word16.word -> int |
| 124 |
val castFromWord32: Primitive.Word32.word -> int |
| 125 |
val castFromWord64: Primitive.Word64.word -> int |
| 126 |
val castToInt8: int -> Primitive.Int8.int |
| 127 |
val castToInt16: int -> Primitive.Int16.int |
| 128 |
val castToInt32: int -> Primitive.Int32.int |
| 129 |
val castToInt64: int -> Primitive.Int64.int |
| 130 |
val castToIntInf: int -> Primitive.IntInf.int |
| 131 |
val castToWord8: int -> Primitive.Word8.word |
| 132 |
val castToWord16: int -> Primitive.Word16.word |
| 133 |
val castToWord32: int -> Primitive.Word32.word |
| 134 |
val castToWord64: int -> Primitive.Word64.word |
| 135 |
|
| 136 |
val zchckFromInt8: Primitive.Int8.int -> int |
| 137 |
val zchckFromInt16: Primitive.Int16.int -> int |
| 138 |
val zchckFromInt32: Primitive.Int32.int -> int |
| 139 |
val zchckFromInt64: Primitive.Int64.int -> int |
| 140 |
val zchckFromIntInf: Primitive.IntInf.int -> int |
| 141 |
val zchckFromWord8: Primitive.Word8.word -> int |
| 142 |
val zchckFromWord16: Primitive.Word16.word -> int |
| 143 |
val zchckFromWord32: Primitive.Word32.word -> int |
| 144 |
val zchckFromWord64: Primitive.Word64.word -> int |
| 145 |
val zchckToInt8: int -> Primitive.Int8.int |
| 146 |
val zchckToInt16: int -> Primitive.Int16.int |
| 147 |
val zchckToInt32: int -> Primitive.Int32.int |
| 148 |
val zchckToInt64: int -> Primitive.Int64.int |
| 149 |
val zchckToIntInf: int -> Primitive.IntInf.int |
| 150 |
val zchckToWord8: int -> Primitive.Word8.word |
| 151 |
val zchckToWord16: int -> Primitive.Word16.word |
| 152 |
val zchckToWord32: int -> Primitive.Word32.word |
| 153 |
val zchckToWord64: int -> Primitive.Word64.word |
| 154 |
|
| 155 |
val schckFromInt8: Primitive.Int8.int -> int |
| 156 |
val schckFromInt16: Primitive.Int16.int -> int |
| 157 |
val schckFromInt32: Primitive.Int32.int -> int |
| 158 |
val schckFromInt64: Primitive.Int64.int -> int |
| 159 |
val schckFromIntInf: Primitive.IntInf.int -> int |
| 160 |
val schckFromWord8: Primitive.Word8.word -> int |
| 161 |
val schckFromWord16: Primitive.Word16.word -> int |
| 162 |
val schckFromWord32: Primitive.Word32.word -> int |
| 163 |
val schckFromWord64: Primitive.Word64.word -> int |
| 164 |
val schckToInt8: int -> Primitive.Int8.int |
| 165 |
val schckToInt16: int -> Primitive.Int16.int |
| 166 |
val schckToInt32: int -> Primitive.Int32.int |
| 167 |
val schckToInt64: int -> Primitive.Int64.int |
| 168 |
val schckToIntInf: int -> Primitive.IntInf.int |
| 169 |
val schckToWord8: int -> Primitive.Word8.word |
| 170 |
val schckToWord16: int -> Primitive.Word16.word |
| 171 |
val schckToWord32: int -> Primitive.Word32.word |
| 172 |
val schckToWord64: int -> Primitive.Word64.word |
| 173 |
end |
| 174 |
|
| 175 |
signature PRIM_INTWORD_CONV = |
| 176 |
sig |
| 177 |
include PRIM_INTWORD_CONV |
| 178 |
|
| 179 |
val idFromIntInfToIntInf: Primitive.IntInf.int -> Primitive.IntInf.int |
| 180 |
|
| 181 |
val zextdFromInt8ToIntInf: Primitive.Int8.int -> Primitive.IntInf.int |
| 182 |
val zextdFromInt16ToIntInf: Primitive.Int16.int -> Primitive.IntInf.int |
| 183 |
val zextdFromInt32ToIntInf: Primitive.Int32.int -> Primitive.IntInf.int |
| 184 |
val zextdFromInt64ToIntInf: Primitive.Int64.int -> Primitive.IntInf.int |
| 185 |
val zextdFromWord8ToIntInf: Primitive.Word8.word -> Primitive.IntInf.int |
| 186 |
val zextdFromWord16ToIntInf: Primitive.Word16.word -> Primitive.IntInf.int |
| 187 |
val zextdFromWord32ToIntInf: Primitive.Word32.word -> Primitive.IntInf.int |
| 188 |
val zextdFromWord64ToIntInf: Primitive.Word64.word -> Primitive.IntInf.int |
| 189 |
|
| 190 |
val zextdFromIntInfToInt8: Primitive.IntInf.int -> Primitive.Int8.int |
| 191 |
val zextdFromIntInfToInt16: Primitive.IntInf.int -> Primitive.Int16.int |
| 192 |
val zextdFromIntInfToInt32: Primitive.IntInf.int -> Primitive.Int32.int |
| 193 |
val zextdFromIntInfToInt64: Primitive.IntInf.int -> Primitive.Int64.int |
| 194 |
val zextdFromIntInfToIntInf: Primitive.IntInf.int -> Primitive.IntInf.int |
| 195 |
val zextdFromIntInfToWord8: Primitive.IntInf.int -> Primitive.Word8.word |
| 196 |
val zextdFromIntInfToWord16: Primitive.IntInf.int -> Primitive.Word16.word |
| 197 |
val zextdFromIntInfToWord32: Primitive.IntInf.int -> Primitive.Word32.word |
| 198 |
val zextdFromIntInfToWord64: Primitive.IntInf.int -> Primitive.Word64.word |
| 199 |
|
| 200 |
|
| 201 |
val sextdFromInt8ToIntInf: Primitive.Int8.int -> Primitive.IntInf.int |
| 202 |
val sextdFromInt16ToIntInf: Primitive.Int16.int -> Primitive.IntInf.int |
| 203 |
val sextdFromInt32ToIntInf: Primitive.Int32.int -> Primitive.IntInf.int |
| 204 |
val sextdFromInt64ToIntInf: Primitive.Int64.int -> Primitive.IntInf.int |
| 205 |
val sextdFromWord8ToIntInf: Primitive.Word8.word -> Primitive.IntInf.int |
| 206 |
val sextdFromWord16ToIntInf: Primitive.Word16.word -> Primitive.IntInf.int |
| 207 |
val sextdFromWord32ToIntInf: Primitive.Word32.word -> Primitive.IntInf.int |
| 208 |
val sextdFromWord64ToIntInf: Primitive.Word64.word -> Primitive.IntInf.int |
| 209 |
|
| 210 |
val sextdFromIntInfToInt8: Primitive.IntInf.int -> Primitive.Int8.int |
| 211 |
val sextdFromIntInfToInt16: Primitive.IntInf.int -> Primitive.Int16.int |
| 212 |
val sextdFromIntInfToInt32: Primitive.IntInf.int -> Primitive.Int32.int |
| 213 |
val sextdFromIntInfToInt64: Primitive.IntInf.int -> Primitive.Int64.int |
| 214 |
val sextdFromIntInfToIntInf: Primitive.IntInf.int -> Primitive.IntInf.int |
| 215 |
val sextdFromIntInfToWord8: Primitive.IntInf.int -> Primitive.Word8.word |
| 216 |
val sextdFromIntInfToWord16: Primitive.IntInf.int -> Primitive.Word16.word |
| 217 |
val sextdFromIntInfToWord32: Primitive.IntInf.int -> Primitive.Word32.word |
| 218 |
val sextdFromIntInfToWord64: Primitive.IntInf.int -> Primitive.Word64.word |
| 219 |
|
| 220 |
|
| 221 |
val castFromInt8ToIntInf: Primitive.Int8.int -> Primitive.IntInf.int |
| 222 |
val castFromInt16ToIntInf: Primitive.Int16.int -> Primitive.IntInf.int |
| 223 |
val castFromInt32ToIntInf: Primitive.Int32.int -> Primitive.IntInf.int |
| 224 |
val castFromInt64ToIntInf: Primitive.Int64.int -> Primitive.IntInf.int |
| 225 |
val castFromWord8ToIntInf: Primitive.Word8.word -> Primitive.IntInf.int |
| 226 |
val castFromWord16ToIntInf: Primitive.Word16.word -> Primitive.IntInf.int |
| 227 |
val castFromWord32ToIntInf: Primitive.Word32.word -> Primitive.IntInf.int |
| 228 |
val castFromWord64ToIntInf: Primitive.Word64.word -> Primitive.IntInf.int |
| 229 |
|
| 230 |
val castFromIntInfToInt8: Primitive.IntInf.int -> Primitive.Int8.int |
| 231 |
val castFromIntInfToInt16: Primitive.IntInf.int -> Primitive.Int16.int |
| 232 |
val castFromIntInfToInt32: Primitive.IntInf.int -> Primitive.Int32.int |
| 233 |
val castFromIntInfToInt64: Primitive.IntInf.int -> Primitive.Int64.int |
| 234 |
val castFromIntInfToIntInf: Primitive.IntInf.int -> Primitive.IntInf.int |
| 235 |
val castFromIntInfToWord8: Primitive.IntInf.int -> Primitive.Word8.word |
| 236 |
val castFromIntInfToWord16: Primitive.IntInf.int -> Primitive.Word16.word |
| 237 |
val castFromIntInfToWord32: Primitive.IntInf.int -> Primitive.Word32.word |
| 238 |
val castFromIntInfToWord64: Primitive.IntInf.int -> Primitive.Word64.word |
| 239 |
|
| 240 |
|
| 241 |
val zchckFromInt8ToIntInf: Primitive.Int8.int -> Primitive.IntInf.int |
| 242 |
val zchckFromInt16ToIntInf: Primitive.Int16.int -> Primitive.IntInf.int |
| 243 |
val zchckFromInt32ToIntInf: Primitive.Int32.int -> Primitive.IntInf.int |
| 244 |
val zchckFromInt64ToIntInf: Primitive.Int64.int -> Primitive.IntInf.int |
| 245 |
val zchckFromWord8ToIntInf: Primitive.Word8.word -> Primitive.IntInf.int |
| 246 |
val zchckFromWord16ToIntInf: Primitive.Word16.word -> Primitive.IntInf.int |
| 247 |
val zchckFromWord32ToIntInf: Primitive.Word32.word -> Primitive.IntInf.int |
| 248 |
val zchckFromWord64ToIntInf: Primitive.Word64.word -> Primitive.IntInf.int |
| 249 |
|
| 250 |
val zchckFromIntInfToInt8: Primitive.IntInf.int -> Primitive.Int8.int |
| 251 |
val zchckFromIntInfToInt16: Primitive.IntInf.int -> Primitive.Int16.int |
| 252 |
val zchckFromIntInfToInt32: Primitive.IntInf.int -> Primitive.Int32.int |
| 253 |
val zchckFromIntInfToInt64: Primitive.IntInf.int -> Primitive.Int64.int |
| 254 |
val zchckFromIntInfToIntInf: Primitive.IntInf.int -> Primitive.IntInf.int |
| 255 |
val zchckFromIntInfToWord8: Primitive.IntInf.int -> Primitive.Word8.word |
| 256 |
val zchckFromIntInfToWord16: Primitive.IntInf.int -> Primitive.Word16.word |
| 257 |
val zchckFromIntInfToWord32: Primitive.IntInf.int -> Primitive.Word32.word |
| 258 |
val zchckFromIntInfToWord64: Primitive.IntInf.int -> Primitive.Word64.word |
| 259 |
|
| 260 |
|
| 261 |
val schckFromInt8ToIntInf: Primitive.Int8.int -> Primitive.IntInf.int |
| 262 |
val schckFromInt16ToIntInf: Primitive.Int16.int -> Primitive.IntInf.int |
| 263 |
val schckFromInt32ToIntInf: Primitive.Int32.int -> Primitive.IntInf.int |
| 264 |
val schckFromInt64ToIntInf: Primitive.Int64.int -> Primitive.IntInf.int |
| 265 |
val schckFromWord8ToIntInf: Primitive.Word8.word -> Primitive.IntInf.int |
| 266 |
val schckFromWord16ToIntInf: Primitive.Word16.word -> Primitive.IntInf.int |
| 267 |
val schckFromWord32ToIntInf: Primitive.Word32.word -> Primitive.IntInf.int |
| 268 |
val schckFromWord64ToIntInf: Primitive.Word64.word -> Primitive.IntInf.int |
| 269 |
|
| 270 |
val schckFromIntInfToInt8: Primitive.IntInf.int -> Primitive.Int8.int |
| 271 |
val schckFromIntInfToInt16: Primitive.IntInf.int -> Primitive.Int16.int |
| 272 |
val schckFromIntInfToInt32: Primitive.IntInf.int -> Primitive.Int32.int |
| 273 |
val schckFromIntInfToInt64: Primitive.IntInf.int -> Primitive.Int64.int |
| 274 |
val schckFromIntInfToIntInf: Primitive.IntInf.int -> Primitive.IntInf.int |
| 275 |
val schckFromIntInfToWord8: Primitive.IntInf.int -> Primitive.Word8.word |
| 276 |
val schckFromIntInfToWord16: Primitive.IntInf.int -> Primitive.Word16.word |
| 277 |
val schckFromIntInfToWord32: Primitive.IntInf.int -> Primitive.Word32.word |
| 278 |
val schckFromIntInfToWord64: Primitive.IntInf.int -> Primitive.Word64.word |
| 279 |
end |
| 280 |
signature PRIM_INTEGER = |
| 281 |
sig |
| 282 |
include PRIM_INTEGER |
| 283 |
|
| 284 |
val zextdFromIntInf: Primitive.IntInf.int -> int |
| 285 |
val zextdToIntInf: int -> Primitive.IntInf.int |
| 286 |
|
| 287 |
val sextdFromIntInf: Primitive.IntInf.int -> int |
| 288 |
val sextdToIntInf: int -> Primitive.IntInf.int |
| 289 |
|
| 290 |
val castFromIntInf: Primitive.IntInf.int -> int |
| 291 |
val castToIntInf: int -> Primitive.IntInf.int |
| 292 |
|
| 293 |
val zchckFromIntInf: Primitive.IntInf.int -> int |
| 294 |
val zchckToIntInf: int -> Primitive.IntInf.int |
| 295 |
|
| 296 |
val schckFromIntInf: Primitive.IntInf.int -> int |
| 297 |
val schckToIntInf: int -> Primitive.IntInf.int |
| 298 |
end |
| 299 |
signature PRIM_WORD = |
| 300 |
sig |
| 301 |
include PRIM_WORD |
| 302 |
|
| 303 |
val zextdFromIntInf: Primitive.IntInf.int -> word |
| 304 |
val zextdToIntInf: word -> Primitive.IntInf.int |
| 305 |
|
| 306 |
val sextdFromIntInf: Primitive.IntInf.int -> word |
| 307 |
val sextdToIntInf: word -> Primitive.IntInf.int |
| 308 |
|
| 309 |
val castFromIntInf: Primitive.IntInf.int -> word |
| 310 |
val castToIntInf: word -> Primitive.IntInf.int |
| 311 |
|
| 312 |
val zchckFromIntInf: Primitive.IntInf.int -> word |
| 313 |
val zchckToIntInf: word -> Primitive.IntInf.int |
| 314 |
|
| 315 |
val schckFromIntInf: Primitive.IntInf.int -> word |
| 316 |
val schckToIntInf: word -> Primitive.IntInf.int |
| 317 |
end |
| 318 |
|
| 319 |
structure Primitive = struct |
| 320 |
|
| 321 |
open Primitive |
| 322 |
|
| 323 |
structure IntInf = |
| 324 |
struct |
| 325 |
structure Prim = Primitive.IntInf |
| 326 |
structure MLton = Primitive.MLton |
| 327 |
|
| 328 |
structure A = Primitive.Array |
| 329 |
structure V = Primitive.Vector |
| 330 |
structure S = SeqIndex |
| 331 |
structure ObjptrWord = struct |
| 332 |
open ObjptrWord |
| 333 |
local |
| 334 |
structure S = |
| 335 |
ObjptrInt_ChooseIntN |
| 336 |
(type 'a t = 'a -> ObjptrWord.word |
| 337 |
val fInt8 = ObjptrWord.zextdFromInt8 |
| 338 |
val fInt16 = ObjptrWord.zextdFromInt16 |
| 339 |
val fInt32 = ObjptrWord.zextdFromInt32 |
| 340 |
val fInt64 = ObjptrWord.zextdFromInt64) |
| 341 |
in |
| 342 |
val idFromObjptrInt = S.f |
| 343 |
end |
| 344 |
local |
| 345 |
structure S = |
| 346 |
ObjptrInt_ChooseIntN |
| 347 |
(type 'a t = ObjptrWord.word -> 'a |
| 348 |
val fInt8 = ObjptrWord.zextdToInt8 |
| 349 |
val fInt16 = ObjptrWord.zextdToInt16 |
| 350 |
val fInt32 = ObjptrWord.zextdToInt32 |
| 351 |
val fInt64 = ObjptrWord.zextdToInt64) |
| 352 |
in |
| 353 |
val idToObjptrInt = S.f |
| 354 |
end |
| 355 |
local |
| 356 |
structure S = |
| 357 |
C_MPLimb_ChooseWordN |
| 358 |
(type 'a t = 'a -> ObjptrWord.word |
| 359 |
val fWord8 = ObjptrWord.castFromWord8 |
| 360 |
val fWord16 = ObjptrWord.castFromWord16 |
| 361 |
val fWord32 = ObjptrWord.castFromWord32 |
| 362 |
val fWord64 = ObjptrWord.castFromWord64) |
| 363 |
in |
| 364 |
val castFromMPLimb = S.f |
| 365 |
end |
| 366 |
local |
| 367 |
structure S = |
| 368 |
C_MPLimb_ChooseWordN |
| 369 |
(type 'a t = ObjptrWord.word -> 'a |
| 370 |
val fWord8 = ObjptrWord.castToWord8 |
| 371 |
val fWord16 = ObjptrWord.castToWord16 |
| 372 |
val fWord32 = ObjptrWord.castToWord32 |
| 373 |
val fWord64 = ObjptrWord.castToWord64) |
| 374 |
in |
| 375 |
val castToMPLimb = S.f |
| 376 |
end |
| 377 |
end |
| 378 |
structure W = ObjptrWord |
| 379 |
structure I = ObjptrInt |
| 380 |
structure MPLimb = C_MPLimb |
| 381 |
structure Sz = struct |
| 382 |
open C_Size |
| 383 |
local |
| 384 |
structure S = |
| 385 |
SeqIndex_ChooseIntN |
| 386 |
(type 'a t = 'a -> C_Size.word |
| 387 |
val fInt8 = C_Size.zextdFromInt8 |
| 388 |
val fInt16 = C_Size.zextdFromInt16 |
| 389 |
val fInt32 = C_Size.zextdFromInt32 |
| 390 |
val fInt64 = C_Size.zextdFromInt64) |
| 391 |
in |
| 392 |
val zextdFromSeqIndex = S.f |
| 393 |
end |
| 394 |
end |
| 395 |
|
| 396 |
type bigInt = Prim.int |
| 397 |
|
| 398 |
val zero: bigInt = 0 |
| 399 |
val one: bigInt = 1 |
| 400 |
val negOne: bigInt = ~1 |
| 401 |
|
| 402 |
(* Check if an IntInf.int is small (i.e., a fixnum). *) |
| 403 |
fun isSmall (i: bigInt): bool = |
| 404 |
0w0 <> W.andb (Prim.toWord i, 0w1) |
| 405 |
|
| 406 |
(* Check if two IntInf.int's are both small (i.e., fixnums). *) |
| 407 |
fun areSmall (i: bigInt, i': bigInt): bool = |
| 408 |
0w0 <> W.andb (W.andb (Prim.toWord i, Prim.toWord i'), 0w1) |
| 409 |
|
| 410 |
(* Return the number of `limbs' in a bigInt. *) |
| 411 |
fun bigNumLimbs i = S.- (V.length (Prim.toVector i), 1) |
| 412 |
fun numLimbs i = |
| 413 |
if isSmall i |
| 414 |
then 1 |
| 415 |
else bigNumLimbs i |
| 416 |
|
| 417 |
fun dropTag (w: W.word): W.word = W.~>>? (w, 0w1) |
| 418 |
fun dropTagCoerce (i: bigInt): W.word = dropTag (Prim.toWord i) |
| 419 |
fun dropTagCoerceInt (i: bigInt): I.int = W.idToObjptrInt (dropTagCoerce i) |
| 420 |
fun addTag (w: W.word): W.word = W.orb (W.<<? (w, 0w1), 0w1) |
| 421 |
fun addTagCoerce (w: W.word): bigInt = Prim.fromWord (addTag w) |
| 422 |
fun addTagCoerceInt (i: I.int): bigInt = addTagCoerce (W.idFromObjptrInt i) |
| 423 |
fun zeroTag (w: W.word): W.word = W.andb (w, W.notb 0w1) |
| 424 |
fun oneTag (w: W.word): W.word = W.orb (w, 0w1) |
| 425 |
fun oneTagCoerce (w: W.word): bigInt = Prim.fromWord (oneTag w) |
| 426 |
|
| 427 |
|
| 428 |
datatype rep = |
| 429 |
Big of MPLimb.t V.vector |
| 430 |
| Small of ObjptrInt.int |
| 431 |
|
| 432 |
fun rep i = |
| 433 |
if isSmall i |
| 434 |
then Small (dropTagCoerceInt i) |
| 435 |
else Big (Prim.toVector i) |
| 436 |
|
| 437 |
fun fromRep r = |
| 438 |
case r of |
| 439 |
Big v => |
| 440 |
let |
| 441 |
val limbsPerObjptr = |
| 442 |
if Int32.>= (MPLimb.sizeInBits, ObjptrWord.sizeInBits) |
| 443 |
then 1 |
| 444 |
else S.sextdFromInt32 (Int32.quot (ObjptrWord.sizeInBits, MPLimb.sizeInBits)) |
| 445 |
|
| 446 |
val l = V.length v |
| 447 |
val ok = |
| 448 |
(* sign limb + magnitude limb(s) *) |
| 449 |
S.>= (l, 2) andalso |
| 450 |
(* sign limb is 0w0 (positive) or 0w1 (negative) *) |
| 451 |
MPLimb.<= (V.unsafeSub (v, 0), 0w1) andalso |
| 452 |
(* most-significant magnitude limb is non-zero *) |
| 453 |
MPLimb.> (V.unsafeSub (v, S.- (l, 1)), 0w0) andalso |
| 454 |
(* value exceeds Small representation; |
| 455 |
* if positive, then mag in [1, 2^(ObjptrWord.sizeInBits - 2)]. |
| 456 |
* if negative, then mag in [0, 2^(ObjptrWord.sizeInBits - 2) - 1]. |
| 457 |
*) |
| 458 |
(S.> (l, S.+ (1, limbsPerObjptr)) orelse |
| 459 |
if Int32.<= (ObjptrWord.sizeInBits, MPLimb.sizeInBits) |
| 460 |
then let |
| 461 |
val mag = V.unsafeSub (v, 1) |
| 462 |
in |
| 463 |
MPLimb.>= |
| 464 |
(if MPLimb.>= (V.unsafeSub (v, 0), 0w1) then MPLimb.- (mag, 0w1) else mag, |
| 465 |
MPLimb.<<? (0w1, Word32.- (ObjptrWord.sizeInBitsWord, 0w2))) |
| 466 |
end |
| 467 |
else let |
| 468 |
fun loop (i, mag) = |
| 469 |
if S.< (i, l) |
| 470 |
then ObjptrWord.andb |
| 471 |
(ObjptrWord.<<? (mag, MPLimb.sizeInBitsWord), |
| 472 |
W.castFromMPLimb (V.unsafeSub (v, i))) |
| 473 |
else mag |
| 474 |
val mag = loop (2, W.castFromMPLimb (V.unsafeSub (v, 1))) |
| 475 |
in |
| 476 |
ObjptrWord.>= |
| 477 |
(if MPLimb.>= (V.unsafeSub (v, 0), 0w1) then ObjptrWord.- (mag, 0w1) else mag, |
| 478 |
ObjptrWord.<<? (0w1, Word32.- (ObjptrWord.sizeInBitsWord, 0w2))) |
| 479 |
end) |
| 480 |
in |
| 481 |
if ok |
| 482 |
then SOME (Prim.fromVector v) |
| 483 |
else NONE |
| 484 |
end |
| 485 |
| Small i => |
| 486 |
let |
| 487 |
val w = ObjptrWord.idFromObjptrInt i |
| 488 |
val wt = addTag w |
| 489 |
val ok = w = dropTag wt |
| 490 |
in |
| 491 |
if ok |
| 492 |
then SOME (Prim.fromWord wt) |
| 493 |
else NONE |
| 494 |
end |
| 495 |
|
| 496 |
|
| 497 |
local |
| 498 |
fun 'a make {zextdToMPLimb: 'a -> MPLimb.word, |
| 499 |
zextdToObjptrWord: 'a -> ObjptrWord.word, |
| 500 |
sextdToObjptrWord: 'a -> ObjptrWord.word, |
| 501 |
other : {sizeInBits: Int32.int, |
| 502 |
zero: 'a, |
| 503 |
eq: 'a * 'a -> bool, |
| 504 |
isNeg: 'a -> bool, |
| 505 |
neg: 'a -> 'a, |
| 506 |
notb: 'a -> 'a, |
| 507 |
rashift: 'a * Word32.word -> 'a, |
| 508 |
rshift: 'a * Word32.word -> 'a}} |
| 509 |
(sextd, w) = |
| 510 |
if Int32.> (ObjptrWord.sizeInBits, #sizeInBits other) |
| 511 |
orelse let |
| 512 |
val shift = Word32.- (ObjptrWord.sizeInBitsWord, 0w2) |
| 513 |
val upperBits = (#rashift other) (w, shift) |
| 514 |
val zeroBits = #zero other |
| 515 |
val oneBits = (#notb other) zeroBits |
| 516 |
in |
| 517 |
(#eq other) (upperBits, zeroBits) |
| 518 |
orelse |
| 519 |
(sextd andalso (#eq other) (upperBits, oneBits)) |
| 520 |
end |
| 521 |
then if sextd |
| 522 |
then Prim.fromWord (addTag (sextdToObjptrWord w)) |
| 523 |
else Prim.fromWord (addTag (zextdToObjptrWord w)) |
| 524 |
else let |
| 525 |
fun loop (w, i, acc) = |
| 526 |
if (#eq other) (w, (#zero other)) |
| 527 |
then (i, acc) |
| 528 |
else |
| 529 |
let |
| 530 |
val limb = zextdToMPLimb w |
| 531 |
val w = |
| 532 |
(#rshift other) |
| 533 |
(w, MPLimb.sizeInBitsWord) |
| 534 |
in |
| 535 |
loop (w, S.+ (i, 1), (i, limb) :: acc) |
| 536 |
end |
| 537 |
val (n, acc) = |
| 538 |
if sextd andalso (#isNeg other) w |
| 539 |
then loop ((#neg other) w, 1, [(0,0w1)]) |
| 540 |
else loop (w, 1, [(0,0w0)]) |
| 541 |
val a = A.unsafeAlloc n |
| 542 |
fun loop acc = |
| 543 |
case acc of |
| 544 |
[] => () |
| 545 |
| (i, v) :: acc => (A.unsafeUpdate (a, i, v) |
| 546 |
; loop acc) |
| 547 |
val () = loop acc |
| 548 |
in |
| 549 |
Prim.fromVector (V.unsafeFromArray a) |
| 550 |
end |
| 551 |
in |
| 552 |
fun extdFromWord8 (sextd, w) = |
| 553 |
make {zextdToMPLimb = MPLimb.zextdFromWord8, |
| 554 |
zextdToObjptrWord = ObjptrWord.zextdFromWord8, |
| 555 |
sextdToObjptrWord = ObjptrWord.sextdFromWord8, |
| 556 |
other = {sizeInBits = Word8.sizeInBits, |
| 557 |
zero = Word8.zero, |
| 558 |
eq = ((op =) : Word8.word * Word8.word -> bool), |
| 559 |
isNeg = fn w => Int8.< (IntWordConv.idFromWord8ToInt8 w, 0), |
| 560 |
neg = Word8.~, |
| 561 |
notb = Word8.notb, |
| 562 |
rashift = Word8.~>>?, |
| 563 |
rshift = Word8.>>?}} |
| 564 |
(sextd, w) |
| 565 |
fun zextdFromWord8 w = extdFromWord8 (false, w) |
| 566 |
fun zextdFromInt8 i = zextdFromWord8 (IntWordConv.idFromInt8ToWord8 i) |
| 567 |
fun sextdFromWord8 w = extdFromWord8 (true, w) |
| 568 |
fun sextdFromInt8 i = sextdFromWord8 (IntWordConv.idFromInt8ToWord8 i) |
| 569 |
val castFromInt8 = sextdFromInt8 |
| 570 |
val castFromWord8 = zextdFromWord8 |
| 571 |
val zchckFromInt8 = zextdFromInt8 |
| 572 |
val zchckFromWord8 = zextdFromWord8 |
| 573 |
val schckFromInt8 = sextdFromInt8 |
| 574 |
val schckFromWord8 = sextdFromWord8 |
| 575 |
|
| 576 |
fun extdFromWord16 (sextd, w) = |
| 577 |
make {zextdToMPLimb = MPLimb.zextdFromWord16, |
| 578 |
zextdToObjptrWord = ObjptrWord.zextdFromWord16, |
| 579 |
sextdToObjptrWord = ObjptrWord.sextdFromWord16, |
| 580 |
other = {sizeInBits = Word16.sizeInBits, |
| 581 |
zero = Word16.zero, |
| 582 |
eq = ((op =) : Word16.word * Word16.word -> bool), |
| 583 |
isNeg = fn w => Int16.< (IntWordConv.idFromWord16ToInt16 w, 0), |
| 584 |
neg = Word16.~, |
| 585 |
notb = Word16.notb, |
| 586 |
rashift = Word16.~>>?, |
| 587 |
rshift = Word16.>>?}} |
| 588 |
(sextd, w) |
| 589 |
fun zextdFromWord16 w = extdFromWord16 (false, w) |
| 590 |
fun zextdFromInt16 i = zextdFromWord16 (IntWordConv.idFromInt16ToWord16 i) |
| 591 |
fun sextdFromWord16 w = extdFromWord16 (true, w) |
| 592 |
fun sextdFromInt16 i = sextdFromWord16 (IntWordConv.idFromInt16ToWord16 i) |
| 593 |
val castFromInt16 = sextdFromInt16 |
| 594 |
val castFromWord16 = zextdFromWord16 |
| 595 |
val zchckFromInt16 = zextdFromInt16 |
| 596 |
val zchckFromWord16 = zextdFromWord16 |
| 597 |
val schckFromInt16 = sextdFromInt16 |
| 598 |
val schckFromWord16 = sextdFromWord16 |
| 599 |
|
| 600 |
fun extdFromWord32 (sextd, w) = |
| 601 |
make {zextdToMPLimb = MPLimb.zextdFromWord32, |
| 602 |
zextdToObjptrWord = ObjptrWord.zextdFromWord32, |
| 603 |
sextdToObjptrWord = ObjptrWord.sextdFromWord32, |
| 604 |
other = {sizeInBits = Word32.sizeInBits, |
| 605 |
zero = Word32.zero, |
| 606 |
eq = ((op =) : Word32.word * Word32.word -> bool), |
| 607 |
isNeg = fn w => Int32.< (IntWordConv.idFromWord32ToInt32 w, 0), |
| 608 |
neg = Word32.~, |
| 609 |
notb = Word32.notb, |
| 610 |
rashift = Word32.~>>?, |
| 611 |
rshift = Word32.>>?}} |
| 612 |
(sextd, w) |
| 613 |
fun zextdFromWord32 w = extdFromWord32 (false, w) |
| 614 |
fun zextdFromInt32 i = zextdFromWord32 (IntWordConv.idFromInt32ToWord32 i) |
| 615 |
fun sextdFromWord32 w = extdFromWord32 (true, w) |
| 616 |
fun sextdFromInt32 i = sextdFromWord32 (IntWordConv.idFromInt32ToWord32 i) |
| 617 |
val castFromInt32 = sextdFromInt32 |
| 618 |
val castFromWord32 = zextdFromWord32 |
| 619 |
val zchckFromInt32 = zextdFromInt32 |
| 620 |
val zchckFromWord32 = zextdFromWord32 |
| 621 |
val schckFromInt32 = sextdFromInt32 |
| 622 |
val schckFromWord32 = sextdFromWord32 |
| 623 |
|
| 624 |
fun extdFromWord64 (sextd, w) = |
| 625 |
make {zextdToMPLimb = MPLimb.zextdFromWord64, |
| 626 |
zextdToObjptrWord = ObjptrWord.zextdFromWord64, |
| 627 |
sextdToObjptrWord = ObjptrWord.sextdFromWord64, |
| 628 |
other = {sizeInBits = Word64.sizeInBits, |
| 629 |
zero = Word64.zero, |
| 630 |
eq = ((op =) : Word64.word * Word64.word -> bool), |
| 631 |
isNeg = fn w => Int64.< (IntWordConv.idFromWord64ToInt64 w, 0), |
| 632 |
neg = Word64.~, |
| 633 |
notb = Word64.notb, |
| 634 |
rashift = Word64.~>>?, |
| 635 |
rshift = Word64.>>?}} |
| 636 |
(sextd, w) |
| 637 |
fun zextdFromWord64 w = extdFromWord64 (false, w) |
| 638 |
fun zextdFromInt64 i = zextdFromWord64 (IntWordConv.idFromInt64ToWord64 i) |
| 639 |
fun sextdFromWord64 w = extdFromWord64 (true, w) |
| 640 |
fun sextdFromInt64 i = sextdFromWord64 (IntWordConv.idFromInt64ToWord64 i) |
| 641 |
val castFromInt64 = sextdFromInt64 |
| 642 |
val castFromWord64 = zextdFromWord64 |
| 643 |
val zchckFromInt64 = zextdFromInt64 |
| 644 |
val zchckFromWord64 = zextdFromWord64 |
| 645 |
val schckFromInt64 = sextdFromInt64 |
| 646 |
val schckFromWord64 = sextdFromWord64 |
| 647 |
|
| 648 |
fun zextdFromIntInf ii = ii |
| 649 |
fun sextdFromIntInf ii = ii |
| 650 |
fun castFromIntInf ii = ii |
| 651 |
fun zchckFromIntInf ii = ii |
| 652 |
fun schckFromIntInf ii = ii |
| 653 |
end |
| 654 |
|
| 655 |
local |
| 656 |
structure S = |
| 657 |
ObjptrInt_ChooseIntN |
| 658 |
(type 'a t = 'a -> bigInt |
| 659 |
val fInt8 = sextdFromInt8 |
| 660 |
val fInt16 = sextdFromInt16 |
| 661 |
val fInt32 = sextdFromInt32 |
| 662 |
val fInt64 = sextdFromInt64) |
| 663 |
in |
| 664 |
val sextdFromObjptrInt = S.f |
| 665 |
end |
| 666 |
|
| 667 |
local |
| 668 |
datatype 'a ans = |
| 669 |
Big of bool * bool * 'a |
| 670 |
| Small of ObjptrWord.word |
| 671 |
fun 'a make {zextdFromMPLimb: MPLimb.word -> 'a, |
| 672 |
other : {sizeInBits: Int32.int, |
| 673 |
sizeInBitsWord: Word32.word, |
| 674 |
zero: 'a, |
| 675 |
lshift: 'a * Word32.word -> 'a, |
| 676 |
orb: 'a * 'a -> 'a}} i = |
| 677 |
if isSmall i |
| 678 |
then Small (dropTagCoerce i) |
| 679 |
else let |
| 680 |
val v = Prim.toVector i |
| 681 |
val n = V.length v |
| 682 |
val isneg = V.unsafeSub (v, 0) <> 0w0 |
| 683 |
in |
| 684 |
if Int32.>= (MPLimb.sizeInBits, #sizeInBits other) |
| 685 |
then let |
| 686 |
val limbsPer : S.t = 1 |
| 687 |
val limb = V.unsafeSub (v, 1) |
| 688 |
val extra = |
| 689 |
S.> (n, S.+ (limbsPer, 1)) |
| 690 |
orelse |
| 691 |
(MPLimb.>>? (limb, #sizeInBitsWord other)) <> 0w0 |
| 692 |
val ans = zextdFromMPLimb limb |
| 693 |
in |
| 694 |
Big (isneg, extra, ans) |
| 695 |
end |
| 696 |
else let |
| 697 |
val limbsPer = |
| 698 |
S.sextdFromInt32 |
| 699 |
(Int32.quot (#sizeInBits other, |
| 700 |
MPLimb.sizeInBits)) |
| 701 |
val extra = S.> (n, S.+ (limbsPer, 1)) |
| 702 |
val ans = |
| 703 |
let |
| 704 |
fun loop (i, ans) = |
| 705 |
if S.> (i, 0) |
| 706 |
then let |
| 707 |
val limb = V.unsafeSub (v, i) |
| 708 |
val ans = |
| 709 |
(#orb other) |
| 710 |
((#lshift other) |
| 711 |
(ans, MPLimb.sizeInBitsWord), |
| 712 |
zextdFromMPLimb limb) |
| 713 |
in |
| 714 |
loop (S.- (i, 1), ans) |
| 715 |
end |
| 716 |
else ans |
| 717 |
in |
| 718 |
loop (S.min (S.- (n, 1), limbsPer), #zero other) |
| 719 |
end |
| 720 |
in |
| 721 |
Big (isneg, extra, ans) |
| 722 |
end |
| 723 |
end |
| 724 |
in |
| 725 |
val chckToWord8Aux = |
| 726 |
make {zextdFromMPLimb = MPLimb.zextdToWord8, |
| 727 |
other = {sizeInBits = Word8.sizeInBits, |
| 728 |
sizeInBitsWord = Word8.sizeInBitsWord, |
| 729 |
zero = Word8.zero, |
| 730 |
lshift = Word8.<<?, |
| 731 |
orb = Word8.orb}} |
| 732 |
fun sextdToWord8 i = |
| 733 |
case chckToWord8Aux i of |
| 734 |
Small w => ObjptrWord.sextdToWord8 w |
| 735 |
| Big (isneg, _, ans) => if isneg then Word8.~ ans else ans |
| 736 |
fun sextdToInt8 i = IntWordConv.idFromWord8ToInt8 (sextdToWord8 i) |
| 737 |
val zextdToWord8 = sextdToWord8 |
| 738 |
fun zextdToInt8 i = IntWordConv.idFromWord8ToInt8 (zextdToWord8 i) |
| 739 |
val castToWord8 = sextdToWord8 |
| 740 |
val castToInt8 = sextdToInt8 |
| 741 |
fun schckToWord8 i = |
| 742 |
if not Primitive.Controls.detectOverflow |
| 743 |
then sextdToWord8 i |
| 744 |
else |
| 745 |
case chckToWord8Aux i of |
| 746 |
Small w => ObjptrWord.schckToWord8 w |
| 747 |
| Big (isneg, extra, ans) => |
| 748 |
if extra |
| 749 |
then raise Overflow |
| 750 |
else if isneg |
| 751 |
then let |
| 752 |
val ans = Word8.~ ans |
| 753 |
val ans' = IntWordConv.idFromWord8ToInt8 ans |
| 754 |
in |
| 755 |
if Int8.> (ans', 0) |
| 756 |
then raise Overflow |
| 757 |
else ans |
| 758 |
end |
| 759 |
else let |
| 760 |
val ans' = IntWordConv.idFromWord8ToInt8 ans |
| 761 |
in |
| 762 |
if Int8.< (ans', 0) |
| 763 |
then raise Overflow |
| 764 |
else ans |
| 765 |
end |
| 766 |
fun schckToInt8 i = IntWordConv.idFromWord8ToInt8 (schckToWord8 i) |
| 767 |
fun zchckToWord8 i = |
| 768 |
if not Primitive.Controls.detectOverflow |
| 769 |
then zextdToWord8 i |
| 770 |
else |
| 771 |
case chckToWord8Aux i of |
| 772 |
Small w => ObjptrWord.schckToWord8 w |
| 773 |
| Big (isneg, extra, ans) => |
| 774 |
if isneg orelse extra |
| 775 |
then raise Overflow |
| 776 |
else ans |
| 777 |
fun zchckToInt8 i = IntWordConv.idFromWord8ToInt8 (zchckToWord8 i) |
| 778 |
|
| 779 |
val chckToWord16Aux = |
| 780 |
make {zextdFromMPLimb = MPLimb.zextdToWord16, |
| 781 |
other = {sizeInBits = Word16.sizeInBits, |
| 782 |
sizeInBitsWord = Word16.sizeInBitsWord, |
| 783 |
zero = Word16.zero, |
| 784 |
lshift = Word16.<<?, |
| 785 |
orb = Word16.orb}} |
| 786 |
fun sextdToWord16 i = |
| 787 |
case chckToWord16Aux i of |
| 788 |
Small w => ObjptrWord.sextdToWord16 w |
| 789 |
| Big (isneg, _, ans) => if isneg then Word16.~ ans else ans |
| 790 |
fun sextdToInt16 i = IntWordConv.idFromWord16ToInt16 (sextdToWord16 i) |
| 791 |
val zextdToWord16 = sextdToWord16 |
| 792 |
fun zextdToInt16 i = IntWordConv.idFromWord16ToInt16 (zextdToWord16 i) |
| 793 |
val castToWord16 = sextdToWord16 |
| 794 |
val castToInt16 = sextdToInt16 |
| 795 |
fun schckToWord16 i = |
| 796 |
if not Primitive.Controls.detectOverflow |
| 797 |
then sextdToWord16 i |
| 798 |
else |
| 799 |
case chckToWord16Aux i of |
| 800 |
Small w => ObjptrWord.schckToWord16 w |
| 801 |
| Big (isneg, extra, ans) => |
| 802 |
if extra |
| 803 |
then raise Overflow |
| 804 |
else if isneg |
| 805 |
then let |
| 806 |
val ans = Word16.~ ans |
| 807 |
val ans' = IntWordConv.idFromWord16ToInt16 ans |
| 808 |
in |
| 809 |
if Int16.> (ans', 0) |
| 810 |
then raise Overflow |
| 811 |
else ans |
| 812 |
end |
| 813 |
else let |
| 814 |
val ans' = IntWordConv.idFromWord16ToInt16 ans |
| 815 |
in |
| 816 |
if Int16.< (ans', 0) |
| 817 |
then raise Overflow |
| 818 |
else ans |
| 819 |
end |
| 820 |
fun schckToInt16 i = IntWordConv.idFromWord16ToInt16 (schckToWord16 i) |
| 821 |
fun zchckToWord16 i = |
| 822 |
if not Primitive.Controls.detectOverflow |
| 823 |
then zextdToWord16 i |
| 824 |
else |
| 825 |
case chckToWord16Aux i of |
| 826 |
Small w => ObjptrWord.schckToWord16 w |
| 827 |
| Big (isneg, extra, ans) => |
| 828 |
if isneg orelse extra |
| 829 |
then raise Overflow |
| 830 |
else ans |
| 831 |
fun zchckToInt16 i = IntWordConv.idFromWord16ToInt16 (zchckToWord16 i) |
| 832 |
|
| 833 |
val chckToWord32Aux = |
| 834 |
make {zextdFromMPLimb = MPLimb.zextdToWord32, |
| 835 |
other = {sizeInBits = Word32.sizeInBits, |
| 836 |
sizeInBitsWord = Word32.sizeInBitsWord, |
| 837 |
zero = Word32.zero, |
| 838 |
lshift = Word32.<<?, |
| 839 |
orb = Word32.orb}} |
| 840 |
fun sextdToWord32 i = |
| 841 |
case chckToWord32Aux i of |
| 842 |
Small w => ObjptrWord.sextdToWord32 w |
| 843 |
| Big (isneg, _, ans) => if isneg then Word32.~ ans else ans |
| 844 |
fun sextdToInt32 i = IntWordConv.idFromWord32ToInt32 (sextdToWord32 i) |
| 845 |
val zextdToWord32 = sextdToWord32 |
| 846 |
fun zextdToInt32 i = IntWordConv.idFromWord32ToInt32 (zextdToWord32 i) |
| 847 |
val castToWord32 = sextdToWord32 |
| 848 |
val castToInt32 = sextdToInt32 |
| 849 |
fun schckToWord32 i = |
| 850 |
if not Primitive.Controls.detectOverflow |
| 851 |
then sextdToWord32 i |
| 852 |
else |
| 853 |
case chckToWord32Aux i of |
| 854 |
Small w => ObjptrWord.schckToWord32 w |
| 855 |
| Big (isneg, extra, ans) => |
| 856 |
if extra |
| 857 |
then raise Overflow |
| 858 |
else if isneg |
| 859 |
then let |
| 860 |
val ans = Word32.~ ans |
| 861 |
val ans' = IntWordConv.idFromWord32ToInt32 ans |
| 862 |
in |
| 863 |
if Int32.> (ans', 0) |
| 864 |
then raise Overflow |
| 865 |
else ans |
| 866 |
end |
| 867 |
else let |
| 868 |
val ans' = IntWordConv.idFromWord32ToInt32 ans |
| 869 |
in |
| 870 |
if Int32.< (ans', 0) |
| 871 |
then raise Overflow |
| 872 |
else ans |
| 873 |
end |
| 874 |
fun schckToInt32 i = IntWordConv.idFromWord32ToInt32 (schckToWord32 i) |
| 875 |
fun zchckToWord32 i = |
| 876 |
if not Primitive.Controls.detectOverflow |
| 877 |
then zextdToWord32 i |
| 878 |
else |
| 879 |
case chckToWord32Aux i of |
| 880 |
Small w => ObjptrWord.schckToWord32 w |
| 881 |
| Big (isneg, extra, ans) => |
| 882 |
if isneg orelse extra |
| 883 |
then raise Overflow |
| 884 |
else ans |
| 885 |
fun zchckToInt32 i = IntWordConv.idFromWord32ToInt32 (zchckToWord32 i) |
| 886 |
|
| 887 |
val chckToWord64Aux = |
| 888 |
make {zextdFromMPLimb = MPLimb.zextdToWord64, |
| 889 |
other = {sizeInBits = Word64.sizeInBits, |
| 890 |
sizeInBitsWord = Word64.sizeInBitsWord, |
| 891 |
zero = Word64.zero, |
| 892 |
lshift = Word64.<<?, |
| 893 |
orb = Word64.orb}} |
| 894 |
fun sextdToWord64 i = |
| 895 |
case chckToWord64Aux i of |
| 896 |
Small w => ObjptrWord.sextdToWord64 w |
| 897 |
| Big (isneg, _, ans) => if isneg then Word64.~ ans else ans |
| 898 |
fun sextdToInt64 i = IntWordConv.idFromWord64ToInt64 (sextdToWord64 i) |
| 899 |
val zextdToWord64 = sextdToWord64 |
| 900 |
fun zextdToInt64 i = IntWordConv.idFromWord64ToInt64 (zextdToWord64 i) |
| 901 |
val castToWord64 = sextdToWord64 |
| 902 |
val castToInt64 = sextdToInt64 |
| 903 |
fun schckToWord64 i = |
| 904 |
if not Primitive.Controls.detectOverflow |
| 905 |
then sextdToWord64 i |
| 906 |
else |
| 907 |
case chckToWord64Aux i of |
| 908 |
Small w => ObjptrWord.schckToWord64 w |
| 909 |
| Big (isneg, extra, ans) => |
| 910 |
if extra |
| 911 |
then raise Overflow |
| 912 |
else if isneg |
| 913 |
then let |
| 914 |
val ans = Word64.~ ans |
| 915 |
val ans' = IntWordConv.idFromWord64ToInt64 ans |
| 916 |
in |
| 917 |
if Int64.> (ans', 0) |
| 918 |
then raise Overflow |
| 919 |
else ans |
| 920 |
end |
| 921 |
else let |
| 922 |
val ans' = IntWordConv.idFromWord64ToInt64 ans |
| 923 |
in |
| 924 |
if Int64.< (ans', 0) |
| 925 |
then raise Overflow |
| 926 |
else ans |
| 927 |
end |
| 928 |
fun schckToInt64 i = IntWordConv.idFromWord64ToInt64 (schckToWord64 i) |
| 929 |
fun zchckToWord64 i = |
| 930 |
if not Primitive.Controls.detectOverflow |
| 931 |
then zextdToWord64 i |
| 932 |
else |
| 933 |
case chckToWord64Aux i of |
| 934 |
Small w => ObjptrWord.schckToWord64 w |
| 935 |
| Big (isneg, extra, ans) => |
| 936 |
if isneg orelse extra |
| 937 |
then raise Overflow |
| 938 |
else ans |
| 939 |
fun zchckToInt64 i = IntWordConv.idFromWord64ToInt64 (zchckToWord64 i) |
| 940 |
|
| 941 |
fun zextdToIntInf ii = ii |
| 942 |
fun sextdToIntInf ii = ii |
| 943 |
fun castToIntInf ii = ii |
| 944 |
fun zchckToIntInf ii = ii |
| 945 |
fun schckToIntInf ii = ii |
| 946 |
end |
| 947 |
|
| 948 |
local |
| 949 |
val bytesPerMPLimb = Sz.zextdFromInt32 (Int32.quot (MPLimb.sizeInBits, 8)) |
| 950 |
in |
| 951 |
val bytesPerSequenceMetaData = Sz.zextdFromInt32 SequenceMetaDataSize.bytes |
| 952 |
(* Reserve heap space for a large IntInf.int with room for num + extra |
| 953 |
* `limbs'. The reason for splitting this up is that extra is intended |
| 954 |
* to be a constant, and so can be combined at compile time. |
| 955 |
*) |
| 956 |
fun reserve (num: S.int, extra: S.int) = |
| 957 |
Sz.+ (Sz.* (bytesPerMPLimb, Sz.zextdFromSeqIndex num), |
| 958 |
Sz.+ (Sz.* (bytesPerMPLimb, Sz.zextdFromSeqIndex extra), |
| 959 |
Sz.+ (bytesPerMPLimb, (* isneg Field *) |
| 960 |
Sz.+ (bytesPerSequenceMetaData, (* Sequence MetaData *) |
| 961 |
case MLton.Align.align of (* alignment *) |
| 962 |
MLton.Align.Align4 => 0w3 |
| 963 |
| MLton.Align.Align8 => 0w7 |
| 964 |
)))) |
| 965 |
end |
| 966 |
|
| 967 |
(* badObjptr{Int,Word}{,Tagged} is the fixnum IntInf.int whose |
| 968 |
* negation and absolute values are not fixnums. |
| 969 |
* negBadIntInf is the negation (and absolute value) of that IntInf.int. |
| 970 |
*) |
| 971 |
val badObjptrInt: I.int = I.~>>? (I.minInt', 0w1) |
| 972 |
val badObjptrWord: W.word = W.idFromObjptrInt badObjptrInt |
| 973 |
val badObjptrWordTagged: W.word = addTag badObjptrWord |
| 974 |
(* val badObjptrIntTagged: I.int = W.idToObjptrInt badObjptrWordTagged *) |
| 975 |
val negBadIntInf: bigInt = sextdFromObjptrInt (I.~ badObjptrInt) |
| 976 |
|
| 977 |
(* Given two ObjptrWord.word's, check if they have the same 'high'/'sign' bit. |
| 978 |
*) |
| 979 |
fun sameSignBit (lhs: W.word, rhs: W.word): bool = |
| 980 |
I.>= (W.idToObjptrInt (W.xorb (lhs, rhs)), 0) |
| 981 |
|
| 982 |
(* Given a bignum bigint, test if it is (strictly) negative. |
| 983 |
*) |
| 984 |
fun bigIsNeg (arg: bigInt): bool = |
| 985 |
V.unsafeSub (Prim.toVector arg, 0) <> 0w0 |
| 986 |
|
| 987 |
local |
| 988 |
fun make (smallOp, bigOp, limbsFn, extra) |
| 989 |
(lhs: bigInt, rhs: bigInt): bigInt = |
| 990 |
let |
| 991 |
val res = |
| 992 |
if areSmall (lhs, rhs) |
| 993 |
then let |
| 994 |
val lhsw = dropTagCoerce lhs |
| 995 |
val lhsi = W.idToObjptrInt lhsw |
| 996 |
val rhsw = dropTagCoerce rhs |
| 997 |
val rhsi = W.idToObjptrInt rhsw |
| 998 |
val ansi = smallOp (lhsi, rhsi) |
| 999 |
val answ = W.idFromObjptrInt ansi |
| 1000 |
val ans = addTag answ |
| 1001 |
in |
| 1002 |
if sameSignBit (ans, answ) |
| 1003 |
then SOME (Prim.fromWord ans) |
| 1004 |
else NONE |
| 1005 |
end handle Overflow => NONE |
| 1006 |
else NONE |
| 1007 |
in |
| 1008 |
case res of |
| 1009 |
NONE => bigOp (lhs, rhs, |
| 1010 |
reserve (limbsFn (numLimbs lhs, numLimbs rhs), extra)) |
| 1011 |
| SOME i => i |
| 1012 |
end |
| 1013 |
in |
| 1014 |
val bigAdd = make (I.+$, Prim.+, S.max, 1) |
| 1015 |
val bigSub = make (I.-$, Prim.-, S.max, 1) |
| 1016 |
val bigMul = make (I.*$, Prim.*, S.+, 0) |
| 1017 |
end |
| 1018 |
|
| 1019 |
fun bigNeg (arg: bigInt): bigInt = |
| 1020 |
if isSmall arg |
| 1021 |
then let |
| 1022 |
val argw = Prim.toWord arg |
| 1023 |
in |
| 1024 |
if argw = badObjptrWordTagged |
| 1025 |
then negBadIntInf |
| 1026 |
else Prim.fromWord (W.- (0w2, argw)) |
| 1027 |
end |
| 1028 |
else Prim.~ (arg, reserve (numLimbs arg, 1)) |
| 1029 |
|
| 1030 |
|
| 1031 |
fun bigQuot (num: bigInt, den: bigInt): bigInt = |
| 1032 |
if areSmall (num, den) |
| 1033 |
then let |
| 1034 |
val numw = dropTagCoerce num |
| 1035 |
val numi = W.idToObjptrInt numw |
| 1036 |
val denw = dropTagCoerce den |
| 1037 |
val deni = W.idToObjptrInt denw |
| 1038 |
in |
| 1039 |
if numw = badObjptrWord |
| 1040 |
andalso deni = ~1 |
| 1041 |
then negBadIntInf |
| 1042 |
else let |
| 1043 |
val ansi = I.quot (numi, deni) |
| 1044 |
val answ = W.idFromObjptrInt ansi |
| 1045 |
val ans = addTag answ |
| 1046 |
in |
| 1047 |
Prim.fromWord ans |
| 1048 |
end |
| 1049 |
end |
| 1050 |
else let |
| 1051 |
val nlimbs = numLimbs num |
| 1052 |
val dlimbs = numLimbs den |
| 1053 |
in |
| 1054 |
if S.< (nlimbs, dlimbs) |
| 1055 |
then zero |
| 1056 |
else if den = zero |
| 1057 |
then raise Div |
| 1058 |
else Prim.quot (num, den, |
| 1059 |
reserve (S.- (nlimbs, dlimbs), 2)) |
| 1060 |
end |
| 1061 |
|
| 1062 |
fun bigRem (num: bigInt, den: bigInt): bigInt = |
| 1063 |
if areSmall (num, den) |
| 1064 |
then let |
| 1065 |
val numw = dropTagCoerce num |
| 1066 |
val numi = W.idToObjptrInt numw |
| 1067 |
val denw = dropTagCoerce den |
| 1068 |
val deni = W.idToObjptrInt denw |
| 1069 |
val ansi = I.rem (numi, deni) |
| 1070 |
val answ = W.idFromObjptrInt ansi |
| 1071 |
val ans = addTag answ |
| 1072 |
in |
| 1073 |
Prim.fromWord ans |
| 1074 |
end |
| 1075 |
else let |
| 1076 |
val nlimbs = numLimbs num |
| 1077 |
val dlimbs = numLimbs den |
| 1078 |
in |
| 1079 |
if S.< (nlimbs, dlimbs) |
| 1080 |
then num |
| 1081 |
else if den = zero |
| 1082 |
then raise Div |
| 1083 |
else Prim.rem (num, den, |
| 1084 |
reserve (dlimbs, 1)) |
| 1085 |
end |
| 1086 |
|
| 1087 |
(* Based on code from PolySpace. *) |
| 1088 |
local |
| 1089 |
open I |
| 1090 |
|
| 1091 |
fun mod2 x = I.andb (x, 1) |
| 1092 |
fun div2 x = I.>>? (x, 0w1) |
| 1093 |
|
| 1094 |
fun smallGcd (a, b, acc) = |
| 1095 |
case (a, b) of |
| 1096 |
(0, _) => b * acc |
| 1097 |
| (_, 0) => a * acc |
| 1098 |
| (_, 1) => acc |
| 1099 |
| (1, _) => acc |
| 1100 |
| (_ : I.t * I.t) => |
| 1101 |
if a = b |
| 1102 |
then a * acc |
| 1103 |
else |
| 1104 |
let |
| 1105 |
val a_2 = div2 a |
| 1106 |
val a_r2 = mod2 a |
| 1107 |
val b_2 = div2 b |
| 1108 |
val b_r2 = mod2 b |
| 1109 |
in |
| 1110 |
if 0 = a_r2 |
| 1111 |
then |
| 1112 |
if 0 = b_r2 |
| 1113 |
then smallGcd (a_2, b_2, acc + acc) |
| 1114 |
else smallGcd (a_2, b, acc) |
| 1115 |
else |
| 1116 |
if 0 = b_r2 |
| 1117 |
then smallGcd (a, b_2, acc) |
| 1118 |
else |
| 1119 |
if a >= b |
| 1120 |
then smallGcd (div2 (a - b), b, acc) |
| 1121 |
else smallGcd (a, div2 (b - a), acc) |
| 1122 |
end |
| 1123 |
in |
| 1124 |
fun bigGcd (lhs: bigInt, rhs: bigInt): bigInt = |
| 1125 |
if areSmall (lhs, rhs) |
| 1126 |
then addTagCoerceInt |
| 1127 |
(smallGcd (I.abs (dropTagCoerceInt lhs), |
| 1128 |
I.abs (dropTagCoerceInt rhs), |
| 1129 |
1)) |
| 1130 |
else Prim.gcd |
| 1131 |
(lhs, rhs, reserve (S.max (numLimbs lhs, numLimbs rhs), 0)) |
| 1132 |
end |
| 1133 |
|
| 1134 |
local |
| 1135 |
fun make (smallTest: I.int * I.int -> 'a, |
| 1136 |
int32Test: Int32.int * Int32.int -> 'a) |
| 1137 |
(lhs: bigInt, rhs: bigInt): 'a = |
| 1138 |
if areSmall (lhs, rhs) |
| 1139 |
then smallTest (W.idToObjptrInt (Prim.toWord lhs), |
| 1140 |
W.idToObjptrInt (Prim.toWord rhs)) |
| 1141 |
else int32Test (Prim.compare (lhs, rhs), 0) |
| 1142 |
in |
| 1143 |
val bigCompare = make (I.compare, Int32.compare) |
| 1144 |
val bigLT = make (I.<, Int32.<) |
| 1145 |
val bigLE = make (I.<=, Int32.<=) |
| 1146 |
val bigGT = make (I.>, Int32.>) |
| 1147 |
val bigGE = make (I.>=, Int32.>=) |
| 1148 |
end |
| 1149 |
|
| 1150 |
fun bigAbs (arg: bigInt): bigInt = |
| 1151 |
if isSmall arg |
| 1152 |
then let |
| 1153 |
val argw = Prim.toWord arg |
| 1154 |
in |
| 1155 |
if argw = badObjptrWordTagged |
| 1156 |
then negBadIntInf |
| 1157 |
else if I.< (W.idToObjptrInt argw, 0) |
| 1158 |
then Prim.fromWord (W.- (0w2, argw)) |
| 1159 |
else arg |
| 1160 |
end |
| 1161 |
else if bigIsNeg arg |
| 1162 |
then Prim.~ (arg, reserve (numLimbs arg, 1)) |
| 1163 |
else arg |
| 1164 |
|
| 1165 |
fun bigMin (lhs: bigInt, rhs: bigInt): bigInt = |
| 1166 |
if bigLE (lhs, rhs) then lhs else rhs |
| 1167 |
|
| 1168 |
fun bigMax (lhs: bigInt, rhs: bigInt): bigInt = |
| 1169 |
if bigLE (lhs, rhs) then rhs else lhs |
| 1170 |
|
| 1171 |
local |
| 1172 |
fun bigLTU (lhs, rhs) = |
| 1173 |
case (bigCompare (lhs, 0), bigCompare (rhs, 0)) of |
| 1174 |
(LESS, LESS) => bigLT (lhs, rhs) |
| 1175 |
| (LESS, GREATER) => false |
| 1176 |
| (_, EQUAL) => false |
| 1177 |
| (EQUAL, _) => true |
| 1178 |
| (GREATER, LESS) => true |
| 1179 |
| (GREATER, GREATER) => bigLT (lhs, rhs) |
| 1180 |
structure S = IntegralComparisons(type t = bigInt |
| 1181 |
val op < = bigLTU) |
| 1182 |
in |
| 1183 |
val bigLTU = S.< |
| 1184 |
val bigLEU = S.<= |
| 1185 |
val bigGTU = S.> |
| 1186 |
val bigGEU = S.>= |
| 1187 |
end |
| 1188 |
|
| 1189 |
local |
| 1190 |
val op + = bigAdd |
| 1191 |
val op - = bigSub |
| 1192 |
val op > = bigGT |
| 1193 |
val op >= = bigGE |
| 1194 |
val op < = bigLT |
| 1195 |
val quot = bigQuot |
| 1196 |
val rem = bigRem |
| 1197 |
in |
| 1198 |
fun bigDiv (x, y) = |
| 1199 |
if x >= zero |
| 1200 |
then if y > zero |
| 1201 |
then quot (x, y) |
| 1202 |
else if y < zero |
| 1203 |
then if x = zero |
| 1204 |
then zero |
| 1205 |
else quot (x - one, y) - one |
| 1206 |
else raise Div |
| 1207 |
else if y < zero |
| 1208 |
then quot (x, y) |
| 1209 |
else if y > zero |
| 1210 |
then quot (x + one, y) - one |
| 1211 |
else raise Div |
| 1212 |
|
| 1213 |
fun bigMod (x, y) = |
| 1214 |
if x >= zero |
| 1215 |
then if y > zero |
| 1216 |
then rem (x, y) |
| 1217 |
else if y < zero |
| 1218 |
then if x = zero |
| 1219 |
then zero |
| 1220 |
else rem (x - one, y) + (one + y) |
| 1221 |
else raise Div |
| 1222 |
else if y < zero |
| 1223 |
then rem (x, y) |
| 1224 |
else if y > zero |
| 1225 |
then rem (x + one, y) + (y - one) |
| 1226 |
else raise Div |
| 1227 |
|
| 1228 |
fun bigDivMod (x, y) = (bigDiv (x, y), bigMod (x, y)) |
| 1229 |
fun bigQuotRem (x, y) = (bigQuot (x, y), bigRem (x, y)) |
| 1230 |
end |
| 1231 |
|
| 1232 |
local |
| 1233 |
fun make (smallOp, bigOp) |
| 1234 |
(lhs: bigInt, rhs: bigInt) = |
| 1235 |
if areSmall (lhs, rhs) |
| 1236 |
then |
| 1237 |
let |
| 1238 |
val answ = smallOp (Prim.toWord lhs, Prim.toWord rhs) |
| 1239 |
val ans = oneTagCoerce answ |
| 1240 |
in |
| 1241 |
ans |
| 1242 |
end |
| 1243 |
else bigOp (lhs, rhs, |
| 1244 |
reserve (S.max (numLimbs lhs, numLimbs rhs), 0)) |
| 1245 |
in |
| 1246 |
val bigAndb = make (W.andb, Prim.andb) |
| 1247 |
val bigOrb = make (W.orb, Prim.orb) |
| 1248 |
val bigXorb = make (W.xorb, Prim.xorb) |
| 1249 |
end |
| 1250 |
|
| 1251 |
fun bigNotb (arg: bigInt): bigInt = |
| 1252 |
if isSmall arg |
| 1253 |
then oneTagCoerce (W.notb (Prim.toWord arg)) |
| 1254 |
else Prim.notb (arg, reserve (numLimbs arg, 0)) |
| 1255 |
|
| 1256 |
local |
| 1257 |
val bitsPerLimb = MPLimb.sizeInBitsWord |
| 1258 |
fun shiftSize shift = S.sextdFromWord32 (Word32.div (shift, bitsPerLimb)) |
| 1259 |
in |
| 1260 |
fun bigLshift (arg: bigInt, shift: Word32.word): bigInt = |
| 1261 |
if shift = 0wx0 |
| 1262 |
then arg |
| 1263 |
else Prim.<< (arg, shift, |
| 1264 |
reserve (S.+ (numLimbs arg, shiftSize shift), 1)) |
| 1265 |
fun bigRashift (arg: bigInt, shift: Word32.word): bigInt = |
| 1266 |
if shift = 0wx0 |
| 1267 |
then arg |
| 1268 |
else Prim.~>> (arg, shift, |
| 1269 |
reserve (S.max (0, S.- (numLimbs arg, shiftSize shift)), 1)) |
| 1270 |
end |
| 1271 |
|
| 1272 |
fun mkBigCvt {base: Int32.int, |
| 1273 |
smallCvt: I.int -> Primitive.String8.string} |
| 1274 |
(arg: bigInt) |
| 1275 |
: Primitive.String8.string = |
| 1276 |
if isSmall arg |
| 1277 |
then smallCvt (dropTagCoerceInt arg) |
| 1278 |
else let |
| 1279 |
val bpd = Int32.log2 base |
| 1280 |
val bpl = MPLimb.sizeInBits |
| 1281 |
val dpl = |
| 1282 |
Int32.+ (Int32.quot (bpl, bpd), |
| 1283 |
if Int32.mod (bpl, bpd) = 0 |
| 1284 |
then 0 else 1) |
| 1285 |
val bytes = |
| 1286 |
Sz.+ (Sz.+ (bytesPerSequenceMetaData (* Sequence MetaData *), |
| 1287 |
Sz.+ (0w1 (* sign *), |
| 1288 |
case MLton.Align.align of (* alignment *) |
| 1289 |
MLton.Align.Align4 => 0w3 |
| 1290 |
| MLton.Align.Align8 => 0w7)), |
| 1291 |
Sz.* (Sz.zextdFromInt32 dpl, |
| 1292 |
Sz.zextdFromSeqIndex (numLimbs arg))) |
| 1293 |
in |
| 1294 |
Prim.toString (arg, base, bytes) |
| 1295 |
end |
| 1296 |
|
| 1297 |
fun mkBigLog2 {fromSmall: {smallLog2: Primitive.Int32.int} -> 'a, |
| 1298 |
fromLarge: {numLimbsMinusOne: SeqIndex.int, |
| 1299 |
mostSigLimbLog2: Primitive.Int32.int} -> 'a} |
| 1300 |
(arg: bigInt) = |
| 1301 |
if bigLE (arg, 0) |
| 1302 |
then raise Domain |
| 1303 |
else if isSmall arg |
| 1304 |
then fromSmall {smallLog2 = W.log2 (dropTagCoerce arg)} |
| 1305 |
else let |
| 1306 |
val v = Prim.toVector arg |
| 1307 |
val n = V.length v |
| 1308 |
val w = MPLimb.log2 (V.unsafeSub (v, S.- (n, 1))) |
| 1309 |
in |
| 1310 |
fromLarge {numLimbsMinusOne = S.- (n, 2), |
| 1311 |
mostSigLimbLog2 = w} |
| 1312 |
end |
| 1313 |
|
| 1314 |
type int = bigInt |
| 1315 |
type t = int |
| 1316 |
|
| 1317 |
val precision = NONE |
| 1318 |
|
| 1319 |
val maxInt = NONE |
| 1320 |
val minInt = NONE |
| 1321 |
|
| 1322 |
val abs = bigAbs |
| 1323 |
val op +! = bigAdd |
| 1324 |
val op + = bigAdd |
| 1325 |
val divMod = bigDivMod |
| 1326 |
val op div = bigDiv |
| 1327 |
val gcd = bigGcd |
| 1328 |
val op mod = bigMod |
| 1329 |
val op *! = bigMul |
| 1330 |
val op * = bigMul |
| 1331 |
val op ~! = bigNeg |
| 1332 |
val op ~ = bigNeg |
| 1333 |
val quotRem = bigQuotRem |
| 1334 |
val quot = bigQuot |
| 1335 |
val rem = bigRem |
| 1336 |
val op -! = bigSub |
| 1337 |
val op - = bigSub |
| 1338 |
|
| 1339 |
val op < = bigLT |
| 1340 |
val op <= = bigLE |
| 1341 |
val op > = bigGT |
| 1342 |
val op >= = bigGE |
| 1343 |
val compare = bigCompare |
| 1344 |
val min = bigMin |
| 1345 |
val max = bigMax |
| 1346 |
val ltu = bigLTU |
| 1347 |
val leu = bigLEU |
| 1348 |
val gtu = bigGTU |
| 1349 |
val geu = bigGEU |
| 1350 |
|
| 1351 |
val andb = bigAndb |
| 1352 |
val <<? = bigLshift |
| 1353 |
val << = bigLshift |
| 1354 |
val notb = bigNotb |
| 1355 |
val orb = bigOrb |
| 1356 |
val ~>>? = bigRashift |
| 1357 |
val ~>> = bigRashift |
| 1358 |
val xorb = bigXorb |
| 1359 |
|
| 1360 |
val mkCvt = mkBigCvt |
| 1361 |
val mkLog2 = mkBigLog2 |
| 1362 |
end |
| 1363 |
|
| 1364 |
structure IntWordConv : PRIM_INTWORD_CONV = |
| 1365 |
struct |
| 1366 |
open IntWordConv |
| 1367 |
|
| 1368 |
val idFromIntInfToIntInf = fn i => i |
| 1369 |
|
| 1370 |
|
| 1371 |
val zextdFromInt8ToIntInf = IntInf.zextdFromInt8 |
| 1372 |
val zextdFromInt16ToIntInf = IntInf.zextdFromInt16 |
| 1373 |
val zextdFromInt32ToIntInf = IntInf.zextdFromInt32 |
| 1374 |
val zextdFromInt64ToIntInf = IntInf.zextdFromInt64 |
| 1375 |
val zextdFromWord8ToIntInf = IntInf.zextdFromWord8 |
| 1376 |
val zextdFromWord16ToIntInf = IntInf.zextdFromWord16 |
| 1377 |
val zextdFromWord32ToIntInf = IntInf.zextdFromWord32 |
| 1378 |
val zextdFromWord64ToIntInf = IntInf.zextdFromWord64 |
| 1379 |
|
| 1380 |
val zextdFromIntInfToInt8 = IntInf.zextdToInt8 |
| 1381 |
val zextdFromIntInfToInt16 = IntInf.zextdToInt16 |
| 1382 |
val zextdFromIntInfToInt32 = IntInf.zextdToInt32 |
| 1383 |
val zextdFromIntInfToInt64 = IntInf.zextdToInt64 |
| 1384 |
val zextdFromIntInfToIntInf = IntInf.zextdToIntInf |
| 1385 |
val zextdFromIntInfToWord8 = IntInf.zextdToWord8 |
| 1386 |
val zextdFromIntInfToWord16 = IntInf.zextdToWord16 |
| 1387 |
val zextdFromIntInfToWord32 = IntInf.zextdToWord32 |
| 1388 |
val zextdFromIntInfToWord64 = IntInf.zextdToWord64 |
| 1389 |
|
| 1390 |
|
| 1391 |
val sextdFromInt8ToIntInf = IntInf.sextdFromInt8 |
| 1392 |
val sextdFromInt16ToIntInf = IntInf.sextdFromInt16 |
| 1393 |
val sextdFromInt32ToIntInf = IntInf.sextdFromInt32 |
| 1394 |
val sextdFromInt64ToIntInf = IntInf.sextdFromInt64 |
| 1395 |
val sextdFromWord8ToIntInf = IntInf.sextdFromWord8 |
| 1396 |
val sextdFromWord16ToIntInf = IntInf.sextdFromWord16 |
| 1397 |
val sextdFromWord32ToIntInf = IntInf.sextdFromWord32 |
| 1398 |
val sextdFromWord64ToIntInf = IntInf.sextdFromWord64 |
| 1399 |
|
| 1400 |
val sextdFromIntInfToInt8 = IntInf.sextdToInt8 |
| 1401 |
val sextdFromIntInfToInt16 = IntInf.sextdToInt16 |
| 1402 |
val sextdFromIntInfToInt32 = IntInf.sextdToInt32 |
| 1403 |
val sextdFromIntInfToInt64 = IntInf.sextdToInt64 |
| 1404 |
val sextdFromIntInfToIntInf = IntInf.sextdToIntInf |
| 1405 |
val sextdFromIntInfToWord8 = IntInf.sextdToWord8 |
| 1406 |
val sextdFromIntInfToWord16 = IntInf.sextdToWord16 |
| 1407 |
val sextdFromIntInfToWord32 = IntInf.sextdToWord32 |
| 1408 |
val sextdFromIntInfToWord64 = IntInf.sextdToWord64 |
| 1409 |
|
| 1410 |
|
| 1411 |
val castFromInt8ToIntInf = IntInf.castFromInt8 |
| 1412 |
val castFromInt16ToIntInf = IntInf.castFromInt16 |
| 1413 |
val castFromInt32ToIntInf = IntInf.castFromInt32 |
| 1414 |
val castFromInt64ToIntInf = IntInf.castFromInt64 |
| 1415 |
val castFromWord8ToIntInf = IntInf.castFromWord8 |
| 1416 |
val castFromWord16ToIntInf = IntInf.castFromWord16 |
| 1417 |
val castFromWord32ToIntInf = IntInf.castFromWord32 |
| 1418 |
val castFromWord64ToIntInf = IntInf.castFromWord64 |
| 1419 |
|
| 1420 |
val castFromIntInfToInt8 = IntInf.castToInt8 |
| 1421 |
val castFromIntInfToInt16 = IntInf.castToInt16 |
| 1422 |
val castFromIntInfToInt32 = IntInf.castToInt32 |
| 1423 |
val castFromIntInfToInt64 = IntInf.castToInt64 |
| 1424 |
val castFromIntInfToIntInf = IntInf.castToIntInf |
| 1425 |
val castFromIntInfToWord8 = IntInf.castToWord8 |
| 1426 |
val castFromIntInfToWord16 = IntInf.castToWord16 |
| 1427 |
val castFromIntInfToWord32 = IntInf.castToWord32 |
| 1428 |
val castFromIntInfToWord64 = IntInf.castToWord64 |
| 1429 |
|
| 1430 |
|
| 1431 |
val zchckFromInt8ToIntInf = IntInf.zchckFromInt8 |
| 1432 |
val zchckFromInt16ToIntInf = IntInf.zchckFromInt16 |
| 1433 |
val zchckFromInt32ToIntInf = IntInf.zchckFromInt32 |
| 1434 |
val zchckFromInt64ToIntInf = IntInf.zchckFromInt64 |
| 1435 |
val zchckFromWord8ToIntInf = IntInf.zchckFromWord8 |
| 1436 |
val zchckFromWord16ToIntInf = IntInf.zchckFromWord16 |
| 1437 |
val zchckFromWord32ToIntInf = IntInf.zchckFromWord32 |
| 1438 |
val zchckFromWord64ToIntInf = IntInf.zchckFromWord64 |
| 1439 |
|
| 1440 |
val zchckFromIntInfToInt8 = IntInf.zchckToInt8 |
| 1441 |
val zchckFromIntInfToInt16 = IntInf.zchckToInt16 |
| 1442 |
val zchckFromIntInfToInt32 = IntInf.zchckToInt32 |
| 1443 |
val zchckFromIntInfToInt64 = IntInf.zchckToInt64 |
| 1444 |
val zchckFromIntInfToIntInf = IntInf.zchckToIntInf |
| 1445 |
val zchckFromIntInfToWord8 = IntInf.zchckToWord8 |
| 1446 |
val zchckFromIntInfToWord16 = IntInf.zchckToWord16 |
| 1447 |
val zchckFromIntInfToWord32 = IntInf.zchckToWord32 |
| 1448 |
val zchckFromIntInfToWord64 = IntInf.zchckToWord64 |
| 1449 |
|
| 1450 |
|
| 1451 |
val schckFromInt8ToIntInf = IntInf.schckFromInt8 |
| 1452 |
val schckFromInt16ToIntInf = IntInf.schckFromInt16 |
| 1453 |
val schckFromInt32ToIntInf = IntInf.schckFromInt32 |
| 1454 |
val schckFromInt64ToIntInf = IntInf.schckFromInt64 |
| 1455 |
val schckFromWord8ToIntInf = IntInf.schckFromWord8 |
| 1456 |
val schckFromWord16ToIntInf = IntInf.schckFromWord16 |
| 1457 |
val schckFromWord32ToIntInf = IntInf.schckFromWord32 |
| 1458 |
val schckFromWord64ToIntInf = IntInf.schckFromWord64 |
| 1459 |
|
| 1460 |
val schckFromIntInfToInt8 = IntInf.schckToInt8 |
| 1461 |
val schckFromIntInfToInt16 = IntInf.schckToInt16 |
| 1462 |
val schckFromIntInfToInt32 = IntInf.schckToInt32 |
| 1463 |
val schckFromIntInfToInt64 = IntInf.schckToInt64 |
| 1464 |
val schckFromIntInfToIntInf = IntInf.schckToIntInf |
| 1465 |
val schckFromIntInfToWord8 = IntInf.schckToWord8 |
| 1466 |
val schckFromIntInfToWord16 = IntInf.schckToWord16 |
| 1467 |
val schckFromIntInfToWord32 = IntInf.schckToWord32 |
| 1468 |
val schckFromIntInfToWord64 = IntInf.schckToWord64 |
| 1469 |
end |
| 1470 |
|
| 1471 |
structure Int8 : PRIM_INTEGER = |
| 1472 |
struct |
| 1473 |
open Int8 |
| 1474 |
|
| 1475 |
val zextdFromIntInf = IntWordConv.zextdFromIntInfToInt8 |
| 1476 |
val zextdToIntInf = IntWordConv.zextdFromInt8ToIntInf |
| 1477 |
|
| 1478 |
val sextdFromIntInf = IntWordConv.sextdFromIntInfToInt8 |
| 1479 |
val sextdToIntInf = IntWordConv.sextdFromInt8ToIntInf |
| 1480 |
|
| 1481 |
val castFromIntInf = IntWordConv.castFromIntInfToInt8 |
| 1482 |
val castToIntInf = IntWordConv.castFromInt8ToIntInf |
| 1483 |
|
| 1484 |
val zchckFromIntInf = IntWordConv.zchckFromIntInfToInt8 |
| 1485 |
val zchckToIntInf = IntWordConv.zchckFromInt8ToIntInf |
| 1486 |
|
| 1487 |
val schckFromIntInf = IntWordConv.schckFromIntInfToInt8 |
| 1488 |
val schckToIntInf = IntWordConv.schckFromInt8ToIntInf |
| 1489 |
end |
| 1490 |
structure Int16 : PRIM_INTEGER = |
| 1491 |
struct |
| 1492 |
open Int16 |
| 1493 |
|
| 1494 |
val zextdFromIntInf = IntWordConv.zextdFromIntInfToInt16 |
| 1495 |
val zextdToIntInf = IntWordConv.zextdFromInt16ToIntInf |
| 1496 |
|
| 1497 |
val sextdFromIntInf = IntWordConv.sextdFromIntInfToInt16 |
| 1498 |
val sextdToIntInf = IntWordConv.sextdFromInt16ToIntInf |
| 1499 |
|
| 1500 |
val castFromIntInf = IntWordConv.castFromIntInfToInt16 |
| 1501 |
val castToIntInf = IntWordConv.castFromInt16ToIntInf |
| 1502 |
|
| 1503 |
val zchckFromIntInf = IntWordConv.zchckFromIntInfToInt16 |
| 1504 |
val zchckToIntInf = IntWordConv.zchckFromInt16ToIntInf |
| 1505 |
|
| 1506 |
val schckFromIntInf = IntWordConv.schckFromIntInfToInt16 |
| 1507 |
val schckToIntInf = IntWordConv.schckFromInt16ToIntInf |
| 1508 |
end |
| 1509 |
structure Int32 : PRIM_INTEGER = |
| 1510 |
struct |
| 1511 |
open Int32 |
| 1512 |
|
| 1513 |
val zextdFromIntInf = IntWordConv.zextdFromIntInfToInt32 |
| 1514 |
val zextdToIntInf = IntWordConv.zextdFromInt32ToIntInf |
| 1515 |
|
| 1516 |
val sextdFromIntInf = IntWordConv.sextdFromIntInfToInt32 |
| 1517 |
val sextdToIntInf = IntWordConv.sextdFromInt32ToIntInf |
| 1518 |
|
| 1519 |
val castFromIntInf = IntWordConv.castFromIntInfToInt32 |
| 1520 |
val castToIntInf = IntWordConv.castFromInt32ToIntInf |
| 1521 |
|
| 1522 |
val zchckFromIntInf = IntWordConv.zchckFromIntInfToInt32 |
| 1523 |
val zchckToIntInf = IntWordConv.zchckFromInt32ToIntInf |
| 1524 |
|
| 1525 |
val schckFromIntInf = IntWordConv.schckFromIntInfToInt32 |
| 1526 |
val schckToIntInf = IntWordConv.schckFromInt32ToIntInf |
| 1527 |
end |
| 1528 |
structure Int64 : PRIM_INTEGER = |
| 1529 |
struct |
| 1530 |
open Int64 |
| 1531 |
|
| 1532 |
val zextdFromIntInf = IntWordConv.zextdFromIntInfToInt64 |
| 1533 |
val zextdToIntInf = IntWordConv.zextdFromInt64ToIntInf |
| 1534 |
|
| 1535 |
val sextdFromIntInf = IntWordConv.sextdFromIntInfToInt64 |
| 1536 |
val sextdToIntInf = IntWordConv.sextdFromInt64ToIntInf |
| 1537 |
|
| 1538 |
val castFromIntInf = IntWordConv.castFromIntInfToInt64 |
| 1539 |
val castToIntInf = IntWordConv.castFromInt64ToIntInf |
| 1540 |
|
| 1541 |
val zchckFromIntInf = IntWordConv.zchckFromIntInfToInt64 |
| 1542 |
val zchckToIntInf = IntWordConv.zchckFromInt64ToIntInf |
| 1543 |
|
| 1544 |
val schckFromIntInf = IntWordConv.schckFromIntInfToInt64 |
| 1545 |
val schckToIntInf = IntWordConv.schckFromInt64ToIntInf |
| 1546 |
end |
| 1547 |
structure Word8 : PRIM_WORD = |
| 1548 |
struct |
| 1549 |
open Word8 |
| 1550 |
|
| 1551 |
val zextdFromIntInf = IntWordConv.zextdFromIntInfToWord8 |
| 1552 |
val zextdToIntInf = IntWordConv.zextdFromWord8ToIntInf |
| 1553 |
|
| 1554 |
val sextdFromIntInf = IntWordConv.sextdFromIntInfToWord8 |
| 1555 |
val sextdToIntInf = IntWordConv.sextdFromWord8ToIntInf |
| 1556 |
|
| 1557 |
val castFromIntInf = IntWordConv.castFromIntInfToWord8 |
| 1558 |
val castToIntInf = IntWordConv.castFromWord8ToIntInf |
| 1559 |
|
| 1560 |
val zchckFromIntInf = IntWordConv.zchckFromIntInfToWord8 |
| 1561 |
val zchckToIntInf = IntWordConv.zchckFromWord8ToIntInf |
| 1562 |
|
| 1563 |
val schckFromIntInf = IntWordConv.schckFromIntInfToWord8 |
| 1564 |
val schckToIntInf = IntWordConv.schckFromWord8ToIntInf |
| 1565 |
end |
| 1566 |
structure Word16 : PRIM_WORD = |
| 1567 |
struct |
| 1568 |
open Word16 |
| 1569 |
|
| 1570 |
val zextdFromIntInf = IntWordConv.zextdFromIntInfToWord16 |
| 1571 |
val zextdToIntInf = IntWordConv.zextdFromWord16ToIntInf |
| 1572 |
|
| 1573 |
val sextdFromIntInf = IntWordConv.sextdFromIntInfToWord16 |
| 1574 |
val sextdToIntInf = IntWordConv.sextdFromWord16ToIntInf |
| 1575 |
|
| 1576 |
val castFromIntInf = IntWordConv.castFromIntInfToWord16 |
| 1577 |
val castToIntInf = IntWordConv.castFromWord16ToIntInf |
| 1578 |
|
| 1579 |
val zchckFromIntInf = IntWordConv.zchckFromIntInfToWord16 |
| 1580 |
val zchckToIntInf = IntWordConv.zchckFromWord16ToIntInf |
| 1581 |
|
| 1582 |
val schckFromIntInf = IntWordConv.schckFromIntInfToWord16 |
| 1583 |
val schckToIntInf = IntWordConv.schckFromWord16ToIntInf |
| 1584 |
end |
| 1585 |
structure Word32 : PRIM_WORD = |
| 1586 |
struct |
| 1587 |
open Word32 |
| 1588 |
|
| 1589 |
val zextdFromIntInf = IntWordConv.zextdFromIntInfToWord32 |
| 1590 |
val zextdToIntInf = IntWordConv.zextdFromWord32ToIntInf |
| 1591 |
|
| 1592 |
val sextdFromIntInf = IntWordConv.sextdFromIntInfToWord32 |
| 1593 |
val sextdToIntInf = IntWordConv.sextdFromWord32ToIntInf |
| 1594 |
|
| 1595 |
val castFromIntInf = IntWordConv.castFromIntInfToWord32 |
| 1596 |
val castToIntInf = IntWordConv.castFromWord32ToIntInf |
| 1597 |
|
| 1598 |
val zchckFromIntInf = IntWordConv.zchckFromIntInfToWord32 |
| 1599 |
val zchckToIntInf = IntWordConv.zchckFromWord32ToIntInf |
| 1600 |
|
| 1601 |
val schckFromIntInf = IntWordConv.schckFromIntInfToWord32 |
| 1602 |
val schckToIntInf = IntWordConv.schckFromWord32ToIntInf |
| 1603 |
end |
| 1604 |
structure Word64 : PRIM_WORD = |
| 1605 |
struct |
| 1606 |
open Word64 |
| 1607 |
|
| 1608 |
val zextdFromIntInf = IntWordConv.zextdFromIntInfToWord64 |
| 1609 |
val zextdToIntInf = IntWordConv.zextdFromWord64ToIntInf |
| 1610 |
|
| 1611 |
val sextdFromIntInf = IntWordConv.sextdFromIntInfToWord64 |
| 1612 |
val sextdToIntInf = IntWordConv.sextdFromWord64ToIntInf |
| 1613 |
|
| 1614 |
val castFromIntInf = IntWordConv.castFromIntInfToWord64 |
| 1615 |
val castToIntInf = IntWordConv.castFromWord64ToIntInf |
| 1616 |
|
| 1617 |
val zchckFromIntInf = IntWordConv.zchckFromIntInfToWord64 |
| 1618 |
val zchckToIntInf = IntWordConv.zchckFromWord64ToIntInf |
| 1619 |
|
| 1620 |
val schckFromIntInf = IntWordConv.schckFromIntInfToWord64 |
| 1621 |
val schckToIntInf = IntWordConv.schckFromWord64ToIntInf |
| 1622 |
end |
| 1623 |
|
| 1624 |
structure IntInf : PRIM_INT_INF = IntInf |
| 1625 |
|
| 1626 |
end |