module Foundation.Primitive.Utils
( primCopyFreezedBytes
, primCopyFreezedBytesOffset
, primCopyFreezedW32
, primCopyFreezedW64
, primMutableAddrSlideToStart
, primMutableByteArraySlideToStart
) where
import Foundation.Internal.Base
import Foundation.Primitive.Types.OffsetSize
import Foundation.Internal.Primitive
import Foundation.Primitive.Monad
import GHC.Prim
import GHC.Types
primCopyFreezedBytes :: PrimMonad m => MutableByteArray# (PrimState m) -> ByteArray# -> m ()
primCopyFreezedBytes mba ba = primitive $ \st ->
(# copyByteArray# ba 0# mba 0# (sizeofByteArray# ba) st , () #)
primCopyFreezedBytesOffset :: PrimMonad m => MutableByteArray# (PrimState m) -> Int# -> ByteArray# -> Int# -> m ()
primCopyFreezedBytesOffset mba ofs ba nbBytes = primitive $ \st ->
(# copyByteArray# ba 0# mba ofs nbBytes st , () #)
primCopyFreezedW32 :: PrimMonad m => MutableByteArray# (PrimState m) -> ByteArray# -> m ()
primCopyFreezedW32 mba ba = primitive $ \st -> (# loop st 0#, () #)
where
!len = quotInt# (sizeofByteArray# ba) 8#
loop !st !n
| bool# (n ==# len) = st
| otherwise = loop (writeWord32Array# mba n (indexWord32Array# ba n) st) (n +# 1#)
primCopyFreezedW64 :: PrimMonad m => MutableByteArray# (PrimState m) -> ByteArray# -> m ()
primCopyFreezedW64 mba ba = primitive $ \st -> (# loop st 0#, () #)
where
!len = quotInt# (sizeofByteArray# ba) 8#
loop !st !n
| bool# (n ==# len) = st
| otherwise = loop (writeWord64Array# mba n (indexWord64Array# ba n) st) (n +# 1#)
primMutableByteArraySlideToStart :: PrimMonad m => MutableByteArray# (PrimState m) -> Offset8 -> Offset8 -> m ()
primMutableByteArraySlideToStart mba (Offset (I# ofs)) (Offset (I# end)) = primitive $ \st ->
(# copyMutableByteArray# mba 0# mba ofs (end -# ofs) st, () #)
primMutableAddrSlideToStart :: PrimMonad m => Addr# -> Offset8 -> Offset8 -> m ()
primMutableAddrSlideToStart addr (Offset (I# ofsIni)) (Offset (I# end)) = primitive $ \st -> (# loop st 0# ofsIni, () #)
where
loop !st !dst !ofs
| bool# (ofs ==# end) = st
| otherwise =
case readWord8OffAddr# addr ofs st of { (# st', v #) ->
case writeWord8OffAddr# addr dst v st' of { st'' ->
loop st'' (dst +# 1#) (ofs +# 1#) }}