{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE TypeApplications #-}
-- |
-- Module       : Data.ByteString.Base16.Internal.W64.Loop
-- Copyright 	: (c) 2020 Emily Pillmore
-- License	: BSD-style
--
-- Maintainer	: Emily Pillmore <emilypi@cohomolo.gy>
-- Stability	: Experimental
-- Portability	: portable
--
-- Encoding loop optimized for 'Word64' architectures
--
module Data.ByteString.Base16.Internal.W64.Loop
( innerLoop
, decodeLoop
) where


import Data.Bits
import Data.ByteString.Internal
import Data.ByteString.Base16.Internal.Utils
import Data.Text (Text)
import qualified Data.Text as T

import Foreign.ForeignPtr
import Foreign.Ptr
import Foreign.Storable

import GHC.Word


-- | Hex encoding inner loop optimized for 64-bit architectures
--
innerLoop
    :: Ptr Word64
    -> Ptr Word32
    -> Ptr Word8
    -> IO ()
innerLoop :: Ptr Word64 -> Ptr Word32 -> Ptr Word8 -> IO ()
innerLoop !Ptr Word64
dptr !Ptr Word32
sptr !Ptr Word8
end = Ptr Word64 -> Ptr Word32 -> IO ()
go Ptr Word64
dptr Ptr Word32
sptr
  where
    lix :: a -> Word8
lix !a
a = Word8 -> Addr# -> Word8
aix (a -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral a
a Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.&. 0x0f) Addr#
alphabet
    {-# INLINE lix #-}

    !alphabet :: Addr#
alphabet = "0123456789abcdef"#

    tailRound16 :: Ptr Word16 -> Ptr Word8 -> IO ()
tailRound16 !Ptr Word16
dst !Ptr Word8
src
      | Ptr Word8
src Ptr Word8 -> Ptr Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Ptr Word8
end = () -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
      | Bool
otherwise = do
        !Word8
t <- Ptr Word8 -> IO Word8
forall a. Storable a => Ptr a -> IO a
peek @Word8 Ptr Word8
src

        let !a :: Word16
a = Word8 -> Word16
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8 -> Word8
forall a. Integral a => a -> Word8
lix (Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
unsafeShiftR Word8
t 4))
            !b :: Word16
b = Word8 -> Word16
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8 -> Word8
forall a. Integral a => a -> Word8
lix Word8
t)

        let !w :: Word16
w = Word16
a Word16 -> Word16 -> Word16
forall a. Bits a => a -> a -> a
.|. (Word16 -> Int -> Word16
forall a. Bits a => a -> Int -> a
unsafeShiftL Word16
b 8)

        Ptr Word16 -> Word16 -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke @Word16 Ptr Word16
dst Word16
w

        Ptr Word16 -> Ptr Word8 -> IO ()
tailRound16 (Ptr Word16 -> Int -> Ptr Word16
forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word16
dst 2) (Ptr Word8 -> Int -> Ptr Word8
forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
src 1)

    tailRound32 :: Ptr Word32 -> Ptr Word16 -> IO ()
tailRound32 !Ptr Word32
dst !Ptr Word16
src
      | Ptr Word16 -> Int -> Ptr Word8
forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word16
src 3 Ptr Word8 -> Ptr Word8 -> Bool
forall a. Ord a => a -> a -> Bool
>= Ptr Word8
end = Ptr Word16 -> Ptr Word8 -> IO ()
tailRound16 (Ptr Word32 -> Ptr Word16
forall a b. Ptr a -> Ptr b
castPtr Ptr Word32
dst) (Ptr Word16 -> Ptr Word8
forall a b. Ptr a -> Ptr b
castPtr Ptr Word16
src)
      | Bool
otherwise = do
#ifdef WORDS_BIGENDIAN
        !t <- peek src
#else
        !Word16
t <- Word16 -> Word16
byteSwap16 (Word16 -> Word16) -> IO Word16 -> IO Word16
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Ptr Word16 -> IO Word16
forall a. Storable a => Ptr a -> IO a
peek @Word16 Ptr Word16
src
#endif
        let !a :: Word16
a = Word16 -> Int -> Word16
forall a. Bits a => a -> Int -> a
unsafeShiftR Word16
t 12
            !b :: Word16
