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

import           Unwitch.Errors
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 (Word(..), int16ToInt#, intToInt8#, int8ToInt#,
                           int2Word#, word2Int#,
                           wordToWord8#, word8ToWord#,
                           wordToWord16#, wordToWord32#, wordToWord64#,
                           (==#), (>=#))
import           GHC.Int (Int8(..), Int16(..))
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 :: Int16 -> Maybe Int8
toInt8 :: Int16 -> Maybe Int8
toInt8 = Int16 -> Maybe Int8
forall a b.
(Integral a, Integral b, Bits a, Bits b) =>
a -> Maybe b
Bits.toIntegralSized

toInt32 :: Int16 -> Int32
toInt32 :: Int16 -> Int32
toInt32 = Int16 -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral

toInt64 :: Int16 -> Int64
toInt64 :: Int16 -> Int64
toInt64 = Int16 -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral

toInt :: Int16 -> Int
toInt :: Int16 -> Int
toInt = Int16 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral

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

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

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

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

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

toWord :: Int16 -> Maybe Word
toWord :: Int16 -> Maybe Word
toWord = Int16 -> 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 :: Int16 -> Either Overflows Natural
toNatural :: Int16 -> Either Overflows Natural
toNatural Int16
x = if
  | Int16
x Int16 -> Int16 -> Bool
forall a. Ord a => a -> a -> Bool
< Int16
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
$ Int16 -> Natural
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int16
x

#ifdef __GLASGOW_HASKELL__
-- | Widening conversion via Int32, always succeeds.
toCInt :: Int16 -> CInt
toCInt :: Int16 -> CInt
toCInt Int16
x = Int32 -> CInt
CInt (Int32 -> CInt) -> Int32 -> CInt
forall a b. (a -> b) -> a -> b
$ Int16 -> Int32
toInt32 Int16
x
#endif

toFloat :: Int16 -> Float
toFloat :: Int16 -> Float
toFloat = Int16 -> Float
forall a b. (Integral a, Num b) => a -> b
fromIntegral

toDouble :: Int16 -> Double
toDouble :: Int16 -> Double
toDouble = Int16 -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral

#ifdef __GLASGOW_HASKELL__
-- | Signed narrowing, roundtrip at Int#
toInt8# :: Int16 -> (# Int8 | (# #) #)
toInt8# :: Int16 -> (# Int8 | (# #) #)
toInt8# (I16# Int16#
x16#) =
  let i# :: Int#
i# = Int16# -> Int#
int16ToInt# Int16#
x16#
      n# :: Int8#
n# = Int# -> Int8#
intToInt8# Int#
i#
  in case Int8# -> Int#
int8ToInt# Int8#
n# Int# -> Int# -> Int#
==# Int#
i# of
    Int#
1# -> (# Int8# -> Int8
I8# Int8#
n# | #)
    Int#
_  -> (# | (# #) #)

-- | Signed->unsigned narrow, roundtrip via Word# back to Int#
toWord8# :: Int16 -> (# Word8 | (# #) #)
toWord8# :: Int16 -> (# Word8 | (# #) #)
toWord8# (I16# Int16#
x16#) =
  let i# :: Int#
i# = Int16# -> Int#
int16ToInt# Int16#
x16#
      n# :: Word8#
n# = Word# -> Word8#
wordToWord8# (Int# -> Word#
int2Word# Int#
i#)
  in case Word# -> Int#
word2Int# (Word8# -> Word#
word8ToWord# Word8#
n#) Int# -> Int# -> Int#
==# Int#
i# of
    Int#
1# -> (# Word8# -> Word8
W8# Word8#
n# | #)
    Int#
_  -> (# | (# #) #)

-- | Signed->unsigned, check non-negative
toWord16# :: Int16 -> (# Word16 | (# #) #)
toWord16# :: Int16 -> (# Word16 | (# #) #)
toWord16# (I16# Int16#
x16#) = case Int16# -> Int#
int16ToInt# Int16#
x16# Int# -> Int# -> Int#
>=# Int#
0# of
  Int#
1# -> (# Word16# -> Word16
W16# (Word# -> Word16#
wordToWord16# (Int# -> Word#
int2Word# (Int16# -> Int#
int16ToInt# Int16#
x16#))) | #)
  Int#
_  -> (# | (# #) #)

-- | Signed->unsigned, check non-negative
toWord32# :: Int16 -> (# Word32 | (# #) #)
toWord32# :: Int16 -> (# Word32 | (# #) #)
toWord32# (I16# Int16#
x16#) = case Int16# -> Int#
int16ToInt# Int16#
x16# Int# -> Int# -> Int#
>=# Int#
0# of
  Int#
1# -> (# Word32# -> Word32
W32# (Word# -> Word32#
wordToWord32# (Int# -> Word#
int2Word# (Int16# -> Int#
int16ToInt# Int16#
x16#))) | #)
  Int#
_  -> (# | (# #) #)

-- | Signed->unsigned, check non-negative
toWord64# :: Int16 -> (# Word64 | (# #) #)
toWord64# :: Int16 -> (# Word64 | (# #) #)
toWord64# (I16# Int16#
x16#) = case Int16# -> Int#
int16ToInt# Int16#
x16# Int# -> Int# -> Int#
>=# Int#
0# of
  Int#
1# -> (# Word64# -> Word64
W64# (Word# -> Word64#
wordToWord64# (Int# -> Word#
int2Word# (Int16# -> Int#
int16ToInt# Int16#
x16#))) | #)
  Int#
_  -> (# | (# #) #)

-- | Signed->unsigned, check non-negative
toWord# :: Int16 -> (# Word | (# #) #)
toWord# :: Int16 -> (# Word | (# #) #)
toWord# (I16# Int16#
x16#) = case Int16# -> Int#
int16ToInt# Int16#
x16# Int# -> Int# -> Int#
>=# Int#
0# of
  Int#
1# -> (# Word# -> Word
W# (Int# -> Word#
int2Word# (Int16# -> Int#
int16ToInt# Int16#
x16#)) | #)
  Int#
_  -> (# | (# #) #)

-- | Check non-negative, construct NS directly
toNatural# :: Int16 -> (# Overflows | Natural #)
toNatural# :: Int16 -> (# Overflows | Natural #)
toNatural# (I16# Int16#
x16#) = case Int16# -> Int#
int16ToInt# Int16#
x16# Int# -> Int# -> Int#
>=# Int#
0# of
  Int#
1# -> (# | Word# -> Natural
NS (Int# -> Word#
int2Word# (Int16# -> Int#
int16ToInt# Int16#
x16#)) #)
  Int#
_  -> (# Overflows
Underflow | #)
#endif