module Endian (endianness, Endianness(..)) where -- import Foreign.Marshal.Alloc import Foreign.Marshal.Utils import Foreign.Marshal.Array import Foreign.Ptr -- import Foreign.Storable import Data.Word -- | See http://en.wikipedia.org/wiki/Endianness -- Some machines (luckily mostly historical) used endiannesses -- other than strictly big-endian or strictly little-endian. -- We will just try to ignore them since they are probably -- entirely unsupported by other things including our libffi anyway. data Endianness = BigEndian | LittleEndian -- | OtherEndianness deriving (Eq, Ord, Show) -- | endianness returns the current environment's endianness. -- It assumes that this is well-defined and always succeeds. -- (Note: this could be changed so that an error is signalled -- for, say, 4-byte integer-style quantities where the 1 is at -- neither end, but this would not even be a complete test for -- unusual endiannesses.) endianness :: IO Endianness endianness = with (1 :: Word16) $ \ptr16 -> do let ptr8 = castPtr ptr16 :: Ptr Word8 bytes <- peekArray 2 ptr8 case bytes of [1,0] -> return LittleEndian [0,1] -> return BigEndian _ -> fail $ "endianness: something weird is going on; bytes of 1::Word16 are: " ++ show bytes