b = Word16 -> Int -> Word16
forall a. Bits a => a -> Int -> a
unsafeShiftR Word16
t 8
            !c :: Word16
c = Word16 -> Int -> Word16
forall a. Bits a => a -> Int -> a
unsafeShiftR Word16
t 4

        let !w :: Word32
w = Word8 -> Word32
w32 (Word16 -> Word8
forall a. Integral a => a -> Word8
lix Word16
a)
            !x :: Word32
x = Word8 -> Word32
w32 (Word16 -> Word8
forall a. Integral a => a -> Word8
lix Word16
b)
            !y :: Word32
y = Word8 -> Word32
w32 (Word16 -> Word8
forall a. Integral a => a -> Word8
lix Word16
c)
            !z :: Word32
z = Word8 -> Word32
w32 (Word16 -> Word8
forall a. Integral a => a -> Word8
lix Word16
t)

        let !xx :: Word32
xx = Word32
w
              Word32 -> Word32 -> Word32
forall a. Bits a => a -> a -> a
.|. (Word32 -> Int -> Word32
forall a. Bits a => a -> Int -> a
unsafeShiftL Word32
x 8)
              Word32 -> Word32 -> Word32
forall a. Bits a => a -> a -> a
.|. (Word32 -> Int -> Word32
forall a. Bits a => a -> Int -> a
unsafeShiftL Word32
y 16)
              Word32 -> Word32 -> Word32
forall a. Bits a => a -> a -> a
.|. (Word32 -> Int -> Word32
forall a. Bits a => a -> Int -> a
unsafeShiftL Word32
z 24)

        Ptr Word32 -> Word32 -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke @Word32 Ptr Word32
dst Word32
xx

        Ptr Word32 -> Ptr Word16 -> IO ()
tailRound32 (Ptr Word32 -> Int -> Ptr Word32
forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word32
dst 4) (Ptr Word16 -> Int -> Ptr Word16
forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word16
src 2)

    go :: Ptr Word64 -> Ptr Word32 -> IO ()
go !Ptr Word64
dst !Ptr Word32
src
      | Ptr Word32 -> Int -> Ptr Word8
forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word32
src 7 Ptr Word8 -> Ptr Word8 -> Bool
forall a. Ord a => a -> a -> Bool
>= Ptr Word8
end = Ptr Word32 -> Ptr Word16 -> IO ()
tailRound32 (Ptr Word64 -> Ptr Word32
forall a b. Ptr a -> Ptr b
castPtr Ptr Word64
dst) (Ptr Word32 -> Ptr Word16
forall a b. Ptr a -> Ptr b
castPtr Ptr Word32
src)
      | Bool
otherwise = do
#ifdef WORDS_BIGENDIAN
        !t <- peek src
#else
        !Word32
t <- Word32 -> Word32
byteSwap32 (Word32 -> Word32) -> IO Word32 -> IO Word32
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Ptr Word32 -> IO Word32
forall a. Storable a => Ptr a -> IO a
peek @Word32 Ptr Word32
src
#endif
        let !a :: Word32
a = Word32 -> Int -> Word32
forall a. Bits a => a -> Int -> a
unsafeShiftR Word32
t 28
            !b :: Word32
b = Word32 -> Int -> Word32
forall a. Bits a => a -> Int -> a
unsafeShiftR Word32
t 24
            !c :: Word32
c = Word32 -> Int -> Word32
forall a. Bits a => a -> Int -> a
unsafeShiftR Word32
t 20
            !d :: Word32
d = Word32 -> Int -> Word32
forall a. Bits a => a -> Int -> a
unsafeShiftR Word32
t 16
            !e :: Word32
e = Word32 -> Int -> Word32
forall a. Bits a => a -> Int -> a
unsafeShiftR Word32
t 12
            !f :: Word32
f = Word32 -> Int -> Word32
forall a. Bits a => a -> Int -> a
unsafeShiftR Word32
t 8
            !g :: Word32
g = Word32 -> Int -> Word32
forall a. Bits a => a -> Int -> a
unsafeShiftR Word32
t 4

        let !p :: Word64
p = Word8 -> Word64
w64 (Word32 -> Word8
forall a. Integral a => a -> Word8
lix Word32
a)
            !q :: Word64
