#ifndef MIN_VERSION_base
#define MIN_VERSION_base(x,y,z) 1
#endif
module Data.Serialize.IEEE754 (
getFloat32le
, getFloat32be
, getFloat64le
, getFloat64be
, putFloat32le
, putFloat32be
, putFloat64le
, putFloat64be
) where
import Data.Word ( Word32, Word64 )
import Data.Serialize.Get
import Data.Serialize.Put
import qualified Data.ByteString.Builder as Builder
import System.IO.Unsafe (unsafeDupablePerformIO)
import Foreign.Marshal.Alloc (alloca)
import Foreign.Storable (peek, poke)
import Foreign.Ptr (castPtr, Ptr)
#if !(MIN_VERSION_base(4,8,0))
import Control.Applicative ( (<$>) )
#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 = putBuilder . Builder.floatLE
putFloat32be :: Float -> Put
putFloat32be = putBuilder . Builder.floatBE
putFloat64le :: Double -> Put
putFloat64le = putBuilder . Builder.doubleLE
putFloat64be :: Double -> Put
putFloat64be = putBuilder . Builder.doubleBE
wordToFloat :: Word32 -> Float
wordToFloat w = unsafeDupablePerformIO $ alloca $ \(ptr :: Ptr Word32) -> do
poke ptr w
peek (castPtr ptr)
wordToDouble :: Word64 -> Double
wordToDouble w = unsafeDupablePerformIO $ alloca $ \(ptr :: Ptr Word64) -> do
poke ptr w
peek (castPtr ptr)