/
real.sml
1136 lines (1068 loc) · 41.4 KB
/
real.sml
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(* Copyright (C) 2011-2013 Matthew Fluet.
* Copyright (C) 2003-2007 Henry Cejtin, Matthew Fluet, Suresh
* Jagannathan, and Stephen Weeks.
*
* MLton is released under a BSD-style license.
* See the file MLton-LICENSE for details.
*)
functor Real (structure W: WORD_EXTRA
structure R:
sig
include PRE_REAL
val castToWord: real -> W.word
val castFromWord: W.word -> real
end): REAL_EXTRA =
struct
structure MLton = Primitive.MLton
structure Prim = R
local
open IEEEReal
in
datatype float_class = datatype float_class
datatype rounding_mode = datatype rounding_mode
end
infix 4 == != ?=
type real = R.real
local
open Prim
in
val realSize = Int32.toInt realSize
val exponentBias = Int32.toInt exponentBias
val precision = Int32.toInt precision
val radix = Int32.toInt radix
end
val signBits = Word.one
val exponentSignificandBits = Word.- (Word.fromInt realSize, signBits)
val significandBits = Word.- (Word.fromInt precision, Word.one)
val exponentBits = Word.- (exponentSignificandBits, significandBits)
local
val mkMask : Word.word -> W.word =
fn b => W.notb (W.<< (W.notb W.zero, b))
in
val signMask =
W.<< (mkMask signBits, exponentSignificandBits)
val exponentMask =
W.<< (mkMask exponentBits, significandBits)
val significandMask =
mkMask significandBits
end
val class : real -> float_class =
fn r =>
let
val w = R.castToWord r
in
if W.andb (w, exponentMask) = exponentMask
then if W.andb (w, significandMask) = W.zero
then IEEEReal.INF
else IEEEReal.NAN
else if W.andb (w, exponentMask) = W.zero
then if W.andb (w, significandMask) = W.zero
then IEEEReal.ZERO
else IEEEReal.SUBNORMAL
else IEEEReal.NORMAL
end
val toBits : real -> {sign: bool, exponent: W.word, significand: W.word} =
fn r =>
let
val w = R.castToWord r
val significand =
W.andb (w, significandMask)
val exponent =
W.>> (W.andb (w, exponentMask), significandBits)
val sign =
W.andb (w, signMask) = signMask
in
{sign = sign,
exponent = exponent,
significand = significand}
end
val fromBits : {sign: bool, exponent: W.word, significand: W.word} -> real =
fn {sign, exponent, significand} =>
let
val w =
W.orb (if sign then W.<< (W.one, exponentSignificandBits) else W.zero,
W.orb (W.andb (W.<< (exponent, significandBits), exponentMask),
W.andb (significand, significandMask)))
val r = R.castFromWord w
in
r
end
local
open Prim
in
val op *+ = op *+
val op *- = op *-
val op * = op *
val op + = op +
val op - = op -
val op / = op /
val op < = op <
val op <= = op <=
val op > = op >
val op >= = op >=
val ~ = ~
val abs = abs
end
local
fun 'a make {fromRealUnsafe: 'a -> real,
toRealUnsafe: real -> 'a,
other : {precision: Primitive.Int32.int}} =
if R.precision = #precision other
then (fn (_: rounding_mode) => fromRealUnsafe,
toRealUnsafe)
else (fn (m: rounding_mode) => fn r =>
IEEEReal.withRoundingMode (m, fn () => fromRealUnsafe r),
toRealUnsafe)
in
val (fromReal32,toReal32) =
make {fromRealUnsafe = R.fromReal32Unsafe,
toRealUnsafe = R.toReal32Unsafe,
other = {precision = Primitive.Real32.precision}}
val (fromReal64,toReal64) =
make {fromRealUnsafe = R.fromReal64Unsafe,
toRealUnsafe = R.toReal64Unsafe,
other = {precision = Primitive.Real64.precision}}
end
local
structure S =
LargeReal_ChooseRealN
(type 'a t = real -> 'a
val fReal32 = toReal32
val fReal64 = toReal64)
in
val toLarge = S.f
end
local
structure S =
LargeReal_ChooseRealN
(type 'a t = rounding_mode -> 'a -> real
val fReal32 = fromReal32
val fReal64 = fromReal64)
in
val fromLarge = S.f
end
val negInf = R.castFromWord (W.orb (signMask, exponentMask))
val negOne = R.castFromWord (W.orb (signMask, W.<< (W.fromInt exponentBias, significandBits)))
val negZero = R.castFromWord signMask
val zero = R.castFromWord W.zero
val minPos = R.castFromWord W.one
val minNormalPos = R.castFromWord (W.<< (W.one, significandBits))
val half = R.castFromWord (W.<< (W.- (W.fromInt exponentBias, W.one), significandBits))
val one = R.castFromWord (W.<< (W.fromInt exponentBias, significandBits))
val two = R.castFromWord (W.<< (W.+ (W.fromInt exponentBias, W.one), significandBits))
val maxFinite = R.castFromWord (W.- (exponentMask, W.one))
val posInf = R.castFromWord exponentMask
val nan = posInf + negInf
val posNan = R.castFromWord (W.andb (R.castToWord nan, W.notb signMask))
val negNan = R.castFromWord (W.orb (R.castToWord nan, signMask))
fun isFinite r =
abs r <= maxFinite
val op == = Prim.==
val op != = not o op ==
fun isNan r = r != r
fun isNormal r = class r = NORMAL
val op ?= =
if MLton.Codegen.isX86 orelse MLton.Codegen.isAmd64
then R.?=
else
fn (x, y) =>
case (class x, class y) of
(NAN, _) => true
| (_, NAN) => true
| (ZERO, ZERO) => true
| _ => R.== (x, y)
fun min (x, y) =
if x <= y then x
else if x > y then y
else if isNan y then x
else y
fun max (x, y) =
if x >= y then x
else if x < y then y
else if isNan y then x
else y
fun sign (x: real): int =
if x > zero then 1
else if x < zero then ~1
else if x == zero then 0
else raise Domain
val signBit = #sign o toBits
fun sameSign (x, y) = signBit x = signBit y
fun copySign (x, y) =
if sameSign (x, y)
then x
else ~ x
local
structure I = IEEEReal
in
fun compareReal (x, y) =
if x < y then I.LESS
else if x > y then I.GREATER
else if x == y then I.EQUAL
else I.UNORDERED
end
local
structure I = IEEEReal
structure G = General
in
fun compare (x, y) =
case compareReal (x, y) of
I.EQUAL => G.EQUAL
| I.GREATER => G.GREATER
| I.LESS => G.LESS
| I.UNORDERED => raise IEEEReal.Unordered
end
fun unordered (x, y) = isNan x orelse isNan y
(* nextAfter for subnormal and normal values works by converting
* the real to a word of equivalent size and doing an increment
* or decrement on the word. Because of the way IEEE floating
* point numbers are represented, word {de,in}crement
* automatically does the right thing at the boundary between
* normals and denormals. Also, convienently,
* maxFinite+1 = posInf and minFinite-1 = negInf.
*)
val nextAfter: real * real -> real =
fn (r, t) =>
case (class r, class t) of
(NAN, _) => nan
| (_, NAN) => nan
| (INF, _) => r
| (ZERO, ZERO) => t (* want "t", not "r", to get the sign right *)
| (ZERO, _) => if t > zero then minPos else ~minPos
| _ =>
if r == t then
r
else if (r > t) = (r > zero) then
R.castFromWord (W.- (R.castToWord r, W.one))
else
R.castFromWord (W.+ (R.castToWord r, W.one))
local
val one = One.make (fn () => ref (0 : C_Int.t))
in
fun toManExp x =
case class x of
INF => {exp = 0, man = x}
| NAN => {exp = 0, man = nan}
| ZERO => {exp = 0, man = x}
| _ => One.use (one, fn r =>
let
val man = R.frexp (x, r)
in
{exp = C_Int.toInt (!r), man = man}
end)
end
fun fromManExp {exp, man} =
(R.ldexp (man, C_Int.fromInt exp))
handle Overflow =>
man * (if Int.< (exp, 0) then zero else posInf)
val fromManExp =
if MLton.Codegen.isX86
then fromManExp
else
fn {exp, man} =>
case class man of
INF => man
| NAN => man
| ZERO => man
| _ => fromManExp {exp = exp, man = man}
local
val one = One.make (fn () => ref zero)
in
fun split x =
case class x of
INF => {frac = if x > zero then zero else ~zero,
whole = x}
| NAN => {frac = nan, whole = nan}
| _ =>
let
val (frac, whole) =
One.use (one, fn int =>
(R.modf (x, int), !int))
(* Some platforms' C libraries don't get sign of
* zero right.
*)
fun fix y =
if class y = ZERO andalso not (sameSign (x, y))
then ~ y
else y
in
{frac = fix frac,
whole = fix whole}
end
end
val realMod = #frac o split
fun checkFloat x =
if isFinite x then x
else if isNan x then raise Div
else raise Overflow
val realCeil = R.realCeil
val realFloor = R.realFloor
val realTrunc = R.realTrunc
(* Unfortunately, libc round ties to zero instead of even values. *)
(* Fortunately, if any rounding mode is supported, it's TO_NEAREST. *)
val realRound = fn r => IEEEReal.withRoundingMode (TO_NEAREST, fn () => R.round r)
fun rem (x, y) =
(case class x of
INF => nan
| NAN => nan
| ZERO => zero
| _ => (case class y of
INF => x
| NAN => nan
| ZERO => nan
| _ => x - realTrunc (x/y) * y))
(* fromDecimal, scan, fromString: decimal -> binary conversions *)
fun strtor (str: NullString.t,
rounding_mode: IEEEReal.rounding_mode) =
let
val rounding : C_Int.int =
case rounding_mode of
TO_NEAREST => 1
| TO_NEGINF => 3
| TO_POSINF => 2
| TO_ZERO => 0
in
Prim.strtor (str, rounding)
end
exception Bad
fun fromDecimalWithRoundingMode
({class, digits, exp, sign}: IEEEReal.decimal_approx,
rounding_mode: IEEEReal.rounding_mode) =
let
fun doit () =
let
val exp =
if Int.< (exp, 0)
then concat ["-", Int.toString (Int.~ exp)]
else Int.toString exp
(*
val str = concat [if sign then "-" else "",
"0.", digits,
"E", exp, "\000"]
*)
val n = Int.+ (if sign then 1 else 0,
Int.+ (4 (* "0." + "E" + "\000" *),
Int.+ (List.length digits,
String.size exp)))
val a = Array.arrayUninit n
fun upd (i, c) = (Array.update (a, i, c); Int.+ (i, 1))
val i = 0
val i = if sign then upd (i, #"-") else i
val i = upd (i, #"0")
val i = upd (i, #".")
val i =
List.foldl
(fn (d, i) =>
if Int.< (d, 0) orelse Int.> (d, 9)
then raise Bad
else upd (i, Char.chr (Int.+ (d, Char.ord #"0"))))
i digits
val i = upd (i, #"E")
val i = CharVector.foldl (fn (c, i) => upd (i, c)) i exp
val _ = upd (i, #"\000")
val str = Vector.unsafeFromArray a
val x = strtor (NullString.fromString str, rounding_mode)
in
x
end
in
SOME (case class of
INF => if sign then negInf else posInf
| NAN => if sign then negNan else posNan
| NORMAL => doit ()
| SUBNORMAL => doit ()
| ZERO => if sign then negZero else zero)
handle Bad => NONE
end
fun fromDecimal da = fromDecimalWithRoundingMode (da, TO_NEAREST)
fun scan reader state =
case IEEEReal.scan reader state of
NONE => NONE
| SOME (da, state) =>
SOME (valOf (fromDecimalWithRoundingMode
(da, IEEEReal.getRoundingMode ())),
state)
val fromString = StringCvt.scanString scan
(* toDecimal, fmt, toString: binary -> decimal conversions. *)
datatype mode = Fix | Gen | Sci
local
val one = One.make (fn () => ref (0: C_Int.int))
in
fun gdtoa (x: real, mode: mode, ndig: int,
rounding_mode: IEEEReal.rounding_mode) =
let
val mode : C_Int.int =
case mode of
Fix => 3
| Gen => 0
| Sci => 2
val ndig : C_Int.int = C_Int.fromInt ndig
val rounding : C_Int.int =
case rounding_mode of
TO_NEAREST => 1
| TO_NEGINF => 3
| TO_POSINF => 2
| TO_ZERO => 0
in
One.use (one, fn decpt =>
(Prim.gdtoa (x, mode, ndig, rounding, decpt),
C_Int.toInt (!decpt)))
end
end
fun toDecimal (x: real): IEEEReal.decimal_approx =
case class x of
INF => {class = INF,
digits = [],
exp = 0,
sign = x < zero}
| NAN => {class = NAN,
digits = [],
exp = 0,
sign = signBit x}
| ZERO => {class = ZERO,
digits = [],
exp = 0,
sign = signBit x}
| c =>
let
val (cs, exp) = gdtoa (x, Gen, 0, TO_NEAREST)
fun loop (i, ac) =
if Int.< (i, 0)
then ac
else loop (Int.- (i, 1),
(Int.- (Char.ord (CUtil.C_String.sub (cs, i)),
Char.ord #"0"))
:: ac)
val digits = loop (Int.- (CUtil.C_String.length cs, 1), [])
in
{class = c,
digits = digits,
exp = exp,
sign = x < zero}
end
datatype realfmt = datatype StringCvt.realfmt
local
fun fix (sign: string, cs: CUtil.C_String.t, decpt: int, ndig: int): string =
let
val length = CUtil.C_String.length cs
in
if Int.< (decpt, 0)
then
concat [sign,
"0.",
String.new (Int.~ decpt, #"0"),
CUtil.C_String.toString cs,
String.new (Int.+ (Int.- (ndig, length),
decpt),
#"0")]
else
let
val whole =
if decpt = 0
then "0"
else
String.tabulate (decpt, fn i =>
if Int.< (i, length)
then CUtil.C_String.sub (cs, i)
else #"0")
in
if 0 = ndig
then concat [sign, whole]
else
let
val frac =
String.tabulate
(ndig, fn i =>
let
val j = Int.+ (i, decpt)
in
if Int.< (j, length)
then CUtil.C_String.sub (cs, j)
else #"0"
end)
in
concat [sign, whole, ".", frac]
end
end
end
fun sci (x: real, ndig: int): string =
let
val sign = if x < zero then "~" else ""
val (cs, decpt) =
gdtoa (x, Sci, Int.+ (1, ndig), IEEEReal.getRoundingMode ())
val length = CUtil.C_String.length cs
val whole = String.tabulate (1, fn _ => CUtil.C_String.sub (cs, 0))
val frac =
if 0 = ndig
then ""
else concat [".",
String.tabulate
(ndig, fn i =>
let
val j = Int.+ (i, 1)
in
if Int.< (j, length)
then CUtil.C_String.sub (cs, j)
else #"0"
end)]
val exp = Int.- (decpt, 1)
val exp =
let
val (exp, sign) =
if Int.< (exp, 0)
then (Int.~ exp, "~")
else (exp, "")
in
concat [sign, Int.toString exp]
end
in
concat [sign, whole, frac, "E", exp]
end
fun gen (x: real, n: int): string =
let
val (prefix, x) =
if x < zero
then ("~", ~ x)
else ("", x)
val ss = Substring.full (sci (x, Int.- (n, 1)))
fun isE c = c = #"E"
fun isZero c = c = #"0"
val expS =
Substring.string (Substring.taker (not o isE) ss)
val exp = valOf (Int.fromString expS)
val man =
String.translate
(fn #"." => "" | c => str c)
(Substring.string (Substring.dropr isZero
(Substring.takel (not o isE) ss)))
val manSize = String.size man
fun zeros i = CharVector.tabulate (i, fn _ => #"0")
fun dotAt i =
concat [String.substring (man, 0, i),
".", String.extract (man, i, NONE)]
fun sci () = concat [prefix,
if manSize = 1 then man else dotAt 1,
"E", expS]
val op - = Int.-
val op + = Int.+
val ~ = Int.~
val op >= = Int.>=
in
if exp >= (if manSize = 1 then 3 else manSize + 3)
then sci ()
else if exp >= manSize - 1
then concat [prefix, man, zeros (exp - (manSize - 1))]
else if exp >= 0
then concat [prefix, dotAt (exp + 1)]
else if exp >= (if manSize = 1 then ~2 else ~3)
then concat [prefix, "0.", zeros (~exp - 1), man]
else sci ()
end
in
fun fmt spec =
let
val doit =
case spec of
EXACT => IEEEReal.toString o toDecimal
| FIX opt =>
let
val n =
case opt of
NONE => 6
| SOME n =>
if Primitive.Controls.safe andalso Int.< (n, 0)
then raise Size
else n
in
fn x =>
let
val sign = if x < zero then "~" else ""
val (cs, decpt) =
gdtoa (x, Fix, n, IEEEReal.getRoundingMode ())
in
fix (sign, cs, decpt, n)
end
end
| GEN opt =>
let
val n =
case opt of
NONE => 12
| SOME n =>
if Primitive.Controls.safe andalso Int.< (n, 1)
then raise Size
else n
in
fn x => gen (x, n)
end
| SCI opt =>
let
val n =
case opt of
NONE => 6
| SOME n =>
if Primitive.Controls.safe andalso Int.< (n, 0)
then raise Size
else n
in
fn x => sci (x, n)
end
in
fn x =>
case class x of
NAN => (* if signBit x then "~nan" else *) "nan"
| INF => if x > zero then "inf" else "~inf"
| _ => doit x
end
end
val toString = fmt (StringCvt.GEN NONE)
(* Not all devices support all rounding modes.
* However, every device has ceil/floor/round/trunc.
*)
fun safeConvert (m, cvt, x) =
case m of
TO_POSINF => cvt (realCeil x)
| TO_NEGINF => cvt (realFloor x)
| TO_NEAREST => cvt (realRound x)
| TO_ZERO => cvt (realTrunc x)
local
fun 'a make {fromIntUnsafe: 'a -> real,
toIntUnsafe: real -> 'a,
other : {maxInt': Word.word -> 'a,
minInt': 'a,
precision': int}} =
(fromIntUnsafe,
if Int.< (precision, #precision' other) then
let
val trim = Int.- (Int.- (#precision' other, precision), 1)
val maxInt' = (#maxInt' other) (Word.fromInt trim)
val minInt' = #minInt' other
val maxInt = fromIntUnsafe maxInt'
val minInt = fromIntUnsafe minInt'
in
fn (m: rounding_mode) => fn x =>
if minInt <= x then
if x <= maxInt then
safeConvert (m, toIntUnsafe, x)
else
raise Overflow
else
if x < minInt then
raise Overflow
else
raise Domain (* NaN *)
end
else
let
val maxInt' = (#maxInt' other) 0w0
val minInt' = #minInt' other
val maxInt = fromIntUnsafe maxInt'
val minInt = fromIntUnsafe minInt'
in
fn (m: rounding_mode) => fn x =>
if minInt <= x then
if x <= maxInt then
safeConvert (m, toIntUnsafe, x)
else
if x < maxInt + one then
(case m of
TO_NEGINF => maxInt'
| TO_POSINF => raise Overflow
| TO_ZERO => maxInt'
| TO_NEAREST =>
(* Depends on maxInt being odd. *)
if x - maxInt >= half then
raise Overflow
else
maxInt')
else
raise Overflow
else
if x < minInt then
if minInt - one < x then
(case m of
TO_NEGINF => raise Overflow
| TO_POSINF => minInt'
| TO_ZERO => minInt'
| TO_NEAREST =>
(* Depends on minInt being even. *)
if x - minInt < ~half then
raise Overflow
else
minInt')
else
raise Overflow
else
raise Domain (* NaN *)
end)
in
val (fromInt8,toInt8) =
make {fromIntUnsafe = R.fromInt8Unsafe,
toIntUnsafe = R.toInt8Unsafe,
other = {maxInt' = fn w => Int8.<< (Int8.>> (Int8.maxInt', w), w),
minInt' = Int8.minInt',
precision' = Int8.precision'}}
val (fromInt16,toInt16) =
make {fromIntUnsafe = R.fromInt16Unsafe,
toIntUnsafe = R.toInt16Unsafe,
other = {maxInt' = fn w => Int16.<< (Int16.>> (Int16.maxInt', w), w),
minInt' = Int16.minInt',
precision' = Int16.precision'}}
val (fromInt32,toInt32) =
make {fromIntUnsafe = R.fromInt32Unsafe,
toIntUnsafe = R.toInt32Unsafe,
other = {maxInt' = fn w => Int32.<< (Int32.>> (Int32.maxInt', w), w),
minInt' = Int32.minInt',
precision' = Int32.precision'}}
val (fromInt64,toInt64) =
make {fromIntUnsafe = R.fromInt64Unsafe,
toIntUnsafe = R.toInt64Unsafe,
other = {maxInt' = fn w => Int64.<< (Int64.>> (Int64.maxInt', w), w),
minInt' = Int64.minInt',
precision' = Int64.precision'}}
end
val fromIntInf: IntInf.int -> real =
fn i =>
let
val str =
if IntInf.< (i, 0)
then "-" ^ (IntInf.toString (IntInf.~ i))
else IntInf.toString i
val x = strtor (NullString.nullTerm str,
IEEEReal.getRoundingMode ())
in
x
end
val toIntInf: rounding_mode -> real -> LargeInt.int =
fn mode => fn x =>
case class x of
INF => raise Overflow
| NAN => raise Domain
| ZERO => (0 : LargeInt.int)
| _ =>
let
(* This round may turn x into an INF, so we need to check the
* class again.
*)
val x =
case mode of
TO_POSINF => realCeil x
| TO_NEGINF => realFloor x
| TO_NEAREST => realRound x
| TO_ZERO => realTrunc x
in
case class x of
INF => raise Overflow
| _ => valOf (IntInf.fromString (fmt (StringCvt.FIX (SOME 0)) x))
end
local
structure S =
Int_ChooseInt
(type 'a t = 'a -> real
val fInt8 = fromInt8
val fInt16 = fromInt16
val fInt32 = fromInt32
val fInt64 = fromInt64
val fIntInf = fromIntInf)
in
val fromInt = S.f
end
local
structure S =
LargeInt_ChooseInt
(type 'a t = 'a -> real
val fInt8 = fromInt8
val fInt16 = fromInt16
val fInt32 = fromInt32
val fInt64 = fromInt64
val fIntInf = fromIntInf)
in
val fromLargeInt = S.f
end
local
structure S =
Int_ChooseInt
(type 'a t = rounding_mode -> real -> 'a
val fInt8 = toInt8
val fInt16 = toInt16
val fInt32 = toInt32
val fInt64 = toInt64
val fIntInf = toIntInf)
in
val toInt = S.f
end
local
structure S =
LargeInt_ChooseInt
(type 'a t = rounding_mode -> real -> 'a
val fInt8 = toInt8
val fInt16 = toInt16
val fInt32 = toInt32
val fInt64 = toInt64
val fIntInf = toIntInf)
in
val toLargeInt = S.f
end
val floor = toInt TO_NEGINF
val ceil = toInt TO_POSINF
val trunc = toInt TO_ZERO
val round = toInt TO_NEAREST
local
fun 'a make {fromWordUnsafe: 'a -> real,
toWordUnsafe: real -> 'a,
other : {maxWord': Word.word -> 'a,
wordSize: int,
zeroWord: 'a}} =
(fromWordUnsafe,
if Int.<= (precision, #wordSize other)
then let
val trim = Int.- (#wordSize other, precision)
val maxWord' = (#maxWord' other) (Word.fromInt trim)
val maxWord = fromWordUnsafe maxWord'
val zeroWord = #zeroWord other
in
fn (m: rounding_mode) => fn x =>
case class x of
INF => raise Overflow
| NAN => raise Domain
| _ => if zero <= x
then if x <= maxWord
then safeConvert (m, toWordUnsafe, x)
else raise Overflow
else if x > ~one
then (case m of
TO_NEGINF => raise Overflow
| TO_POSINF => zeroWord
| TO_ZERO => zeroWord
| TO_NEAREST =>
if x < ~half
then raise Overflow
else zeroWord)
else raise Overflow
end
else let
val maxWord' = (#maxWord' other) 0w0
val maxWord = fromWordUnsafe maxWord'
val zeroWord = #zeroWord other
in
fn (m: rounding_mode) => fn x =>
case class x of
INF => raise Overflow
| NAN => raise Domain
| _ => if zero <= x
then if x <= maxWord
then safeConvert (m, toWordUnsafe, x)
else if x < maxWord + one
then (case m of
TO_NEGINF => maxWord'
| TO_POSINF => raise Overflow
| TO_ZERO => maxWord'
| TO_NEAREST =>
(* Depends on maxWord being odd. *)
if x - maxWord >= half
then raise Overflow
else maxWord')
else raise Overflow
else if x > ~one
then (case m of
TO_NEGINF => raise Overflow
| TO_POSINF => zeroWord
| TO_ZERO => zeroWord
| TO_NEAREST =>
if x < ~half
then raise Overflow
else zeroWord)
else raise Overflow
end)
in
val (fromWord8,toWord8) =
make {fromWordUnsafe = R.fromWord8Unsafe,
toWordUnsafe = R.toWord8Unsafe,
other = {maxWord' = fn w => Word8.<< (Word8.>> (Word8.maxWord', w), w),
wordSize = Word8.wordSize,
zeroWord = Word8.zero}}
val (fromWord16,toWord16) =
make {fromWordUnsafe = R.fromWord16Unsafe,
toWordUnsafe = R.toWord16Unsafe,
other = {maxWord' = fn w => Word16.<< (Word16.>> (Word16.maxWord', w), w),
wordSize = Word16.wordSize,
zeroWord = Word16.zero}}
val (fromWord32,toWord32) =
make {fromWordUnsafe = R.fromWord32Unsafe,
toWordUnsafe = R.toWord32Unsafe,
other = {maxWord' = fn w => Word32.<< (Word32.>> (Word32.maxWord', w), w),
wordSize = Word32.wordSize,
zeroWord = Word32.zero}}
val (fromWord64,toWord64) =
make {fromWordUnsafe = R.fromWord64Unsafe,
toWordUnsafe = R.toWord64Unsafe,
other = {maxWord' = fn w => Word64.<< (Word64.>> (Word64.maxWord', w), w),
wordSize = Word64.wordSize,
zeroWord = Word64.zero}}
end
local
structure S =
Word_ChooseWordN
(type 'a t = 'a -> real
val fWord8 = fromWord8
val fWord16 = fromWord16
val fWord32 = fromWord32
val fWord64 = fromWord64)
in
val fromWord = S.f
end
local
structure S =
LargeWord_ChooseWordN
(type 'a t = 'a -> real
val fWord8 = fromWord8
val fWord16 = fromWord16
val fWord32 = fromWord32
val fWord64 = fromWord64)
in
val fromLargeWord = S.f
end
local
structure S =
Word_ChooseWordN
(type 'a t = rounding_mode -> real -> 'a
val fWord8 = toWord8
val fWord16 = toWord16
val fWord32 = toWord32
val fWord64 = toWord64)
in
val toWord = S.f
end
local
structure S =
LargeWord_ChooseWordN
(type 'a t = rounding_mode -> real -> 'a
val fWord8 = toWord8
val fWord16 = toWord16
val fWord32 = toWord32
val fWord64 = toWord64)
in
val toLargeWord = S.f
end