q = Word8 -> Word64
w64 (Word32 -> Word8
forall a. Integral a => a -> Word8
lix Word32
b)
            !r :: Word64
r = Word8 -> Word64
w64 (Word32 -> Word8
forall a. Integral a => a -> Word8
lix Word32
c)
            !s :: Word64
s = Word8 -> Word64
w64 (Word32 -> Word8
forall a. Integral a => a -> Word8
lix Word32
d)
            !w :: Word64
w = Word8 -> Word64
w64 (Word32 -> Word8
forall a. Integral a => a -> Word8
lix Word32
e)
            !x :: Word64
x = Word8 -> Word64
w64 (Word32 -> Word8
forall a. Integral a => a -> Word8
lix Word32
f)
            !y :: Word64
y = Word8 -> Word64
w64 (Word32 -> Word8
forall a. Integral a => a -> Word8
lix Word32
g)
            !z :: Word64
z = Word8 -> Word64
w64 (Word32 -> Word8
forall a. Integral a => a -> Word8
lix Word32
t)

        let !xx :: Word64
xx = Word64
p
              Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.|. (Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
unsafeShiftL Word64
q 8)
              Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.|. (Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
unsafeShiftL Word64
r 16)
              Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.|. (Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
unsafeShiftL Word64
s 24)

            !yy :: Word64
yy = Word64
w
              Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.|. (Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
unsafeShiftL Word64
x 8)
              Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.|. (Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
unsafeShiftL Word64
y 16)
              Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.|. (Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
unsafeShiftL Word64
z 24)

        let !zz :: Word64
zz = Word64
xx Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.|. Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
unsafeShiftL Word64
yy 32

        Ptr Word64 -> Word64 -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke Ptr Word64
dst Word64
zz

        Ptr Word64 -> Ptr Word32 -> IO ()
