{-# LANGUAGE CPP #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE UnboxedTuples #-}
module Data.Memory.Internal.CompatPrim
( be32Prim
, le32Prim
, byteswap32Prim
, booleanPrim
, eitherDivideBy8#
) where
import GHC.Prim
be32Prim :: Word# -> Word#
#ifdef ARCH_IS_LITTLE_ENDIAN
be32Prim :: Word# -> Word#
be32Prim = Word# -> Word#
byteswap32Prim
#else
be32Prim w = w
#endif
le32Prim :: Word# -> Word#
#ifdef ARCH_IS_LITTLE_ENDIAN
le32Prim :: Word# -> Word#
le32Prim Word#
w = Word#
w
#else
le32Prim = byteswap32Prim
#endif
byteswap32Prim :: Word# -> Word#
#if __GLASGOW_HASKELL__ >= 708
byteswap32Prim :: Word# -> Word#
byteswap32Prim Word#
w = Word# -> Word#
byteSwap32# Word#
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
#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
{-# INLINE booleanPrim #-}
eitherDivideBy8# :: Int#
-> (Int# -> a)
-> (Int# -> a)
-> a
#if __GLASGOW_HASKELL__ > 704
eitherDivideBy8# :: Int# -> (Int# -> a) -> (Int# -> a) -> a
eitherDivideBy8# Int#
v Int# -> a
f8 Int# -> a
f1 =
let !(# Int#
q, Int#
r #) = Int# -> Int# -> (# Int#, Int# #)
quotRemInt# Int#
v Int#
8#
in if Int# -> Bool
booleanPrim (Int#
r Int# -> Int# -> Int#
==# Int#
0#)
then Int# -> a
f8 Int#
q
else Int# -> a
f1 Int#
v
#else
eitherDivideBy8# v f8 f1 =
if booleanPrim ((remInt# v 8#) ==# 0#)
then f8 (quotInt# v 8#)
else f1 v
#endif