1 (* Copyright (C
) 1999-2007 Henry Cejtin
, Matthew Fluet
, Suresh
2 * Jagannathan
, and Stephen Weeks
.
3 * Copyright (C
) 1997-2000 NEC Research Institute
.
5 * MLton is released under a BSD
-style license
.
6 * See the file MLton
-LICENSE for details
.
9 functor PackWord (S
: sig
13 val subArr
: Word8.word array
* SeqIndex
.int -> word
14 val subVec
: Word8.word vector
* SeqIndex
.int -> word
15 val update
: Word8.word array
* SeqIndex
.int * word -> unit
16 val bswap
: word -> word
17 val toLarge
: word -> LargeWord
.word
18 val toLargeX
: word -> LargeWord
.word
19 val fromLarge
: LargeWord
.word -> word
20 end): PACK_WORD_EXTRA
=
25 val bytesPerElem
= Int.div (wordSize
, 8)
30 if Primitive
.Controls
.safe
31 andalso (Int.geu (Int.+ (Int.* (bytesPerElem
, i
),
32 Int.- (bytesPerElem
, 1)), n
))
38 handle Overflow
=> raise Subscript
40 val subArrRev
= bswap
o subArr
41 val subVecRev
= bswap
o subVec
42 fun updateRev (a
, i
, w
) = update (a
, i
, bswap w
)
44 val (subA
, subV
, updA
) =
45 if isBigEndian
= Primitive
.MLton
.Platform
.Arch
.hostIsBigEndian
46 then (subArr
, subVec
, update
)
47 else (subArrRev
, subVecRev
, updateRev
)
49 fun unsafeUpdate (a
, i
, w
) =
51 val i
= SeqIndex
.fromInt i
52 val a
= Word8Array
.toPoly a
54 updA (a
, i
, fromLarge w
)
57 fun update (a
, i
, w
) =
59 val i
= offset (i
, Word8Array
.length a
)
60 val a
= Word8Array
.toPoly a
62 updA (a
, i
, fromLarge w
)
66 fun make (sub
, length
, toPoly
) (av
, i
) =
68 val i
= offset (i
, length av
)
73 val subArr
= toLarge
o (make (subA
, Word8Array
.length
, Word8Array
.toPoly
))
74 val subArrX
= toLargeX
o (make (subA
, Word8Array
.length
, Word8Array
.toPoly
))
75 val subVec
= toLarge
o (make (subV
, Word8Vector
.length
, Word8Vector
.toPoly
))
76 val subVecX
= toLargeX
o (make (subV
, Word8Vector
.length
, Word8Vector
.toPoly
))
80 fun make (sub
, length
, toPoly
) (av
, i
) =
82 val i
= SeqIndex
.fromInt i
87 val unsafeSubArr
= toLarge
o (make (subA
, Word8Array
.length
, Word8Array
.toPoly
))
88 val unsafeSubArrX
= toLargeX
o (make (subA
, Word8Array
.length
, Word8Array
.toPoly
))
89 val unsafeSubVec
= toLarge
o (make (subV
, Word8Vector
.length
, Word8Vector
.toPoly
))
90 val unsafeSubVecX
= toLargeX
o (make (subV
, Word8Vector
.length
, Word8Vector
.toPoly
))
95 structure PackWord8Big
: PACK_WORD_EXTRA
=
96 PackWord (val isBigEndian
= true
97 open Primitive
.PackWord8
99 structure PackWord8Little
: PACK_WORD_EXTRA
=
100 PackWord (val isBigEndian
= false
101 open Primitive
.PackWord8
103 structure PackWord8Host
: PACK_WORD_EXTRA
=
104 PackWord (val isBigEndian
= Primitive
.MLton
.Platform
.Arch
.hostIsBigEndian
105 open Primitive
.PackWord8
107 structure PackWord16Big
: PACK_WORD_EXTRA
=
108 PackWord (val isBigEndian
= true
109 open Primitive
.PackWord16
111 structure PackWord16Little
: PACK_WORD_EXTRA
=
112 PackWord (val isBigEndian
= false
113 open Primitive
.PackWord16
115 structure PackWord16Host
: PACK_WORD_EXTRA
=
116 PackWord (val isBigEndian
= Primitive
.MLton
.Platform
.Arch
.hostIsBigEndian
117 open Primitive
.PackWord16
119 structure PackWord32Big
: PACK_WORD_EXTRA
=
120 PackWord (val isBigEndian
= true
121 open Primitive
.PackWord32
123 structure PackWord32Little
: PACK_WORD_EXTRA
=
124 PackWord (val isBigEndian
= false
125 open Primitive
.PackWord32
127 structure PackWord32Host
: PACK_WORD_EXTRA
=
128 PackWord (val isBigEndian
= Primitive
.MLton
.Platform
.Arch
.hostIsBigEndian
129 open Primitive
.PackWord32
131 structure PackWord64Big
: PACK_WORD_EXTRA
=
132 PackWord (val isBigEndian
= true
133 open Primitive
.PackWord64
135 structure PackWord64Little
: PACK_WORD_EXTRA
=
136 PackWord (val isBigEndian
= false
137 open Primitive
.PackWord64
139 structure PackWord64Host
: PACK_WORD_EXTRA
=
140 PackWord (val isBigEndian
= Primitive
.MLton
.Platform
.Arch
.hostIsBigEndian
141 open Primitive
.PackWord64
149 (type 'a t
= Word8.word array
* SeqIndex
.t
-> 'a
150 val fWord8
= Primitive
.PackWord8
.subArr
151 val fWord16
= Primitive
.PackWord16
.subArr
152 val fWord32
= Primitive
.PackWord32
.subArr
153 val fWord64
= Primitive
.PackWord64
.subArr
)
160 (type 'a t
= Word8.word vector
* SeqIndex
.t
-> 'a
161 val fWord8
= Primitive
.PackWord8
.subVec
162 val fWord16
= Primitive
.PackWord16
.subVec
163 val fWord32
= Primitive
.PackWord32
.subVec
164 val fWord64
= Primitive
.PackWord64
.subVec
)
171 (type 'a t
= Word8.word array
* SeqIndex
.t
* 'a
-> unit
172 val fWord8
= Primitive
.PackWord8
.update
173 val fWord16
= Primitive
.PackWord16
.update
174 val fWord32
= Primitive
.PackWord32
.update
175 val fWord64
= Primitive
.PackWord64
.update
)
181 structure PackWordBig
: PACK_WORD_EXTRA
=
182 PackWord (val isBigEndian
= true
185 structure PackWordLittle
: PACK_WORD_EXTRA
=
186 PackWord (val isBigEndian
= false
189 structure PackWordHost
: PACK_WORD_EXTRA
=
190 PackWord (val isBigEndian
= Primitive
.MLton
.Platform
.Arch
.hostIsBigEndian
195 structure PackLargeWord
=
199 LargeWord_ChooseWordN
200 (type 'a t
= Word8.word array
* SeqIndex
.t
-> 'a
201 val fWord8
= Primitive
.PackWord8
.subArr
202 val fWord16
= Primitive
.PackWord16
.subArr
203 val fWord32
= Primitive
.PackWord32
.subArr
204 val fWord64
= Primitive
.PackWord64
.subArr
)
210 LargeWord_ChooseWordN
211 (type 'a t
= Word8.word vector
* SeqIndex
.t
-> 'a
212 val fWord8
= Primitive
.PackWord8
.subVec
213 val fWord16
= Primitive
.PackWord16
.subVec
214 val fWord32
= Primitive
.PackWord32
.subVec
215 val fWord64
= Primitive
.PackWord64
.subVec
)
221 LargeWord_ChooseWordN
222 (type 'a t
= Word8.word array
* SeqIndex
.t
* 'a
-> unit
223 val fWord8
= Primitive
.PackWord8
.update
224 val fWord16
= Primitive
.PackWord16
.update
225 val fWord32
= Primitive
.PackWord32
.update
226 val fWord64
= Primitive
.PackWord64
.update
)
232 structure PackLargeWordBig
: PACK_WORD_EXTRA
=
233 PackWord (val isBigEndian
= true
236 structure PackLargeWordLittle
: PACK_WORD_EXTRA
=
237 PackWord (val isBigEndian
= false
240 structure PackLargeWordHost
: PACK_WORD_EXTRA
=
241 PackWord (val isBigEndian
= Primitive
.MLton
.Platform
.Arch
.hostIsBigEndian