module Data.Serialize.IEEE754 (
getFloat32le
, getFloat32be
, getFloat64le
, getFloat64be
, putFloat32le
, putFloat32be
, putFloat64le
, putFloat64be
) where
import Control.Applicative ( (<$>) )
import Control.Monad.ST ( runST, ST )
import Data.Array.ST ( newArray, readArray, MArray, STUArray )
import Data.Word ( Word32, Word64 )
import Data.Serialize.Get
import Data.Serialize.Put
#if __GLASGOW_HASKELL__ >= 704
import Data.Array.Unsafe (castSTUArray)
#else
import Data.Array.ST (castSTUArray)
#endif
getFloat32le :: Get Float
getFloat32le = wordToFloat <$> getWord32le
getFloat32be :: Get Float
getFloat32be = wordToFloat <$> getWord32be
getFloat64le :: Get Double
getFloat64le = wordToDouble <$> getWord64le
getFloat64be :: Get Double
getFloat64be = wordToDouble <$> getWord64be
putFloat32le :: Float -> Put
putFloat32le = putWord32le . floatToWord
putFloat32be :: Float -> Put
putFloat32be = putWord32be . floatToWord
putFloat64le :: Double -> Put
putFloat64le = putWord64le . doubleToWord
putFloat64be :: Double -> Put
putFloat64be = putWord64be . doubleToWord
wordToFloat :: Word32 -> Float
wordToFloat x = runST (cast x)
floatToWord :: Float -> Word32
floatToWord x = runST (cast x)
wordToDouble :: Word64 -> Double
wordToDouble x = runST (cast x)
doubleToWord :: Double -> Word64
doubleToWord x = runST (cast x)
cast :: (MArray (STUArray s) a (ST s),
MArray (STUArray s) b (ST s)) =>
a -> ST s b
cast x = newArray (0 :: Int, 0) x >>= castSTUArray >>= flip readArray 0