{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE StrictData #-}
{-# LANGUAGE UnboxedTuples #-}
{-# OPTIONS_GHC -funbox-strict-fields #-}

-----------------------------------------------------------------------------
---- |
---- Module      :  Data.WideWord.Word128
----
---- Maintainer  :  erikd@mega-nerd.com
---- Stability   :  experimental
---- Portability :  non-portable (GHC extensions and primops)
----
---- This module provides an opaque unsigned 128 bit value with the usual set
---- of typeclass instances one would expect for a fixed width unsigned integer
---- type.
---- Operations like addition, subtraction and multiplication etc provide a
---- "modulo 2^128" result as one would expect from a fixed width unsigned word.
-------------------------------------------------------------------------------

#include <MachDeps.h>

module Data.WideWord.Word128
  ( Word128 (..)
  , byteSwapWord128
  , showHexWord128
  , zeroWord128
  ) where

import Control.DeepSeq (NFData (..))

import Data.Bits (Bits (..), FiniteBits (..), shiftL)
import Data.Data (Data, Typeable)
import Data.Ix (Ix)
#if ! MIN_VERSION_base(4,11,0)
import Data.Semigroup ((<>))
#endif
import Data.WideWord.Word64

import Foreign.Ptr (Ptr, castPtr)
import Foreign.Storable (Storable (..))

import GHC.Base (Int (..))
import GHC.Enum (predError, succError)
import GHC.Exts ((*#), (+#), Int#, State#, ByteArray#, MutableByteArray#, Addr#)
import GHC.Generics (Generic)
import GHC.Real ((%), divZeroError)
import GHC.Word (Word32, Word64, byteSwap64)

import Numeric (showHex)

import Data.Primitive.Types (Prim (..), defaultSetByteArray#, defaultSetOffAddr#)

import Data.Hashable (Hashable, hashWithSalt)
import Data.Binary (Binary (get, put))

data Word128 = Word128
  { Word128 -> Word64
word128Hi64 :: !Word64
  , Word128 -> Word64
word128Lo64 :: !Word64
  }
  deriving (Word128 -> Word128 -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Word128 -> Word128 -> Bool
$c/= :: Word128 -> Word128 -> Bool
== :: Word128 -> Word128 -> Bool
$c== :: Word128 -> Word128 -> Bool
Eq, Typeable Word128
Word128 -> DataType
Word128 -> Constr
(forall b. Data b => b -> b) -> Word128 -> Word128
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> Word128 -> u
forall u. (forall d. Data d => d -> u) -> Word128 -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Word128 -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Word128 -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Word128 -> m Word128
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Word128 -> m Word128
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Word128
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Word128 -> c Word128
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Word128)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Word128)
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Word128 -> m Word128
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Word128 -> m Word128
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Word128 -> m Word128
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Word128 -> m Word128
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Word128 -> m Word128
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Word128 -> m Word128
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> Word128 -> u
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> Word128 -> u
gmapQ :: forall u. (forall d. Data d => d -> u) -> Word128 -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> Word128 -> [u]
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Word128 -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Word128 -> r
gmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Word128 -> r
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Word128 -> r
gmapT :: (forall b. Data b => b -> b) -> Word128 -> Word128
$cgmapT :: (forall b. Data b => b -> b) -> Word128 -> Word128
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Word128)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Word128)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Word128)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Word128)
dataTypeOf :: Word128 -> DataType
$cdataTypeOf :: Word128 -> DataType
toConstr :: Word128 -> Constr
$ctoConstr :: Word128 -> Constr
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Word128
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Word128
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Word128 -> c Word128
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Word128 -> c Word128
Data, forall x. Rep Word128 x -> Word128
forall x. Word128 -> Rep Word128 x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Word128 x -> Word128
$cfrom :: forall x. Word128 -> Rep Word128 x
Generic, Ord Word128
(Word128, Word128) -> Int
(Word128, Word128) -> [Word128]
(Word128, Word128) -> Word128 -> Bool
(Word128, Word128) -> Word128 -> Int
forall a.
Ord a
-> ((a, a) -> [a])
-> ((a, a) -> a -> Int)
-> ((a, a) -> a -> Int)
-> ((a, a) -> a -> Bool)
-> ((a, a) -> Int)
-> ((a, a) -> Int)
-> Ix a
unsafeRangeSize :: (Word128, Word128) -> Int
$cunsafeRangeSize :: (Word128, Word128) -> Int
rangeSize :: (Word128, Word128) -> Int
$crangeSize :: (Word128, Word128) -> Int
inRange :: (Word128, Word128) -> Word128 -> Bool
$cinRange :: (Word128, Word128) -> Word128 -> Bool
unsafeIndex :: (Word128, Word128) -> Word128 -> Int
$cunsafeIndex :: (Word128, Word128) -> Word128 -> Int
index :: (Word128, Word128) -> Word128 -> Int
$cindex :: (Word128, Word128) -> Word128 -> Int
range :: (Word128, Word128) -> [Word128]
$crange :: (Word128, Word128) -> [Word128]
Ix, Typeable)

instance Hashable Word128 where
  hashWithSalt :: Int -> Word128 -> Int
hashWithSalt Int
s (Word128 Word64
a1 Word64
a2) = Int
s forall a. Hashable a => Int -> a -> Int
`hashWithSalt` Word64
a1 forall a. Hashable a => Int -> a -> Int
`hashWithSalt` Word64
a2

-- | @since 0.1.5.0
instance Binary Word128 where
  put :: Word128 -> Put
put (Word128 Word64
a1 Word64
a2) = forall t. Binary t => t -> Put
put Word64
a1 forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall t. Binary t => t -> Put
put Word64
a2
  get :: Get Word128
get = Word64 -> Word64 -> Word128
Word128 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall t. Binary t => Get t
get forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall t. Binary t => Get t
get

byteSwapWord128 :: Word128 -> Word128
byteSwapWord128 :: Word128 -> Word128
byteSwapWord128 (Word128 Word64
a1 Word64
a0) = Word64 -> Word64 -> Word128
Word128 (Word64 -> Word64
byteSwap64 Word64
a0) (Word64 -> Word64
byteSwap64 Word64
a1)

showHexWord128 :: Word128 -> String
showHexWord128 :: Word128 -> String
showHexWord128 (Word128 Word64
a1 Word64
a0)
  | Word64
a1 forall a. Eq a => a -> a -> Bool
== Word64
0 = forall a. (Integral a, Show a) => a -> ShowS
showHex Word64
a0 String
""
  | Bool
otherwise = forall a. (Integral a, Show a) => a -> ShowS
showHex Word64
a1 String
zeros forall a. [a] -> [a] -> [a]
++ forall a. (Integral a, Show a) => a -> ShowS
showHex Word64
a0 String
""
  where
    h0 :: String
h0 = forall a. (Integral a, Show a) => a -> ShowS
showHex Word64
a0 String
""
    zeros :: String
