module System.Endian
( Endianness(..)
, getSystemEndianness
, fromLE32
, fromLE64
, toLE32
, toLE64
, fromBE32
, fromBE64
, toBE32
, toBE64
) where
#include "MachDeps.h"
import Foreign.C.Types
import Data.Word
data Endianness = LittleEndian
| BigEndian
deriving (Show,Eq)
getSystemEndianness :: Endianness
#ifdef WORDS_BIGENDIAN
getSystemEndianness = BigEndian
#else
getSystemEndianness = LittleEndian
#endif
fromBE64 :: Word64 -> Word64
fromBE64 = if getSystemEndianness == BigEndian then id else swap64
fromLE64 :: Word64 -> Word64
fromLE64 = if getSystemEndianness == LittleEndian then id else swap64
fromBE32 :: Word32 -> Word32
fromBE32 = if getSystemEndianness == BigEndian then id else swap32
fromLE32 :: Word32 -> Word32
fromLE32 = if getSystemEndianness == LittleEndian then id else swap32
toBE64 :: Word64 -> Word64
toBE64 = fromBE64
toLE64 :: Word64 -> Word64
toLE64 = fromLE64
toBE32 :: Word32 -> Word32
toBE32 = fromBE32
toLE32 :: Word32 -> Word32
toLE32 = fromLE32
swap32 :: Word32 -> Word32
swap32 = fromIntegral . c_swap32 . fromIntegral
swap64 :: Word64 -> Word64
swap64 = fromIntegral . c_swap64 . fromIntegral
foreign import ccall safe "bitfn_swap32" c_swap32 :: CUInt -> CUInt
foreign import ccall safe "bitfn_swap64" c_swap64 :: CULLong -> CULLong