go (Ptr Word64 -> Int -> Ptr Word64
forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word64
dst 8) (Ptr Word32 -> Int -> Ptr Word32
forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word32
src 4)
{-# INLINE innerLoop #-}


-- | Hex decoding loop optimized for 64-bit architectures
--
decodeLoop
  :: ForeignPtr Word8
  -> Ptr Word8
  -> Ptr Word8
  -> Ptr Word32
  -> Ptr Word64
  -> Ptr Word8
  -> IO (Either Text ByteString)
decodeLoop :: ForeignPtr Word8
-> Ptr Word8
-> Ptr Word8
-> Ptr Word32
-> Ptr Word64
-> Ptr Word8
-> IO (Either Text ByteString)
decodeLoop !ForeignPtr Word8
dfp !Ptr Word8
hi !Ptr Word8
lo !Ptr Word32
dptr !Ptr Word64
sptr !Ptr Word8
end = Ptr Word32 -> Ptr Word64 -> Int -> IO (Either Text ByteString)
go Ptr Word32
dptr Ptr Word64
sptr 0
  where
    err :: Ptr a -> m (Either Text b)
err !Ptr a
src = Either Text b -> m (Either Text b)
forall (m :: * -> *) a. Monad m => a -> m a
return (Either Text b -> m (Either Text b))
-> (String -> Either Text b) -> String -> m (Either Text b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Either Text b
forall a b. a -> Either a b
Left (Text -> Either Text b)
-> (String -> Text) -> String -> Either Text b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
T.pack
      (String -> m (Either Text b)) -> String -> m (Either Text b)
forall a b. (a -> b) -> a -> b
$ "invalid character at offset: "
      String -> String -> String
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show (Ptr a
src Ptr a -> Ptr Word64 -> Int
forall a b. Ptr a -> Ptr b -> Int
`minusPtr` Ptr Word64
sptr)

    tailRound16 :: Ptr Word8 -> Ptr Word8 -> Int -> IO (Either Text ByteString)
tailRound16 !Ptr Word8
dst !Ptr Word8
src !Int
n
      | Ptr Word8
src Ptr Word8 -> Ptr Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Ptr Word8
end = Either Text ByteString -> IO (Either Text ByteString)
forall (m :: * -> *) a. Monad m => a -> m a
return (ByteString -> Either Text ByteString
forall a b. b -> Either a b
Right (ForeignPtr Word8 -> Int -> Int -> ByteString
PS ForeignPtr Word8
dfp 0 Int
n))
      | Bool
otherwise = do
        !Word8
x <- Ptr Word8 -> IO Word8
forall a. Storable a => Ptr a -> IO a
peek @Word8 Ptr Word8
src
        !Word8
y <- Ptr Word8 -> IO Word8
forall a. Storable a => Ptr a -> IO a
peek @Word8 (Ptr Word8 -> Int -> Ptr Word8
forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
src 1)

        !Word8
a <- Ptr Word8 -> Int -> IO Word8
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff @Word8 Ptr Word8
hi (Word8 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
x)
        !Word8
b <- Ptr Word8 -> Int -> IO Word8
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff @Word8 Ptr Word8
lo (Word8 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
y)

        if
          | Word8
a Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 0xff -> Ptr Word8 -> IO (Either Text ByteString)
forall (m :: * -> *) a b. Monad m => Ptr a -> m (Either Text b)
err Ptr Word8
src
          | Word8
b Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 0xff -> Ptr Any -> IO (Either Text ByteString)
forall (m :: * -> *) a b. Monad m => Ptr a -> m (Either Text b)
err (Ptr Word8 -> Int -> Ptr Any
forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
src 1)
          | Bool
otherwise -> do
            Ptr Word8 -> Word8 -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke Ptr Word8
dst (Word8
a Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. Word8
b)
            Either Text ByteString -> IO (Either Text ByteString)
forall (m :: * -> *) a. Monad m => a -> m a
return (ByteString -> Either Text ByteString
forall a b. b -> Either a b
Right (ForeignPtr Word8 -> Int -> Int -> ByteString
PS ForeignPtr Word8
dfp 0 (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ 1)))

    tailRound32 :: Ptr Word16 -> Ptr Word32 -> Int -> IO (Either Text ByteString)
tailRound32 !Ptr Word16
dst !Ptr Word32
src !Int
n
      | Ptr Word32 -> Int -> Ptr Word8
forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word32
src 3 Ptr Word8 -> Ptr Word8 -> Bool
forall a. Ord a => a -> a -> Bool
>= Ptr Word8
end = Ptr Word8 -> Ptr Word8 -> Int -> IO (Either Text ByteString)
tailRound16 (Ptr Word16 -> Ptr Word8
forall a b. Ptr a -> Ptr b
castPtr Ptr Word16
dst) (Ptr Word32 -> Ptr Word8
forall a b. Ptr a -> Ptr b
castPtr Ptr Word32
src) Int
n
      | Bool
otherwise = do
#ifdef WORDS_BIGENDIAN
        !t <- peek @Word32 src
#else
        !Word32
t <- Word32 -> Word32
byteSwap32 (Word32 -> Word32) -> IO Word32 -> IO Word32
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Ptr Word32 -> IO Word32
forall a. Storable a => Ptr a -> IO a
peek @Word32 Ptr Word32
src
#endif
        let !w :: Int
w = Word32 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral ((Word32 -> Int -> Word32
forall a. Bits a => a -> Int -> a
unsafeShiftR Word32
t 24) Word32 -> Word32 -> Word32
forall a. Bits a => a -> a -> a
.&. 0xff)
            !x :: Int
x = Word32 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral ((Word32 -> Int -> Word32
forall a. Bits a => a -> Int -> a
unsafeShiftR Word32
t 16) Word32 -> Word32 -> Word32
forall a. Bits a => a -> a -> a
.&. 0xff)
            !y :: Int
y = Word32 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral ((Word32 -> Int -> Word32
forall a. Bits a => a -> Int -> a
unsafeShiftR Word32
t 8) Word32 -> Word32 -> Word32
forall a. Bits a => a -> a -> a
.&. 0xff)
            !z :: Int
z = (Word32 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word32
t Word32 -> Word32 -> Word32
forall a. Bits a => a -> a -> a
.&. 0xff))

        !Word8
a <- Ptr Word8 -> Int -> IO Word8
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff @Word8 Ptr Word8
hi Int
w
        !Word8
b <- Ptr Word8 -> Int -> IO Word8
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff @Word8 Ptr Word8
lo Int
x
        !Word8
c <- Ptr Word8 -> Int -> IO Word8
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff @Word8 Ptr Word8
hi Int
y
        !Word8
d <- Ptr Word8 -> Int -> IO Word8
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff @Word8 Ptr Word8
lo Int
z

        let !zz :: Word16
zz = Word8 -> Word16
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8
a Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. Word8
b)
               Word16 -> Word16 -> Word16
