-- | Conversions from 'Int64'.
module Unwitch.Convert.Int64
  ( -- * Conversions
    toInt8
  , toInt16
  , toInt32
  , toInt
  , toInteger
  , toWord8
  , toWord16
  , toWord32
  , toWord64
  , toWord
  , toNatural
  , toFloat
  , toDouble
#ifdef __GLASGOW_HASKELL__
  , toCInt
#endif
#ifdef __GLASGOW_HASKELL__
  -- * Unboxed conversions
  -- $unboxed
  , toInt8#
  , toInt16#
  , toInt32#
  , toInt#
  , toWord8#
  , toWord16#
  , toWord32#
  , toWord64#
  , toWord#
  , toNatural#
  , toFloat#
  , toDouble#
#endif
  )
where

import           Unwitch.Errors
import           Unwitch.Constant
import qualified Data.Bits as Bits
import           Data.Word
import           Data.Int
import           Numeric.Natural (Natural)
import           Prelude hiding (toInteger)
#ifdef __GLASGOW_HASKELL__
import           Foreign.C.Types (CInt(CInt))
import           GHC.Exts (Int(..), Word(..), Float(..), Double(..),
                           int64ToInt#, intToInt64#,
                           intToInt8#, int8ToInt#,
                           intToInt16#, int16ToInt#,
                           intToInt32#, int32ToInt#,
                           int2Word#, word2Int#,
                           wordToWord8#, word8ToWord#,
                           wordToWord16#, word16ToWord#,
                           wordToWord32#, word32ToWord#,
                           wordToWord64#,
                           int2Float#, int2Double#,
                           (<#), (>#),
                           eqInt64#, geInt64#)
import           GHC.Int (Int8(..), Int16(..), Int32(..), Int64(..))
import           GHC.Word (Word8(..), Word16(..), Word32(..), Word64(..))
import           GHC.Num.Natural (Natural(NS))
#endif

#ifdef __GLASGOW_HASKELL__
-- $unboxed
-- These use GHC unboxed types and unboxed sums for zero-allocation
-- failure handling. Requires the @MagicHash@, @UnboxedSums@ and
-- @UnboxedTuples@ language extensions.
-- See the <https://downloads.haskell.org/ghc/latest/docs/users_guide/exts/primitives.html GHC manual on unboxed types>.
#endif

toInt8 :: Int64 -> Maybe Int8
toInt8 :: Int64 -> Maybe Int8
toInt8 = Int64 -> Maybe Int8
forall a b.
(Integral a, Integral b, Bits a, Bits b) =>
a -> Maybe b
Bits.toIntegralSized

toInt16 :: Int64 -> Maybe Int16
toInt16 :: Int64 -> Maybe Int16
toInt16 = Int64 -> Maybe Int16
forall a b.
(Integral a, Integral b, Bits a, Bits b) =>
a -> Maybe b
Bits.toIntegralSized

toInt32 :: Int64 -> Maybe Int32
toInt32 :: Int64 -> Maybe Int32
toInt32 = Int64 -> Maybe Int32
forall a b.
(Integral a, Integral b, Bits a, Bits b) =>
a -> Maybe b
Bits.toIntegralSized

toInt :: Int64 -> Maybe Int
toInt :: Int64 -> Maybe Int
toInt = Int64 -> Maybe Int
forall a b.
(Integral a, Integral b, Bits a, Bits b) =>
a -> Maybe b
Bits.toIntegralSized

#ifdef __GLASGOW_HASKELL__
-- | Narrowing conversion via Int32, fails if outside Int32 range.
toCInt :: Int64 -> Maybe CInt
toCInt :: Int64 -> Maybe CInt
toCInt Int64
x = Int32 -> CInt
CInt (Int32 -> CInt) -> Maybe Int32 -> Maybe CInt
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int64 -> Maybe Int32
toInt32 Int64
x
#endif

toInteger :: Int64 -> Integer
toInteger :: Int64 -> Integer
toInteger = Int64 -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral

toWord8 :: Int64 -> Maybe Word8
toWord8 :: Int64 -> Maybe Word8
toWord8 = Int64 -> Maybe Word8
forall a b.
(Integral a, Integral b, Bits a, Bits b) =>
a -> Maybe b
Bits.toIntegralSized

toWord16 :: Int64 -> Maybe Word16
toWord16 :: Int64 -> Maybe Word16
toWord16 = Int64 -> Maybe Word16
forall a b.
(Integral a, Integral b, Bits a, Bits b) =>
a -> Maybe b
Bits.toIntegralSized

toWord32 :: Int64 -> Maybe Word32
toWord32 :: Int64 -> Maybe Word32
toWord32 = Int64 -> Maybe Word32
forall a b.
(Integral a, Integral b, Bits a, Bits b) =>
a -> Maybe b
Bits.toIntegralSized

toWord64 :: Int64 -> Maybe Word64
toWord64 :: Int64 -> Maybe Word64
toWord64 = Int64 -> Maybe Word64
forall a b.
(Integral a, Integral b, Bits a, Bits b) =>
a -> Maybe b
Bits.toIntegralSized

toWord :: Int64 -> Maybe Word
toWord :: Int64 -> Maybe Word
toWord = Int64 -> Maybe Word
forall a b.
(Integral a, Integral b, Bits a, Bits b) =>
a -> Maybe b
Bits.toIntegralSized

-- | Signed-to-unsigned conversion, returns 'Left' 'Underflow' for negative values.
toNatural :: Int64 -> Either Overflows Natural
toNatural :: Int64 -> Either Overflows Natural
toNatural Int64
x = if
  | Int64
x Int64 -> Int64 -> Bool
forall a. Ord a => a -> a -> Bool
< Int64
0     -> Overflows -> Either Overflows Natural
forall a b. a -> Either a b
Left Overflows
Underflow
  | Bool
otherwise  -> Natural -> Either Overflows Natural
forall a b. b -> Either a b
Right (Natural -> Either Overflows Natural)
-> Natural -> Either Overflows Natural
forall a b. (a -> b) -> a -> b
$ Int64 -> Natural
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int64
x

-- | Checked conversion, fails if outside exact float integer range (+-16777215).
toFloat :: Int64 -> Either Overflows Float
toFloat :: Int64 -> Either Overflows Float
toFloat Int64
x = if
  | Int64
x Int64 -> Int64 -> Bool
forall a. Ord a => a -> a -> Bool
< -Int64
forall a. Num a => a
maxIntegralRepFloat -> Overflows -> Either Overflows Float
forall a b. a -> Either a b
Left Overflows
Underflow
  | Int64
x Int64 -> Int64 -> Bool
forall a. Ord a => a -> a -> Bool
> Int64
forall a. Num a => a
maxIntegralRepFloat  -> Overflows -> Either Overflows Float
forall a b. a -> Either a b
Left Overflows
Overflow
  | Bool
otherwise                -> Float -> Either Overflows Float
forall a b. b -> Either a b
Right (Float -> Either Overflows Float)
-> Float -> Either Overflows Float
forall a b. (a -> b) -> a -> b
$ Int64 -> Float
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int64
x

-- | Checked conversion, fails if outside exact double integer range (+-9007199254740991).
toDouble :: Int64 -> Either Overflows Double
toDouble :: Int64 -> Either Overflows Double
toDouble Int64
x = if
  | Int64
x Int64 -> Int64 -> Bool
forall a. Ord a => a -> a -> Bool
< -Int64
forall a. Num a => a
maxIntegralRepDouble -> Overflows -> Either Overflows Double
forall a b. a -> Either a b
Left Overflows
Underflow
  | Int64
x Int64 -> Int64 -> Bool
forall a. Ord a => a -> a -> Bool
> Int64
forall a. Num a => a
maxIntegralRepDouble  -> Overflows -> Either Overflows Double
forall a b. a -> Either a b
Left Overflows
Overflow
  | Bool
otherwise                 -> Double -> Either Overflows Double
forall a b. b -> Either a b
Right (Double -> Either Overflows Double)
-> Double -> Either Overflows Double
forall a b. (a -> b) -> a -> b
$ Int64 -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int64
x

#ifdef __GLASGOW_HASKELL__
-- | Narrow through Int#, compare at Int64#
toInt8# :: Int64 -> (# Int8 | (# #) #)
toInt8# :: Int64 -> (# Int8 | (# #) #)
toInt8# (I64# Int64#
x64#) =
  let i# :: Int#
i# = Int64# -> Int#
int64ToInt# Int64#
x64#
      n# :: Int8#
n# = Int# -> Int8#
intToInt8# Int#
i#
  in case Int64# -> Int64# -> Int#
eqInt64# (Int# -> Int64#
intToInt64# (Int8# -> Int#
int8ToInt# Int8#
n#)) Int64#
x64# of
    Int#
1# -> (# Int8# -> Int8
I8# Int8#
n# | #)
    Int#
_  -> (# | (# #) #)

-- | Signed narrowing via Int64# comparison
toInt16# :: Int64 -> (# Int16 | (# #) #)
toInt16# :: Int64 -> (# Int16 | (# #) #)
toInt16# (I64# Int64#
x64#) =
  let i# :: Int#
i# = Int64# -> Int#
int64ToInt# Int64#
x64#
      n# :: Int16#
n# = Int# -> Int16#
intToInt16# Int#
i#
  in case Int64# -> Int64# -> Int#
eqInt64# (Int# -> Int64#
intToInt64# (Int16# -> Int#
int16ToInt# Int16#
n#)) Int64#
x64# of
    Int#
1# -> (# Int16# -> Int16
I16# Int16#
n# | #)
    Int#
_  -> (# | (# #) #)

-- | Signed narrowing via Int64# comparison
toInt32# :: Int64 -> (# Int32 | (# #) #)
toInt32# :: Int64 -> (# Int32 | (# #) #)
toInt32# (I64# Int64#
x64#) =
  let i# :: Int#
i# = Int64# -> Int#
int64ToInt# Int64#
x64#
      n# :: Int32#
n# = Int# -> Int32#
intToInt32# Int#
i#
  in case Int64# -> Int64# -> Int#
eqInt64# (Int# -> Int64#
intToInt64# (Int32# -> Int#
int32ToInt# Int32#
n#)) Int64#
x64# of
    Int#
1# -> (# Int32# -> Int32
I32# Int32#
n# | #)
    Int#
_  -> (# | (# #) #)

-- | Roundtrip check at Int64# level for platform safety
toInt# :: Int64 -> (# Int | (# #) #)
toInt# :: Int64 -> (# Int | (# #) #)
toInt# (I64# Int64#
x64#) =
  let i# :: Int#
i# = Int64# -> Int#
int64ToInt# Int64#
x64#
  in case Int64# -> Int64# -> Int#
eqInt64# (Int# -> Int64#
intToInt64# Int#
i#) Int64#
x64# of
    Int#
1# -> (# Int# -> Int
I# Int#
i# | #)
    Int#
_  -> (# | (# #) #)

-- | Signed->unsigned narrow via Int64# comparison
toWord8# :: Int64 -> (# Word8 | (# #) #)
toWord8# :: Int64 -> (# Word8 | (# #) #)
toWord8# (I64# Int64#
x64#) =
  let i# :: Int#
i# = Int64# -> Int#
int64ToInt# Int64#
x64#
      n# :: Word8#
n# = Word# -> Word8#
wordToWord8# (Int# -> Word#
int2Word# Int#
i#)
  in case Int64# -> Int64# -> Int#
eqInt64# (Int# -> Int64#
intToInt64# (Word# -> Int#
word2Int# (Word8# -> Word#
word8ToWord# Word8#
n#))) Int64#
x64# of
    Int#
1# -> (# Word8# -> Word8
W8# Word8#
n# | #)
    Int#
_  -> (# | (# #) #)

-- | Signed->unsigned narrow via Int64# comparison
toWord16# :: Int64 -> (# Word16 | (# #) #)
toWord16# :: Int64 -> (# Word16 | (# #) #)
toWord16# (I64# Int64#
x64#) =
  let i# :: Int#
i# = Int64# -> Int#
int64ToInt# Int64#
x64#
      n# :: Word16#
n# = Word# -> Word16#
wordToWord16# (Int# -> Word#
int2Word# Int#
i#)
  in case Int64# -> Int64# -> Int#
eqInt64# (Int# -> Int64#
intToInt64# (Word# -> Int#
word2Int# (Word16# -> Word#
word16ToWord# Word16#
n#))) Int64#
x64# of
    Int#
1# -> (# Word16# -> Word16
W16# Word16#
n# | #)
    Int#
_  -> (# | (# #) #)

-- | Signed->unsigned narrow via Int64# comparison
toWord32# :: Int64 -> (# Word32 | (# #) #)
toWord32# :: Int64 -> (# Word32 | (# #) #)
toWord32# (I64# Int64#
x64#) =
  let i# :: Int#
i# = Int64# -> Int#
int64ToInt# Int64#
x64#
      n# :: Word32#
n# = Word# -> Word32#
wordToWord32# (Int# -> Word#
int2Word# Int#
i#)
  in case Int64# -> Int64# -> Int#
eqInt64# (Int# -> Int64#
intToInt64# (Word# -> Int#
word2Int# (Word32# -> Word#
word32ToWord# Word32#
n#))) Int64#
x64# of
    Int#
1# -> (# Word32# -> Word32
W32# Word32#
n# | #)
    Int#
_  -> (# | (# #) #)

-- | Signed->unsigned, check non-negative at Int64# level
toWord64# :: Int64 -> (# Word64 | (# #) #)
toWord64# :: Int64 -> (# Word64 | (# #) #)
toWord64# (I64# Int64#
x64#) = case Int64# -> Int64# -> Int#
geInt64# Int64#
x64# (Int# -> Int64#
intToInt64# Int#
0#) of
  Int#
1# -> (# Word64# -> Word64
W64# (Word# -> Word64#
wordToWord64# (Int# -> Word#
int2Word# (Int64# -> Int#
int64ToInt# Int64#
x64#))) | #)
  Int#
_  -> (# | (# #) #)

-- | Signed->unsigned, check non-negative at Int64# then roundtrip
toWord# :: Int64 -> (# Word | (# #) #)
toWord# :: Int64 -> (# Word | (# #) #)
toWord# (I64# Int64#
x64#) = case Int64# -> Int64# -> Int#
geInt64# Int64#
x64# (Int# -> Int64#
intToInt64# Int#
0#) of
  Int#
1# -> let i# :: Int#
i# = Int64# -> Int#
int64ToInt# Int64#
x64#
         in case Int64# -> Int64# -> Int#
eqInt64# (Int# -> Int64#
intToInt64# Int#
i#) Int64#
x64# of
           Int#
1# -> (# Word# -> Word
W# (Int# -> Word#
int2Word# Int#
i#) | #)
           Int#
_  -> (# | (# #) #)
  Int#
_  -> (# | (# #) #)

-- | Check non-negative at Int64# level, construct NS
toNatural# :: Int64 -> (# Overflows | Natural #)
toNatural# :: Int64 -> (# Overflows | Natural #)
toNatural# (I64# Int64#
x64#) = case Int64# -> Int64# -> Int#
geInt64# Int64#
x64# (Int# -> Int64#
intToInt64# Int#
0#) of
  Int#
1# -> let i# :: Int#
i# = Int64# -> Int#
int64ToInt# Int64#
x64#
         in case Int64# -> Int64# -> Int#
eqInt64# (Int# -> Int64#
intToInt64# Int#
i#) Int64#
x64# of
           Int#
1# -> (# | Word# -> Natural
NS (Int# -> Word#
int2Word# Int#
i#) #)
           Int#
_  -> (# Overflows
Overflow | #)
  Int#
_  -> (# Overflows
Underflow | #)

-- | Bounds-checked float conversion via Int#
toFloat# :: Int64 -> (# Overflows | Float #)
toFloat# :: Int64 -> (# Overflows | Float #)
toFloat# (I64# Int64#
x64#) =
  let i# :: Int#
i# = Int64# -> Int#
int64ToInt# Int64#
x64#
  in case Int64# -> Int64# -> Int#
eqInt64# (Int# -> Int64#
intToInt64# Int#
i#) Int64#
x64# of
    Int#
1# -> case Int#
i# Int# -> Int# -> Int#
<# Int#
-16777215# of
      Int#
1# -> (# Overflows
Underflow | #)
      Int#
_  -> case Int#
i# Int# -> Int# -> Int#
># Int#
16777215# of
        Int#
1# -> (# Overflows
Overflow | #)
        Int#
_  -> (# | Float# -> Float
F# (Int# -> Float#
int2Float# Int#
i#) #)
    Int#
_  -> case Int64# -> Int64# -> Int#
geInt64# Int64#
x64# (Int# -> Int64#
intToInt64# Int#
0#) of
      Int#
1# -> (# Overflows
Overflow | #)
      Int#
_  -> (# Overflows
Underflow | #)

-- | Bounds-checked double conversion via Int#
toDouble# :: Int64 -> (# Overflows | Double #)
toDouble# :: Int64 -> (# Overflows | Double #)
toDouble# (I64# Int64#
x64#) =
  let i# :: Int#
i# = Int64# -> Int#
int64ToInt# Int64#
x64#
  in case Int64# -> Int64# -> Int#
eqInt64# (Int# -> Int64#
intToInt64# Int#
i#) Int64#
x64# of
    Int#
1# -> case Int#
i# Int# -> Int# -> Int#
<# Int#
-9007199254740991# of
      Int#
1# -> (# Overflows
Underflow | #)
      Int#
_  -> case Int#
i# Int# -> Int# -> Int#
># Int#
9007199254740991# of
        Int#
1# -> (# Overflows
Overflow | #)
        Int#
_  -> (# | Double# -> Double
D# (Int# -> Double#
int2Double# Int#
i#) #)
    Int#
_  -> case Int64# -> Int64# -> Int#
geInt64# Int64#
x64# (Int# -> Int64#
intToInt64# Int#
0#) of
      Int#
1# -> (# Overflows
Overflow | #)
      Int#
_  -> (# Overflows
Underflow | #)
#endif