-
Notifications
You must be signed in to change notification settings - Fork 14
/
convert-intinf.sml
executable file
·87 lines (70 loc) · 2.46 KB
/
convert-intinf.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
structure ConvertIntInf :> CONVERT_INT_INF =
struct
open IntInf
fun toLoop len acc x =
if x = 0 then
(len, acc)
else
toLoop (Int.+ (len, 1)) (Word8.fromInt (IntInf.toInt (andb (x, 0xff))) :: acc) (~>> (x, 0w8))
fun toBytesB x =
if x <= 0 then
if x = 0 then
Bytestring.str 0w0
else
let
val x' = IntInf.+ (IntInf.<< (1, Word.+ (Word.fromInt (IntInf.log2 (IntInf.~ x)), 0w1)), x)
val (_, l) = toLoop 0 [] x'
in
Bytestring.implode l
end
else
let
val (_, l) = toLoop 0 [] x
in
Bytestring.implode l
end
fun toBytesL x = Bytestring.rev (toBytesB x)
fun dupOnto x i l =
if Int.<= (i, 0) then
l
else
dupOnto x (Int.- (i, 1)) (x :: l)
fun toFixedBytesB (digits, x) =
if Int.<= (digits, 0) then
raise Domain
else
if x >= 0 then
let
val (len, l) = toLoop 0 [] x
in
if Int.> (len, digits) then
raise Overflow
else
Bytestring.implode (dupOnto 0w0 (Int.- (digits, len)) l)
end
else
let
val x' = IntInf.+ (IntInf.<< (1, Word.+ (Word.fromInt (IntInf.log2 (IntInf.~ x)), 0w1)), x)
val (len, l) = toLoop 0 [] x'
in
if Int.> (len, digits) then
raise Overflow
else
Bytestring.implode (dupOnto 0wxff (Int.- (digits, len)) l)
end
fun toFixedBytesL x = Bytestring.rev (toFixedBytesB x)
fun fromLoop acc l =
(case l of
[] => acc
| b :: t =>
fromLoop (orb (<< (acc, 0w8), IntInf.fromInt (Word8.toInt b))) t)
fun fromBytesB s =
fromLoop 0 (Bytestring.explode s)
fun fromBytesL s = fromBytesB (Bytestring.rev s)
fun fromSignedBytesB (sign, s) =
if sign then
fromLoop 0 (Bytestring.explode s) - << (1, Word.fromInt (Int.* (8, Bytestring.size s)))
else
fromLoop 0 (Bytestring.explode s)
fun fromSignedBytesL (sign, s) = fromSignedBytesB (sign, Bytestring.rev s)
end