module Data.Packer.Endian
( le16Host
, le32Host
, le64Host
, be16Host
, be32Host
, be64Host
) where
import Data.Bits
import Data.Word
#if MIN_VERSION_base(4,7,0)
swap16 :: Word16 -> Word16
swap16 = byteSwap16
swap32 :: Word32 -> Word32
swap32 = byteSwap32
swap64 :: Word64 -> Word64
swap64 = byteSwap64
#else
#if BITS_IS_OLD
shr :: Bits a => a -> Int -> a
shr = shiftR
shl :: Bits a => a -> Int -> a
shl = shiftL
#else
shr :: Bits a => a -> Int -> a
shr = unsafeShiftR
shl :: Bits a => a -> Int -> a
shl = unsafeShiftL
#endif
swap64 :: Word64 -> Word64
swap64 w =
(w `shr` 56) .|. (w `shl` 56)
.|. ((w `shr` 40) .&. 0xff00) .|. ((w .&. 0xff00) `shl` 40)
.|. ((w `shr` 24) .&. 0xff0000) .|. ((w .&. 0xff0000) `shl` 24)
.|. ((w `shr` 8) .&. 0xff000000) .|. ((w .&. 0xff000000) `shl` 8)
swap32 :: Word32 -> Word32
swap32 w =
(w `shr` 24) .|. (w `shl` 24)
.|. ((w `shr` 8) .&. 0xff00) .|. ((w .&. 0xff00) `shl` 8)
swap16 :: Word16 -> Word16
swap16 w = (w `shr` 8) .|. (w `shl` 8)
#endif
#ifdef CPU_BIG_ENDIAN
be16Host :: Word16 -> Word16
be16Host = id
be32Host :: Word32 -> Word32
be32Host = id
be64Host :: Word64 -> Word64
be64Host = id
le16Host :: Word16 -> Word16
le16Host w = swap16 w
le32Host :: Word32 -> Word32
le32Host w = swap32 w
le64Host :: Word64 -> Word64
le64Host w = swap64 w
#else
le16Host :: Word16 -> Word16
le16Host = id
le32Host :: Word32 -> Word32
le32Host = id
le64Host :: Word64 -> Word64
le64Host = id
be16Host :: Word16 -> Word16
be16Host w = swap16 w
be32Host :: Word32 -> Word32
be32Host w = swap32 w
be64Host :: Word64 -> Word64
be64Host w = swap64 w
#endif