6
6
{-# LANGUAGE ViewPatterns #-}
7
7
{-# LANGUAGE UnliftedFFITypes #-}
8
8
9
+ -- Required for WORDS_BIGENDIAN
10
+ #include <ghcautoconf.h>
11
+
9
12
-- |
10
13
-- Module : System.OsString.Data.ByteString.Short.Internal
11
14
-- Copyright : © 2022 Julian Ospald
@@ -21,7 +24,6 @@ module System.OsString.Data.ByteString.Short.Internal where
21
24
22
25
import Control.Monad.ST
23
26
import Control.Exception (assert , throwIO )
24
- import Data.Bits (Bits (.. ))
25
27
import Data.ByteString.Short.Internal (ShortByteString (.. ), length )
26
28
#if !MIN_VERSION_base(4,11,0)
27
29
import Data.Semigroup
@@ -282,64 +284,38 @@ packLenWord16Rev len ws0 =
282
284
go mba (i - 2 ) ws
283
285
284
286
285
- -- | This isn't strictly Word16 array write. Instead it's two consecutive Word8 array
286
- -- writes to avoid endianness issues due to primops doing automatic alignment based
287
- -- on host platform. We want to always write LE to the byte array.
287
+ -- | Encode Word16 as little-endian.
288
288
writeWord16Array :: MBA s
289
289
-> Int -- ^ Word8 index (not Word16)
290
290
-> Word16
291
291
-> ST s ()
292
- writeWord16Array (MBA # mba# ) (I # i# ) (W16 # w# ) =
293
- case encodeWord16LE# w# of
294
- (# lsb# , msb# # ) ->
295
- ST (\ s -> case writeWord8Array# mba# i# lsb# s of
296
- s' -> (# s', () # )) >>
297
- ST (\ s -> case writeWord8Array# mba# (i# +# 1 # ) msb# s of
298
- s' -> (# s', () # ))
292
+ writeWord16Array (MBA # mba# ) (I # i# ) (W16 # w# ) = ST $ \ s ->
293
+ case writeWord8ArrayAsWord16# mba# i# (word16ToLE# w# ) s of
294
+ s' -> (# s', () # )
299
295
300
296
indexWord8Array :: BA
301
297
-> Int -- ^ Word8 index
302
298
-> Word8
303
299
indexWord8Array (BA # ba# ) (I # i# ) = W8 # (indexWord8Array# ba# i# )
304
300
305
- -- | This isn't strictly Word16 array read. Instead it's two Word8 array reads
306
- -- to avoid endianness issues due to primops doing automatic alignment based
307
- -- on host platform. We expect the byte array to be LE always.
301
+ -- | Decode Word16 from little-endian.
308
302
indexWord16Array :: BA
309
303
-> Int -- ^ Word8 index (not Word16)
310
304
-> Word16
311
- indexWord16Array ba i = fromIntegral lsb .|. (fromIntegral msb `shiftL` 8 )
312
- where
313
- lsb = indexWord8Array ba i
314
- msb = indexWord8Array ba (i + 1 )
315
-
316
- #if !MIN_VERSION_base(4,16,0)
317
-
318
- encodeWord16LE# :: Word # -- ^ Word16
319
- -> (# Word # , Word # # ) -- ^ Word8 (LSB, MSB)
320
- encodeWord16LE# x# = (# x# `and # ` int2Word# 0xff #
321
- , x# `and # ` int2Word# 0xff00 # `shiftRL# ` 8 # # )
322
-
323
- decodeWord16LE# :: (# Word # , Word # # ) -- ^ Word8 (LSB, MSB)
324
- -> Word # -- ^ Word16
325
- decodeWord16LE# (# lsb# , msb# # ) = msb# `shiftL# ` 8 # `or # ` lsb#
305
+ indexWord16Array (BA # ba# ) (I # i# ) =
306
+ W16 # (word16FromLE# (indexWord8ArrayAsWord16# ba# i# ))
326
307
308
+ #if MIN_VERSION_base(4,16,0)
309
+ word16ToLE# , word16FromLE# :: Word16 # -> Word16 #
327
310
#else
328
-
329
- encodeWord16LE# :: Word16 # -- ^ Word16
330
- -> (# Word8 # , Word8 # # ) -- ^ Word8 (LSB, MSB)
331
- encodeWord16LE# x# = (# word16ToWord8# x#
332
- , word16ToWord8# (x# `uncheckedShiftRLWord16# ` 8 # ) # )
333
- where
334
- word16ToWord8# y = wordToWord8# (word16ToWord# y)
335
-
336
- decodeWord16LE# :: (# Word8 # , Word8 # # ) -- ^ Word8 (LSB, MSB)
337
- -> Word16 # -- ^ Word16
338
- decodeWord16LE# (# lsb# , msb# # ) = ((word8ToWord16# msb# `uncheckedShiftLWord16# ` 8 # ) `orWord16# ` word8ToWord16# lsb# )
339
- where
340
- word8ToWord16# y = wordToWord16# (word8ToWord# y)
341
-
311
+ word16ToLE# , word16FromLE# :: Word # -> Word #
312
+ #endif
313
+ #ifdef WORDS_BIGENDIAN
314
+ word16ToLE# = byteSwap16#
315
+ #else
316
+ word16ToLE# w# = w#
342
317
#endif
318
+ word16FromLE# = word16ToLE#
343
319
344
320
setByteArray :: MBA s -> Int -> Int -> Int -> ST s ()
345
321
setByteArray (MBA # dst# ) (I # off# ) (I # len# ) (I # c# ) =
0 commit comments