forall a. Bits a => a -> a -> a
.|. (Word16 -> Int -> Word16
forall a. Bits a => a -> Int -> a
unsafeShiftL (Word8 -> Word16
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8
c Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. Word8
d)) 8)

        if
          | Word8
a Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 0xff -> Ptr Word32 -> IO (Either Text ByteString)
forall (m :: * -> *) a b. Monad m => Ptr a -> m (Either Text b)
err Ptr Word32
src
          | Word8
b Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 0xff -> Ptr Any -> IO (Either Text ByteString)
forall (m :: * -> *) a b. Monad m => Ptr a -> m (Either Text b)
err (Ptr Word32 -> Int -> Ptr Any
forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word32
src 1)
          | Word8
c Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 0xff -> Ptr Any -> IO (Either Text ByteString)
forall (m :: * -> *) a b. Monad m => Ptr a -> m (Either Text b)
err (Ptr Word32 -> Int -> Ptr Any
forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word32
src 2)
          | Word8
d Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 0xff -> Ptr Any -> IO (Either Text ByteString)
forall (m :: * -> *) a b. Monad m => Ptr a -> m (Either Text b)
err (Ptr Word32 -> Int -> Ptr Any
forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word32
src 3)
          | Bool
otherwise -> do
            Ptr Word16 -> Word16 -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke @Word16 Ptr Word16
dst Word16
zz
            Ptr Word8 -> Ptr Word8 -> Int -> IO (Either Text ByteString)
tailRound16 (Ptr Word16 -> Int -> Ptr Word8
forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word16
dst 2) (Ptr Word32 -> Int -> Ptr Word8
forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word32
src 4) (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ 2)

    go :: Ptr Word32 -> Ptr Word64 -> Int -> IO (Either Text ByteString)
go !Ptr Word32
dst !Ptr Word64
src !Int
n
      | Ptr Word64 -> Int -> Ptr Word8
forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word64
src 7 Ptr Word8 -> Ptr Word8 -> Bool
forall a. Ord a => a -> a -> Bool
>= Ptr Word8
end = Ptr Word16 -> Ptr Word32 -> Int -> IO (Either Text ByteString)
tailRound32 (Ptr Word32 -> Ptr Word16
forall a b. Ptr a -> Ptr b
castPtr Ptr Word32
dst) (Ptr Word64 -> Ptr Word32
forall a b. Ptr a -> Ptr b
castPtr Ptr Word64
src) Int
n
      | Bool
otherwise = do
#ifdef WORDS_BIGENDIAN
        !tt <- peek @Word64 src
#else
        !Word64
tt <- Word64 -> Word64
byteSwap64 (Word64 -> Word64) -> IO Word64 -> IO Word64
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Ptr Word64 -> IO Word64
forall a. Storable a => Ptr a -> IO a
peek @Word64 Ptr Word64
src
#endif
        let !s :: Int
s = Word64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral ((Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
unsafeShiftR Word64
tt 56) Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.&. 0xff)
            !t :: Int
t = Word64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral ((Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
unsafeShiftR Word64
tt 48) Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.&. 0xff)
            !u :: Int
u = Word64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral ((Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
unsafeShiftR Word64
tt 40) Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.&. 0xff)
            !v :: Int
v = Word64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral ((Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
unsafeShiftR Word64
tt 32) Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.&. 0xff)
            !w :: Int
w = Word64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral ((Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
unsafeShiftR Word64
tt 24) Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.&. 0xff)
            !x :: Int
x = Word64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral ((Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
unsafeShiftR Word64
tt 16) Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.&. 0xff)
            !y :: Int
y = Word64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral ((Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
unsafeShiftR Word64
tt 8) Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.&. 0xff)
            !z :: Int
z = Word64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64
tt Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.&. 0xff)

        !Word8
a <- Ptr Word8 -> Int -> IO Word8
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff @Word8 Ptr Word8
hi Int
s
        !Word8