zeros = forall a. Int -> a -> [a]
replicate (Int
16 forall a. Num a => a -> a -> a
- forall (t :: * -> *) a. Foldable t => t a -> Int
length String
h0) Char
'0'

instance Show Word128 where
  show :: Word128 -> String
show = forall a. Show a => a -> String
show forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word128 -> Integer
toInteger128

instance Read Word128 where
  readsPrec :: Int -> ReadS Word128
readsPrec Int
p String
s = [(Integer -> Word128
fromInteger128 (Integer
x :: Integer), String
r) | (Integer
x, String
r) <- forall a. Read a => Int -> ReadS a
readsPrec Int
p String
s]

instance Ord Word128 where
  compare :: Word128 -> Word128 -> Ordering
compare = Word128 -> Word128 -> Ordering
compare128

instance Bounded Word128 where
  minBound :: Word128
minBound = Word128
zeroWord128
  maxBound :: Word128
maxBound = Word64 -> Word64 -> Word128
Word128 forall a. Bounded a => a
maxBound forall a. Bounded a => a
maxBound

instance Enum Word128 where
  succ :: Word128 -> Word128
succ = Word128 -> Word128
succ128
  pred :: Word128 -> Word128
pred = Word128 -> Word128
pred128
  toEnum :: Int -> Word128
toEnum = Int -> Word128
toEnum128
  fromEnum :: Word128 -> Int
fromEnum = Word128 -> Int
fromEnum128

instance Num Word128 where
  + :: Word128 -> Word128 -> Word128
(+) = Word128 -> Word128 -> Word128
plus128
  (-) = Word128 -> Word128 -> Word128
minus128
  * :: Word128 -> Word128 -> Word128
(*) = Word128 -> Word128 -> Word128
times128
  negate :: Word128 -> Word128
negate = Word128 -> Word128
negate128
  abs :: Word128 -> Word128
abs = forall a. a -> a
id
  signum :: Word128 -> Word128
signum = Word128 -> Word128
signum128
  fromInteger :: Integer -> Word128
fromInteger = Integer -> Word128
fromInteger128

instance Bits Word128 where
  .&. :: Word128 -> Word128 -> Word128
(.&.) = Word128 -> Word128 -> Word128
and128
  .|. :: Word128 -> Word128 -> Word128
(.|.) = Word128 -> Word128 -> Word128
or128
  xor :: Word128 -> Word128 -> Word128
xor = Word128 -> Word128 -> Word128
xor128
  complement :: Word128 -> Word128
complement = Word128 -> Word128
complement128
  shiftL :: Word128 -> Int -> Word128
shiftL = Word128 -> Int -> Word128
shiftL128
  unsafeShiftL :: Word128 -> Int -> Word128
unsafeShiftL = Word128 -> Int -> Word128
shiftL128
  shiftR :: Word128 -> Int -> Word128
shiftR = Word128 -> Int -> Word128
shiftR128
  unsafeShiftR :: Word128 -> Int -> Word128
unsafeShiftR = Word128 -> Int -> Word128
shiftR128
  rotateL :: Word128 -> Int -> Word128
rotateL = Word128 -> Int -> Word128
rotateL128
  rotateR :: Word128 -> Int -> Word128
rotateR = Word128 -> Int -> Word128
rotateR128

  bitSize :: Word128 -> Int
bitSize Word128
_ = Int
128
  bitSizeMaybe :: Word128 -> Maybe Int
bitSizeMaybe Word128
_ = forall a. a -> Maybe a
Just Int
128
  isSigned :: Word128 -> Bool
isSigned Word128
_ = Bool
False

  testBit :: Word128 -> Int -> Bool
testBit = Word128 -> Int -> Bool
testBit128
  bit :: Int -> Word128
bit = Int -> Word128
bit128

  popCount :: Word128 -> Int
popCount = Word128 -> Int
popCount128

instance FiniteBits Word128 where
  finiteBitSize :: Word128 -> Int
finiteBitSize Word128
_ = Int
128
  countLeadingZeros :: Word128 -> Int
countLeadingZeros = Word128 -> Int
countLeadingZeros128
  countTrailingZeros :: Word128 -> Int
countTrailingZeros = Word128 -> Int
countTrailingZeros128

instance Real Word128 where
  toRational :: Word128 -> Rational
toRational Word128
x = Word128 -> Integer
toInteger128 Word128
x forall a. Integral a => a -> a -> Ratio a
% Integer
1

-- For unsigned values, quotRem is the same as divMod.
instance Integral Word128 where
  quot :: Word128 -> Word128 -> Word128
quot Word128
n Word128
d = forall a b. (a, b) -> a
fst (Word128 -> Word128 -> (Word128, Word128)
quotRem128 Word128
n Word128
d)
  rem :: Word128 -> Word128 -> Word128
rem Word128
n Word128
d = forall a b. (a, b) -> b
snd (Word128 -> Word128 -> (Word128, Word128)
quotRem128 Word128
n Word128
d)
  div :: Word128 -> Word128 -> Word128
div Word128
n Word128
d = forall a b. (a, b) -> a
fst (Word128 -> Word128 -> (Word128, Word128)
quotRem128 Word128
n Word128
d)
  mod :: Word128 -> Word128 -> Word128
mod Word128
n Word128
d = forall a b. (a, b) -> b
snd (Word128 -> Word128 -> (Word128, Word128)
quotRem128 Word128
n Word128
d)
  quotRem :: Word128 -> Word128 -> (Word128, Word128)
quotRem = Word128 -> Word128 -> (Word128, Word128)
quotRem128
  divMod :: Word128 -> Word128 -> (Word128, Word128)
divMod = Word128 -> Word128 -> (Word128, Word128)
quotRem128
  toInteger :: Word128 -> Integer
toInteger = Word128 -> Integer
toInteger128

instance Storable Word128 where
  sizeOf :: Word128 -> Int
sizeOf Word128
w = Int# -> Int
I# (Word128 -> Int#
sizeOf128# Word128
w)
  alignment :: Word128 -> Int
alignment Word128
w = Int# -> Int
I# (Word128 -> Int#
alignment128# Word128
w)
  peek :: Ptr Word128 -> IO Word128
peek = Ptr Word128 -> IO Word128
peek128
  peekElemOff :: Ptr Word128 -> Int -> IO Word128
peekElemOff = Ptr Word128 -> Int -> IO Word128
peekElemOff128
  poke :: Ptr Word128 -> Word128 -> IO ()
poke = Ptr Word128 -> Word128 -> IO ()
poke128
  pokeElemOff :: Ptr Word128 -> Int -> Word128 -> IO ()
pokeElemOff = Ptr Word128 -> Int -> Word128 -> IO ()
pokeElemOff128

instance NFData Word128 where
  -- The fields are already strict and unpacked, so do nothing.
  rnf :: Word128 -> ()
rnf !Word128
_ = ()

instance Prim Word128 where
  sizeOf# :: Word128 -> Int#
sizeOf#         = Word128 -> Int#
sizeOf128#
  alignment# :: Word128 -> Int#
