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
|
{-# LANGUAGE MagicHash, UnboxedTuples, CPP #-}
module Main where
#include "MachDeps.h"
import GHC.Ptr(Ptr(..), nullPtr, plusPtr, minusPtr)
import GHC.Stable(
StablePtr(..), castStablePtrToPtr, castPtrToStablePtr, newStablePtr)
import GHC.Exts
import Data.Char(ord)
#if WORD_SIZE_IN_BITS < 64
import GHC.Int (Int64(..))
import GHC.Word (Word64(..))
#endif
assertEqual :: (Show a, Eq a) => a -> a -> IO ()
assertEqual a b
| a /= b = putStrLn (show a ++ " /= " ++ show b)
| otherwise = return ()
readBytes :: MutableByteArray# s -> State# s -> Int# -> (# State# s, [Int] #)
readBytes marr s0 len = go s0 len []
where
go s 0# bs = (# s, bs #)
go s i bs = case readWord8Array# marr (i -# 1#) s of
(# s', b #) -> go s' (i -# 1#) (I# (word2Int# b):bs)
indexBytes :: ByteArray# -> Int# -> [Int]
indexBytes arr len =
[I# (word2Int# (indexWord8Array# arr i)) | I# i <- [0..I# len - 1]]
fillByteArray :: MutableByteArray# s -> Int# -> Int -> State# s -> State# s
fillByteArray arr len (I# a) = go len
where
go 0# s = s
go i s = go (i -# 1#) (writeInt8Array# arr (i -# 1#) a s)
test :: (Eq a, Show a)
=> String
-> (ByteArray# -> Int# -> a)
-> (MutableByteArray# RealWorld -> Int# -> State# RealWorld
-> (# State# RealWorld, a #))
-> (MutableByteArray# RealWorld -> Int# -> a -> State# RealWorld
-> State# RealWorld)
-> a
-> [Int]
-> Int
-> IO ()
test name index read write val valBytes len = do
putStrLn name
mapM_ testAtOffset [0..16]
where
arrLen :: Int#
arrLen = 24#
fillerByte :: Int
fillerByte = 0x34
expectedArrayBytes :: Int -> [Int]
expectedArrayBytes offset =
replicate offset fillerByte
++ valBytes
++ replicate (I# arrLen - len - offset) fillerByte
testAtOffset :: Int -> IO ()
testAtOffset offset@(I# offset#) = runRW# (\s0 -> let
(# s1, marr #) = newByteArray# arrLen s0
s2 = fillByteArray marr arrLen fillerByte s1
s3 = write marr offset# val s2
(# s4, actual0 #) = read marr offset# s3
(# s5, actualBytes0 #) = readBytes marr s4 arrLen
(# _, arr #) = unsafeFreezeByteArray# marr s5
actual1 = index arr offset#
actualBytes1 = indexBytes arr arrLen
in do
assertEqual actual0 val
assertEqual actual1 val
assertEqual actualBytes0 (expectedArrayBytes offset)
assertEqual actualBytes1 (expectedArrayBytes offset)
)
intToBytes :: Int -> Int -> [Int]
intToBytes (I# val0) (I# len0) = let
result = go val0 len0
go v 0# = []
go v len =
I# (v `andI#` 0xff#) : go (v `uncheckedIShiftRL#` 8#) (len -# 1#)
in
#if defined(WORDS_BIGENDIAN)
reverse result
#else
result
#endif
testIntArray ::
String
-> (ByteArray# -> Int# -> Int#)
-> (MutableByteArray# RealWorld -> Int# -> State# RealWorld
-> (# State# RealWorld, Int# #))
-> (MutableByteArray# RealWorld -> Int# -> Int# -> State# RealWorld
-> State# RealWorld)
-> Int
-> Int
-> IO ()
testIntArray name0 index read write val0 len = do
doOne (name0 ++ " positive") val0
doOne (name0 ++ " negative") (negate val0)
where
doOne name val = test
name
(\arr i -> I# (index arr i))
(\arr i s -> case read arr i s of (# s', a #) -> (# s', I# a #))
(\arr i (I# a) s -> write arr i a s)
val
(intToBytes val len)
len
#if WORD_SIZE_IN_BITS == 64
testInt64Array = testIntArray
#else
testInt64Array ::
String
-> (ByteArray# -> Int# -> Int64#)
-> (MutableByteArray# RealWorld -> Int# -> State# RealWorld
-> (# State# RealWorld, Int64# #))
-> (MutableByteArray# RealWorld -> Int# -> Int64# -> State# RealWorld
-> State# RealWorld)
-> Int64
-> Int
-> IO ()
testInt64Array name0 index read write val0 len = do
doOne (name0 ++ " positive") val0
doOne (name0 ++ " negative") (negate val0)
where
doOne :: String -> Int64 -> IO ()
doOne name val = test
name
(\arr i -> I64# (index arr i))
(\arr i s -> case read arr i s of (# s', a #) -> (# s', I64# a #))
(\arr i (I64# a) s -> write arr i a s)
val
(intToBytes (fromIntegral val) len)
len
#endif
testWordArray ::
String
-> (ByteArray# -> Int# -> Word#)
-> (MutableByteArray# RealWorld -> Int# -> State# RealWorld
-> (# State# RealWorld, Word# #))
-> (MutableByteArray# RealWorld -> Int# -> Word# -> State# RealWorld
-> State# RealWorld)
-> Word
-> Int
-> IO ()
testWordArray name index read write val len = test
name
(\arr i -> W# (index arr i))
(\arr i s -> case read arr i s of (# s', a #) -> (# s', W# a #))
(\arr i (W# a) s -> write arr i a s)
val
(intToBytes (fromIntegral val) len)
len
#if WORD_SIZE_IN_BITS == 64
testWord64Array = testWordArray
#else
testWord64Array ::
String
-> (ByteArray# -> Int# -> Word64#)
-> (MutableByteArray# RealWorld -> Int# -> State# RealWorld
-> (# State# RealWorld, Word64# #))
-> (MutableByteArray# RealWorld -> Int# -> Word64# -> State# RealWorld
-> State# RealWorld)
-> Word64
-> Int
-> IO ()
testWord64Array name index read write val len = test
name
(\arr i -> W64# (index arr i))
(\arr i s -> case read arr i s of (# s', a #) -> (# s', W64# a #))
(\arr i (W64# a) s -> write arr i a s)
val
(intToBytes (fromIntegral val) len)
len
#endif
wordSizeInBytes :: Int
wordSizeInBytes = WORD_SIZE_IN_BITS `div` 8
int :: Int
int
| WORD_SIZE_IN_BITS == 32 = 12345678
| otherwise = 1234567890123
word :: Word
word = fromIntegral int
float :: Float
float = 123.456789
-- Test pattern generated by this python code:
-- >>> import struct
-- >>> import binascii
-- >>> binascii.hexlify(struct.pack('>f', 123.456789))
floatBytes :: Int
floatBytes = 0x42f6e9e0
double :: Double
double = 123.45678901234
-- Test pattern generated by this python code:
-- >>> import struct
-- >>> import binascii
-- >>> binascii.hexlify(struct.pack('>d', 123.45678901234))
doubleBytes :: Int
doubleBytes = 0x405edd3c07fb4b09
main :: IO ()
main = do
testIntArray "Int8#"
indexInt8Array# readInt8Array# writeInt8Array#
123 1
testIntArray "Int16#"
indexWord8ArrayAsInt16# readWord8ArrayAsInt16# writeWord8ArrayAsInt16#
12345 2
testIntArray "Int32#"
indexWord8ArrayAsInt32# readWord8ArrayAsInt32# writeWord8ArrayAsInt32#
12345678 4
testInt64Array "Int64#"
indexWord8ArrayAsInt64# readWord8ArrayAsInt64# writeWord8ArrayAsInt64#
1234567890123 8
testIntArray "Int#"
indexWord8ArrayAsInt# readWord8ArrayAsInt# writeWord8ArrayAsInt#
int wordSizeInBytes
testWordArray "Word8#"
indexWord8Array# readWord8Array# writeWord8Array#
123 1
testWordArray "Word16#"
indexWord8ArrayAsWord16# readWord8ArrayAsWord16# writeWord8ArrayAsWord16#
12345 2
testWordArray "Word32#"
indexWord8ArrayAsWord32# readWord8ArrayAsWord32# writeWord8ArrayAsWord32#
12345678 4
testWord64Array "Word64#"
indexWord8ArrayAsWord64# readWord8ArrayAsWord64# writeWord8ArrayAsWord64#
1234567890123 8
testWordArray "Word#"
indexWord8ArrayAsWord# readWord8ArrayAsWord# writeWord8ArrayAsWord#
word wordSizeInBytes
test
"Char#"
(\arr i -> C# (indexWord8ArrayAsChar# arr i))
(\arr i s ->
case readWord8ArrayAsChar# arr i s of (# s', a #) -> (# s', C# a #))
(\arr i (C# a) s -> writeWord8ArrayAsChar# arr i a s)
'z'
[ord 'z']
1
test
"WideChar#"
(\arr i -> C# (indexWord8ArrayAsWideChar# arr i))
(\arr i s ->
case readWord8ArrayAsWideChar# arr i s of (# s', a #) -> (# s', C# a #))
(\arr i (C# a) s -> writeWord8ArrayAsWideChar# arr i a s)
'𠜎' -- See http://www.i18nguy.com/unicode/supplementary-test.html
(intToBytes (ord '𠜎') 4)
4
test
"Addr#"
(\arr i -> Ptr (indexWord8ArrayAsAddr# arr i))
(\arr i s ->
case readWord8ArrayAsAddr# arr i s of (# s', a #) -> (# s', Ptr a #))
(\arr i (Ptr a) s -> writeWord8ArrayAsAddr# arr i a s)
(nullPtr `plusPtr` int)
(intToBytes int wordSizeInBytes)
wordSizeInBytes
stablePtr <- newStablePtr ()
test
"StablePtr#"
(\arr i ->
castStablePtrToPtr (StablePtr (indexWord8ArrayAsStablePtr# arr i)))
(\arr i s -> case readWord8ArrayAsStablePtr# arr i s of
(# s', a #) -> (# s', castStablePtrToPtr (StablePtr a) #))
(\arr i p s -> case castPtrToStablePtr p of
(StablePtr a) -> writeWord8ArrayAsStablePtr# arr i a s)
(castStablePtrToPtr stablePtr)
(intToBytes (castStablePtrToPtr stablePtr `minusPtr` nullPtr)
wordSizeInBytes)
wordSizeInBytes
test
"Float#"
(\arr i -> F# (indexWord8ArrayAsFloat# arr i))
(\arr i s ->
case readWord8ArrayAsFloat# arr i s of (# s', a #) -> (# s', F# a #))
(\arr i (F# a) s -> writeWord8ArrayAsFloat# arr i a s)
float
(intToBytes floatBytes 4)
4
test
"Double#"
(\arr i -> D# (indexWord8ArrayAsDouble# arr i))
(\arr i s ->
case readWord8ArrayAsDouble# arr i s of (# s', a #) -> (# s', D# a #))
(\arr i (D# a) s -> writeWord8ArrayAsDouble# arr i a s)
double
(intToBytes doubleBytes 8)
8
|