[Internal] properly handle big endian architecture

add a CompatPrim module to handle all the primitive compatibility needed,
instead of putting all in Compat.
This commit is contained in:
Vincent Hanquez 2015-04-11 08:23:52 +01:00
parent d1554b36a6
commit 6dcba8d8cd
3 changed files with 43 additions and 2 deletions

View File

@ -0,0 +1,40 @@
-- |
-- Module : Crypto.Internal.CompatPrim
-- License : BSD-style
-- Maintainer : Vincent Hanquez <vincent@snarc.org>
-- Stability : stable
-- Portability : Compat
--
-- This module try to keep all the difference between versions of ghc primitive
-- or other needed packages, so that modules don't need to use CPP.
--
-- Note that MagicHash and CPP conflicts in places, making it "more interesting"
-- to write compat code for primitives
--
{-# LANGUAGE CPP #-}
{-# LANGUAGE MagicHash #-}
module Crypto.Internal.CompatPrim
( be32Prim
, byteswap32Prim
) where
import GHC.Prim
#ifdef ARCH_IS_LITTLE_ENDIAN
be32Prim :: Word# -> Word#
be32Prim = byteswap32Prim
#else
be32Prim w = w
#endif
byteswap32Prim :: Word# -> Word#
#if __GLASGOW_HASKELL__ >= 708
byteswap32Prim w = byteSwap32# w
#else
byteswap32Prim w =
let a = uncheckedShiftL# w 24#
b = and# (uncheckedShiftL# w 8#) 0x00ff0000##
c = and# (uncheckedShiftRL# w 8#) 0x0000ff00##
d = and# (uncheckedShiftRL# w 24#) 0x000000ff##
in or# a (or# b (or# c d))
#endif

View File

@ -35,6 +35,7 @@ module Crypto.Internal.WordArray
import Data.Word
import Data.Bits (xor)
import Crypto.Internal.Compat
import Crypto.Internal.CompatPrim
import GHC.Prim
import GHC.Types
import GHC.Word
@ -102,9 +103,8 @@ mutableArray32FromAddrBE (I# n) a = IO $ \s ->
loop i st mb
| booleanPrim (i ==# n) = (# st, MutableArray32 mb #)
| otherwise =
let st' = writeWord32Array# mb i (be (indexWord32OffAddr# a i)) st
let st' = writeWord32Array# mb i (be32Prim (indexWord32OffAddr# a i)) st
in loop (i +# 1#) st' mb
be = byteSwap32#
mutableArray32Freeze :: MutableArray32 -> IO Array32
mutableArray32Freeze (MutableArray32 mb) = IO $ \st ->

View File

@ -126,6 +126,7 @@ Library
Crypto.PubKey.Internal
Crypto.PubKey.ElGamal
Crypto.Internal.Compat
Crypto.Internal.CompatPrim
Crypto.Internal.Bytes
Crypto.Internal.Endian
Crypto.Internal.Imports