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
|
(* Copyright (C) 1999-2005 Henry Cejtin, Matthew Fluet, Suresh
* Jagannathan, and Stephen Weeks.
* Copyright (C) 1997-2000 NEC Research Institute.
*
* MLton is released under a BSD-style license.
* See the file MLton-LICENSE for details.
*)
functor Integer (I: PRE_INTEGER_EXTRA) =
struct
open I
structure PI = Primitive.Int
val detectOverflow = Primitive.detectOverflow
val (toInt, fromInt) =
if detectOverflow andalso
precision' <> PI.precision'
then if PI.<(precision', PI.precision')
then (I.toInt,
fn i =>
if (PI.<= (I.toInt minInt', i)
andalso PI.<= (i, I.toInt maxInt'))
then I.fromInt i
else raise Overflow)
else (fn i =>
if (I.<= (I.fromInt PI.minInt', i)
andalso I.<= (i, I.fromInt PI.maxInt'))
then I.toInt i
else raise Overflow,
I.fromInt)
else (I.toInt, I.fromInt)
val precision: Int.int option = SOME precision'
val maxInt: int option = SOME maxInt'
val minInt: int option = SOME minInt'
val one: int = fromInt 1
val zero: int = fromInt 0
fun quot (x, y) =
if y = zero
then raise Div
else if detectOverflow andalso x = minInt' andalso y = ~one
then raise Overflow
else I.quot (x, y)
fun rem (x, y) =
if y = zero
then raise Div
else if x = minInt' andalso y = ~one
then zero
else I.rem (x, y)
fun x div y =
if x >= zero
then if y > zero
then I.quot (x, y)
else if y < zero
then if x = zero
then zero
else I.quot (x - one, y) -? one
else raise Div
else if y < zero
then if detectOverflow andalso x = minInt' andalso y = ~one
then raise Overflow
else I.quot (x, y)
else if y > zero
then I.quot (x + one, y) -? one
else raise Div
fun x mod y =
if x >= zero
then if y > zero
then I.rem (x, y)
else if y < zero
then if x = zero
then zero
else I.rem (x - one, y) +? (y + one)
else raise Div
else if y < zero
then if x = minInt' andalso y = ~one
then zero
else I.rem (x, y)
else if y > zero
then I.rem (x + one, y) +? (y - one)
else raise Div
val sign: int -> Int.int =
fn i => if i = zero
then (0: Int.int)
else if i < zero
then (~1: Int.int)
else (1: Int.int)
fun sameSign (x, y) = sign x = sign y
fun abs (x: int) = if x < zero then ~ x else x
val {compare, min, max} = Util.makeCompare (op <)
(* fmt constructs a string to represent the integer by building it into a
* statically allocated buffer. For the most part, this is a textbook
* algorithm: loop starting at the end of the buffer; we use rem to
* extract the next digit to put into the buffer; and we use quot to
* figure out the part of the integer that we haven't yet formatted.
* However, this function uses the negative absolute value of the input
* number, which allows it to take into account minInt without any
* special-casing. This requires the rem function to behave in a very
* specific way, or else things will go terribly wrong. This may be a
* concern when porting to platforms where the division hardware has a
* different interpretation than SML about what happens when doing
* division of negative numbers.
*)
local
(* Allocate a buffer large enough to hold any formatted integer in any radix.
* The most that will be required is for minInt in binary.
*)
val maxNumDigits = PI.+ (precision', 1)
val one = One.make (fn () => CharArray.array (maxNumDigits, #"\000"))
in
fun fmt radix (n: int): string =
One.use
(one, fn buf =>
let
val radix = fromInt (StringCvt.radixToInt radix)
fun loop (q, i: Int.int) =
let
val _ =
CharArray.update
(buf, i, StringCvt.digitToChar (toInt (~? (rem (q, radix)))))
val q = quot (q, radix)
in
if q = zero
then
let
val start =
if n < zero
then
let
val i = PI.- (i, 1)
val () = CharArray.update (buf, i, #"~")
in
i
end
else i
in
CharArraySlice.vector
(CharArraySlice.slice (buf, start, NONE))
end
else loop (q, PI.- (i, 1))
end
in
loop (if n < zero then n else ~? n, PI.- (maxNumDigits, 1))
end)
end
val toString = fmt StringCvt.DEC
fun scan radix reader s =
let
(* Works with the negative of the number so that minInt can be scanned. *)
val s = StringCvt.skipWS reader s
fun charToDigit c =
case StringCvt.charToDigit radix c of
NONE => NONE
| SOME n => SOME (fromInt n)
val radixInt = fromInt (StringCvt.radixToInt radix)
fun finishNum (s, n) =
case reader s of
NONE => SOME (n, s)
| SOME (c, s') =>
case charToDigit c of
NONE => SOME (n, s)
| SOME n' => finishNum (s', n * radixInt - n')
fun num s =
case (reader s, radix) of
(NONE, _) => NONE
| (SOME (#"0", s), StringCvt.HEX) =>
(case reader s of
NONE => SOME (zero, s)
| SOME (c, s') =>
if c = #"x" orelse c = #"X" then
case reader s' of
NONE => SOME (zero, s)
| SOME (c, s') =>
case charToDigit c of
NONE => SOME (zero, s)
| SOME n => finishNum (s', ~? n)
else
case charToDigit c of
NONE => SOME (zero, s)
| SOME n => finishNum (s', ~? n))
| (SOME (c, s), _) =>
case charToDigit c of
NONE => NONE
| SOME n => finishNum (s, ~? n)
fun negate s =
case num s of
NONE => NONE
| SOME (n, s) => SOME (~ n, s)
in
case reader s of
NONE => NONE
| SOME (c, s') =>
case c of
#"~" => num s'
| #"-" => num s'
| #"+" => negate s'
| _ => negate s
end
val fromString = StringCvt.scanString (scan StringCvt.DEC)
fun power {base, exp} =
if Primitive.safe andalso exp < zero
then raise Fail "Int.power"
else let
fun loop (exp, accum) =
if exp <= zero
then accum
else loop (exp - one, base * accum)
in loop (exp, one)
end
end
structure Int8 = Integer (Primitive.Int8)
structure Int16 = Integer (Primitive.Int16)
structure Int32 = Integer (Primitive.Int32)
structure Int = Int32
structure IntGlobal: INTEGER_GLOBAL = Int
open IntGlobal
structure Int64 =
struct
local
structure P = Primitive.Int64
structure I = Integer (P)
in
open I
val toWord = P.toWord
end
end
|