module System.Endian
( Endianness(..)
, getSystemEndianness
, fromLE32
, fromLE64
, toLE32
, toLE64
, fromBE32
, fromBE64
, toBE32
, toBE64
) where
import Foreign.Marshal.Alloc
import Foreign.Storable
import Foreign.Ptr
import Foreign.C.Types
import System.IO.Unsafe (unsafePerformIO)
import Data.Word
data Endianness = LittleEndian
| BigEndian
deriving (Show,Eq)
getSystemEndianness :: Endianness
getSystemEndianness = unsafePerformIO $ alloca $ \p ->
poke p cst >> peek (castPtr p) >>= return . check
where cst :: Word32
cst = 0x01000000
check :: Word8 -> Endianness
check x
| x == 0x01 = BigEndian
| otherwise = LittleEndian
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