Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit 20b194f

Browse files
committed
fix byteswap32 to work on Word32# instead of Word# (with compat for ghc < 9.2)
1 parent cca5d72 commit 20b194f

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

Crypto/Internal/CompatPrim.hs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,21 @@ module Crypto.Internal.CompatPrim
2323
, convert4To32
2424
) where
2525

26-
import GHC.Prim
2726
#if !defined(ARCH_IS_LITTLE_ENDIAN) && !defined(ARCH_IS_BIG_ENDIAN)
2827
import Data.Memory.Endian (getSystemEndianness, Endianness(..))
2928
#endif
3029

30+
#if __GLASGOW_HASKELL__ >= 902
31+
import GHC.Prim
32+
#else
33+
import GHC.Prim hiding (Word32#)
34+
type Word32# = Word#
35+
#endif
36+
3137
-- | Byteswap Word# to or from Big Endian
3238
--
3339
-- On a big endian machine, this function is a nop.
34-
be32Prim :: Word# -> Word#
40+
be32Prim :: Word32# -> Word32#
3541
#ifdef ARCH_IS_LITTLE_ENDIAN
3642
be32Prim = byteswap32Prim
3743
#elif defined(ARCH_IS_BIG_ENDIAN)
@@ -43,7 +49,7 @@ be32Prim w = if getSystemEndianness == LittleEndian then byteswap32Prim w else w
4349
-- | Byteswap Word# to or from Little Endian
4450
--
4551
-- On a little endian machine, this function is a nop.
46-
le32Prim :: Word# -> Word#
52+
le32Prim :: Word32# -> Word32#
4753
#ifdef ARCH_IS_LITTLE_ENDIAN
4854
le32Prim w = w
4955
#elif defined(ARCH_IS_BIG_ENDIAN)
@@ -54,16 +60,11 @@ le32Prim w = if getSystemEndianness == LittleEndian then w else byteswap32Prim w
5460

5561
-- | Simple compatibility for byteswap the lower 32 bits of a Word#
5662
-- at the primitive level
57-
byteswap32Prim :: Word# -> Word#
58-
#if __GLASGOW_HASKELL__ >= 708
59-
byteswap32Prim w = byteSwap32# w
63+
byteswap32Prim :: Word32# -> Word32#
64+
#if __GLASGOW_HASKELL__ >= 902
65+
byteswap32Prim w = wordToWord32# (byteSwap32# (word32ToWord# w))
6066
#else
61-
byteswap32Prim w =
62-
let !a = uncheckedShiftL# w 24#
63-
!b = and# (uncheckedShiftL# w 8#) 0x00ff0000##
64-
!c = and# (uncheckedShiftRL# w 8#) 0x0000ff00##
65-
!d = and# (uncheckedShiftRL# w 24#) 0x000000ff##
66-
in or# a (or# b (or# c d))
67+
byteswap32Prim w = byteSwap32# w
6768
#endif
6869

6970
-- | Combine 4 word8 [a,b,c,d] to a word32 representing [a,b,c,d]

0 commit comments

Comments
 (0)