[endianess] fix some issues on non supported arches

This commit is contained in:
Vincent Hanquez 2015-08-28 17:10:27 +01:00
parent 36d5fde149
commit 71fe77da68

View File

@ -24,6 +24,9 @@ module Crypto.Internal.CompatPrim
) where
import GHC.Prim
#if !defined(ARCH_IS_LITTLE_ENDIAN) && !defined(ARCH_IS_BIG_ENDIAN)
import Data.Memory.Endian (getSystemEndianness, Endianness(..))
#endif
-- | byteswap Word# to or from Big Endian
--
@ -31,8 +34,10 @@ import GHC.Prim
be32Prim :: Word# -> Word#
#ifdef ARCH_IS_LITTLE_ENDIAN
be32Prim = byteswap32Prim
#elif defined(ARCH_IS_BIG_ENDIAN)
be32Prim = id
#else
be32Prim w = w
be32Prim w = if getSystemEndianness == LittleEndian then byteswap32Prim w else w
#endif
-- | byteswap Word# to or from Little Endian
@ -41,8 +46,10 @@ be32Prim w = w
le32Prim :: Word# -> Word#
#ifdef ARCH_IS_LITTLE_ENDIAN
le32Prim w = w
#else
#elif defined(ARCH_IS_BIG_ENDIAN)
le32Prim = byteswap32Prim
#else
le32Prim w = if getSystemEndianness == LittleEndian then w else byteswap32Prim w
#endif
-- | Simple compatibility for byteswap the lower 32 bits of a Word#
@ -69,11 +76,24 @@ convert4To32 a b c d = or# (or# c1 c2) (or# c3 c4)
!c2 = uncheckedShiftL# b 16#
!c3 = uncheckedShiftL# c 8#
!c4 = d
#else
#elif defined(ARCH_IS_BIG_ENDIAN)
!c1 = uncheckedShiftL# d 24#
!c2 = uncheckedShiftL# c 16#
!c3 = uncheckedShiftL# b 8#
!c4 = a
#else
!c1
| getSystemEndianness == LittleEndian = uncheckedShiftL# a 24#
| otherwise = uncheckedShiftL# d 24#
!c2
| getSystemEndianness == LittleEndian = uncheckedShiftL# b 16#
| otherwise = uncheckedShiftL# c 16#
!c3
| getSystemEndianness == LittleEndian = uncheckedShiftL# c 8#
| otherwise = uncheckedShiftL# b 8#
!c4
| getSystemEndianness == LittleEndian = d
| otherwise = a
#endif
-- | Simple wrapper to handle pre 7.8 and future, where