alignment#      = Word128 -> Int#
alignment128#
  indexByteArray# :: ByteArray# -> Int# -> Word128
indexByteArray# = ByteArray# -> Int# -> Word128
indexByteArray128#
  readByteArray# :: forall s.
MutableByteArray# s -> Int# -> State# s -> (# State# s, Word128 #)
readByteArray#  = forall s.
MutableByteArray# s -> Int# -> State# s -> (# State# s, Word128 #)
readByteArray128#
  writeByteArray# :: forall s.
MutableByteArray# s -> Int# -> Word128 -> State# s -> State# s
writeByteArray# = forall s.
MutableByteArray# s -> Int# -> Word128 -> State# s -> State# s
writeByteArray128#
  setByteArray# :: forall s.
MutableByteArray# s
-> Int# -> Int# -> Word128 -> State# s -> State# s
setByteArray#   = forall s.
MutableByteArray# s
-> Int# -> Int# -> Word128 -> State# s -> State# s
setByteArray128#
  indexOffAddr# :: Addr# -> Int# -> Word128
indexOffAddr#   = Addr# -> Int# -> Word128
indexOffAddr128#
  readOffAddr# :: forall s. Addr# -> Int# -> State# s -> (# State# s, Word128 #)
readOffAddr#    = forall s. Addr# -> Int# -> State# s -> (# State# s, Word128 #)
readOffAddr128#
  writeOffAddr# :: forall s. Addr# -> Int# -> Word128 -> State# s -> State# s
writeOffAddr#   = forall s. Addr# -> Int# -> Word128 -> State# s -> State# s
writeOffAddr128#
  setOffAddr# :: forall s. Addr# -> Int# -> Int# -> Word128 -> State# s -> State# s
