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

-----------------------------------------------------------------------------
---- |
---- Module      :  Data.WideWord.Int128
----
---- Maintainer  :  erikd@mega-nerd.com
---- Stability   :  experimental
---- Portability :  non-portable (GHC extensions and primops)
----
---- This module provides an opaque signed 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.
-------------------------------------------------------------------------------

module Data.WideWord.Int128
  ( Int128 (..)
  , byteSwapInt128
  , showHexInt128
  , zeroInt128
  ) 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.Word128

import Numeric

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

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

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

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

import Data.WideWord.Word64

#if MIN_VERSION_base(4,17,0)
#define ONE (wordToWord64# 1##)
#else
#define ONE (1##)
#endif

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

instance Hashable Int128 where
  hashWithSalt :: Int -> Int128 -> Int
hashWithSalt Int
s (Int128 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 Int128 where
  put :: Int128 -> Put
put (Int128 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 Int128
get = Word64 -> Word64 -> Int128
Int128 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

byteSwapInt128 :: Int128 -> Int128
byteSwapInt128 :: Int128 -> Int128
byteSwapInt128 (Int128 Word64
a1 Word64
a0) = Word64 -> Word64 -> Int128
Int128 (Word64 -> Word64
byteSwap64 Word64
a0) (Word64 -> Word64
byteSwap64 Word64
a1)

showHexInt128 :: Int128 -> String
showHexInt128 :: Int128 -> String
showHexInt128 (Int128 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 Int128 where
  show :: Int128 -> String
show = forall a. Show a => a -> String
show forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Integral a => a -> Integer
toInteger

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

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

instance Bounded Int128 where
  minBound :: Int128
minBound = Word64 -> Word64 -> Int128
Int128 Word64
0x8000000000000000 Word64
0
  maxBound :: Int128
maxBound = Word64 -> Word64 -> Int128
Int128 Word64
0x7fffffffffffffff forall a. Bounded a => a
maxBound

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

instance Num Int128 where
  + :: Int128 -> Int128 -> Int128
(+) = Int128 -> Int128 -> Int128
plus128
  (-) = Int128 -> Int128 -> Int128
minus128
  * :: Int128 -> Int128 -> Int128
(*) = Int128 -> Int128 -> Int128
times128
  negate :: Int128 -> Int128
negate = Int128 -> Int128
negate128
  abs :: Int128 -> Int128
abs = Int128 -> Int128
abs128
  signum :: Int128 -> Int128
signum = Int128 -> Int128
signum128
  fromInteger :: Integer -> Int128
fromInteger = Integer -> Int128
fromInteger128

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

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

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

  popCount :: Int128 -> Int
popCount = Int128 -> Int
popCount128

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

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

instance Integral Int128 where
  quot :: Int128 -> Int128 -> Int128
quot Int128
n Int128
d = forall a b. (a, b) -> a
fst (Int128 -> Int128 -> (Int128, Int128)
quotRem128 Int128
n Int128
d)
  rem :: Int128 -> Int128 -> Int128
rem Int128
n Int128
d = forall a b. (a, b) -> b
snd (Int128 -> Int128 -> (Int128, Int128)
quotRem128 Int128
n Int128
d)
  div :: Int128 -> Int128 -> Int128
div Int128
n Int128
d = forall a b. (a, b) -> a
fst (Int128 -> Int128 -> (Int128, Int128)
divMod128 Int128
n Int128
d)
  mod :: Int128 -> Int128 -> Int128
mod Int128
n Int128
d = forall a b. (a, b) -> b
snd (Int128 -> Int128 -> (Int128, Int128)
divMod128 Int128
n Int128
d)
  quotRem :: Int128 -> Int128 -> (Int128, Int128)
quotRem = Int128 -> Int128 -> (Int128, Int128)
quotRem128
  divMod :: Int128 -> Int128 -> (Int128, Int128)
divMod = Int128 -> Int128 -> (Int128, Int128)
divMod128
  toInteger :: Int128 -> Integer
toInteger = Int128 -> Integer
toInteger128

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

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

instance Prim Int128 where
  sizeOf# :: Int128 -> Int#
sizeOf#         = Int128 -> Int#
sizeOf128#
  alignment# :: Int128 -> Int#
alignment#      = Int128 -> Int#
alignment128#
  indexByteArray# :: ByteArray# -> Int# -> Int128
indexByteArray# = ByteArray# -> Int# -> Int128
indexByteArray128#
  readByteArray# :: forall s.
MutableByteArray# s -> Int# -> State# s -> (# State# s, Int128 #)
readByteArray#  = forall s.
MutableByteArray# s -> Int# -> State# s -> (# State# s, Int128 #)
readByteArray128#
  writeByteArray# :: forall s.
MutableByteArray# s -> Int# -> Int128 -> State# s -> State# s
writeByteArray# = forall s.
MutableByteArray# s -> Int# -> Int128 -> State# s -> State# s
writeByteArray128#
  setByteArray# :: forall s.
MutableByteArray# s
-> Int# -> Int# -> Int128 -> State# s -> State# s
setByteArray#   = forall s.
MutableByteArray# s
-> Int# -> Int# -> Int128 -> State# s -> State# s
setByteArray128#
  indexOffAddr# :: Addr# -> Int# -> Int128
indexOffAddr#   = Addr# -> Int# -> Int128
indexOffAddr128#
  readOffAddr# :: forall s. Addr# -> Int# -> State# s -> (# State# s, Int128 #)
readOffAddr#    = forall s. Addr# -> Int# -> State# s -> (# State# s, Int128 #)
readOffAddr128#
  writeOffAddr# :: forall s. Addr# -> Int# -> Int128 -> State# s -> State# s
writeOffAddr#   = forall s. Addr# -> Int# -> Int128 -> State# s -> State# s
writeOffAddr128#
  setOffAddr# :: forall s. Addr# -> Int# -> Int# -> Int128 -> State# s -> State# s
setOffAddr#     = forall s. Addr# -> Int# -> Int# -> Int128 -> 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 :: Int -> Int128"     fromIntegral = fromInt
"fromIntegral :: Word -> Int128"    fromIntegral = fromWord
"fromIntegral :: Word32 -> Int128"  fromIntegral = fromWord32
"fromIntegral :: Word64 -> Int128"  fromIntegral = Int128 0

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

{-# INLINE fromInt #-}
fromInt :: Int -> Int128
fromInt :: Int -> Int128
fromInt = Word64 -> Word64 -> Int128
Int128 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 -> Int128
fromWord :: Word -> Int128
fromWord = Word64 -> Word64 -> Int128
Int128 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 -> Int128
fromWord32 :: Word32 -> Int128
fromWord32 = Word64 -> Word64 -> Int128
Int128 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 :: Int128 -> Int
toInt :: Int128 -> Int
toInt (Int128 Word64
_ Word64
w) = forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
w

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

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

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

compare128 :: Int128 -> Int128 -> Ordering
compare128 :: Int128 -> Int128 -> Ordering
compare128 Int128
a Int128
b = forall a. Ord a => a -> a -> Ordering
compare (Int128 -> Integer
toInteger128 Int128
a) (Int128 -> Integer
toInteger128 Int128
b)

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

succ128 :: Int128 -> Int128
succ128 :: Int128 -> Int128
succ128 (Int128 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
== Word64
0x7fffffffffffffff
        then forall a. String -> a
succError String
"Int128"
        else Word64 -> Word64 -> Int128
Int128 (Word64
a1 forall a. Num a => a -> a -> a
+ Word64
1) Word64
0
  | Bool
otherwise = Word64 -> Word64 -> Int128
Int128 Word64
a1 (Word64
a0 forall a. Num a => a -> a -> a
+ Word64
1)


pred128 :: Int128 -> Int128
pred128 :: Int128 -> Int128
pred128 (Int128 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
0x8000000000000000
        then forall a. String -> a
predError String
"Int128"
        else Word64 -> Word64 -> Int128
Int128 (Word64
a1 forall a. Num a => a -> a -> a
- Word64
1) forall a. Bounded a => a
maxBound
  | Bool
otherwise = Word64 -> Word64 -> Int128
Int128 Word64
a1 (Word64
a0 forall a. Num a => a -> a -> a
- Word64
1)


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

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

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

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

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

times128 :: Int128 -> Int128 -> Int128
times128 :: Int128 -> Int128 -> Int128
times128 (Int128 Word64
a1 Word64
a0) (Int128 Word64
b1 Word64
b0) =
  Word64 -> Word64 -> Int128
Int128 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 :: Int128 -> Int128
negate128 :: Int128 -> Int128
negate128 (Int128 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 -> Int128
Int128 (forall a. Bits a => a -> a
complement Word64
a1 forall a. Num a => a -> a -> a
+ Word64
c) Word64
s

{-# INLINABLE abs128 #-}
abs128 :: Int128 -> Int128
abs128 :: Int128 -> Int128
abs128 i :: Int128
i@(Int128 Word64
a1 Word64
_)
  | forall a. Bits a => a -> Int -> Bool
testBit Word64
a1 Int
63 = Int128 -> Int128
negate128 Int128
i
  | Bool
otherwise = Int128
i

{-# INLINABLE signum128 #-}
signum128 :: Int128 -> Int128
signum128 :: Int128 -> Int128
signum128 (Int128 Word64
a1 Word64
a0)
  | Word64
a1 forall a. Eq a => a -> a -> Bool
== Word64
0 Bool -> Bool -> Bool
&& Word64
a0 forall a. Eq a => a -> a -> Bool
== Word64
0 = Int128
zeroInt128
  | forall a. Bits a => a -> Int -> Bool
testBit Word64
a1 Int
63 = Int128
minusOneInt128
  | Bool
otherwise = Int128
oneInt128

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

fromInteger128 :: Integer -> Int128
fromInteger128 :: Integer -> Int128
fromInteger128 Integer
i =
  Word64 -> Word64 -> Int128
Int128 (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 :: Int128 -> Int128 -> Int128
and128 :: Int128 -> Int128 -> Int128
and128 (Int128 Word64
a1 Word64
a0) (Int128 Word64
b1 Word64
b0) =
  Word64 -> Word64 -> Int128
Int128 (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 :: Int128 -> Int128 -> Int128
or128 :: Int128 -> Int128 -> Int128
or128 (Int128 Word64
a1 Word64
a0) (Int128 Word64
b1 Word64
b0) =
  Word64 -> Word64 -> Int128
Int128 (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 :: Int128 -> Int128 -> Int128
xor128 :: Int128 -> Int128 -> Int128
xor128 (Int128 Word64
a1 Word64
a0) (Int128 Word64
b1 Word64
b0) =
  Word64 -> Word64 -> Int128
Int128 (forall a. Bits a => a -> a -> a
xor Word64
a1 Word64
b1) (forall a. Bits a => a -> a -> a
xor Word64
a0 Word64
b0)

-- 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 :: Int128 -> Int -> Int128
shiftL128 :: Int128 -> Int -> Int128
shiftL128 w :: Int128
w@(Int128 Word64
a1 Word64
a0) Int
s
  | Int
s forall a. Eq a => a -> a -> Bool
== Int
0 = Int128
w
  | Int
s forall a. Eq a => a -> a -> Bool
== forall a. Bounded a => a
minBound = Int128
zeroInt128
  | Int
s forall a. Ord a => a -> a -> Bool
< Int
0 = Int128 -> Int -> Int128
shiftR128 Int128
w (forall a. Num a => a -> a
negate Int
s)
  | Int
s forall a. Ord a => a -> a -> Bool
>= Int
128 = Int128
zeroInt128
  | Int
s forall a. Eq a => a -> a -> Bool
== Int
64 = Word64 -> Word64 -> Int128
Int128 Word64
a0 Word64
0
  | Int
s forall a. Ord a => a -> a -> Bool
> Int
64 = Word64 -> Word64 -> Int128
Int128 (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 -> Int128
Int128 (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)) (Word64
a0 forall a. Bits a => a -> Int -> a
`shiftL` Int
s)

{-# INLINABLE shiftR128 #-}
shiftR128 :: Int128 -> Int -> Int128
shiftR128 :: Int128 -> Int -> Int128
shiftR128 i :: Int128
i@(Int128 Word64
a1 Word64
a0) Int
s
  | Int
s forall a. Eq a => a -> a -> Bool
== Int
0 = Int128
i
  | Int
s forall a. Eq a => a -> a -> Bool
== forall a. Bounded a => a
minBound = Int128
zeroInt128
  | Int
s forall a. Ord a => a -> a -> Bool
< Int
0 = Int128 -> Int -> Int128
shiftL128 Int128
i (forall a. Num a => a -> a
negate Int
s)
  | Word64 -> Bool
topBitSetWord64 Word64
a1 = Int128 -> Int128
complement128 (Int128 -> Int -> Int128
shiftR128 (Int128 -> Int128
complement128 Int128
i) Int
s)
  | Int
s forall a. Ord a => a -> a -> Bool
>= Int
128 = Int128
zeroInt128
  | Int
s forall a. Eq a => a -> a -> Bool
== Int
64 = Word64 -> Word64 -> Int128
Int128 Word64
0 Word64
a1
  | Int
s forall a. Ord a => a -> a -> Bool
> Int
64 = Word64 -> Word64 -> Int128
Int128 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 -> Int128
Int128 (Word64
a1 forall a. Bits a => a -> Int -> a
`shiftR` Int
s) (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 :: Int128 -> Int -> Int128
rotateL128 :: Int128 -> Int -> Int128
rotateL128 w :: Int128
w@(Int128 Word64
a1 Word64
a0) Int
r
  | Int
r forall a. Ord a => a -> a -> Bool
< Int
0 = Int128 -> Int -> Int128
rotateL128 Int128
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. Eq a => a -> a -> Bool
== Int
0 = Int128
w
  | Int
r forall a. Ord a => a -> a -> Bool
>= Int
128 = Int128 -> Int -> Int128
rotateL128 Int128
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 -> Int128
Int128 Word64
a0 Word64
a1
  | Int
r forall a. Ord a => a -> a -> Bool
> Int
64 = Int128 -> Int -> Int128
rotateL128 (Word64 -> Word64 -> Int128
Int128 Word64
a0 Word64
a1) (Int
r forall a. Integral a => a -> a -> a
`mod` Int
64)
  | Bool
otherwise =
      Word64 -> Word64 -> Int128
Int128 (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)) (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))

{-# INLINABLE rotateR128 #-}
rotateR128 :: Int128 -> Int -> Int128
rotateR128 :: Int128 -> Int -> Int128
rotateR128 w :: Int128
w@(Int128 Word64
a1 Word64
a0) Int
r
  | Int
r forall a. Ord a => a -> a -> Bool
< Int
0 = Int128 -> Int -> Int128
rotateR128 Int128
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. Eq a => a -> a -> Bool
== Int
0 = Int128
w
  | Int
r forall a. Ord a => a -> a -> Bool
>= Int
128 = Int128 -> Int -> Int128
rotateR128 Int128
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 -> Int128
Int128 Word64
a0 Word64
a1
  | Int
r forall a. Ord a => a -> a -> Bool
> Int
64 = Int128 -> Int -> Int128
rotateR128 (Word64 -> Word64 -> Int128
Int128 Word64
a0 Word64
a1) (Int
r forall a. Integral a => a -> a -> a
`mod` Int
64)
  | Bool
otherwise =
      Word64 -> Word64 -> Int128
Int128 (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)) (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))

{-# INLINABLE testBit128 #-}
testBit128 :: Int128 -> Int -> Bool
testBit128 :: Int128 -> Int -> Bool
testBit128 (Int128 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 -> Int128
bit128 :: Int -> Int128
bit128 Int
indx
  | Int
indx forall a. Ord a => a -> a -> Bool
< Int
0 = Int128
zeroInt128
  | Int
indx forall a. Ord a => a -> a -> Bool
>= Int
128 = Int128
zeroInt128
  | Bool
otherwise = Int128 -> Int -> Int128
shiftL128 Int128
oneInt128 Int
indx

{-# INLINABLE popCount128 #-}
popCount128 :: Int128 -> Int
popCount128 :: Int128 -> Int
popCount128 (Int128 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.

countLeadingZeros128 :: Int128 -> Int
countLeadingZeros128 :: Int128 -> Int
countLeadingZeros128 (Int128 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

countTrailingZeros128 :: Int128 -> Int
countTrailingZeros128 :: Int128 -> Int
countTrailingZeros128 (Int128 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 :: Int128 -> Int128 -> (Int128, Int128)
quotRem128 :: Int128 -> Int128 -> (Int128, Int128)
quotRem128 Int128
numer Int128
denom
  | Int128 -> Bool
isNeg Int128
numer Bool -> Bool -> Bool
&& Int128 -> Bool
isNeg Int128
denom = (Word128 -> Int128
word128ToInt128 Word128
wq, Word128 -> Int128
word128ToInt128 (forall a. Num a => a -> a
negate Word128
wr))
  | Int128 -> Bool
isNeg Int128
numer = (Word128 -> Int128
word128ToInt128 (forall a. Num a => a -> a
negate Word128
wq), Word128 -> Int128
word128ToInt128 (forall a. Num a => a -> a
negate Word128
wr))
  | Int128 -> Bool
isNeg Int128
denom = (Word128 -> Int128
word128ToInt128 (forall a. Num a => a -> a
negate Word128
wq), Word128 -> Int128
word128ToInt128 Word128
wr)
  | Bool
otherwise = (Word128 -> Int128
word128ToInt128 Word128
wq, Word128 -> Int128
word128ToInt128 Word128
wr)
  where
    (Word128
wq, Word128
wr) = forall a. Integral a => a -> a -> (a, a)
quotRem Word128
absNumerW Word128
absDenomW
    absNumerW :: Word128
absNumerW = Int128 -> Word128
int128ToWord128 forall a b. (a -> b) -> a -> b
$ Int128 -> Int128
abs128 Int128
numer
    absDenomW :: Word128
absDenomW = Int128 -> Word128
int128ToWord128 forall a b. (a -> b) -> a -> b
$ Int128 -> Int128
abs128 Int128
denom
    isNeg :: Int128 -> Bool
isNeg = Word64 -> Bool
topBitSetWord64 forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int128 -> Word64
int128Hi64


divMod128 :: Int128 -> Int128 -> (Int128, Int128)
divMod128 :: Int128 -> Int128 -> (Int128, Int128)
divMod128 Int128
numer Int128
denom
  | Int128 -> Bool
isNeg Int128
numer Bool -> Bool -> Bool
&& Int128 -> Bool
isNeg Int128
denom = (Word128 -> Int128
word128ToInt128 Word128
wq, Word128 -> Int128
word128ToInt128 (forall a. Num a => a -> a
negate Word128
wr))
  | Int128 -> Bool
isNeg Int128
numer Bool -> Bool -> Bool
&& Word128
wr forall a. Eq a => a -> a -> Bool
== Word128
0 = (Word128 -> Int128
word128ToInt128 (forall a. Num a => a -> a
negate Word128
wq), Int128
0)
  | Int128 -> Bool
isNeg Int128
numer = (Word128 -> Int128
word128ToInt128 (forall a. Num a => a -> a
negate forall a b. (a -> b) -> a -> b
$ Word128
wq forall a. Num a => a -> a -> a
+ Word128
1), Word128 -> Int128
word128ToInt128 (Word128
absDenomW forall a. Num a => a -> a -> a
- Word128
wr))
  | Int128 -> Bool
isNeg Int128
denom Bool -> Bool -> Bool
&& Word128
wr forall a. Eq a => a -> a -> Bool
== Word128
0 = (Word128 -> Int128
word128ToInt128 (forall a. Num a => a -> a
negate Word128
wq), Int128
0)
  | Int128 -> Bool
isNeg Int128
denom = (Word128 -> Int128
word128ToInt128 (forall a. Num a => a -> a
negate forall a b. (a -> b) -> a -> b
$ Word128
wq forall a. Num a => a -> a -> a
+ Word128
1), Word128 -> Int128
word128ToInt128 (forall a. Num a => a -> a
negate forall a b. (a -> b) -> a -> b
$ Word128
absDenomW forall a. Num a => a -> a -> a
- Word128
wr))
  | Bool
otherwise = (Word128 -> Int128
word128ToInt128 Word128
wq, Word128 -> Int128
word128ToInt128 Word128
wr)
  where
    (Word128
wq, Word128
wr) = forall a. Integral a => a -> a -> (a, a)
quotRem Word128
absNumerW Word128
absDenomW
    isNeg :: Int128 -> Bool
isNeg = Word64 -> Bool
topBitSetWord64 forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int128 -> Word64
int128Hi64
    absNumerW :: Word128
absNumerW = Int128 -> Word128
int128ToWord128 forall a b. (a -> b) -> a -> b
$ Int128 -> Int128
abs128 Int128
numer
    absDenomW :: Word128
absDenomW = Int128 -> Word128
int128ToWord128 forall a b. (a -> b) -> a -> b
$ Int128 -> Int128
abs128 Int128
denom


toInteger128 :: Int128 -> Integer
toInteger128 :: Int128 -> Integer
toInteger128 i :: Int128
i@(Int128 Word64
a1 Word64
a0)
  | forall a. Bits a => a -> Int
popCount Word64
a1 forall a. Eq a => a -> a -> Bool
== Int
64 Bool -> Bool -> Bool
&& forall a. Bits a => a -> Int
popCount Word64
a0 forall a. Eq a => a -> a -> Bool
== Int
64 = -Integer
1
  | Bool -> Bool
not (forall a. Bits a => a -> Int -> Bool
testBit Word64
a1 Int
63) = 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
  | Bool
otherwise =
      case Int128 -> Int128
negate128 Int128
i of
        Int128 Word64
n1 Word64
n0 -> forall a. Num a => a -> a
negate (forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
n1 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
n0)

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

peek128 :: Ptr Int128 -> IO Int128
peek128 :: Ptr Int128 -> IO Int128
peek128 Ptr Int128
ptr =
  Word64 -> Word64 -> Int128
Int128 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 Int128
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 Int128
ptr) Int
index0

peekElemOff128 :: Ptr Int128 -> Int -> IO Int128
peekElemOff128 :: Ptr Int128 -> Int -> IO Int128
peekElemOff128 Ptr Int128
ptr Int
idx =
  Word64 -> Word64 -> Int128
Int128 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 Int128
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 Int128
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 Int128 -> Int128 -> IO ()
poke128 :: Ptr Int128 -> Int128 -> IO ()
poke128 Ptr Int128
ptr (Int128 Word64
a1 Word64
a0) =
  forall a. Storable a => Ptr a -> Int -> a -> IO ()
pokeElemOff (forall a b. Ptr a -> Ptr b
castPtr Ptr Int128
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 Int128
ptr) Int
index0 Word64
a0

pokeElemOff128 :: Ptr Int128 -> Int -> Int128 -> IO ()
pokeElemOff128 :: Ptr Int128 -> Int -> Int128 -> IO ()
pokeElemOff128 Ptr Int128
ptr Int
idx (Int128 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 Int128
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 Int128
ptr) (Int
idx2 forall a. Num a => a -> a -> a
+ Int
index1) Word64
a1

-- -----------------------------------------------------------------------------
-- Helpers.

{-# INLINE int128ToWord128 #-}
int128ToWord128 :: Int128 -> Word128
int128ToWord128 :: Int128 -> Word128
int128ToWord128 (Int128 Word64
a1 Word64
a0) = Word64 -> Word64 -> Word128
Word128 Word64
a1 Word64
a0

{-# INLINE topBitSetWord64 #-}
topBitSetWord64 :: Word64 -> Bool
topBitSetWord64 :: Word64 -> Bool
topBitSetWord64 Word64
w = forall a. Bits a => a -> Int -> Bool
testBit Word64
w Int
63

{-# INLINE word128ToInt128 #-}
word128ToInt128 :: Word128 -> Int128
word128ToInt128 :: Word128 -> Int128
word128ToInt128 (Word128 Word64
a1 Word64
a0) = Word64 -> Word64 -> Int128
Int128 Word64
a1 Word64
a0

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

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

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

{-# INLINE indexByteArray128# #-}
indexByteArray128# :: ByteArray# -> Int# -> Int128
indexByteArray128# :: ByteArray# -> Int# -> Int128
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 -> Int128
Int128 Word64
x Word64
y

{-# INLINE readByteArray128# #-}
readByteArray128# :: MutableByteArray# s -> Int# -> State# s -> (# State# s, Int128 #)
readByteArray128# :: forall s.
MutableByteArray# s -> Int# -> State# s -> (# State# s, Int128 #)
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 -> Int128
Int128 Word64
x Word64
y #)
  where i2# :: Int#
i2# = Int#
2# Int# -> Int# -> Int#
*# Int#
i#

{-# INLINE writeByteArray128# #-}
writeByteArray128# :: MutableByteArray# s -> Int# -> Int128 -> State# s -> State# s
writeByteArray128# :: forall s.
MutableByteArray# s -> Int# -> Int128 -> State# s -> State# s
writeByteArray128# MutableByteArray# s
arr# Int#
i# (Int128 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# -> Int128 -> State# s -> State# s
setByteArray128# :: forall s.
MutableByteArray# s
-> Int# -> Int# -> Int128 -> 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# -> Int128
indexOffAddr128# :: Addr# -> Int# -> Int128
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 -> Int128
Int128 Word64
x Word64
y

{-# INLINE readOffAddr128# #-}
readOffAddr128# :: Addr# -> Int# -> State# s -> (# State# s, Int128 #)
readOffAddr128# :: forall s. Addr# -> Int# -> State# s -> (# State# s, Int128 #)
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 -> Int128
Int128 Word64
x Word64
y #)
  where i2# :: Int#
i2# = Int#
2# Int# -> Int# -> Int#
*# Int#
i#

{-# INLINE writeOffAddr128# #-}
writeOffAddr128# :: Addr# -> Int# -> Int128 -> State# s -> State# s
writeOffAddr128# :: forall s. Addr# -> Int# -> Int128 -> State# s -> State# s
writeOffAddr128# Addr#
addr# Int#
i# (Int128 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# -> Int128 -> State# s -> State# s
setOffAddr128# :: forall s. Addr# -> Int# -> Int# -> Int128 -> State# s -> State# s
setOffAddr128# = forall a s.
Prim a =>
Addr# -> Int# -> Int# -> a -> State# s -> State# s
defaultSetOffAddr#

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

zeroInt128 :: Int128
zeroInt128 :: Int128
zeroInt128 = Word64 -> Word64 -> Int128
Int128 Word64
0 Word64
0

oneInt128 :: Int128
oneInt128 :: Int128
oneInt128 = Word64 -> Word64 -> Int128
Int128 Word64
0 Word64
1

minusOneInt128 :: Int128
minusOneInt128 :: Int128
minusOneInt128 = Word64 -> Word64 -> Int128
Int128 forall a. Bounded a => a
maxBound forall a. Bounded a => a
maxBound

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

index0, index1 :: Int
#if WORDS_BIGENDIAN
index0 = 1
index1 = 0
#else
index0 :: Int
index0 = Int
0
index1 :: Int
index1 = Int
1
#endif