module Numeric.Commons
( ElemRep, ElemPrim
, PrimBytes (..), FloatBytes, DoubleBytes, IntBytes, WordBytes
) where
#include "MachDeps.h"
#include "HsBaseConfig.h"
import GHC.Base (runRW#)
import GHC.Int (Int16 (..), Int32 (..), Int64 (..), Int8 (..))
import GHC.Prim
import GHC.Types (Double (..), Float (..), Int (..), RuntimeRep (..),
Type, Word (..))
import GHC.Word (Word16 (..), Word32 (..), Word64 (..), Word8 (..))
type family ElemRep a :: RuntimeRep
type instance ElemRep Float = 'FloatRep
type instance ElemRep Double = 'DoubleRep
type instance ElemRep Int = 'IntRep
type instance ElemRep Int8 = 'IntRep
type instance ElemRep Int16 = 'IntRep
type instance ElemRep Int32 = 'IntRep
#if SIZEOF_HSWORD < 8
type instance ElemRep Int64 = 'Int64Rep
#else
type instance ElemRep Int64 = 'IntRep
#endif
type instance ElemRep Word = 'WordRep
type instance ElemRep Word8 = 'WordRep
type instance ElemRep Word16 = 'WordRep
type instance ElemRep Word32 = 'WordRep
#if SIZEOF_HSWORD < 8
type instance ElemRep Word64 = 'Word64Rep
#else
type instance ElemRep Word64 = 'WordRep
#endif
type family ElemPrim a :: TYPE (r :: RuntimeRep)
type instance ElemPrim Float = Float#
type instance ElemPrim Double = Double#
type instance ElemPrim Int = Int#
type instance ElemPrim Int8 = Int#
type instance ElemPrim Int16 = Int#
type instance ElemPrim Int32 = Int#
#if SIZEOF_HSWORD < 8
type instance ElemPrim Int64 = Int64#
#else
type instance ElemPrim Int64 = Int#
#endif
type instance ElemPrim Word = Word#
type instance ElemPrim Word8 = Word#
type instance ElemPrim Word16 = Word#
type instance ElemPrim Word32 = Word#
#if SIZEOF_HSWORD < 8
type instance ElemPrim Word64 = Word64#
#else
type instance ElemPrim Word64 = Word#
#endif
type FloatBytes a = (PrimBytes a, ElemRep a ~ 'FloatRep , ElemPrim a ~ Float#)
type DoubleBytes a = (PrimBytes a, ElemRep a ~ 'DoubleRep, ElemPrim a ~ Double#)
type IntBytes a = (PrimBytes a, ElemRep a ~ 'IntRep , ElemPrim a ~ Int#)
type WordBytes a = (PrimBytes a, ElemRep a ~ 'WordRep , ElemPrim a ~ Word#)
class PrimBytes (a :: Type) where
toBytes :: a -> (# Int# , Int# , ByteArray# #)
fromBytes :: (# Int# , Int# , ByteArray# #) -> a
byteSize :: a -> Int#
byteAlign :: a -> Int#
elementByteSize :: a -> Int#
ix :: Int# -> a -> (ElemPrim a :: TYPE (ElemRep a))
instance PrimBytes Float where
toBytes v@(F# x) = case runRW#
( \s0 -> case newByteArray# (byteSize v) s0 of
(# s1, marr #) -> case writeFloatArray# marr 0# x s1 of
s2 -> unsafeFreezeByteArray# marr s2
) of (# _, a #) -> (# 0#, 1#, a #)
fromBytes (# off, _, arr #) = F# (indexFloatArray# arr off)
byteSize _ = SIZEOF_HSFLOAT#
byteAlign _ = ALIGNMENT_HSFLOAT#
elementByteSize = byteSize
ix _ (F# x) = x
instance PrimBytes Double where
toBytes v@(D# x) = case runRW#
( \s0 -> case newByteArray# (byteSize v) s0 of
(# s1, marr #) -> case writeDoubleArray# marr 0# x s1 of
s2 -> unsafeFreezeByteArray# marr s2
) of (# _, a #) -> (# 0#, 1#, a #)
fromBytes (# off, _, arr #) = D# (indexDoubleArray# arr off)
byteSize _ = SIZEOF_HSDOUBLE#
byteAlign _ = ALIGNMENT_HSDOUBLE#
elementByteSize = byteSize
ix _ (D# x) = x
instance PrimBytes Int where
toBytes v@(I# x) = case runRW#
( \s0 -> case newByteArray# (byteSize v) s0 of
(# s1, marr #) -> case writeIntArray# marr 0# x s1 of
s2 -> unsafeFreezeByteArray# marr s2
) of (# _, a #) -> (# 0#, 1#, a #)
fromBytes (# off, _, arr #) = I# (indexIntArray# arr off)
byteSize _ = SIZEOF_HSINT#
byteAlign _ = ALIGNMENT_HSINT#
elementByteSize = byteSize
ix _ (I# x) = x
instance PrimBytes Int8 where
toBytes v@(I8# x) = case runRW#
( \s0 -> case newByteArray# (byteSize v) s0 of
(# s1, marr #) -> case writeInt8Array# marr 0# x s1 of
s2 -> unsafeFreezeByteArray# marr s2
) of (# _, a #) -> (# 0#, 1#, a #)
fromBytes (# off, _, arr #) = I8# (indexInt8Array# arr off)
byteSize _ = SIZEOF_INT8#
byteAlign _ = ALIGNMENT_INT8#
elementByteSize = byteSize
ix _ (I8# x) = x
instance PrimBytes Int16 where
toBytes v@(I16# x) = case runRW#
( \s0 -> case newByteArray# (byteSize v) s0 of
(# s1, marr #) -> case writeInt16Array# marr 0# x s1 of
s2 -> unsafeFreezeByteArray# marr s2
) of (# _, a #) -> (# 0#, 1#, a #)
fromBytes (# off, _, arr #) = I16# (indexInt16Array# arr off)
byteSize _ = SIZEOF_INT16#
byteAlign _ = ALIGNMENT_INT16#
elementByteSize = byteSize
ix _ (I16# x) = x
instance PrimBytes Int32 where
toBytes v@(I32# x) = case runRW#
( \s0 -> case newByteArray# (byteSize v) s0 of
(# s1, marr #) -> case writeInt32Array# marr 0# x s1 of
s2 -> unsafeFreezeByteArray# marr s2
) of (# _, a #) -> (# 0#, 1#, a #)
fromBytes (# off, _, arr #) = I32# (indexInt32Array# arr off)
byteSize _ = SIZEOF_INT32#
byteAlign _ = ALIGNMENT_INT32#
elementByteSize = byteSize
ix _ (I32# x) = x
#ifndef ghcjs_HOST_OS
instance PrimBytes Int64 where
toBytes v@(I64# x) = case runRW#
( \s0 -> case newByteArray# (byteSize v) s0 of
(# s1, marr #) -> case writeInt64Array# marr 0# x s1 of
s2 -> unsafeFreezeByteArray# marr s2
) of (# _, a #) -> (# 0#, 1#, a #)
fromBytes (# off, _, arr #) = I64# (indexInt64Array# arr off)
byteSize _ = SIZEOF_INT64#
byteAlign _ = ALIGNMENT_INT64#
elementByteSize = byteSize
ix _ (I64# x) = x
#endif
instance PrimBytes Word where
toBytes v@(W# x) = case runRW#
( \s0 -> case newByteArray# (byteSize v) s0 of
(# s1, marr #) -> case writeWordArray# marr 0# x s1 of
s2 -> unsafeFreezeByteArray# marr s2
) of (# _, a #) -> (# 0#, 1#, a #)
fromBytes (# off, _, arr #) = W# (indexWordArray# arr off)
byteSize _ = SIZEOF_HSWORD#
byteAlign _ = ALIGNMENT_HSWORD#
elementByteSize = byteSize
ix _ (W# x) = x
instance PrimBytes Word8 where
toBytes v@(W8# x) = case runRW#
( \s0 -> case newByteArray# (byteSize v) s0 of
(# s1, marr #) -> case writeWord8Array# marr 0# x s1 of
s2 -> unsafeFreezeByteArray# marr s2
) of (# _, a #) -> (# 0#, 1#, a #)
fromBytes (# off, _, arr #) = W8# (indexWord8Array# arr off)
byteSize _ = SIZEOF_WORD8#
byteAlign _ = ALIGNMENT_WORD8#
elementByteSize = byteSize
ix _ (W8# x) = x
instance PrimBytes Word16 where
toBytes v@(W16# x) = case runRW#
( \s0 -> case newByteArray# (byteSize v) s0 of
(# s1, marr #) -> case writeWord16Array# marr 0# x s1 of
s2 -> unsafeFreezeByteArray# marr s2
) of (# _, a #) -> (# 0#, 1#, a #)
fromBytes (# off, _, arr #) = W16# (indexWord16Array# arr off)
byteSize _ = SIZEOF_WORD16#
byteAlign _ = ALIGNMENT_WORD16#
elementByteSize = byteSize
ix _ (W16# x) = x
instance PrimBytes Word32 where
toBytes v@(W32# x) = case runRW#
( \s0 -> case newByteArray# (byteSize v) s0 of
(# s1, marr #) -> case writeWord32Array# marr 0# x s1 of
s2 -> unsafeFreezeByteArray# marr s2
) of (# _, a #) -> (# 0#, 1#, a #)
fromBytes (# off, _, arr #) = W32# (indexWord32Array# arr off)
byteSize _ = SIZEOF_WORD32#
byteAlign _ = ALIGNMENT_WORD32#
elementByteSize = byteSize
ix _ (W32# x) = x
#ifndef ghcjs_HOST_OS
instance PrimBytes Word64 where
toBytes v@(W64# x) = case runRW#
( \s0 -> case newByteArray# (byteSize v) s0 of
(# s1, marr #) -> case writeWord64Array# marr 0# x s1 of
s2 -> unsafeFreezeByteArray# marr s2
) of (# _, a #) -> (# 0#, 1#, a #)
fromBytes (# off, _, arr #) = W64# (indexWord64Array# arr off)
byteSize _ = SIZEOF_WORD64#
byteAlign _ = ALIGNMENT_WORD64#
elementByteSize = byteSize
ix _ (W64# x) = x
#endif