Import Upstream version 20180207
[hcoop/debian/mlton.git] / mlton / control / bits.sml
CommitLineData
7f918cf1
CE
1(* Copyright (C) 2009 Matthew Fluet.
2 * Copyright (C) 2004-2007 Henry Cejtin, Matthew Fluet, Suresh
3 * Jagannathan, and Stephen Weeks.
4 *
5 * MLton is released under a BSD-style license.
6 * See the file MLton-LICENSE for details.
7 *)
8
9local
10 structure All:>
11 sig
12 type bits
13 type bytes
14
15 structure Bits:
16 sig
17 eqtype t
18
19 val + : t * t -> t
20 val - : t * t -> t
21 val ~ : t -> t
22 val < : t * t -> bool
23 val <= : t * t -> bool
24 val > : t * t -> bool
25 val >= : t * t -> bool
26 (* val align: t * {alignment: t} -> t *)
27 val alignDown: t * {alignment: t} -> t
28 (* val alignWord32: t -> t *)
29 (* val alignWord64: t -> t *)
30 val compare: t * t -> Relation.t
31 val equals: t * t -> bool
32 val fromInt: int -> t
33 val fromIntInf: IntInf.t -> t
34 val inByte: t
35 val inWord8: t
36 val inWord16: t
37 val inWord32: t
38 val inWord64: t
39 val isAligned: t * {alignment: t} -> bool
40 val isByteAligned: t -> bool
41 val isWord8Aligned: t -> bool
42 val isWord16Aligned: t -> bool
43 val isWord32Aligned: t -> bool
44 val isWord64Aligned: t -> bool
45 val isZero: t -> bool
46 val layout: t -> Layout.t
47 val max: t * t -> t
48 val min: t * t -> t
49 val one: t
50 val toBytes: t -> bytes
51 val toInt: t -> int
52 val toIntInf: t -> IntInf.t
53 val toString: t -> string
54 val toWord: t -> word
55 val zero: t
56 end
57
58 structure Bytes:
59 sig
60 type t
61
62 val + : t * t -> t
63 val - : t * t -> t
64 val ~ : t -> t
65 val < : t * t -> bool
66 val <= : t * t -> bool
67 val > : t * t -> bool
68 val >= : t * t -> bool
69 val align: t * {alignment: t} -> t
70 (* val alignDown: t * {alignment: t} -> t *)
71 (* val alignWord8: t -> t *)
72 (* val alignWord16: t -> t *)
73 val alignWord32: t -> t
74 val alignWord64: t -> t
75 val compare: t * t -> Relation.t
76 val equals: t * t -> bool
77 val fromInt: int -> t
78 val fromIntInf: IntInf.t -> t
79 (* val inWord8: t *)
80 (* val inWord16: t *)
81 val inWord32: t
82 val inWord64: t
83 (* val isAligned: t * {alignment: t} -> bool *)
84 val isWord32Aligned: t -> bool
85 (* val isWord64Aligned: t -> bool *)
86 val isZero: t -> bool
87 val layout: t -> Layout.t
88 val max: t * t -> t
89 val min: t * t -> t
90 val one: t
91 val toBits: t -> Bits.t
92 val toInt: t -> int
93 val toIntInf: t -> IntInf.t
94 val toString: t -> string
95 val zero: t
96 end
97
98 sharing type bits = Bits.t
99 sharing type bytes = Bytes.t
100 end =
101 struct
102 type bits = IntInf.t
103 type bytes = IntInf.t
104
105 val rem = IntInf.rem
106
107 fun align (b, {alignment = a}) =
108 let
109 val b = b + (a - 1)
110 in
111 b - rem (b, a)
112 end
113 fun alignDown (b, {alignment = a}) =
114 let
115 in
116 b - rem (b, a)
117 end
118
119 structure Bits =
120 struct
121 open IntInf
122
123 val inByte: bits = 8
124 val inWord8: bits = 8
125 val inWord16: bits = 16
126 val inWord32: bits = 32
127 val inWord64: bits = 64
128
129 fun isAligned (b, {alignment = a}) = 0 = rem (b, a)
130 fun isByteAligned b = isAligned (b, {alignment = inByte})
131 fun isWord8Aligned b = isAligned (b, {alignment = inWord8})
132 fun isWord16Aligned b = isAligned (b, {alignment = inWord16})
133 fun isWord32Aligned b = isAligned (b, {alignment = inWord32})
134 fun isWord64Aligned b = isAligned (b, {alignment = inWord64})
135
136 fun toBytes b =
137 if isByteAligned b
138 then quot (b, inByte)
139 else Error.bug "Bits.toBytes"
140
141 val toWord = Word.fromIntInf
142
143 (* val align = align *)
144 val alignDown = alignDown
145 (* fun alignWord32 b = align (b, {alignment = inWord32}) *)
146 (* fun alignWord64 b = align (b, {alignment = inWord64}) *)
147 end
148
149 structure Bytes =
150 struct
151 open IntInf
152
153 (* val inWord8: bytes = 1 *)
154 (* val inWord16: bytes = 2 *)
155 val inWord32: bytes = 4
156 val inWord64: bytes = 8
157
158 fun isAligned (b, {alignment = a}) = 0 = rem (b, a)
159 (* fun isWord8Aligned b = isAligned (b, {alignment = inWord8}) *)
160 (* fun isWord16Aligned b = isAligned (b, {alignment = inWord16}) *)
161 fun isWord32Aligned b = isAligned (b, {alignment = inWord32})
162 (* fun isWord64Aligned b = isAligned (b, {alignment = inWord64}) *)
163
164 fun toBits b = b * Bits.inByte
165
166 val align = align
167 (* val alignDown = alignDown *)
168 (* fun alignWord8 b = align (b, {alignment = inWord8}) *)
169 (* fun alignWord16 b = align (b, {alignment = inWord16}) *)
170 fun alignWord32 b = align (b, {alignment = inWord32})
171 fun alignWord64 b = align (b, {alignment = inWord64})
172 end
173 end
174
175 open All
176in
177 structure Bits = Bits
178 structure Bytes = Bytes
179end