{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE NoMonomorphismRestriction #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE Trustworthy #-}
module Data.FloatCast
( floatToWord
, wordToFloat
, doubleToWord
, wordToDouble
, runST
, cast
)
where
import Data.Array.ST (MArray, STUArray, newArray, readArray)
import Data.Array.Unsafe (castSTUArray)
import Data.Word (Word32, Word64)
import GHC.ST (ST, runST)
wordToFloat :: Word32 -> Float
wordToFloat :: Word32 -> Float
wordToFloat Word32
x = forall a. (forall s. ST s a) -> a
runST (forall s a b.
(MArray (STUArray s) a (ST s), MArray (STUArray s) b (ST s)) =>
a -> ST s b
cast Word32
x)
{-# INLINE wordToFloat #-}
floatToWord :: Float -> Word32
floatToWord :: Float -> Word32
floatToWord Float
x = forall a. (forall s. ST s a) -> a
runST (forall s a b.
(MArray (STUArray s) a (ST s), MArray (STUArray s) b (ST s)) =>
a -> ST s b
cast Float
x)
{-# INLINE floatToWord #-}
{-# INLINE doubleToWord #-}
doubleToWord :: Double -> Word64
doubleToWord :: Double -> Word64
doubleToWord Double
x = forall a. (forall s. ST s a) -> a
runST (forall s a b.
(MArray (STUArray s) a (ST s), MArray (STUArray s) b (ST s)) =>
a -> ST s b
cast Double
x)
{-# INLINE wordToDouble #-}
wordToDouble :: Word64 -> Double
wordToDouble :: Word64 -> Double
wordToDouble Word64
x = forall a. (forall s. ST s a) -> a
runST (forall s a b.
(MArray (STUArray s) a (ST s), MArray (STUArray s) b (ST s)) =>
a -> ST s b
cast Word64
x)
cast
:: (MArray (STUArray s) a (ST s), MArray (STUArray s) b (ST s)) => a -> ST s b
cast :: forall s a b.
(MArray (STUArray s) a (ST s), MArray (STUArray s) b (ST s)) =>
a -> ST s b
cast a
x = forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
(i, i) -> e -> m (a i e)
newArray (Int
0 :: Int, Int
0) a
x forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall s ix a b. STUArray s ix a -> ST s (STUArray s ix b)
castSTUArray forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall a b c. (a -> b -> c) -> b -> a -> c
flip forall (a :: * -> * -> *) e (m :: * -> *) i.
(MArray a e m, Ix i) =>
a i e -> i -> m e
readArray Int
0
{-# INLINE cast #-}