module Data.ByteArray.Mapping
( toW64BE
, toW64LE
, fromW64BE
, mapAsWord64
, mapAsWord128
) where
import Data.ByteArray.Types
import Data.ByteArray.Methods
import Data.Memory.Internal.Compat
import Data.Memory.Internal.Imports hiding (empty)
import Data.Memory.Endian
import Data.Memory.ExtendedWords
import Foreign.Storable
import Foreign.Ptr
import Prelude hiding (length, take, drop, span, concat, replicate, splitAt, null, pred)
toW64BE :: ByteArrayAccess bs => bs -> Int -> BE Word64
toW64BE :: forall bs. ByteArrayAccess bs => bs -> Int -> BE Word64
toW64BE bs
bs Int
ofs = forall a. IO a -> a
unsafeDoIO forall a b. (a -> b) -> a -> b
$ forall ba p a. ByteArrayAccess ba => ba -> (Ptr p -> IO a) -> IO a
withByteArray bs
bs forall a b. (a -> b) -> a -> b
$ \Ptr Any
p -> forall a. Storable a => Ptr a -> IO a
peek (Ptr Any
p forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
ofs)
toW64LE :: ByteArrayAccess bs => bs -> Int -> LE Word64
toW64LE :: forall bs. ByteArrayAccess bs => bs -> Int -> LE Word64
toW64LE bs
bs Int
ofs = forall a. IO a -> a
unsafeDoIO forall a b. (a -> b) -> a -> b
$ forall ba p a. ByteArrayAccess ba => ba -> (Ptr p -> IO a) -> IO a
withByteArray bs
bs forall a b. (a -> b) -> a -> b
$ \Ptr Any
p -> forall a. Storable a => Ptr a -> IO a
peek (Ptr Any
p forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
ofs)
fromW64BE :: (ByteArray ba) => Word64 -> ba
fromW64BE :: forall ba. ByteArray ba => Word64 -> ba
fromW64BE Word64
n = forall a p. ByteArray a => Int -> (Ptr p -> IO ()) -> a
allocAndFreeze Int
8 forall a b. (a -> b) -> a -> b
$ \Ptr (BE Word64)
p -> forall a. Storable a => Ptr a -> a -> IO ()
poke Ptr (BE Word64)
p (forall a. ByteSwap a => a -> BE a
toBE Word64
n)
mapAsWord128 :: ByteArray bs => (Word128 -> Word128) -> bs -> bs
mapAsWord128 :: forall bs. ByteArray bs => (Word128 -> Word128) -> bs -> bs
mapAsWord128 Word128 -> Word128
f bs
bs =
forall a p. ByteArray a => Int -> (Ptr p -> IO ()) -> a
unsafeCreate Int
len forall a b. (a -> b) -> a -> b
$ \Ptr (BE Word64)
dst ->
forall ba p a. ByteArrayAccess ba => ba -> (Ptr p -> IO a) -> IO a
withByteArray bs
bs forall a b. (a -> b) -> a -> b
$ \Ptr (BE Word64)
src ->
Int -> Ptr (BE Word64) -> Ptr (BE Word64) -> IO ()
loop (Int
len forall a. Integral a => a -> a -> a
`div` Int
16) Ptr (BE Word64)
dst Ptr (BE Word64)
src
where
len :: Int
len = forall ba. ByteArrayAccess ba => ba -> Int
length bs
bs
loop :: Int -> Ptr (BE Word64) -> Ptr (BE Word64) -> IO ()
loop :: Int -> Ptr (BE Word64) -> Ptr (BE Word64) -> IO ()
loop Int
0 Ptr (BE Word64)
_ Ptr (BE Word64)
_ = forall (m :: * -> *) a. Monad m => a -> m a
return ()
loop Int
i Ptr (BE Word64)
d Ptr (BE Word64)
s = do
BE Word64
w1 <- forall a. Storable a => Ptr a -> IO a
peek Ptr (BE Word64)
s
BE Word64
w2 <- forall a. Storable a => Ptr a -> IO a
peek (Ptr (BE Word64)
s forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
8)
let (Word128 Word64
r1 Word64
r2) = Word128 -> Word128
f (Word64 -> Word64 -> Word128
Word128 (forall a. ByteSwap a => BE a -> a
fromBE BE Word64
w1) (forall a. ByteSwap a => BE a -> a
fromBE BE Word64
w2))
forall a. Storable a => Ptr a -> a -> IO ()
poke Ptr (BE Word64)
d (forall a. ByteSwap a => a -> BE a
toBE Word64
r1)
forall a. Storable a => Ptr a -> a -> IO ()
poke (Ptr (BE Word64)
d forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
8) (forall a. ByteSwap a => a -> BE a
toBE Word64
r2)
Int -> Ptr (BE Word64) -> Ptr (BE Word64) -> IO ()
loop (Int
iforall a. Num a => a -> a -> a
-Int
1) (Ptr (BE Word64)
d forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
16) (Ptr (BE Word64)
s forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
16)
mapAsWord64 :: ByteArray bs => (Word64 -> Word64) -> bs -> bs
mapAsWord64 :: forall bs. ByteArray bs => (Word64 -> Word64) -> bs -> bs
mapAsWord64 Word64 -> Word64
f bs
bs =
forall a p. ByteArray a => Int -> (Ptr p -> IO ()) -> a
unsafeCreate Int
len forall a b. (a -> b) -> a -> b
$ \Ptr (BE Word64)
dst ->
forall ba p a. ByteArrayAccess ba => ba -> (Ptr p -> IO a) -> IO a
withByteArray bs
bs forall a b. (a -> b) -> a -> b
$ \Ptr (BE Word64)
src ->
Int -> Ptr (BE Word64) -> Ptr (BE Word64) -> IO ()
loop (Int
len forall a. Integral a => a -> a -> a
`div` Int
8) Ptr (BE Word64)
dst Ptr (BE Word64)
src
where
len :: Int
len = forall ba. ByteArrayAccess ba => ba -> Int
length bs
bs
loop :: Int -> Ptr (BE Word64) -> Ptr (BE Word64) -> IO ()
loop :: Int -> Ptr (BE Word64) -> Ptr (BE Word64) -> IO ()
loop Int
0 Ptr (BE Word64)
_ Ptr (BE Word64)
_ = forall (m :: * -> *) a. Monad m => a -> m a
return ()
loop Int
i Ptr (BE Word64)
d Ptr (BE Word64)
s = do
BE Word64
w <- forall a. Storable a => Ptr a -> IO a
peek Ptr (BE Word64)
s
let r :: Word64
r = Word64 -> Word64
f (forall a. ByteSwap a => BE a -> a
fromBE BE Word64
w)
forall a. Storable a => Ptr a -> a -> IO ()
poke Ptr (BE Word64)
d (forall a. ByteSwap a => a -> BE a
toBE Word64
r)
Int -> Ptr (BE Word64) -> Ptr (BE Word64) -> IO ()
loop (Int
iforall a. Num a => a -> a -> a
-Int
1) (Ptr (BE Word64)
d forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
8) (Ptr (BE Word64)
s forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
8)