setOffAddr#     = forall s. Addr# -> Int# -> Int# -> Word128 -> State# s -> State# s
setOffAddr128#
  {-# INLINE sizeOf# #-}
  {-# INLINE alignment# #-}
  {-# INLINE indexByteArray# #-}
  {-# INLINE readByteArray# #-}
  {-# INLINE writeByteArray# #-}
  {-# INLINE setByteArray# #-}
  {-# INLINE indexOffAddr# #-}
  {-# INLINE readOffAddr# #-}
  {-# INLINE writeOffAddr# #-}
  {-# INLINE setOffAddr# #-}

-- -----------------------------------------------------------------------------
-- Rewrite rules.

{-# RULES
"fromIntegral :: Word128 -> Word128" fromIntegral = id :: Word128 -> Word128

"fromIntegral :: Int -> Word128"     fromIntegral = fromInt
"fromIntegral :: Word -> Word128"    fromIntegral = fromWord
"fromIntegral :: Word32 -> Word128"  fromIntegral = fromWord32
"fromIntegral :: Word64 -> Word128"  fromIntegral = Word128 0

"fromIntegral :: Word128 -> Int"     fromIntegral = toInt
"fromIntegral :: Word128 -> Word"    fromIntegral = toWord
"fromIntegral :: Word128 -> Word32"  fromIntegral = toWord32
"fromIntegral :: Word128 -> Word64"  fromIntegral = \(Word128 _ w) -> w
  #-}

{-# INLINE fromInt #-}
fromInt :: Int -> Word128
fromInt :: Int -> Word128
fromInt = Word64 -> Word64 -> Word128
Word128 Word64
0 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
fromIntegral

{-# INLINE fromWord #-}
fromWord :: Word -> Word128
fromWord :: Word -> Word128
fromWord = Word64 -> Word64 -> Word128
Word128 Word64
0 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
fromIntegral

{-# INLINE fromWord32 #-}
fromWord32 :: Word32 -> Word128
fromWord32 :: Word32 -> Word128
fromWord32 = Word64 -> Word64 -> Word128
Word128 Word64
0 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
fromIntegral

{-# INLINE toInt #-}
toInt :: Word128 -> Int
toInt :: Word128 -> Int
toInt (Word128 Word64
_ Word64
w) = forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
w

{-# INLINE toWord #-}
toWord :: Word128 -> Word
toWord :: Word128 -> Word
toWord (Word128 Word64
_ Word64
w) = forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
w

{-# INLINE toWord32 #-}
toWord32 :: Word128 -> Word32
toWord32 :: Word128 -> Word32
toWord32 (Word128 Word64
_ Word64
w) = forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
w

-- -----------------------------------------------------------------------------
-- Functions for `Ord` instance.

compare128 :: Word128 -> Word128 -> Ordering
compare128 :: Word128 -> Word128 -> Ordering
compare128 (Word128 Word64
a1 Word64
a0) (Word128 Word64
b1 Word64
b0) =
  forall a. Ord a => a -> a -> Ordering
compare Word64
a1 Word64
b1 forall a. Semigroup a => a -> a -> a
<> forall a. Ord a => a -> a -> Ordering
compare Word64
a0 Word64
b0

-- -----------------------------------------------------------------------------
-- Functions for `Enum` instance.

succ128 :: Word128 -> Word128
succ128 :: Word128 -> Word128
succ128 (Word128 Word64
a1 Word64
a0)
  | Word64
a0 forall a. Eq a => a -> a -> Bool
== forall a. Bounded a => a
maxBound = if Word64
a1 forall a. Eq a => a -> a -> Bool
== forall a. Bounded a => a
maxBound
                     then forall a. String -> a
succError String
"Word128"
                     else Word64 -> Word64 -> Word128
Word128 (Word64
a1 forall a. Num a => a -> a -> a
+ Word64
1) Word64
0
  | Bool
otherwise = Word64 -> Word64 -> Word128
Word128 Word64
a1 (Word64
a0 forall a. Num a => a -> a -> a
+ Word64
1)


pred128 :: Word128 -> Word128
pred128 :: Word128 -> Word128
pred128 (Word128 Word64
a1 Word64
a0)
  | Word64
a0 forall a. Eq a => a -> a -> Bool
== Word64
0 = if Word64
a1 forall a. Eq a => a -> a -> Bool
== Word64
0
              then forall a. String -> a
predError String
"Word128"
              else Word64 -> Word64 -> Word128
Word128 (Word64
a1 forall a. Num a => a -> a -> a
- Word64
1) forall a. Bounded a => a
maxBound
  | Bool
otherwise = Word64 -> Word64 -> Word128
Word128 Word64
a1 (Word64
a0 forall a. Num a => a -> a -> a
- Word64
1)


{-# INLINABLE toEnum128 #-}
toEnum128 :: Int -> Word128
toEnum128 :: Int -> Word128
toEnum128 Int
i = Word64 -> Word64 -> Word128
Word128 Word64
0 (forall a. Enum a => Int -> a
toEnum Int
i)

{-# INLINABLE fromEnum128 #-}
fromEnum128 :: Word128 -> Int
fromEnum128 :: Word128 -> Int
fromEnum128 (Word128 Word64
_ Word64
a0) = forall a. Enum a => a -> Int
fromEnum Word64
a0

-- -----------------------------------------------------------------------------
-- Functions for `Num` instance.

{-# INLINABLE plus128 #-}
plus128 :: Word128 -> Word128 -> Word128
plus128 :: Word128 -> Word128 -> Word128
plus128 (Word128 Word64
a1 Word64
a0) (Word128 Word64
b1 Word64
b0) =
    Word64 -> Word64 -> Word128
Word128 Word64
s1 Word64
s0
  where
    !(Word64
c1, Word64
s0) = Word64 -> Word64 -> (Word64, Word64)
plusCarrySum Word64
a0 Word64
b0
    !s1 :: Word64
s1 = Word64
a1 forall a. Num a => a -> a -> a
+ Word64
b1 forall a. Num a => a -> a -> a
+ Word64
c1

{-# INLINABLE minus128 #-}
minus128 :: Word128 -> Word128 -> Word128
minus128 :: Word128 -> Word128 -> Word128
minus128 (Word128 Word64
a1 Word64
a0) (Word128 Word64
b1 Word64
b0) =
    Word64 -> Word64 -> Word128
Word128 Word64
d1 Word64
d0
  where
    !(Word64
c1, Word64
d0) = Word64 -> Word64 -> (Word64, Word64)
subCarryDiff Word64
a0 Word64
b0
    !d1 :: Word64
d1 = Word64
a1 forall a. Num a => a -> a -> a
- Word64
c1 forall a. Num a => a -> a -> a
- Word64
b1

times128 :: Word128 -> Word128 -> Word128
times128 :: Word128 -> Word128 -> Word128
times128 (Word128 Word64
a1 Word64
a0) (Word128 Word64
b1 Word64
b0) =
    Word64 -> Word64 -> Word128
Word128 Word64
p1 Word64
p0
  where
    !(Word64
c1, Word64
p0) = Word64 -> Word64 -> (Word64, Word64)
timesCarryProd Word64
a0 Word64
b0
    !p1a :: Word64
p1a = Word64
a1 forall a. Num a => a -> a -> a
* Word64
b0
    !p1b :: Word64
p1b = Word64
a0 forall a. Num a => a -> a -> a
* Word64
b1
    !p1c :: Word64
p1c = Word64
p1a forall a. Num a => a -> a -> a
+ Word64
p1b
    !p1 :: Word64
p1 = Word64
p1c forall a. Num a => a -> a -> a
+ Word64
c1

{-# INLINABLE negate128 #-}
negate128 :: Word128 -> Word128
negate128 :: Word128 -> Word128
negate128 (Word128 Word64
a1 Word64
a0) =
  case Word64 -> Word64 -> (Word64, Word64)
plusCarrySum (forall a. Bits a => a -> a
complement Word64
a0) Word64
1 of
    (Word64
c, Word64
s) -> Word64 -> Word64 -> Word128
Word128 (forall a. Bits a => a -> a
complement Word64
a1 forall a. Num a => a -> a -> a
+ Word64
c) Word64
s

{-# INLINABLE signum128 #-}
signum128 :: Word128 -> Word128
signum128 :: Word128 -> Word128
signum128 (Word128 Word64
a Word64
b) =
  if Word64
a forall a. Eq a => a -> a -> Bool
== Word64
0 Bool -> Bool -> Bool
&& Word64
b forall a. Eq a => a -> a -> Bool
== Word64
0
    then Word128
zeroWord128
    else Word128
oneWord128

fromInteger128 :: Integer -> Word128
fromInteger128 :: Integer -> Word128
fromInteger128 Integer
i =
  Word64 -> Word64 -> Word128
Word128 (forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ Integer
i forall a. Bits a => a -> Int -> a
`shiftR` Int
64) (forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
i)

-- -----------------------------------------------------------------------------
-- Functions for `Bits` instance.

{-# INLINABLE and128 #-}
and128 :: Word128 -> Word128 -> Word128
and128 :: Word128 -> Word128 -> Word128
and128 (Word128 Word64
a1 Word64
a0) (Word128 Word64
b1 Word64
b0) = Word64 -> Word64 -> Word128
Word128 (Word64
a1 forall a. Bits a => a -> a -> a
.&. Word64
b1) (Word64
a0 forall a. Bits a => a -> a -> a
.&. Word64
b0)

{-# INLINABLE or128 #-}
or128 :: Word128 -> Word128 -> Word128
or128 :: Word128 -> Word128 -> Word128
or128 (Word128 Word64
a1 Word64
a0) (Word128 Word64
b1 Word64
b0) = Word64 -> Word64 -> Word128
Word128 (Word64
a1 forall a. Bits a => a -> a -> a
.|. Word64
b1) (Word64
a0 forall a. Bits a => a -> a -> a
.|. Word64
b0)

{-# INLINABLE xor128 #-}
xor128 :: Word128 -> Word128 -> Word128
xor128 :: Word128 -> Word128 -> Word128
xor128 (Word128 Word64
a1 Word64
a0) (Word128 Word64
b1 Word64
b0) = Word64 -> Word64 -> Word128
Word128 (forall a. Bits a => a -> a -> a
xor Word64
a1 Word64
b1) (forall a. Bits a => a -> a -> a
xor Word64
a0 Word64
b0)

{-# INLINABLE complement128 #-}
complement128 :: Word128 -> Word128
complement128 :: Word128 -> Word128
complement128 (Word128 Word64
a1 Word64
a0) = Word64 -> Word64 -> Word128
Word128 (forall a. Bits a => a -> a
complement Word64
a1) (forall a. Bits a => a -> a
complement Word64
a0)

-- Some of the following functions have quite complicated guard clauses, but we make them
-- inlineable anyway so that if the things like the shift amount is a compile time constant
-- most of the function can be dropped leaving only the needed bits inlined.

{-# INLINABLE shiftL128 #-}
shiftL128 :: Word128 -> Int -> Word128
shiftL128 :: Word128 -> Int -> Word128
shiftL128 w :: Word128
w@(Word128 Word64
a1 Word64
a0) Int
s
  | Int
s forall a. Eq a => a -> a -> Bool
== Int
0 = Word128
w
  | Int
s forall a. Eq a => a -> a -> Bool
== forall a. Bounded a => a
minBound = Word128
zeroWord128
  | Int
s forall a. Ord a => a -> a -> Bool
< Int
0 = Word128 -> Int -> Word128
shiftR128 Word128
w (forall a. Num a => a -> a
negate Int
s)
  | Int
s forall a. Ord a => a -> a -> Bool
>= Int
128 = Word128
zeroWord128
  | Int
s forall a. Eq a => a -> a -> Bool
== Int
64 = Word64 -> Word64 -> Word128
Word128 Word64
a0 Word64
0
  | Int
s forall a. Ord a => a -> a -> Bool
> Int
64 = Word64 -> Word64 -> Word128
Word128 (Word64
a0 forall a. Bits a => a -> Int -> a
`shiftL` (Int
s forall a. Num a => a -> a -> a
- Int
64)) Word64
0
  | Bool
otherwise =
      Word64 -> Word64 -> Word128
Word128 Word64
s1 Word64
s0
      where
        s0 :: Word64
s0 = Word64
a0 forall a. Bits a => a -> Int -> a
`shiftL` Int
s
        s1 :: Word64
s1 = Word64
a1 forall a. Bits a => a -> Int -> a
`shiftL` Int
s forall a. Num a => a -> a -> a
+ Word64
a0 forall a. Bits a => a -> Int -> a
`shiftR` (Int
64 forall a. Num a => a -> a -> a
- Int
s)

{-# INLINABLE shiftR128 #-}
shiftR128 :: Word128 -> Int -> Word128
shiftR128 :: Word128 -> Int -> Word128
shiftR128 w :: Word128
w@(Word128 Word64
a1 Word64
a0) Int
s
  | Int
s forall a. Eq a => a -> a -> Bool
== Int
0 = Word128
w
  | Int
s forall a. Eq a => a -> a -> Bool
== forall a. Bounded a => a
minBound = Word128
zeroWord128
  | Int
s forall a. Ord a => a -> a -> Bool
< Int
0 = Word128 -> Int -> Word128
shiftL128 Word128
w (forall a. Num a => a -> a
negate Int
s)
  | Int
s forall a. Ord a => a -> a -> Bool
>= Int
128 = Word128
zeroWord128
  | Int
s forall a. Eq a => a -> a -> Bool
== Int
64 = Word64 -> Word64 -> Word128
Word128 Word64
0 Word64
a1
  | Int
s forall a. Ord a => a -> a -> Bool
> Int
64 = Word64 -> Word64 -> Word128
Word128 Word64
0 (Word64
a1 forall a. Bits a => a -> Int -> a
`shiftR` (Int
s forall a. Num a => a -> a -> a
- Int
64))
  | Bool
otherwise =
      Word64 -> Word64 -> Word128
Word128 Word64
s1 Word64
s0
      where
        s1 :: Word64
s1 = Word64
a1 forall a. Bits a => a -> Int -> a
`shiftR` Int
s
        s0 :: Word64
s0 = Word64
a0 forall a. Bits a => a -> Int -> a
`shiftR` Int
s forall a. Num a => a -> a -> a
+ Word64
a1 forall a. Bits a => a -> Int -> a
`shiftL` (Int
64 forall a. Num a => a -> a -> a
- Int
s)

{-# INLINABLE rotateL128 #-}
rotateL128 :: Word128 -> Int -> Word128
rotateL128 :: Word128 -> Int -> Word128
rotateL128 w :: Word128
w@(Word128 Word64
a1 Word64
a0) Int
r
  | Int
r forall a. Eq a => a -> a -> Bool
== Int
0 = Word128
w
  | Int
r forall a. Ord a => a -> a -> Bool
< Int
0 = Word128 -> Int -> Word128
rotateL128 Word128
w (Int
128 forall a. Num a => a -> a -> a
- (forall a. Num a => a -> a
abs Int
r forall a. Integral a => a -> a -> a
`mod` Int
128))
  | Int
r forall a. Ord a => a -> a -> Bool
>= Int
128 = Word128 -> Int -> Word128
rotateL128 Word128
w (Int
r forall a. Integral a => a -> a -> a
`mod` Int
128)
  | Int
r forall a. Eq a => a -> a -> Bool
== Int
64 = Word64 -> Word64 -> Word128
Word128 Word64
a0 Word64
a1
  | Int
r forall a. Ord a => a -> a -> Bool
> Int
64 = Word128 -> Int -> Word128
rotateL128 (Word64 -> Word64 -> Word128
Word128 Word64
a0 Word64
a1) (Int
r forall a. Integral a => a -> a -> a
`mod` Int
64)
  | Bool
otherwise =
      Word64 -> Word64 -> Word128
Word128 Word64
s1 Word64
s0
      where
        s0 :: Word64
s0 = Word64
a0 forall a. Bits a => a -> Int -> a
`shiftL` Int
r forall a. Num a => a -> a -> a
+ Word64
a1 forall a. Bits a => a -> Int -> a
`shiftR` (Int
64 forall a. Num a => a -> a -> a
- Int
r)
        s1 :: Word64
s1 = Word64
a1 forall a. Bits a => a -> Int -> a
`shiftL` Int
r forall a. Num a => a -> a -> a
+ Word64
a0 forall a. Bits a => a -> Int -> a
`shiftR` (Int
64 forall a. Num a => a -> a -> a
- Int
r)

{-# INLINABLE rotateR128 #-}
rotateR128 :: Word128 -> Int -> Word128
rotateR128 :: Word128 -> Int -> Word128
rotateR128 w :: Word128
w@(Word128 Word64
a1 Word64
a0) Int
r
  | Int
r forall a. Eq a => a -> a -> Bool
== Int
0 = Word128
w
  | Int
r forall a. Ord a => a -> a -> Bool
< Int
0 = Word128 -> Int -> Word128
rotateR128 Word128
w (Int
128 forall a. Num a => a -> a -> a
- (forall a. Num a => a -> a
abs Int
r forall a. Integral a => a -> a -> a
`mod` Int
128))
  | Int
r forall a. Ord a => a -> a -> Bool
>= Int
128 = Word128 -> Int -> Word128
rotateR128 Word128
w (Int
r forall a. Integral a => a -> a -> a
`mod` Int
128)
  | Int
r forall a. Eq a => a -> a -> Bool
== Int
64 = Word64 -> Word64 -> Word128
Word128 Word64
a0 Word64
a1
  | Int
r forall a. Ord a => a -> a -> Bool
> Int
64 = Word128 -> Int -> Word128
rotateR128 (Word64 -> Word64 -> Word128
Word128 Word64
a0 Word64
a1) (Int
r forall a. Integral a => a -> a -> a
`mod` Int
64)
  | Bool
otherwise =
      Word64 -> Word64 -> Word128
Word128 Word64
s1 Word64
s0
      where
        s0 :: Word64
s0 = Word64
a0 forall a. Bits a => a -> Int -> a
`shiftR` Int
r forall a. Num a => a -> a -> a
+ Word64
a1 forall a. Bits a => a -> Int -> a
`shiftL` (Int
64 forall a. Num a => a -> a -> a
- Int
r)
        s1 :: Word64
s1 = Word64
a1 forall a. Bits a => a -> Int -> a
`shiftR` Int
r forall a. Num a => a -> a -> a
+ Word64
a0 forall a. Bits a => a -> Int -> a
`shiftL` (Int
64 forall a. Num a => a -> a -> a
- Int
r)

{-# INLINABLE testBit128 #-}
testBit128 :: Word128 -> Int -> Bool
testBit128 :: Word128 -> Int -> Bool
testBit128 (Word128 Word64
a1 Word64
a0) Int
i
  | Int
i forall a. Ord a => a -> a -> Bool
< Int
0 = Bool
False
  | Int
i forall a. Ord a => a -> a -> Bool
>= Int
128 = Bool
False
  | Int
i forall a. Ord a => a -> a -> Bool
>= Int
64 = forall a. Bits a => a -> Int -> Bool
testBit Word64
a1 (Int
i forall a. Num a => a -> a -> a
- Int
64)
  | Bool
otherwise = forall a. Bits a => a -> Int -> Bool
testBit Word64
a0 Int
i

{-# INLINABLE bit128 #-}
bit128 :: Int -> Word128
bit128 :: Int -> Word128
bit128 Int
indx
  | Int
indx forall a. Ord a => a -> a -> Bool
< Int
0 = Word128
zeroWord128
  | Int
indx forall a. Ord a => a -> a -> Bool
>= Int
128 = Word128
zeroWord128
  | Bool
otherwise = Word128 -> Int -> Word128
shiftL128 Word128
oneWord128 Int
indx

{-# INLINABLE popCount128 #-}
popCount128 :: Word128 -> Int
popCount128 :: Word128 -> Int
popCount128 (Word128 Word64
a1 Word64
a0) = forall a. Bits a => a -> Int
popCount Word64
a1 forall a. Num a => a -> a -> a
+ forall a. Bits a => a -> Int
popCount Word64
a0

-- -----------------------------------------------------------------------------
-- Functions for `FiniteBits` instance.

{-# INLINABLE countLeadingZeros128 #-}
countLeadingZeros128 :: Word128 -> Int
countLeadingZeros128 :: Word128 -> Int
countLeadingZeros128 (Word128 Word64
a1 Word64
a0) =
  case forall b. FiniteBits b => b -> Int
countLeadingZeros Word64
a1 of
    Int
64 -> Int
64 forall a. Num a => a -> a -> a
+  forall b. FiniteBits b => b -> Int
countLeadingZeros Word64
a0
    Int
res -> Int
res

{-# INLINABLE countTrailingZeros128 #-}
countTrailingZeros128 :: Word128 -> Int
countTrailingZeros128 :: Word128 -> Int
countTrailingZeros128 (Word128 Word64
a1 Word64
a0) =
  case forall b. FiniteBits b => b -> Int
countTrailingZeros Word64
a0 of
    Int
64 -> Int
64 forall a. Num a => a -> a -> a
+ forall b. FiniteBits b => b -> Int
countTrailingZeros Word64
a1
    Int
res -> Int
res

-- -----------------------------------------------------------------------------
-- Functions for `Integral` instance.

quotRem128 :: Word128 -> Word128 -> (Word128, Word128)
quotRem128 :: Word128 -> Word128 -> (Word128, Word128)
quotRem128 num :: Word128
num@(Word128 Word64
n1 Word64
n0) den :: Word128
den@(Word128 Word64
d1 Word64
d0)
  | Word64
n1 forall a. Eq a => a -> a -> Bool
== Word64
0 Bool -> Bool -> Bool
&& Word64
d1 forall a. Eq a => a -> a -> Bool
== Word64
0 = Word64 -> Word64 -> (Word128, Word128)
quotRemTwo Word64
n0 Word64
d0
  | Word64
n1 forall a. Ord a => a -> a -> Bool
< Word64
d1 = (Word128
zeroWord128, Word128
num)
  | Word64
d1 forall a. Eq a => a -> a -> Bool
== Word64
0 = Word128 -> Word64 -> (Word128, Word128)
quotRemThree Word128
num Word64
d0
  | Word64
n1 forall a. Eq a => a -> a -> Bool
== Word64
d1 =
      case forall a. Ord a => a -> a -> Ordering
compare Word64
n0 Word64
d0 of
        Ordering
LT -> (Word128
zeroWord128, Word128
num)
        Ordering
EQ -> (Word128
oneWord128, Word128
zeroWord128)
        Ordering
GT -> (Word64 -> Word64 -> Word128
Word128 Word64
0 Word64
1, Word64 -> Word64 -> Word128
Word128 Word64
0 (Word64
n0 forall a. Num a => a -> a -> a
- Word64
d0))
  | Bool
otherwise = Word128 -> Word128 -> (Word128, Word128)
quotRemFour Word128
num Word128
den

{-# INLINE quotRemFour #-}
quotRemFour :: Word128 -> Word128 -> (Word128, Word128)
quotRemFour :: Word128 -> Word128 -> (Word128, Word128)
quotRemFour num :: Word128
num@(Word128 Word64
n1 Word64
_) den :: Word128
den@(Word128 Word64
d1 Word64
_)
  | Word128
remain forall a. Ord a => a -> a -> Bool
< Word128
den = (Word64 -> Word64 -> Word128
Word128 Word64
0 Word64
qest, Word128
remain)
    -- The above is correct in most cases, but for the case where is not
    -- we have the following. While the following is correct, it is rather
    -- suboptimal. Would be nice to find something better.
  | Bool
otherwise =
      forall a b. (a -> b) -> (a, a) -> (b, b)
mapPair Integer -> Word128
fromInteger128 forall a b. (a -> b) -> a -> b
$ forall a. Integral a => a -> a -> (a, a)
quotRem (forall a. Integral a => a -> Integer
toInteger Word128
num) (forall a. Integral a => a -> Integer
toInteger Word128
den)
  where
    qest :: Word64
qest = forall a. Integral a => a -> a -> a
quot Word64
n1 Word64
d1
    prod :: Word128
prod = Word128 -> Word64 -> Word128
halfTimes128 Word128
den Word64
qest
    remain :: Word128
remain = Word128 -> Word128 -> Word128
minus128 Word128
num Word128
prod

{-# INLINE halfTimes128 #-}
halfTimes128 :: Word128 -> Word64 -> Word128
halfTimes128 :: Word128 -> Word64 -> Word128
halfTimes128 (Word128 Word64
a1 Word64
a0) Word64
b0 =
  Word64 -> Word64 -> Word128
Word128 Word64
p1 Word64
p0
  where
    !(Word64
c1, Word64
p0) = Word64 -> Word64 -> (Word64, Word64)
timesCarryProd Word64
a0 Word64
b0
    p1a :: Word64
p1a = Word64
a1 forall a. Num a => a -> a -> a
* Word64
b0
    p1 :: Word64
p1 = Word64
p1a forall a. Num a => a -> a -> a
+ Word64
c1

{-# INLINE quotRemThree #-}
quotRemThree :: Word128 -> Word64 -> (Word128, Word128)
quotRemThree :: Word128 -> Word64 -> (Word128, Word128)
quotRemThree num :: Word128
num@(Word128 Word64
n1 Word64
n0) Word64
den
  | Word64
den forall a. Eq a => a -> a -> Bool
== Word64
0 = forall a. a
divZeroError
  | Word64
den forall a. Eq a => a -> a -> Bool
== Word64
1 = (Word128
num, Word128
zeroWord128)
  | Word64
n1 forall a. Ord a => a -> a -> Bool
< Word64
den = case Word64 -> Word64 -> Word64 -> (Word64, Word64)
quotRem2Word64 Word64
n1 Word64
n0 Word64
den of
                (Word64
q, Word64
r) -> (Word64 -> Word64 -> Word128
Word128 Word64
0 Word64
q, Word64 -> Word64 -> Word128
Word128 Word64
0 Word64
r)
  | Bool
otherwise =
      case forall a. Integral a => a -> a -> (a, a)
quotRem Word64
n1 Word64
den of
        (Word64
q1, Word64
r1) -> case Word64 -> Word64 -> Word64 -> (Word64, Word64)
quotRem2Word64 Word64
r1 Word64
n0 Word64
den of
             (Word64
q0, Word64
r0) -> (Word64 -> Word64 -> Word128
Word128 Word64
q1 Word64
q0, Word64 -> Word64 -> Word128
Word128 Word64
0 Word64
r0)

{-# INLINE quotRemTwo #-}
quotRemTwo :: Word64 -> Word64 -> (Word128, Word128)
quotRemTwo :: Word64 -> Word64 -> (Word128, Word128)
quotRemTwo Word64
n0 Word64
d0 =
  case forall a. Integral a => a -> a -> (a, a)
quotRem Word64
n0 Word64
d0 of
    (Word64
q, Word64
r) -> (Word64 -> Word64 -> Word128
Word128 Word64
0 Word64
q, Word64 -> Word64 -> Word128
Word128 Word64
0 Word64
r)

{-# INLINE toInteger128 #-}
toInteger128 :: Word128 -> Integer
toInteger128 :: Word128 -> Integer
toInteger128 (Word128 Word64
a1 Word64
a0) = forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
a1 forall a. Bits a => a -> Int -> a
`shiftL` Int
64 forall a. Num a => a -> a -> a
+ forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
a0

-- -----------------------------------------------------------------------------
-- Functions for `Storable` instance.

peek128 :: Ptr Word128 -> IO Word128
peek128 :: Ptr Word128 -> IO Word128
peek128 Ptr Word128
ptr =
  Word64 -> Word64 -> Word128
Word128 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. Storable a => Ptr a -> Int -> IO a
peekElemOff (forall a b. Ptr a -> Ptr b
castPtr Ptr Word128
ptr) Int
index1 forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. Storable a => Ptr a -> Int -> IO a
peekElemOff (forall a b. Ptr a -> Ptr b
castPtr Ptr Word128
ptr) Int
index0

peekElemOff128 :: Ptr Word128 -> Int -> IO Word128
peekElemOff128 :: Ptr Word128 -> Int -> IO Word128
peekElemOff128 Ptr Word128
ptr Int
idx =
  Word64 -> Word64 -> Word128
Word128 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. Storable a => Ptr a -> Int -> IO a
peekElemOff (forall a b. Ptr a -> Ptr b
castPtr Ptr Word128
ptr) (Int
idx2 forall a. Num a => a -> a -> a
+ Int
index1)
            forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. Storable a => Ptr a -> Int -> IO a
peekElemOff (forall a b. Ptr a -> Ptr b
castPtr Ptr Word128
ptr) (Int
idx2 forall a. Num a => a -> a -> a
+ Int
index0)
  where idx2 :: Int
idx2 = Int
2 forall a. Num a => a -> a -> a
* Int
idx

poke128 :: Ptr Word128 -> Word128 -> IO ()
poke128 :: Ptr Word128 -> Word128 -> IO ()
poke128 Ptr Word128
ptr (Word128 Word64
a1 Word64
a0) =
  forall a. Storable a => Ptr a -> Int -> a -> IO ()
pokeElemOff (forall a b. Ptr a -> Ptr b
castPtr Ptr Word128
ptr) Int
index1 Word64
a1 forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall a. Storable a => Ptr a -> Int -> a -> IO ()
pokeElemOff (forall a b. Ptr a -> Ptr b
castPtr Ptr Word128
ptr) Int
index0 Word64
a0

pokeElemOff128 :: Ptr Word128 -> Int -> Word128 -> IO ()
pokeElemOff128 :: Ptr Word128 -> Int -> Word128 -> IO ()
pokeElemOff128 Ptr Word128
ptr Int
idx (Word128 Word64
a1 Word64
a0) = do
  let idx2 :: Int
idx2 = Int
2 forall a. Num a => a -> a -> a
* Int
idx
  forall a. Storable a => Ptr a -> Int -> a -> IO ()
pokeElemOff (forall a b. Ptr a -> Ptr b
castPtr Ptr Word128
ptr) (Int
idx2 forall a. Num a => a -> a -> a
+ Int
index0) Word64
a0
  forall a. Storable a => Ptr a -> Int -> a -> IO ()
pokeElemOff (forall a b. Ptr a -> Ptr b
castPtr Ptr Word128
ptr) (Int
idx2 forall a. Num a => a -> a -> a
+ Int
index1) Word64
a1

-- -----------------------------------------------------------------------------
-- Functions for `Prim` instance.

{-# INLINE sizeOf128# #-}
sizeOf128# :: Word128 -> Int#
sizeOf128# :: Word128 -> Int#
sizeOf128# Word128
_ = Int#
2# Int# -> Int# -> Int#
*# forall a. Prim a => a -> Int#
sizeOf# (Word64
0 :: Word64)

{-# INLINE alignment128# #-}
alignment128# :: Word128 -> Int#
alignment128# :: Word128 -> Int#
alignment128# Word128
_ = Int#
2# Int# -> Int# -> Int#
*# forall a. Prim a => a -> Int#
alignment# (Word64
0 :: Word64)

{-# INLINE indexByteArray128# #-}
indexByteArray128# :: ByteArray# -> Int# -> Word128
indexByteArray128# :: ByteArray# -> Int# -> Word128
indexByteArray128# ByteArray#
arr# Int#
i# =
  let i2# :: Int#
i2# = Int#
2# Int# -> Int# -> Int#
*# Int#
i#
      x :: Word64
x = forall a. Prim a => ByteArray# -> Int# -> a
indexByteArray# ByteArray#
arr# (Int#
i2# Int# -> Int# -> Int#
+# Int -> Int#
unInt Int
index1)
      y :: Word64
y = forall a. Prim a => ByteArray# -> Int# -> a
indexByteArray# ByteArray#
arr# (Int#
i2# Int# -> Int# -> Int#
+# Int -> Int#
unInt Int
index0)
  in Word64 -> Word64 -> Word128
Word128 Word64
x Word64
y

{-# INLINE readByteArray128# #-}
readByteArray128# :: MutableByteArray# s -> Int# -> State# s -> (# State# s, Word128 #)
readByteArray128# :: forall s.
MutableByteArray# s -> Int# -> State# s -> (# State# s, Word128 #)
readByteArray128# MutableByteArray# s
arr# Int#
i# =
  \State# s
s0 -> case forall a s.
Prim a =>
MutableByteArray# s -> Int# -> State# s -> (# State# s, a #)
readByteArray# MutableByteArray# s
arr# (Int#
i2# Int# -> Int# -> Int#
+# Int -> Int#
unInt Int
index1) State# s
s0 of
    (# State# s
s1, Word64
x #) -> case forall a s.
Prim a =>
MutableByteArray# s -> Int# -> State# s -> (# State# s, a #)
readByteArray# MutableByteArray# s
arr# (Int#
i2# Int# -> Int# -> Int#
+# Int -> Int#
unInt Int
index0) State# s
s1 of
      (# State# s
s2, Word64
y #) -> (# State# s
s2, Word64 -> Word64 -> Word128
Word128 Word64
x Word64
y #)
  where i2# :: Int#
i2# = Int#
2# Int# -> Int# -> Int#
*# Int#
i#

{-# INLINE writeByteArray128# #-}
writeByteArray128# :: MutableByteArray# s -> Int# -> Word128 -> State# s -> State# s
writeByteArray128# :: forall s.
MutableByteArray# s -> Int# -> Word128 -> State# s -> State# s
writeByteArray128# MutableByteArray# s
arr# Int#
i# (Word128 Word64
a Word64
b) =
  \State# s
s0 -> case forall a s.
Prim a =>
MutableByteArray# s -> Int# -> a -> State# s -> State# s
writeByteArray# MutableByteArray# s
arr# (Int#
i2# Int# -> Int# -> Int#
+# Int -> Int#
unInt Int
index1) Word64
a State# s
s0 of
    State# s
s1 -> case forall a s.
Prim a =>
MutableByteArray# s -> Int# -> a -> State# s -> State# s
writeByteArray# MutableByteArray# s
arr# (Int#
i2# Int# -> Int# -> Int#
+# Int -> Int#
unInt Int
index0) Word64
b State# s
s1 of
      State# s
s2 -> State# s
s2
  where i2# :: Int#
i2# = Int#
2# Int# -> Int# -> Int#
*# Int#
i#

{-# INLINE setByteArray128# #-}
setByteArray128# :: MutableByteArray# s -> Int# -> Int# -> Word128 -> State# s -> State# s
setByteArray128# :: forall s.
MutableByteArray# s
-> Int# -> Int# -> Word128 -> State# s -> State# s
setByteArray128# = forall a s.
Prim a =>
MutableByteArray# s -> Int# -> Int# -> a -> State# s -> State# s
defaultSetByteArray#

{-# INLINE indexOffAddr128# #-}
indexOffAddr128# :: Addr# -> Int# -> Word128
indexOffAddr128# :: Addr# -> Int# -> Word128
indexOffAddr128# Addr#
addr# Int#
i# =
  let i2# :: Int#
i2# = Int#
2# Int# -> Int# -> Int#
*# Int#
i#
      x :: Word64
x = forall a. Prim a => Addr# -> Int# -> a
indexOffAddr# Addr#
addr# (Int#
i2# Int# -> Int# -> Int#
+# Int -> Int#
unInt Int
index1)
      y :: Word64
y = forall a. Prim a => Addr# -> Int# -> a
indexOffAddr# Addr#
addr# (Int#
i2# Int# -> Int# -> Int#
+# Int -> Int#
unInt Int
index0)
  in Word64 -> Word64 -> Word128
Word128 Word64
x Word64
y

{-# INLINE readOffAddr128# #-}
readOffAddr128# :: Addr# -> Int# -> State# s -> (# State# s, Word128 #)
readOffAddr128# :: forall s. Addr# -> Int# -> State# s -> (# State# s, Word128 #)
readOffAddr128# Addr#
addr# Int#
i# =
  \State# s
s0 -> case forall a s.
Prim a =>
Addr# -> Int# -> State# s -> (# State# s, a #)
readOffAddr# Addr#
addr# (Int#
i2# Int# -> Int# -> Int#
+# Int -> Int#
unInt Int
index1) State# s
s0 of
    (# State# s
s1, Word64
x #) -> case forall a s.
Prim a =>
Addr# -> Int# -> State# s -> (# State# s, a #)
readOffAddr# Addr#
addr# (Int#
i2# Int# -> Int# -> Int#
+# Int -> Int#
unInt Int
index0) State# s
s1 of
      (# State# s
s2, Word64
y #) -> (# State# s
s2, Word64 -> Word64 -> Word128
Word128 Word64
x Word64
y #)
  where i2# :: Int#
i2# = Int#
2# Int# -> Int# -> Int#
*# Int#
i#

{-# INLINE writeOffAddr128# #-}
writeOffAddr128# :: Addr# -> Int# -> Word128 -> State# s -> State# s
writeOffAddr128# :: forall s. Addr# -> Int# -> Word128 -> State# s -> State# s
writeOffAddr128# Addr#
addr# Int#
i# (Word128 Word64
a Word64
b) =
  \State# s
s0 -> case forall a s. Prim a => Addr# -> Int# -> a -> State# s -> State# s
writeOffAddr# Addr#
addr# (Int#
i2# Int# -> Int# -> Int#
+# Int -> Int#
unInt Int
index1) Word64
a State# s
s0 of
    State# s
s1 -> case forall a s. Prim a => Addr# -> Int# -> a -> State# s -> State# s
writeOffAddr# Addr#
addr# (Int#
i2# Int# -> Int# -> Int#
+# Int -> Int#
unInt Int
index0) Word64
b State# s
s1 of
      State# s
s2 -> State# s
s2
  where i2# :: Int#
i2# = Int#
2# Int# -> Int# -> Int#
*# Int#
i#

{-# INLINE setOffAddr128# #-}
setOffAddr128# :: Addr# -> Int# -> Int# -> Word128 -> State# s -> State# s
setOffAddr128# :: forall s. Addr# -> Int# -> Int# -> Word128 -> State# s -> State# s
setOffAddr128# = forall a s.
Prim a =>
Addr# -> Int# -> Int# -> a -> State# s -> State# s
defaultSetOffAddr#

mapPair :: (a -> b) -> (a, a) -> (b, b)
mapPair :: forall a b. (a -> b) -> (a, a) -> (b, b)
mapPair a -> b
f (a
a, a
b) = (a -> b
f a
a, a -> b
f a
b)

-- -----------------------------------------------------------------------------
-- Constants.

zeroWord128 :: Word128
zeroWord128 :: Word128
zeroWord128 = Word64 -> Word64 -> Word128
Word128 Word64
0 Word64
0

oneWord128 :: Word128
oneWord128 :: Word128
oneWord128 = Word64 -> Word64 -> Word128
Word128 Word64
0 Word64
1

unInt :: Int -> Int#
unInt :: Int -> Int#
unInt (I# Int#
i#) = Int#
i#

-- Use these indices to get the peek/poke ordering endian correct.
index0, index1 :: Int
#if WORDS_BIGENDIAN
index0 = 1
index1 = 0
#else
index0 :: Int
index0 = Int
0
index1 :: Int
index1 = Int
1
#endif