b <- Ptr Word8 -> Int -> IO Word8
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff @Word8 Ptr Word8
lo Int
t
        !Word8
c <- Ptr Word8 -> Int -> IO Word8
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff @Word8 Ptr Word8
hi Int
u
        !Word8
d <- Ptr Word8 -> Int -> IO Word8
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff @Word8 Ptr Word8
lo Int
v
        !Word8
e <- Ptr Word8 -> Int -> IO Word8
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff @Word8 Ptr Word8
hi Int
w
        !Word8
f <- Ptr Word8 -> Int -> IO Word8
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff @Word8 Ptr Word8
lo Int
x
        !Word8
g <- Ptr Word8 -> Int -> IO Word8
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff @Word8 Ptr Word8
hi Int
y
        !Word8
h <- Ptr Word8 -> Int -> IO Word8
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff @Word8 Ptr Word8
lo Int
z

        let !zz :: Word32
zz = Word8 -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8
a Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. Word8
b)
               Word32 -> Word32 -> Word32
forall a. Bits a => a -> a -> a
.|. (Word32 -> Int -> Word32
forall a. Bits a => a -> Int -> a
unsafeShiftL (Word8 -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8
c Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. Word8
d)) 8)
               Word32 -> Word32 -> Word32
forall a. Bits a => a -> a -> a
.|. (Word32 -> Int -> Word32
forall a. Bits a => a -> Int -> a
unsafeShiftL (Word8 -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8
e Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. Word8
f)) 16)
               Word32 -> Word32 -> Word32
forall a. Bits a => a -> a -> a
.|. (Word32 -> Int -> Word32
forall a. Bits a => a -> Int -> a
unsafeShiftL (Word8 -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8
g Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. Word8
h)) 24)

        if
          | Word8
a Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 0xff -> Ptr Word64 -> IO (Either Text ByteString)
forall (m :: * -> *) a b. Monad m => Ptr a -> m (Either Text b)
err Ptr Word64
src
          | Word8
b Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 0xff -> Ptr Any -> IO (Either Text ByteString)
forall (m :: * -> *) a b. Monad m => Ptr a -> m (Either Text b)
err (Ptr Word64 -> Int -> Ptr Any
forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word64
src 1)
          | Word8
c Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 0xff -> Ptr Any -> IO (Either Text ByteString)
forall (m :: * -> *) a b. Monad m => Ptr a -> m (Either Text b)
err (Ptr Word64 -> Int -> Ptr Any
forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word64
src 2)
          | Word8
d Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 0xff -> Ptr Any -> IO (Either Text ByteString)
forall (m :: * -> *) a b. Monad m => Ptr a -> m (Either Text b)
err (Ptr Word64 -> Int -> Ptr Any
forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word64
src 3)
          | Word8
e Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 0xff -> Ptr Any -> IO (Either Text ByteString)
forall (m :: * -> *) a b. Monad m => Ptr a -> m (Either Text b)
err (Ptr Word64 -> Int -> Ptr Any
forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word64
src 4)
          | Word8
f Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 0xff -> Ptr Any -> IO (Either Text ByteString)
forall (m :: * -> *) a b. Monad m => Ptr a -> m (Either Text b)
err (Ptr Word64 -> Int -> Ptr Any
forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word64
src 5)
          | Word8
g Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 0xff -> Ptr Any -> IO (Either Text ByteString)
forall (m :: * -> *) a b. Monad m => Ptr a -> m (Either Text b)
err (Ptr Word64 -> Int -> Ptr Any
forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word64
src 6)
          | Word8
h Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 0xff -> Ptr Any -> IO (Either Text ByteString)
forall (m :: * -> *) a b. Monad m => Ptr a -> m (Either Text b)
err (Ptr Word64 -> Int -> Ptr Any
forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word64
src 7)
          | Bool
otherwise -> do
            Ptr Word32 -> Word32 -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke @Word32 Ptr Word32
dst Word32
zz
            Ptr Word32 -> Ptr Word64 -> Int -> IO (Either Text ByteString)
go (Ptr Word32 -> Int -> Ptr Word32
forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word32
dst 4) (Ptr Word64 -> Int -> Ptr Word64
forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word64
src 8) (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ 4)
{-# INLINE decodeLoop #-}