{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE UnboxedTuples #-}
module Crypto.Internal.CompatPrim (
be32Prim,
le32Prim,
byteswap32Prim,
booleanPrim,
convert4To32,
) where
#if !defined(ARCH_IS_LITTLE_ENDIAN) && !defined(ARCH_IS_BIG_ENDIAN)
import Data.Memory.Endian (getSystemEndianness, Endianness(..))
#endif
#if __GLASGOW_HASKELL__ >= 902
import GHC.Prim
#else
import GHC.Prim hiding (Word32#)
type Word32# = Word#
#endif
be32Prim :: Word32# -> Word32#
#ifdef ARCH_IS_LITTLE_ENDIAN
be32Prim = byteswap32Prim
#elif defined(ARCH_IS_BIG_ENDIAN)
be32Prim = id
#else
be32Prim :: Word32# -> Word32#
be32Prim Word32#
w = if Endianness
getSystemEndianness Endianness -> Endianness -> Bool
forall a. Eq a => a -> a -> Bool
== Endianness
LittleEndian then Word32# -> Word32#
byteswap32Prim Word32#
w else Word32#
w
#endif
le32Prim :: Word32# -> Word32#
#ifdef ARCH_IS_LITTLE_ENDIAN
le32Prim w = w
#elif defined(ARCH_IS_BIG_ENDIAN)
le32Prim = byteswap32Prim
#else
le32Prim :: Word32# -> Word32#
le32Prim Word32#
w = if Endianness
getSystemEndianness Endianness -> Endianness -> Bool
forall a. Eq a => a -> a -> Bool
== Endianness
LittleEndian then Word32#
w else Word32# -> Word32#
byteswap32Prim Word32#
w
#endif
byteswap32Prim :: Word32# -> Word32#
#if __GLASGOW_HASKELL__ >= 902
byteswap32Prim :: Word32# -> Word32#
byteswap32Prim Word32#
w = Word# -> Word32#
wordToWord32# (Word# -> Word#
byteSwap32# (Word32# -> Word#
word32ToWord# Word32#
w))
#else
byteswap32Prim w = byteSwap32# w
#endif
convert4To32
:: Word#
-> Word#
-> Word#
-> Word#
-> Word#
convert4To32 :: Word# -> Word# -> Word# -> Word# -> Word#
convert4To32 Word#
a Word#
b Word#
c Word#
d = Word# -> Word# -> Word#
or# (Word# -> Word# -> Word#
or# Word#
c1 Word#
c2) (Word# -> Word# -> Word#
or# Word#
c3 Word#
c4)
where
#ifdef ARCH_IS_LITTLE_ENDIAN
!c1 = uncheckedShiftL# a 24#
!c2 = uncheckedShiftL# b 16#
!c3 = uncheckedShiftL# c 8#
!c4 = d
#elif defined(ARCH_IS_BIG_ENDIAN)
!c1 = uncheckedShiftL# d 24#
!c2 = uncheckedShiftL# c 16#
!c3 = uncheckedShiftL# b 8#
!c4 = a
#else
!c1 :: Word#
c1
| Endianness
getSystemEndianness Endianness -> Endianness -> Bool
forall a. Eq a => a -> a -> Bool
== Endianness
LittleEndian = Word# -> Int# -> Word#
uncheckedShiftL# Word#
a Int#
24#
| Bool
otherwise = Word# -> Int# -> Word#
uncheckedShiftL# Word#
d Int#
24#
!c2 :: Word#
c2
| Endianness
getSystemEndianness Endianness -> Endianness -> Bool
forall a. Eq a => a -> a -> Bool
== Endianness
LittleEndian = Word# -> Int# -> Word#
uncheckedShiftL# Word#
b Int#
16#
| Bool
otherwise = Word# -> Int# -> Word#
uncheckedShiftL# Word#
c Int#
16#
!c3 :: Word#
c3
| Endianness
getSystemEndianness Endianness -> Endianness -> Bool
forall a. Eq a => a -> a -> Bool
== Endianness
LittleEndian = Word# -> Int# -> Word#
uncheckedShiftL# Word#
c Int#
8#
| Bool
otherwise = Word# -> Int# -> Word#
uncheckedShiftL# Word#
b Int#
8#
!c4 :: Word#
c4
| Endianness
getSystemEndianness Endianness -> Endianness -> Bool
forall a. Eq a => a -> a -> Bool
== Endianness
LittleEndian = Word#
d
| Bool
otherwise = Word#
a
#endif
#if __GLASGOW_HASKELL__ >= 708
booleanPrim :: Int# -> Bool
booleanPrim :: Int# -> Bool
booleanPrim Int#
v = Int# -> Bool
forall a. Int# -> a
tagToEnum# Int#
v
#else
booleanPrim :: Bool -> Bool
booleanPrim b = b
#endif