{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE Trustworthy #-}
module Data.ByteString.Builder.Prim.Internal.Floating
(
encodeFloatViaWord32F
, encodeDoubleViaWord64F
) where
import Foreign
import Data.ByteString.Builder.Prim.Internal
{-# INLINE encodeFloatViaWord32F #-}
encodeFloatViaWord32F :: FixedPrim Word32 -> FixedPrim Float
encodeFloatViaWord32F :: FixedPrim Word32 -> FixedPrim Float
encodeFloatViaWord32F FixedPrim Word32
w32fe
| forall a. FixedPrim a -> Int
size FixedPrim Word32
w32fe forall a. Ord a => a -> a -> Bool
< forall a. Storable a => a -> Int
sizeOf (forall a. HasCallStack => a
undefined :: Float) =
forall a. HasCallStack => [Char] -> a
error [Char]
"encodeFloatViaWord32F: encoding not wide enough"
| Bool
otherwise = forall a. Int -> (a -> Ptr Word8 -> IO ()) -> FixedPrim a
fixedPrim (forall a. FixedPrim a -> Int
size FixedPrim Word32
w32fe) forall a b. (a -> b) -> a -> b
$ \Float
x Ptr Word8
op -> do
forall a. Storable a => Ptr a -> a -> IO ()
poke (forall a b. Ptr a -> Ptr b
castPtr Ptr Word8
op) Float
x
Word32
x' <- forall a. Storable a => Ptr a -> IO a
peek (forall a b. Ptr a -> Ptr b
castPtr Ptr Word8
op)
forall a. FixedPrim a -> a -> Ptr Word8 -> IO ()
runF FixedPrim Word32
w32fe Word32
x' Ptr Word8
op
{-# INLINE encodeDoubleViaWord64F #-}
encodeDoubleViaWord64F :: FixedPrim Word64 -> FixedPrim Double
encodeDoubleViaWord64F :: FixedPrim Word64 -> FixedPrim Double
encodeDoubleViaWord64F FixedPrim Word64
w64fe
| forall a. FixedPrim a -> Int
size FixedPrim Word64
w64fe forall a. Ord a => a -> a -> Bool
< forall a. Storable a => a -> Int
sizeOf (forall a. HasCallStack => a
undefined :: Float) =
forall a. HasCallStack => [Char] -> a
error [Char]
"encodeDoubleViaWord64F: encoding not wide enough"
| Bool
otherwise = forall a. Int -> (a -> Ptr Word8 -> IO ()) -> FixedPrim a
fixedPrim (forall a. FixedPrim a -> Int
size FixedPrim Word64
w64fe) forall a b. (a -> b) -> a -> b
$ \Double
x Ptr Word8
op -> do
forall a. Storable a => Ptr a -> a -> IO ()
poke (forall a b. Ptr a -> Ptr b
castPtr Ptr Word8
op) Double
x
Word64
x' <- forall a. Storable a => Ptr a -> IO a
peek (forall a b. Ptr a -> Ptr b
castPtr Ptr Word8
op)
forall a. FixedPrim a -> a -> Ptr Word8 -> IO ()
runF FixedPrim Word64
w64fe Word64
x' Ptr Word8
op