    {-|
    Copyright  :  (C) 2013-2016, University of Twente,
                      2016     , Myrtle Software Ltd,
                      2021-2024, QBayLogic B.V.
    License    :  BSD2 (see the file LICENSE)
    Maintainer :  QBayLogic B.V. <devops@qbaylogic.com>
    -}

    {-# LANGUAGE CPP #-}
    {-# LANGUAGE DeriveAnyClass #-}
    {-# LANGUAGE MultiParamTypeClasses #-}
    {-# LANGUAGE MultiWayIf #-}
    {-# LANGUAGE RoleAnnotations #-}
    {-# LANGUAGE TemplateHaskell #-}
    {-# LANGUAGE TypeFamilies #-}
    {-# LANGUAGE UndecidableInstances #-}

    {-# LANGUAGE Unsafe #-}

    {-# OPTIONS_HADDOCK show-extensions not-home #-}
    {-# OPTIONS_GHC -fplugin GHC.TypeLits.Normalise #-}
    {-# OPTIONS_GHC -fplugin GHC.TypeLits.KnownNat.Solver #-}

    module Clash.Sized.Internal.Signed
      ( -- * Datatypes
        Signed (..)
        -- * Accessors
        -- ** Length information
      , size#
        -- * Type classes
        -- ** BitPack
      , pack#
      , unpack#
        -- ** Eq
      , eq#
      , neq#
        -- ** Ord
      , lt#
      , ge#
      , gt#
      , le#
        -- ** Enum
      , toEnum#
      , fromEnum#
        -- ** Enum (not synthesizable)
      , enumFrom#
      , enumFromThen#
      , enumFromTo#
      , enumFromThenTo#
        -- ** Bounded
      , minBound#
      , maxBound#
        -- ** Num
      , (+#)
      , (-#)
      , (*#)
      , negate#
      , abs#
      , fromInteger#
        -- ** ExtendingNum
      , plus#
      , minus#
      , times#
        -- ** Integral
      , quot#
      , rem#
      , div#
      , mod#
      , toInteger#
        -- ** Bits
      , and#
      , or#
      , xor#
      , complement#
      , shiftL#
      , shiftR#
      , rotateL#
      , rotateR#
        -- ** Resize
      , resize#
      , truncateB#
        -- ** SaturatingNum
      , minBoundSym#
      )
    where

    import Prelude hiding                 (odd, even)

    import Control.DeepSeq                (NFData (..))
    import Control.Lens                   (Index, Ixed (..), IxValue)
    import Data.Bits                      (Bits (..), FiniteBits (..))
    import Data.Data                      (Data)
    import Data.Default.Class             (Default (..))
    import Data.Proxy                     (Proxy (..))
    import Text.Read                      (Read (..), ReadPrec)
    import Text.Printf                    (PrintfArg (..), printf)
    import GHC.Generics                   (Generic)
    import GHC.Natural                    (naturalFromInteger, naturalToInteger)

    import GHC.TypeLits                   (KnownNat, Nat, type (+), natVal)
    import GHC.TypeLits.Extra             (Max)
    import Data.Ix                        (Ix(..))
    import Language.Haskell.TH            (appT, conT, litT, numTyLit, sigE)
    import Language.Haskell.TH.Syntax     (Lift(..))
    #if MIN_VERSION_template_haskell(2,16,0)
    import Language.Haskell.TH.Compat
    #endif
    #if MIN_VERSION_template_haskell(2,17,0)
    import Language.Haskell.TH            (Quote, Type)
    #else
    import Language.Haskell.TH            (TypeQ)
    #endif
    import Test.QuickCheck.Arbitrary      (Arbitrary (..), CoArbitrary (..),
                                           arbitraryBoundedIntegral,
                                           coarbitraryIntegral, shrinkIntegral)

    import Clash.Annotations.Primitive (hasBlackBox)
    import Clash.Class.BitPack            (BitPack (..), packXWith)
    import Clash.Class.Num                (ExtendingNum (..), SaturatingNum (..),
                                           SaturationMode (..))
    import Clash.Class.Parity             (Parity (..))
    import Clash.Class.Resize             (Resize (..))
    import Clash.Class.BitPack.BitIndex   ((!), msb, replaceBit, split)
    import Clash.Class.BitPack.BitReduction (reduceAnd, reduceOr)
    import Clash.Promoted.Nat             (natToNatural)
    import Clash.Sized.Internal.BitVector (BitVector (BV), Bit, (++#), high, low, undefError)
    import qualified Clash.Sized.Internal.BitVector as BV
    import Clash.XException
      (ShowX (..), NFDataX (..), errorX, showsPrecXWith, rwhnfX)

    {- $setup
    >>> :m -Prelude
    >>> import Clash.Prelude
    -}

    type role Signed nominal

    -- | Arbitrary-width signed integer represented by @n@ bits, including the sign
    -- bit
    --
    -- Uses standard 2-complements representation. Meaning that, given @n@ bits,
    -- a 'Signed' @n@ number has a range of: [-(2^(@n@-1)) .. 2^(@n@-1)-1] for
    -- @n > 0@. When @n = 0@, both the min and max bound are 0.
    --
    -- * __NB__: The usual Haskell method of converting an integral numeric type to
    -- another, 'fromIntegral', is not well suited for Clash as it will go through
    -- 'Integer' which is arbitrarily bounded in HDL. Instead use
    -- 'Clash.Class.BitPack.bitCoerce' and the 'Resize' class.
    -- * __NB__: The 'Num' operators perform @wrap-around@ on overflow. If you want
    -- saturation on overflow, check out the 'SaturatingNum' class.
    --
    -- >>>  maxBound :: Signed 3
    -- 3
    -- >>> minBound :: Signed 3
    -- -4
    -- >>> read (show (minBound :: Signed 3)) :: Signed 3
    -- -4
    -- >>> 1 + 2 :: Signed 3
    -- 3
    -- >>> 2 + 3 :: Signed 3
    -- -3
    -- >>> (-2) + (-3) :: Signed 3
    -- 3
    -- >>> 2 * 3 :: Signed 4
    -- 6
    -- >>> 2 * 4 :: Signed 4
    -- -8
    -- >>> (2 :: Signed 3) `mul` (4 :: Signed 4) :: Signed 7
    -- 8
    -- >>> (2 :: Signed 3) `add` (3 :: Signed 3) :: Signed 4
    -- 5
    -- >>> (-2 :: Signed 3) `add` (-3 :: Signed 3) :: Signed 4
    -- -5
    -- >>> satAdd SatSymmetric 2 3 :: Signed 3
    -- 3
    -- >>> satAdd SatSymmetric (-2) (-3) :: Signed 3
    -- -3
    --
    -- Signed has the <https://downloads.haskell.org/ghc/latest/docs/html/users_guide/exts/roles.html type role>
    --
    -- >>> :i Signed
    -- type role Signed nominal
    -- ...
    --
    -- as it is not safe to coerce between different width Signed. To change the
    -- width, use the functions in the 'Clash.Class.Resize.Resize' class.
    data Signed (n :: Nat) =
        -- | The constructor, 'S', and the field, 'unsafeToInteger', are not
        -- synthesizable.
        S { forall (n :: Nat). Signed n -> Integer
    unsafeToInteger :: !Integer}
      deriving (Typeable (Signed n)
    Typeable (Signed n) =>
    (forall (c :: Type -> Type).
     (forall d b. Data d => c (d -> b) -> d -> c b)
     -> (forall g. g -> c g) -> Signed n -> c (Signed n))
    -> (forall (c :: Type -> Type).
        (forall b r. Data b => c (b -> r) -> c r)
        -> (forall r. r -> c r) -> Constr -> c (Signed n))
    -> (Signed n -> Constr)
    -> (Signed n -> DataType)
    -> (forall (t :: Type -> Type) (c :: Type -> Type).
        Typeable t =>
        (forall d. Data d => c (t d)) -> Maybe (c (Signed n)))
    -> (forall (t :: Type -> Type -> Type) (c :: Type -> Type).
        Typeable t =>
        (forall d e. (Data d, Data e) => c (t d e))
        -> Maybe (c (Signed n)))
    -> ((forall b. Data b => b -> b) -> Signed n -> Signed n)
    -> (forall r r'.
        (r -> r' -> r)
        -> r -> (forall d. Data d => d -> r') -> Signed n -> r)
    -> (forall r r'.
        (r' -> r -> r)
        -> r -> (forall d. Data d => d -> r') -> Signed n -> r)
    -> (forall u. (forall d. Data d => d -> u) -> Signed n -> [u])
    -> (forall u. Int -> (forall d. Data d => d -> u) -> Signed n -> u)
    -> (forall (m :: Type -> Type).
        Monad m =>
        (forall d. Data d => d -> m d) -> Signed n -> m (Signed n))
    -> (forall (m :: Type -> Type).
        MonadPlus m =>
        (forall d. Data d => d -> m d) -> Signed n -> m (Signed n))
    -> (forall (m :: Type -> Type).
        MonadPlus m =>
        (forall d. Data d => d -> m d) -> Signed n -> m (Signed n))
    -> Data (Signed n)
    Signed n -> Constr
    Signed n -> DataType
    (forall b. Data b => b -> b) -> Signed n -> Signed n
    forall (n :: Nat). KnownNat n => Typeable (Signed n)
    forall (n :: Nat). KnownNat n => Signed n -> Constr
    forall (n :: Nat). KnownNat n => Signed n -> DataType
    forall (n :: Nat).
    KnownNat n =>
    (forall b. Data b => b -> b) -> Signed n -> Signed n
    forall (n :: Nat) u.
    KnownNat n =>
    Int -> (forall d. Data d => d -> u) -> Signed n -> u
    forall (n :: Nat) u.
    KnownNat n =>
    (forall d. Data d => d -> u) -> Signed n -> [u]
    forall (n :: Nat) r r'.
    KnownNat n =>
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> Signed n -> r
    forall (n :: Nat) r r'.
    KnownNat n =>
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> Signed n -> r
    forall (n :: Nat) (m :: Type -> Type).
    (KnownNat n, Monad m) =>
    (forall d. Data d => d -> m d) -> Signed n -> m (Signed n)
    forall (n :: Nat) (m :: Type -> Type).
    (KnownNat n, MonadPlus m) =>
    (forall d. Data d => d -> m d) -> Signed n -> m (Signed n)
    forall (n :: Nat) (c :: Type -> Type).
    KnownNat n =>
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c (Signed n)
    forall (n :: Nat) (c :: Type -> Type).
    KnownNat n =>
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> Signed n -> c (Signed n)
    forall (n :: Nat) (t :: Type -> Type) (c :: Type -> Type).
    (KnownNat n, Typeable t) =>
    (forall d. Data d => c (t d)) -> Maybe (c (Signed n))
    forall (n :: Nat) (t :: Type -> Type -> Type) (c :: Type -> Type).
    (KnownNat n, Typeable t) =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Signed n))
    forall a.
    Typeable a =>
    (forall (c :: Type -> Type).
     (forall d b. Data d => c (d -> b) -> d -> c b)
     -> (forall g. g -> c g) -> a -> c a)
    -> (forall (c :: Type -> Type).
        (forall b r. Data b => c (b -> r) -> c r)
        -> (forall r. r -> c r) -> Constr -> c a)
    -> (a -> Constr)
    -> (a -> DataType)
    -> (forall (t :: Type -> Type) (c :: Type -> Type).
        Typeable t =>
        (forall d. Data d => c (t d)) -> Maybe (c a))
    -> (forall (t :: Type -> Type -> Type) (c :: Type -> Type).
        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 :: Type -> Type).
        Monad m =>
        (forall d. Data d => d -> m d) -> a -> m a)
    -> (forall (m :: Type -> Type).
        MonadPlus m =>
        (forall d. Data d => d -> m d) -> a -> m a)
    -> (forall (m :: Type -> Type).
        MonadPlus m =>
        (forall d. Data d => d -> m d) -> a -> m a)
    -> Data a
    forall u. Int -> (forall d. Data d => d -> u) -> Signed n -> u
    forall u. (forall d. Data d => d -> u) -> Signed n -> [u]
    forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> Signed n -> r
    forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> Signed n -> r
    forall (m :: Type -> Type).
    Monad m =>
    (forall d. Data d => d -> m d) -> Signed n -> m (Signed n)
    forall (m :: Type -> Type).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> Signed n -> m (Signed n)
    forall (c :: Type -> Type).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c (Signed n)
    forall (c :: Type -> Type).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> Signed n -> c (Signed n)
    forall (t :: Type -> Type) (c :: Type -> Type).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c (Signed n))
    forall (t :: Type -> Type -> Type) (c :: Type -> Type).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Signed n))
    $cgfoldl :: forall (n :: Nat) (c :: Type -> Type).
    KnownNat n =>
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> Signed n -> c (Signed n)
    gfoldl :: forall (c :: Type -> Type).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> Signed n -> c (Signed n)
    $cgunfold :: forall (n :: Nat) (c :: Type -> Type).
    KnownNat n =>
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c (Signed n)
    gunfold :: forall (c :: Type -> Type).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c (Signed n)
    $ctoConstr :: forall (n :: Nat). KnownNat n => Signed n -> Constr
    toConstr :: Signed n -> Constr
    $cdataTypeOf :: forall (n :: Nat). KnownNat n => Signed n -> DataType
    dataTypeOf :: Signed n -> DataType
    $cdataCast1 :: forall (n :: Nat) (t :: Type -> Type) (c :: Type -> Type).
    (KnownNat n, Typeable t) =>
    (forall d. Data d => c (t d)) -> Maybe (c (Signed n))
    dataCast1 :: forall (t :: Type -> Type) (c :: Type -> Type).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c (Signed n))
    $cdataCast2 :: forall (n :: Nat) (t :: Type -> Type -> Type) (c :: Type -> Type).
    (KnownNat n, Typeable t) =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Signed n))
    dataCast2 :: forall (t :: Type -> Type -> Type) (c :: Type -> Type).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Signed n))
    $cgmapT :: forall (n :: Nat).
    KnownNat n =>
    (forall b. Data b => b -> b) -> Signed n -> Signed n
    gmapT :: (forall b. Data b => b -> b) -> Signed n -> Signed n
    $cgmapQl :: forall (n :: Nat) r r'.
    KnownNat n =>
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> Signed n -> r
    gmapQl :: forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> Signed n -> r
    $cgmapQr :: forall (n :: Nat) r r'.
    KnownNat n =>
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> Signed n -> r
    gmapQr :: forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> Signed n -> r
    $cgmapQ :: forall (n :: Nat) u.
    KnownNat n =>
    (forall d. Data d => d -> u) -> Signed n -> [u]
    gmapQ :: forall u. (forall d. Data d => d -> u) -> Signed n -> [u]
    $cgmapQi :: forall (n :: Nat) u.
    KnownNat n =>
    Int -> (forall d. Data d => d -> u) -> Signed n -> u
    gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> Signed n -> u
    $cgmapM :: forall (n :: Nat) (m :: Type -> Type).
    (KnownNat n, Monad m) =>
    (forall d. Data d => d -> m d) -> Signed n -> m (Signed n)
    gmapM :: forall (m :: Type -> Type).
    Monad m =>
    (forall d. Data d => d -> m d) -> Signed n -> m (Signed n)
    $cgmapMp :: forall (n :: Nat) (m :: Type -> Type).
    (KnownNat n, MonadPlus m) =>
    (forall d. Data d => d -> m d) -> Signed n -> m (Signed n)
    gmapMp :: forall (m :: Type -> Type).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> Signed n -> m (Signed n)
    $cgmapMo :: forall (n :: Nat) (m :: Type -> Type).
    (KnownNat n, MonadPlus m) =>
    (forall d. Data d => d -> m d) -> Signed n -> m (Signed n)
    gmapMo :: forall (m :: Type -> Type).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> Signed n -> m (Signed n)
    Data, (forall x. Signed n -> Rep (Signed n) x)
    -> (forall x. Rep (Signed n) x -> Signed n) -> Generic (Signed n)
    forall (n :: Nat) x. Rep (Signed n) x -> Signed n
    forall (n :: Nat) x. Signed n -> Rep (Signed n) x
    forall x. Rep (Signed n) x -> Signed n
    forall x. Signed n -> Rep (Signed n) x
    forall a.
    (forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
    $cfrom :: forall (n :: Nat) x. Signed n -> Rep (Signed n) x
    from :: forall x. Signed n -> Rep (Signed n) x
    $cto :: forall (n :: Nat) x. Rep (Signed n) x -> Signed n
    to :: forall x. Rep (Signed n) x -> Signed n
    Generic)

    {-# ANN S hasBlackBox #-}

    instance NFDataX (Signed n) where
      deepErrorX :: HasCallStack => String -> Signed n
    deepErrorX = String -> Signed n
    forall a. HasCallStack => String -> a
    errorX
      ensureSpine :: Signed n -> Signed n
    ensureSpine = Signed n -> Signed n
    forall a. a -> a
    id
      rnfX :: Signed n -> ()
    rnfX = Signed n -> ()
    forall a. a -> ()
    rwhnfX

    -- See: https://github.com/clash-lang/clash-compiler/pull/2511
    {-# CLASH_OPAQUE size# #-}
    {-# ANN size# hasBlackBox #-}
    size# :: KnownNat n => Signed n -> Int
    size# :: forall (n :: Nat). KnownNat n => Signed n -> Int
    size# Signed n
    bv = Integer -> Int
    forall a. Num a => Integer -> a
    fromInteger (Signed n -> Integer
    forall (n :: Nat) (proxy :: Nat -> Type).
    KnownNat n =>
    proxy n -> Integer
    natVal Signed n
    bv)

    instance NFData (Signed n) where
      rnf :: Signed n -> ()
    rnf (S Integer
    i) = Integer -> ()
    forall a. NFData a => a -> ()
    rnf Integer
    i () -> () -> ()
    forall a b. a -> b -> b
    `seq` ()
      {-# NOINLINE rnf #-}
      -- NOINLINE is needed so that Clash doesn't trip on the "Signed ~# Integer"
      -- coercion

    instance Show (Signed n) where
      show :: Signed n -> String
    show (S Integer
    i) = Integer -> String
    forall a. Show a => a -> String
    show Integer
    i
      {-# NOINLINE show #-}

    instance ShowX (Signed n) where
      showsPrecX :: Int -> Signed n -> ShowS
    showsPrecX = (Int -> Signed n -> ShowS) -> Int -> Signed n -> ShowS
    forall a. (Int -> a -> ShowS) -> Int -> a -> ShowS
    showsPrecXWith Int -> Signed n -> ShowS
    forall a. Show a => Int -> a -> ShowS
    showsPrec

    -- | None of the 'Read' class' methods are synthesizable.
    instance KnownNat n => Read (Signed n) where
      readPrec :: ReadPrec (Signed n)
    readPrec = Integer -> Signed n
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (Integer -> Signed n) -> ReadPrec Integer -> ReadPrec (Signed n)
    forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
    <$> (ReadPrec Integer
    forall a. Read a => ReadPrec a
    readPrec :: ReadPrec Integer)

    instance KnownNat n => BitPack (Signed n) where
      type BitSize (Signed n) = n
      pack :: Signed n -> BitVector (BitSize (Signed n))
    pack   = (Signed n -> BitVector n) -> Signed n -> BitVector n
    forall (n :: Nat) a.
    KnownNat n =>
    (a -> BitVector n) -> a -> BitVector n
    packXWith Signed n -> BitVector n
    forall (n :: Nat). KnownNat n => Signed n -> BitVector n
    pack#
      unpack :: BitVector (BitSize (Signed n)) -> Signed n
    unpack = BitVector n -> Signed n
    BitVector (BitSize (Signed n)) -> Signed n
    forall (n :: Nat). KnownNat n => BitVector n -> Signed n
    unpack#

    -- See: https://github.com/clash-lang/clash-compiler/pull/2511
    {-# CLASH_OPAQUE pack# #-}
    {-# ANN pack# hasBlackBox #-}
    pack# :: forall n . KnownNat n => Signed n -> BitVector n
    pack# :: forall (n :: Nat). KnownNat n => Signed n -> BitVector n
    pack# (S Integer
    i) = let m :: Integer
    m = Integer
    1 Integer -> Int -> Integer
    `shiftL0` Integer -> Int
    forall a. Num a => Integer -> a
    fromInteger (Proxy n -> Integer
    forall (n :: Nat) (proxy :: Nat -> Type).
    KnownNat n =>
    proxy n -> Integer
    natVal (forall (t :: Nat). Proxy t
    forall {k} (t :: k). Proxy t
    Proxy @n))
                  in  if Integer
    i Integer -> Integer -> Bool
    forall a. Ord a => a -> a -> Bool
    < Integer
    0 then Nat -> Nat -> BitVector n
    forall (n :: Nat). Nat -> Nat -> BitVector n
    BV Nat
    0 (Integer -> Nat
    naturalFromInteger (Integer
    m Integer -> Integer -> Integer
    forall a. Num a => a -> a -> a
    + Integer
    i)) else Nat -> Nat -> BitVector n
    forall (n :: Nat). Nat -> Nat -> BitVector n
    BV Nat
    0 (Integer -> Nat
    naturalFromInteger Integer
    i)

    -- See: https://github.com/clash-lang/clash-compiler/pull/2511
    {-# CLASH_OPAQUE unpack# #-}
    {-# ANN unpack# hasBlackBox #-}
    unpack# :: forall n . KnownNat n => BitVector n -> Signed n
    unpack# :: forall (n :: Nat). KnownNat n => BitVector n -> Signed n
    unpack# (BV Nat
    0 Nat
    i) =
      let m :: Integer
    m = Integer
    1 Integer -> Int -> Integer
    `shiftL0` Integer -> Int
    forall a. Num a => Integer -> a
    fromInteger (Proxy n -> Integer
    forall (n :: Nat) (proxy :: Nat -> Type).
    KnownNat n =>
    proxy n -> Integer
    natVal (forall (t :: Nat). Proxy t
    forall {k} (t :: k). Proxy t
    Proxy @n) Integer -> Integer -> Integer
    forall a. Num a => a -> a -> a
    - Integer
    1)
          n :: Integer
    n = Nat -> Integer
    naturalToInteger Nat
    i
      in  if Integer
    n Integer -> Integer -> Bool
    forall a. Ord a => a -> a -> Bool
    >= Integer
    m then Integer -> Signed n
    forall (n :: Nat). Integer -> Signed n
    S (Integer
    nInteger -> Integer -> Integer
    forall a. Num a => a -> a -> a
    -Integer
    2Integer -> Integer -> Integer
    forall a. Num a => a -> a -> a
    *Integer
    m) else Integer -> Signed n
    forall (n :: Nat). Integer -> Signed n
    S Integer
    n
    unpack# BitVector n
    bv = String -> [BitVector n] -> Signed n
    forall (n :: Nat) a. KnownNat n => String -> [BitVector n] -> a
    undefError String
    "Signed.unpack" [BitVector n
    bv]

    instance Eq (Signed n) where
      == :: Signed n -> Signed n -> Bool
    (==) = Signed n -> Signed n -> Bool
    forall (n :: Nat). Signed n -> Signed n -> Bool
    eq#
      /= :: Signed n -> Signed n -> Bool
    (/=) = Signed n -> Signed n -> Bool
    forall (n :: Nat). Signed n -> Signed n -> Bool
    neq#

    -- See: https://github.com/clash-lang/clash-compiler/pull/2511
    {-# CLASH_OPAQUE eq# #-}
    {-# ANN eq# hasBlackBox #-}
    eq# :: Signed n -> Signed n -> Bool
    eq# :: forall (n :: Nat). Signed n -> Signed n -> Bool
    eq# (S Integer
    v1) (S Integer
    v2) = Integer
    v1 Integer -> Integer -> Bool
    forall a. Eq a => a -> a -> Bool
    == Integer
    v2

    -- See: https://github.com/clash-lang/clash-compiler/pull/2511
    {-# CLASH_OPAQUE neq# #-}
    {-# ANN neq# hasBlackBox #-}
    neq# :: Signed n -> Signed n -> Bool
    neq# :: forall (n :: Nat). Signed n -> Signed n -> Bool
    neq# (S Integer
    v1) (S Integer
    v2) = Integer
    v1 Integer -> Integer -> Bool
    forall a. Eq a => a -> a -> Bool
    /= Integer
    v2

    instance Ord (Signed n) where
      < :: Signed n -> Signed n -> Bool
    (<)  = Signed n -> Signed n -> Bool
    forall (n :: Nat). Signed n -> Signed n -> Bool
    lt#
      >= :: Signed n -> Signed n -> Bool
    (>=) = Signed n -> Signed n -> Bool
    forall (n :: Nat). Signed n -> Signed n -> Bool
    ge#
      > :: Signed n -> Signed n -> Bool
    (>)  = Signed n -> Signed n -> Bool
    forall (n :: Nat). Signed n -> Signed n -> Bool
    gt#
      <= :: Signed n -> Signed n -> Bool
    (<=) = Signed n -> Signed n -> Bool
    forall (n :: Nat). Signed n -> Signed n -> Bool
    le#

    lt#,ge#,gt#,le# :: Signed n -> Signed n -> Bool
    -- See: https://github.com/clash-lang/clash-compiler/pull/2511
    {-# CLASH_OPAQUE lt# #-}
    {-# ANN lt# hasBlackBox #-}
    lt# :: forall (n :: Nat). Signed n -> Signed n -> Bool
    lt# (S Integer
    n) (S Integer
    m) = Integer
    n Integer -> Integer -> Bool
    forall a. Ord a => a -> a -> Bool
    < Integer
    m
    -- See: https://github.com/clash-lang/clash-compiler/pull/2511
    {-# CLASH_OPAQUE ge# #-}
    {-# ANN ge# hasBlackBox #-}
    ge# :: forall (n :: Nat). Signed n -> Signed n -> Bool
    ge# (S Integer
    n) (S Integer
    m) = Integer
    n Integer -> Integer -> Bool
    forall a. Ord a => a -> a -> Bool
    >= Integer
    m
    -- See: https://github.com/clash-lang/clash-compiler/pull/2511
    {-# CLASH_OPAQUE gt# #-}
    {-# ANN gt# hasBlackBox #-}
    gt# :: forall (n :: Nat). Signed n -> Signed n -> Bool
    gt# (S Integer
    n) (S Integer
    m) = Integer
    n Integer -> Integer -> Bool
    forall a. Ord a => a -> a -> Bool
    > Integer
    m
    -- See: https://github.com/clash-lang/clash-compiler/pull/2511
    {-# CLASH_OPAQUE le# #-}
    {-# ANN le# hasBlackBox #-}
    le# :: forall (n :: Nat). Signed n -> Signed n -> Bool
    le# (S Integer
    n) (S Integer
    m) = Integer
    n Integer -> Integer -> Bool
    forall a. Ord a => a -> a -> Bool
    <= Integer
    m

    -- | The functions: 'enumFrom', 'enumFromThen', 'enumFromTo', and
    -- 'enumFromThenTo', are not synthesizable.
    instance KnownNat n => Enum (Signed n) where
      succ :: Signed n -> Signed n
    succ Signed n
    n
        | Signed n
    n Signed n -> Signed n -> Bool
    forall a. Eq a => a -> a -> Bool
    == Signed n
    forall a. Bounded a => a
    maxBound =
            String -> Signed n
    forall a. HasCallStack => String -> a
    error (String -> Signed n) -> String -> Signed n
    forall a b. (a -> b) -> a -> b
    $ String
    "'succ' was called on (" String -> ShowS
    forall a. Semigroup a => a -> a -> a
    <> forall a. Show a => a -> String
    show @(Signed n) Signed n
    forall a. Bounded a => a
    maxBound String -> ShowS
    forall a. Semigroup a => a -> a -> a
    <> String
    " :: "
                 String -> ShowS
    forall a. Semigroup a => a -> a -> a
    <> String
    "Signed " String -> ShowS
    forall a. Semigroup a => a -> a -> a
    <> Nat -> String
    forall a. Show a => a -> String
    show (forall (n :: Nat). KnownNat n => Nat
    natToNatural @n) String -> ShowS
    forall a. Semigroup a => a -> a -> a
    <> String
    ") and caused an "
                 String -> ShowS
    forall a. Semigroup a => a -> a -> a
    <> String
    "overflow. Use 'satSucc' and specify a SaturationMode if you "
                 String -> ShowS
    forall a. Semigroup a => a -> a -> a
    <> String
    "need other behavior."
        | Bool
    otherwise = Signed n
    n Signed n -> Signed n -> Signed n
    forall (n :: Nat). KnownNat n => Signed n -> Signed n -> Signed n
    +# Integer -> Signed n
    forall (n :: Nat). KnownNat n => Integer -> Signed n
    fromInteger# Integer
    1

      pred :: Signed n -> Signed n
    pred Signed n
    n
        | Signed n
    n Signed n -> Signed n -> Bool
    forall a. Eq a => a -> a -> Bool
    == Signed n
    forall a. Bounded a => a
    minBound =
            String -> Signed n
    forall a. HasCallStack => String -> a
    error (String -> Signed n) -> String -> Signed n
    forall a b. (a -> b) -> a -> b
    $ String
    "'pred' was called on (" String -> ShowS
    forall a. Semigroup a => a -> a -> a
    <> forall a. Show a => a -> String
    show @(Signed n) Signed n
    forall a. Bounded a => a
    maxBound String -> ShowS
    forall a. Semigroup a => a -> a -> a
    <> String
    " :: "
                 String -> ShowS
    forall a. Semigroup a => a -> a -> a
    <> String
    "Signed " String -> ShowS
    forall a. Semigroup a => a -> a -> a
    <> Nat -> String
    forall a. Show a => a -> String
    show (forall (n :: Nat). KnownNat n => Nat
    natToNatural @n) String -> ShowS
    forall a. Semigroup a => a -> a -> a
    <> String
    ") and caused an "
                 String -> ShowS
    forall a. Semigroup a => a -> a -> a
    <> String
    "underflow. Use 'satPred' and specify a SaturationMode if you "
                 String -> ShowS
    forall a. Semigroup a => a -> a -> a
    <> String
    "need other behavior."
        | Bool
    otherwise = Signed n
    n Signed n -> Signed n -> Signed n
    forall (n :: Nat). KnownNat n => Signed n -> Signed n -> Signed n
    -# Integer -> Signed n
    forall (n :: Nat). KnownNat n => Integer -> Signed n
    fromInteger# Integer
    1

      toEnum :: Int -> Signed n
    toEnum         = Int -> Signed n
    forall (n :: Nat). KnownNat n => Int -> Signed n
    toEnum#
      fromEnum :: Signed n -> Int
    fromEnum       = Signed n -> Int
    forall (n :: Nat). KnownNat n => Signed n -> Int
    fromEnum#
      enumFrom :: Signed n -> [Signed n]
    enumFrom       = Signed n -> [Signed n]
    forall (n :: Nat). KnownNat n => Signed n -> [Signed n]
    enumFrom#
      enumFromThen :: Signed n -> Signed n -> [Signed n]
    enumFromThen   = Signed n -> Signed n -> [Signed n]
    forall (n :: Nat). KnownNat n => Signed n -> Signed n -> [Signed n]
    enumFromThen#
      enumFromTo :: Signed n -> Signed n -> [Signed n]
    enumFromTo     = Signed n -> Signed n -> [Signed n]
    forall (n :: Nat). KnownNat n => Signed n -> Signed n -> [Signed n]
    enumFromTo#
      enumFromThenTo :: Signed n -> Signed n -> Signed n -> [Signed n]
    enumFromThenTo = Signed n -> Signed n -> Signed n -> [Signed n]
    forall (n :: Nat).
    KnownNat n =>
    Signed n -> Signed n -> Signed n -> [Signed n]
    enumFromThenTo#

    toEnum# :: forall n. KnownNat n => Int -> Signed n
    toEnum# :: forall (n :: Nat). KnownNat n => Int -> Signed n
    toEnum# = Integer -> Signed n
    forall (n :: Nat). KnownNat n => Integer -> Signed n
    fromInteger# (Integer -> Signed n) -> (Int -> Integer) -> Int -> Signed n
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Int -> Integer
    forall a. Integral a => a -> Integer
    toInteger
    -- See: https://github.com/clash-lang/clash-compiler/pull/2511
    {-# CLASH_OPAQUE toEnum# #-}
    {-# ANN toEnum# hasBlackBox #-}

    fromEnum# :: forall n. KnownNat n => Signed n -> Int
    fromEnum# :: forall (n :: Nat). KnownNat n => Signed n -> Int
    fromEnum# = Integer -> Int
    forall a. Enum a => a -> Int
    fromEnum (Integer -> Int) -> (Signed n -> Integer) -> Signed n -> Int
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Signed n -> Integer
    forall (n :: Nat). Signed n -> Integer
    toInteger#
    -- See: https://github.com/clash-lang/clash-compiler/pull/2511
    {-# CLASH_OPAQUE fromEnum# #-}
    {-# ANN fromEnum# hasBlackBox #-}

    enumFrom# :: forall n. KnownNat n => Signed n -> [Signed n]
    enumFrom# :: forall (n :: Nat). KnownNat n => Signed n -> [Signed n]
    enumFrom# Signed n
    x = (Integer -> Signed n) -> [Integer] -> [Signed n]
    forall a b. (a -> b) -> [a] -> [b]
    map (Int -> Integer -> Integer -> Integer -> Signed n
    forall (n :: Nat). Int -> Integer -> Integer -> Integer -> Signed n
    fromInteger_INLINE Int
    sz Integer
    mB Integer
    mask) [Signed n -> Integer
    forall (n :: Nat). Signed n -> Integer
    unsafeToInteger Signed n
    x .. Signed n -> Integer
    forall (n :: Nat). Signed n -> Integer
    unsafeToInteger (Signed n
    forall a. Bounded a => a
    maxBound :: Signed n)]
      where sz :: Int
    sz   = Integer -> Int
    forall a. Num a => Integer -> a
    fromInteger (Proxy n -> Integer
    forall (n :: Nat) (proxy :: Nat -> Type).
    KnownNat n =>
    proxy n -> Integer
    natVal (forall (t :: Nat). Proxy t
    forall {k} (t :: k). Proxy t
    Proxy @n)) Int -> Int -> Int
    forall a. Num a => a -> a -> a
    - Int
    1
            mB :: Integer
    mB   = Integer
    1 Integer -> Int -> Integer
    forall a. Bits a => a -> Int -> a
    `shiftL` Int
    sz
            mask :: Integer
    mask = Integer
    mB Integer -> Integer -> Integer
    forall a. Num a => a -> a -> a
    - Integer
    1
    -- See: https://github.com/clash-lang/clash-compiler/pull/2511
    {-# CLASH_OPAQUE enumFrom# #-}

    enumFromThen# :: forall n. KnownNat n => Signed n -> Signed n -> [Signed n]
    enumFromThen# :: forall (n :: Nat). KnownNat n => Signed n -> Signed n -> [Signed n]
    enumFromThen# Signed n
    x Signed n
    y =
      [Integer] -> [Signed n]
    toSigneds [Signed n -> Integer
    forall (n :: Nat). Signed n -> Integer
    unsafeToInteger Signed n
    x, Signed n -> Integer
    forall (n :: Nat). Signed n -> Integer
    unsafeToInteger Signed n
    y .. Signed n -> Integer
    forall (n :: Nat). Signed n -> Integer
    unsafeToInteger Signed n
    bound]
     where
      bound :: Signed n
    bound = if Signed n
    x Signed n -> Signed n -> Bool
    forall a. Ord a => a -> a -> Bool
    <= Signed n
    y then Signed n
    forall a. Bounded a => a
    maxBound else Signed n
    forall a. Bounded a => a
    minBound :: Signed n
      toSigneds :: [Integer] -> [Signed n]
    toSigneds = (Integer -> Signed n) -> [Integer] -> [Signed n]
    forall a b. (a -> b) -> [a] -> [b]
    map (Int -> Integer -> Integer -> Integer -> Signed n
    forall (n :: Nat). Int -> Integer -> Integer -> Integer -> Signed n
    fromInteger_INLINE Int
    sz Integer
    mB Integer
    mask)
      sz :: Int
    sz = Integer -> Int
    forall a. Num a => Integer -> a
    fromInteger (Proxy n -> Integer
    forall (n :: Nat) (proxy :: Nat -> Type).
    KnownNat n =>
    proxy n -> Integer
    natVal (forall (t :: Nat). Proxy t
    forall {k} (t :: k). Proxy t
    Proxy @n)) Int -> Int -> Int
    forall a. Num a => a -> a -> a
    - Int
    1
      mB :: Integer
    mB = Integer
    1 Integer -> Int -> Integer
    forall a. Bits a => a -> Int -> a
    `shiftL` Int
    sz
      mask :: Integer
    mask = Integer
    mB Integer -> Integer -> Integer
    forall a. Num a => a -> a -> a
    - Integer
    1
    -- See: https://github.com/clash-lang/clash-compiler/pull/2511
    {-# CLASH_OPAQUE enumFromThen# #-}

    enumFromTo# :: forall n. KnownNat n => Signed n -> Signed n -> [Signed n]
    enumFromTo# :: forall (n :: Nat). KnownNat n => Signed n -> Signed n -> [Signed n]
    enumFromTo# Signed n
    x Signed n
    y = (Integer -> Signed n) -> [Integer] -> [Signed n]
    forall a b. (a -> b) -> [a] -> [b]
    map (Int -> Integer -> Integer -> Integer -> Signed n
    forall (n :: Nat). Int -> Integer -> Integer -> Integer -> Signed n
    fromInteger_INLINE Int
    sz Integer
    mB Integer
    mask) [Signed n -> Integer
    forall (n :: Nat). Signed n -> Integer
    unsafeToInteger Signed n
    x .. Signed n -> Integer
    forall (n :: Nat). Signed n -> Integer
    unsafeToInteger Signed n
    y]
      where sz :: Int
    sz   = Integer -> Int
    forall a. Num a => Integer -> a
    fromInteger (Proxy n -> Integer
    forall (n :: Nat) (proxy :: Nat -> Type).
    KnownNat n =>
    proxy n -> Integer
    natVal (forall (t :: Nat). Proxy t
    forall {k} (t :: k). Proxy t
    Proxy @n)) Int -> Int -> Int
    forall a. Num a => a -> a -> a
    - Int
    1
            mB :: Integer
    mB   = Integer
    1 Integer -> Int -> Integer
    forall a. Bits a => a -> Int -> a
    `shiftL` Int
    sz
            mask :: Integer
    mask = Integer
    mB Integer -> Integer -> Integer
    forall a. Num a => a -> a -> a
    - Integer
    1
    -- See: https://github.com/clash-lang/clash-compiler/pull/2511
    {-# CLASH_OPAQUE enumFromTo# #-}

    enumFromThenTo# :: forall n. KnownNat n => Signed n -> Signed n -> Signed n -> [Signed n]
    enumFromThenTo# :: forall (n :: Nat).
    KnownNat n =>
    Signed n -> Signed n -> Signed n -> [Signed n]
    enumFromThenTo# Signed n
    x1 Signed n
    x2 Signed n
    y = (Integer -> Signed n) -> [Integer] -> [Signed n]
    forall a b. (a -> b) -> [a] -> [b]
    map (Int -> Integer -> Integer -> Integer -> Signed n
    forall (n :: Nat). Int -> Integer -> Integer -> Integer -> Signed n
    fromInteger_INLINE Int
    sz Integer
    mB Integer
    mask) [Signed n -> Integer
    forall (n :: Nat). Signed n -> Integer
    unsafeToInteger Signed n
    x1, Signed n -> Integer
    forall (n :: Nat). Signed n -> Integer
    unsafeToInteger Signed n
    x2 .. Signed n -> Integer
    forall (n :: Nat). Signed n -> Integer
    unsafeToInteger Signed n
    y]
      where sz :: Int
    sz   = Integer -> Int
    forall a. Num a => Integer -> a
    fromInteger (Proxy n -> Integer
    forall (n :: Nat) (proxy :: Nat -> Type).
    KnownNat n =>
    proxy n -> Integer
    natVal (forall (t :: Nat). Proxy t
    forall {k} (t :: k). Proxy t
    Proxy @n)) Int -> Int -> Int
    forall a. Num a => a -> a -> a
    - Int
    1
            mB :: Integer
    mB   = Integer
    1 Integer -> Int -> Integer
    forall a. Bits a => a -> Int -> a
    `shiftL` Int
    sz
            mask :: Integer
    mask = Integer
    mB Integer -> Integer -> Integer
    forall a. Num a => a -> a -> a
    - Integer
    1
    -- See: https://github.com/clash-lang/clash-compiler/pull/2511
    {-# CLASH_OPAQUE enumFromThenTo# #-}

    instance KnownNat n => Bounded (Signed n) where
      minBound :: Signed n
    minBound = Signed n
    forall (n :: Nat). KnownNat n => Signed n
    minBound#
      maxBound :: Signed n
    maxBound = Signed n
    forall (n :: Nat). KnownNat n => Signed n
    maxBound#

    minBound# :: forall n. KnownNat n => Signed n
    minBound# :: forall (n :: Nat). KnownNat n => Signed n
    minBound# =
      case forall (n :: Nat). KnownNat n => Nat
    natToNatural @n of
        Nat
    0 -> Signed n
    0
        Nat
    n -> Integer -> Signed n
    forall (n :: Nat). Integer -> Signed n
    S (Integer -> Integer
    forall a. Num a => a -> a
    negate (Integer -> Integer) -> Integer -> Integer
    forall a b. (a -> b) -> a -> b
    $ Integer
    2 Integer -> Nat -> Integer
    forall a b. (Num a, Integral b) => a -> b -> a
    ^ (Nat
    n Nat -> Nat -> Nat
    forall a. Num a => a -> a -> a
    - Nat
    1))
    -- See: https://github.com/clash-lang/clash-compiler/pull/2511
    {-# CLASH_OPAQUE minBound# #-}
    {-# ANN minBound# hasBlackBox #-}

    maxBound# :: forall n. KnownNat n => Signed n
    maxBound# :: forall (n :: Nat). KnownNat n => Signed n
    maxBound# =
      case forall (n :: Nat). KnownNat n => Nat
    natToNatural @n of
        Nat
    0 -> Signed n
    0
        Nat
    n -> Integer -> Signed n
    forall (n :: Nat). Integer -> Signed n
    S (Integer
    2 Integer -> Nat -> Integer
    forall a b. (Num a, Integral b) => a -> b -> a
    ^ (Nat
    n Nat -> Nat -> Nat
    forall a. Num a => a -> a -> a
    - Nat
    1) Integer -> Integer -> Integer
    forall a. Num a => a -> a -> a
    - Integer
    1)
    -- See: https://github.com/clash-lang/clash-compiler/pull/2511
    {-# CLASH_OPAQUE maxBound# #-}
    {-# ANN maxBound# hasBlackBox #-}

    -- | Operators do @wrap-around@ on overflow
    --
    -- __NB__: 'fromInteger'/'fromIntegral' can cause unexpected truncation, as
    -- 'Integer' is arbitrarily bounded during synthesis.  Prefer
    -- 'Clash.Class.BitPack.bitCoerce' and the 'Resize' class.
    instance KnownNat n => Num (Signed n) where
      + :: Signed n -> Signed n -> Signed n
    (+)         = Signed n -> Signed n -> Signed n
    forall (n :: Nat). KnownNat n => Signed n -> Signed n -> Signed n
    (+#)
      (-)         = Signed n -> Signed n -> Signed n
    forall (n :: Nat). KnownNat n => Signed n -> Signed n -> Signed n
    (-#)
      * :: Signed n -> Signed n -> Signed n
    (*)         = Signed n -> Signed n -> Signed n
    forall (n :: Nat). KnownNat n => Signed n -> Signed n -> Signed n
    (*#)
      negate :: Signed n -> Signed n
    negate      = Signed n -> Signed n
    forall (n :: Nat). KnownNat n => Signed n -> Signed n
    negate#
      abs :: Signed n -> Signed n
    abs         = Signed n -> Signed n
    forall (n :: Nat). KnownNat n => Signed n -> Signed n
    abs#
      signum :: Signed n -> Signed n
    signum Signed n
    s    = if Signed n
    s Signed n -> Signed n -> Bool
    forall a. Ord a => a -> a -> Bool
    < Signed n
    0 then (-Signed n
    1) else
                       if Signed n
    s Signed n -> Signed n -> Bool
    forall a. Ord a => a -> a -> Bool
    > Signed n
    0 then Signed n
    1 else Signed n
    0
      fromInteger :: Integer -> Signed n
    fromInteger = Integer -> Signed n
    forall (n :: Nat). KnownNat n => Integer -> Signed n
    fromInteger#

    (+#), (-#), (*#) :: forall n . KnownNat n => Signed n -> Signed n -> Signed n
    -- See: https://github.com/clash-lang/clash-compiler/pull/2511
    {-# CLASH_OPAQUE (+#) #-}
    {-# ANN (+#) hasBlackBox #-}
    +# :: forall (n :: Nat). KnownNat n => Signed n -> Signed n -> Signed n
    (+#) =
      \(S Integer
    a) (S Integer
    b) ->
        let z :: Integer
    z = Integer
    a Integer -> Integer -> Integer
    forall a. Num a => a -> a -> a
    + Integer
    b
        in  if Integer
    z Integer -> Integer -> Bool
    forall a. Ord a => a -> a -> Bool
    >= Integer
    m then
              Integer -> Signed n
    forall (n :: Nat). Integer -> Signed n
    S (Integer
    z Integer -> Integer -> Integer
    forall a. Num a => a -> a -> a
    - Integer
    2Integer -> Integer -> Integer
    forall a. Num a => a -> a -> a
    *Integer
    m)
            else if Integer
    z Integer -> Integer -> Bool
    forall a. Ord a => a -> a -> Bool
    < Integer -> Integer
    forall a. Num a => a -> a
    negate Integer
    m then
              Integer -> Signed n
    forall (n :: Nat). Integer -> Signed n
    S (Integer
    z Integer -> Integer -> Integer
    forall a. Num a => a -> a -> a
    + Integer
    2Integer -> Integer -> Integer
    forall a. Num a => a -> a -> a
    *Integer
    m)
            else
              Integer -> Signed n
    forall (n :: Nat). Integer -> Signed n
    S Integer
    z
     where
      m :: Integer
    m = Integer
    1 Integer -> Int -> Integer
    `shiftL0` Integer -> Int
    forall a. Num a => Integer -> a
    fromInteger (Proxy n -> Integer
    forall (n :: Nat) (proxy :: Nat -> Type).
    KnownNat n =>
    proxy n -> Integer
    natVal (forall (t :: Nat). Proxy t
    forall {k} (t :: k). Proxy t
    Proxy @n) Integer -> Integer -> Integer
    forall a. Num a => a -> a -> a
    -Integer
    1)

    -- See: https://github.com/clash-lang/clash-compiler/pull/2511
    {-# CLASH_OPAQUE (-#) #-}
    {-# ANN (-#) hasBlackBox #-}
    -# :: forall (n :: Nat). KnownNat n => Signed n -> Signed n -> Signed n
    (-#) =
      \(S Integer
    a) (S Integer
    b) ->
        let z :: Integer
    z = Integer
    a Integer -> Integer -> Integer
    forall a. Num a => a -> a -> a
    - Integer
    b
        in  if Integer
    z Integer -> Integer -> Bool
    forall a. Ord a => a -> a -> Bool
    < Integer -> Integer
    forall a. Num a => a -> a
    negate Integer
    m then
              Integer -> Signed n
    forall (n :: Nat). Integer -> Signed n
    S (Integer
    z Integer -> Integer -> Integer
    forall a. Num a => a -> a -> a
    + Integer
    2Integer -> Integer -> Integer
    forall a. Num a => a -> a -> a
    *Integer
    m)
            else if Integer
    z Integer -> Integer -> Bool
    forall a. Ord a => a -> a -> Bool
    >= Integer
    m then
              Integer -> Signed n
    forall (n :: Nat). Integer -> Signed n
    S (Integer
    z Integer -> Integer -> Integer
    forall a. Num a => a -> a -> a
    - Integer
    2Integer -> Integer -> Integer
    forall a. Num a => a -> a -> a
    *Integer
    m)
            else
              Integer -> Signed n
    forall (n :: Nat). Integer -> Signed n
    S Integer
    z
     where
      m :: Integer
    m  = Integer
    1 Integer -> Int -> Integer
    `shiftL0` Integer -> Int
    forall a. Num a => Integer -> a
    fromInteger (Proxy n -> Integer
    forall (n :: Nat) (proxy :: Nat -> Type).
    KnownNat n =>
    proxy n -> Integer
    natVal (forall (t :: Nat). Proxy t
    forall {k} (t :: k). Proxy t
    Proxy @n) Integer -> Integer -> Integer
    forall a. Num a => a -> a -> a
    -Integer
    1)

    -- See: https://github.com/clash-lang/clash-compiler/pull/2511
    {-# CLASH_OPAQUE (*#) #-}
    {-# ANN (*#) hasBlackBox #-}
    *# :: forall (n :: Nat). KnownNat n => Signed n -> Signed n -> Signed n
    (*#) = \(S Integer
    a) (S Integer
    b) -> Int -> Integer -> Integer -> Integer -> Signed n
    forall (n :: Nat). Int -> Integer -> Integer -> Integer -> Signed n
    fromInteger_INLINE Int
    sz Integer
    mB Integer
    mask (Integer
    a Integer -> Integer -> Integer
    forall a. Num a => a -> a -> a
    * Integer
    b)
      where sz :: Int
    sz   = Integer -> Int
    forall a. Num a => Integer -> a
    fromInteger (Proxy n -> Integer
    forall (n :: Nat) (proxy :: Nat -> Type).
    KnownNat n =>
    proxy n -> Integer
    natVal (forall (t :: Nat). Proxy t
    forall {k} (t :: k). Proxy t
    Proxy @n)) Int -> Int -> Int
    forall a. Num a => a -> a -> a
    - Int
    1
            mB :: Integer
    mB   = Integer
    1 Integer -> Int -> Integer
    forall a. Bits a => a -> Int -> a
    `shiftL` Int
    sz
            mask :: Integer
    mask = Integer
    mB Integer -> Integer -> Integer
    forall a. Num a => a -> a -> a
    - Integer
    1

    negate#,abs# :: forall n . KnownNat n => Signed n -> Signed n
    -- See: https://github.com/clash-lang/clash-compiler/pull/2511
    {-# CLASH_OPAQUE negate# #-}
    {-# ANN negate# hasBlackBox #-}
    negate# :: forall (n :: Nat). KnownNat n => Signed n -> Signed n
    negate# =
      \(S Integer
    n) ->
        let z :: Integer
    z = Integer -> Integer
    forall a. Num a => a -> a
    negate Integer
    n
        in  if Integer
    z Integer -> Integer -> Bool
    forall a. Eq a => a -> a -> Bool
    == Integer
    m then Integer -> Signed n
    forall (n :: Nat). Integer -> Signed n
    S Integer
    n else Integer -> Signed n
    forall (n :: Nat). Integer -> Signed n
    S Integer
    z
     where
      m :: Integer
    m = Integer
    1 Integer -> Int -> Integer
    `shiftL0` Integer -> Int
    forall a. Num a => Integer -> a
    fromInteger (Proxy n -> Integer
    forall (n :: Nat) (proxy :: Nat -> Type).
    KnownNat n =>
    proxy n -> Integer
    natVal (forall (t :: Nat). Proxy t
    forall {k} (t :: k). Proxy t
    Proxy @n) Integer -> Integer -> Integer
    forall a. Num a => a -> a -> a
    -Integer
    1)

    -- See: https://github.com/clash-lang/clash-compiler/pull/2511
    {-# CLASH_OPAQUE abs# #-}
    {-# ANN abs# hasBlackBox #-}
    abs# :: forall (n :: Nat). KnownNat n => Signed n -> Signed n
    abs# =
      \(S Integer
    n) ->
        let z :: Integer
    z = Integer -> Integer
    forall a. Num a => a -> a
    abs Integer
    n
        in  if Integer
    z Integer -> Integer -> Bool
    forall a. Eq a => a -> a -> Bool
    == Integer
    m then Integer -> Signed n
    forall (n :: Nat). Integer -> Signed n
    S Integer
    n else Integer -> Signed n
    forall (n :: Nat). Integer -> Signed n
    S Integer
    z
     where
      m :: Integer
    m = Integer
    1 Integer -> Int -> Integer
    `shiftL0` Integer -> Int
    forall a. Num a => Integer -> a
    fromInteger (Proxy n -> Integer
    forall (n :: Nat) (proxy :: Nat -> Type).
    KnownNat n =>
    proxy n -> Integer
    natVal (forall (t :: Nat). Proxy t
    forall {k} (t :: k). Proxy t
    Proxy @n) Integer -> Integer -> Integer
    forall a. Num a => a -> a -> a
    -Integer
    1)

    -- See: https://github.com/clash-lang/clash-compiler/pull/2511
    {-# CLASH_OPAQUE fromInteger# #-}
    {-# ANN fromInteger# hasBlackBox #-}
    fromInteger# :: forall n . KnownNat n => Integer -> Signed (n :: Nat)
    fromInteger# :: forall (n :: Nat). KnownNat n => Integer -> Signed n
    fromInteger# = Int -> Integer -> Integer -> Integer -> Signed n
    forall (n :: Nat). Int -> Integer -> Integer -> Integer -> Signed n
    fromInteger_INLINE Int
    sz Integer
    mB Integer
    mask
      where sz :: Int
    sz   = Integer -> Int
    forall a. Num a => Integer -> a
    fromInteger (Proxy n -> Integer
    forall (n :: Nat) (proxy :: Nat -> Type).
    KnownNat n =>
    proxy n -> Integer
    natVal (forall (t :: Nat). Proxy t
    forall {k} (t :: k). Proxy t
    Proxy @n)) Int -> Int -> Int
    forall a. Num a => a -> a -> a
    - Int
    1
            mB :: Integer
    mB   = Integer
    1 Integer -> Int -> Integer
    forall a. Bits a => a -> Int -> a
    `shiftL` Int
    sz
            mask :: Integer
    mask = Integer
    mB Integer -> Integer -> Integer
    forall a. Num a => a -> a -> a
    - Integer
    1

    {-# INLINE fromInteger_INLINE #-}
    fromInteger_INLINE :: Int -> Integer -> Integer -> Integer -> Signed n
    fromInteger_INLINE :: forall (n :: Nat). Int -> Integer -> Integer -> Integer -> Signed n
    fromInteger_INLINE Int
    sz Integer
    mb Integer
    mask =
      \Integer
    i -> let i1 :: Integer
    i1 = Integer
    i Integer -> Integer -> Integer
    forall a. Bits a => a -> a -> a
    .&. Integer
    mask
                i2 :: Integer
    i2 = case Integer
    i Integer -> Int -> Integer
    forall a. Bits a => a -> Int -> a
    `shiftR` Int
    sz of
                       Integer
    q | Integer
    q Integer -> Integer -> Integer
    forall a. Bits a => a -> a -> a
    .&. Integer
    1 Integer -> Integer -> Bool
    forall a. Eq a => a -> a -> Bool
    == Integer
    0 -> Integer
    i1
                         | Bool
    otherwise    -> Integer
    i1 Integer -> Integer -> Integer
    forall a. Num a => a -> a -> a
    - Integer
    mb
            in  if Int
    sz Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    < Int
    0 then Integer -> Signed n
    forall (n :: Nat). Integer -> Signed n
    S Integer
    0 else Integer -> Signed n
    forall (n :: Nat). Integer -> Signed n
    S Integer
    i2

    instance ExtendingNum (Signed m) (Signed n) where
      type AResult (Signed m) (Signed n) = Signed (Max m n + 1)
      add :: Signed m -> Signed n -> AResult (Signed m) (Signed n)
    add  = Signed m -> Signed n -> AResult (Signed m) (Signed n)
    Signed m -> Signed n -> Signed (Max m n + 1)
    forall (m :: Nat) (n :: Nat).
    Signed m -> Signed n -> Signed (Max m n + 1)
    plus#
      sub :: Signed m -> Signed n -> AResult (Signed m) (Signed n)
    sub = Signed m -> Signed n -> AResult (Signed m) (Signed n)
    Signed m -> Signed n -> Signed (Max m n + 1)
    forall (m :: Nat) (n :: Nat).
    Signed m -> Signed n -> Signed (Max m n + 1)
    minus#
      type MResult (Signed m) (Signed n) = Signed (m + n)
      mul :: Signed m -> Signed n -> MResult (Signed m) (Signed n)
    mul = Signed m -> Signed n -> MResult (Signed m) (Signed n)
    Signed m -> Signed n -> Signed (m + n)
    forall (m :: Nat) (n :: Nat).
    Signed m -> Signed n -> Signed (m + n)
    times#

    plus#, minus# :: Signed m -> Signed n -> Signed (Max m n + 1)
    -- See: https://github.com/clash-lang/clash-compiler/pull/2511
    {-# CLASH_OPAQUE plus# #-}
    {-# ANN plus# hasBlackBox #-}
    plus# :: forall (m :: Nat) (n :: Nat).
    Signed m -> Signed n -> Signed (Max m n + 1)
    plus# (S Integer
    a) (S Integer
    b) = Integer -> Signed (Max m n + 1)
    forall (n :: Nat). Integer -> Signed n
    S (Integer
    a Integer -> Integer -> Integer
    forall a. Num a => a -> a -> a
    + Integer
    b)

    -- See: https://github.com/clash-lang/clash-compiler/pull/2511
    {-# CLASH_OPAQUE minus# #-}
    {-# ANN minus# hasBlackBox #-}
    minus# :: forall (m :: Nat) (n :: Nat).
    Signed m -> Signed n -> Signed (Max m n + 1)
    minus# (S Integer
    a) (S Integer
    b) = Integer -> Signed (Max m n + 1)
    forall (n :: Nat). Integer -> Signed n
    S (Integer
    a Integer -> Integer -> Integer
    forall a. Num a => a -> a -> a
    - Integer
    b)

    -- See: https://github.com/clash-lang/clash-compiler/pull/2511
    {-# CLASH_OPAQUE times# #-}
    {-# ANN times# hasBlackBox #-}
    times# :: Signed m -> Signed n -> Signed (m + n)
    times# :: forall (m :: Nat) (n :: Nat).
    Signed m -> Signed n -> Signed (m + n)
    times# (S Integer
    a) (S Integer
    b) = Integer -> Signed (m + n)
    forall (n :: Nat). Integer -> Signed n
    S (Integer
    a Integer -> Integer -> Integer
    forall a. Num a => a -> a -> a
    * Integer
    b)

    instance KnownNat n => Real (Signed n) where
      toRational :: Signed n -> Rational
    toRational = Integer -> Rational
    forall a. Real a => a -> Rational
    toRational (Integer -> Rational)
    -> (Signed n -> Integer) -> Signed n -> Rational
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Signed n -> Integer
    forall (n :: Nat). Signed n -> Integer
    toInteger#

    -- | __NB__: 'toInteger'/'fromIntegral' can cause unexpected truncation, as
    -- 'Integer' is arbitrarily bounded during synthesis.  Prefer
    -- 'Clash.Class.BitPack.bitCoerce' and the 'Resize' class.
    instance KnownNat n => Integral (Signed n) where
      quot :: Signed n -> Signed n -> Signed n
    quot        = Signed n -> Signed n -> Signed n
    forall (n :: Nat). KnownNat n => Signed n -> Signed n -> Signed n
    quot#
      rem :: Signed n -> Signed n -> Signed n
    rem         = Signed n -> Signed n -> Signed n
    forall (n :: Nat). Signed n -> Signed n -> Signed n
    rem#
      div :: Signed n -> Signed n -> Signed n
    div         = Signed n -> Signed n -> Signed n
    forall (n :: Nat). KnownNat n => Signed n -> Signed n -> Signed n
    div#
      mod :: Signed n -> Signed n -> Signed n
    mod         = Signed n -> Signed n -> Signed n
    forall (n :: Nat). Signed n -> Signed n -> Signed n
    mod#
      quotRem :: Signed n -> Signed n -> (Signed n, Signed n)
    quotRem Signed n
    n Signed n
    d = (Signed n
    n Signed n -> Signed n -> Signed n
    forall (n :: Nat). KnownNat n => Signed n -> Signed n -> Signed n
    `quot#` Signed n
    d,Signed n
    n Signed n -> Signed n -> Signed n
    forall (n :: Nat). Signed n -> Signed n -> Signed n
    `rem#` Signed n
    d)
      divMod :: Signed n -> Signed n -> (Signed n, Signed n)
    divMod  Signed n
    n Signed n
    d = (Signed n
    n Signed n -> Signed n -> Signed n
    forall (n :: Nat). KnownNat n => Signed n -> Signed n -> Signed n
    `div#`  Signed n
    d,Signed n
    n Signed n -> Signed n -> Signed n
    forall (n :: Nat). Signed n -> Signed n -> Signed n
    `mod#` Signed n
    d)
      toInteger :: Signed n -> Integer
    toInteger   = Signed n -> Integer
    forall (n :: Nat). Signed n -> Integer
    toInteger#

    -- See: https://github.com/clash-lang/clash-compiler/pull/2511
    {-# CLASH_OPAQUE quot# #-}
    {-# ANN quot# hasBlackBox #-}
    quot# :: forall n. KnownNat n => Signed n -> Signed n -> Signed n
    quot# :: forall (n :: Nat). KnownNat n => Signed n -> Signed n -> Signed n
    quot# (S Integer
    a) (S Integer
    b)
      | Integer
    a Integer -> Integer -> Bool
    forall a. Eq a => a -> a -> Bool
    == Integer
    minB Bool -> Bool -> Bool
    && Integer
    b Integer -> Integer -> Bool
    forall a. Eq a => a -> a -> Bool
    == (-Integer
    1) = Integer -> Signed n
    forall (n :: Nat). Integer -> Signed n
    S Integer
    minB
      | Bool
    otherwise = Integer -> Signed n
    forall (n :: Nat). Integer -> Signed n
    S (Integer
    a Integer -> Integer -> Integer
    forall a. Integral a => a -> a -> a
    `quot` Integer
    b)
     where
      S Integer
    minB = forall a. Bounded a => a
    minBound @(Signed n)

    -- See: https://github.com/clash-lang/clash-compiler/pull/2511
    {-# CLASH_OPAQUE rem# #-}
    {-# ANN rem# hasBlackBox #-}
    rem# :: Signed n -> Signed n -> Signed n
    rem# :: forall (n :: Nat). Signed n -> Signed n -> Signed n
    rem# (S Integer
    a) (S Integer
    b) = Integer -> Signed n
    forall (n :: Nat). Integer -> Signed n
    S (Integer
    a Integer -> Integer -> Integer
    forall a. Integral a => a -> a -> a
    `rem` Integer
    b)

    -- See: https://github.com/clash-lang/clash-compiler/pull/2511
    {-# CLASH_OPAQUE div# #-}
    {-# ANN div# hasBlackBox #-}
    div# :: forall n. KnownNat n => Signed n -> Signed n -> Signed n
    div# :: forall (n :: Nat). KnownNat n => Signed n -> Signed n -> Signed n
    div# (S Integer
    a) (S Integer
    b)
      | Integer
    a Integer -> Integer -> Bool
    forall a. Eq a => a -> a -> Bool
    == Integer
    minB Bool -> Bool -> Bool
    && Integer
    b Integer -> Integer -> Bool
    forall a. Eq a => a -> a -> Bool
    == (-Integer
    1) = Integer -> Signed n
    forall (n :: Nat). Integer -> Signed n
    S Integer
    minB
      | Bool
    otherwise = Integer -> Signed n
    forall (n :: Nat). Integer -> Signed n
    S (Integer
    a Integer -> Integer -> Integer
    forall a. Integral a => a -> a -> a
    `div` Integer
    b)
     where
      S Integer
    minB = forall a. Bounded a => a
    minBound @(Signed n)

    -- See: https://github.com/clash-lang/clash-compiler/pull/2511
    {-# CLASH_OPAQUE mod# #-}
    {-# ANN mod# hasBlackBox #-}
    mod# :: Signed n -> Signed n -> Signed n
    mod# :: forall (n :: Nat). Signed n -> Signed n -> Signed n
    mod# (S Integer
    a) (S Integer
    b) = Integer -> Signed n
    forall (n :: Nat). Integer -> Signed n
    S (Integer
    a Integer -> Integer -> Integer
    forall a. Integral a => a -> a -> a
    `mod` Integer
    b)

    -- See: https://github.com/clash-lang/clash-compiler/pull/2511
    {-# CLASH_OPAQUE toInteger# #-}
    {-# ANN toInteger# hasBlackBox #-}
    toInteger# :: Signed n -> Integer
    toInteger# :: forall (n :: Nat). Signed n -> Integer
    toInteger# (S Integer
    n) = Integer
    n

    instance KnownNat n => PrintfArg (Signed n) where
      formatArg :: Signed n -> FieldFormatter
    formatArg = Integer -> FieldFormatter
    forall a. PrintfArg a => a -> FieldFormatter
    formatArg (Integer -> FieldFormatter)
    -> (Signed n -> Integer) -> Signed n -> FieldFormatter
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Signed n -> Integer
    forall a. Integral a => a -> Integer
    toInteger

    instance KnownNat n => Parity (Signed n) where
      even :: Signed n -> Bool
    even = BitVector n -> Bool
    forall a. Parity a => a -> Bool
    even (BitVector n -> Bool)
    -> (Signed n -> BitVector n) -> Signed n -> Bool
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Signed n -> BitVector n
    Signed n -> BitVector (BitSize (Signed n))
    forall a. BitPack a => a -> BitVector (BitSize a)
    pack
      odd :: Signed n -> Bool
    odd = BitVector n -> Bool
    forall a. Parity a => a -> Bool
    odd (BitVector n -> Bool)
    -> (Signed n -> BitVector n) -> Signed n -> Bool
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Signed n -> BitVector n
    Signed n -> BitVector (BitSize (Signed n))
    forall a. BitPack a => a -> BitVector (BitSize a)
    pack

    instance KnownNat n => Bits (Signed n) where
      .&. :: Signed n -> Signed n -> Signed n
    (.&.)             = Signed n -> Signed n -> Signed n
    forall (n :: Nat). KnownNat n => Signed n -> Signed n -> Signed n
    and#
      .|. :: Signed n -> Signed n -> Signed n
    (.|.)             = Signed n -> Signed n -> Signed n
    forall (n :: Nat). KnownNat n => Signed n -> Signed n -> Signed n
    or#
      xor :: Signed n -> Signed n -> Signed n
    xor               = Signed n -> Signed n -> Signed n
    forall (n :: Nat). KnownNat n => Signed n -> Signed n -> Signed n
    xor#
      complement :: Signed n -> Signed n
    complement        = Signed n -> Signed n
    forall (n :: Nat). KnownNat n => Signed n -> Signed n
    complement#
      zeroBits :: Signed n
    zeroBits          = Signed n
    0
      bit :: Int -> Signed n
    bit Int
    i             = Int -> Bit -> Signed n -> Signed n
    forall a i. (BitPack a, Enum i) => i -> Bit -> a -> a
    replaceBit Int
    i Bit
    high Signed n
    0
      setBit :: Signed n -> Int -> Signed n
    setBit Signed n
    v Int
    i        = Int -> Bit -> Signed n -> Signed n
    forall a i. (BitPack a, Enum i) => i -> Bit -> a -> a
    replaceBit Int
    i Bit
    high Signed n
    v
      clearBit :: Signed n -> Int -> Signed n
    clearBit Signed n
    v Int
    i      = Int -> Bit -> Signed n -> Signed n
    forall a i. (BitPack a, Enum i) => i -> Bit -> a -> a
    replaceBit Int
    i Bit
    low  Signed n
    v
      complementBit :: Signed n -> Int -> Signed n
    complementBit Signed n
    v Int
    i = Int -> Bit -> Signed n -> Signed n
    forall a i. (BitPack a, Enum i) => i -> Bit -> a -> a
    replaceBit Int
    i (Bit -> Bit
    BV.complement## (Signed n
    v Signed n -> Int -> Bit
    forall a i. (BitPack a, Enum i) => a -> i -> Bit
    ! Int
    i)) Signed n
    v
      testBit :: Signed n -> Int -> Bool
    testBit Signed n
    v Int
    i       = Signed n
    v Signed n -> Int -> Bit
    forall a i. (BitPack a, Enum i) => a -> i -> Bit
    ! Int
    i Bit -> Bit -> Bool
    forall a. Eq a => a -> a -> Bool
    == Bit
    1
      bitSizeMaybe :: Signed n -> Maybe Int
    bitSizeMaybe Signed n
    v    = Int -> Maybe Int
    forall a. a -> Maybe a
    Just (Signed n -> Int
    forall (n :: Nat). KnownNat n => Signed n -> Int
    size# Signed n
    v)
      bitSize :: Signed n -> Int
    bitSize           = Signed n -> Int
    forall (n :: Nat). KnownNat n => Signed n -> Int
    size#
      isSigned :: Signed n -> Bool
    isSigned Signed n
    _        = Bool
    True
      shiftL :: Signed n -> Int -> Signed n
    shiftL Signed n
    v Int
    i        = Signed n -> Int -> Signed n
    forall (n :: Nat). KnownNat n => Signed n -> Int -> Signed n
    shiftL# Signed n
    v Int
    i
      shiftR :: Signed n -> Int -> Signed n
    shiftR Signed n
    v Int
    i        = Signed n -> Int -> Signed n
    forall (n :: Nat). KnownNat n => Signed n -> Int -> Signed n
    shiftR# Signed n
    v Int
    i
      rotateL :: Signed n -> Int -> Signed n
    rotateL Signed n
    v Int
    i       = Signed n -> Int -> Signed n
    forall (n :: Nat). KnownNat n => Signed n -> Int -> Signed n
    rotateL# Signed n
    v Int
    i
      rotateR :: Signed n -> Int -> Signed n
    rotateR Signed n
    v Int
    i       = Signed n -> Int -> Signed n
    forall (n :: Nat). KnownNat n => Signed n -> Int -> Signed n
    rotateR# Signed n
    v Int
    i
      popCount :: Signed n -> Int
    popCount Signed n
    s        = BitVector n -> Int
    forall a. Bits a => a -> Int
    popCount (Signed n -> BitVector n
    forall (n :: Nat). KnownNat n => Signed n -> BitVector n
    pack# Signed n
    s)

    and#,or#,xor# :: forall n . KnownNat n => Signed n -> Signed n -> Signed n
    -- See: https://github.com/clash-lang/clash-compiler/pull/2511
    {-# CLASH_OPAQUE and# #-}
    {-# ANN and# hasBlackBox #-}
    and# :: forall (n :: Nat). KnownNat n => Signed n -> Signed n -> Signed n
    and# = \(S Integer
    a) (S Integer
    b) -> Int -> Integer -> Integer -> Integer -> Signed n
    forall (n :: Nat). Int -> Integer -> Integer -> Integer -> Signed n
    fromInteger_INLINE Int
    sz Integer
    mB Integer
    mask (Integer
    a Integer -> Integer -> Integer
    forall a. Bits a => a -> a -> a
    .&. Integer
    b)
      where sz :: Int
    sz   = Integer -> Int
    forall a. Num a => Integer -> a
    fromInteger (Proxy n -> Integer
    forall (n :: Nat) (proxy :: Nat -> Type).
    KnownNat n =>
    proxy n -> Integer
    natVal (forall (t :: Nat). Proxy t
    forall {k} (t :: k). Proxy t
    Proxy @n)) Int -> Int -> Int
    forall a. Num a => a -> a -> a
    - Int
    1
            mB :: Integer
    mB   = Integer
    1 Integer -> Int -> Integer
    forall a. Bits a => a -> Int -> a
    `shiftL` Int
    sz
            mask :: Integer
    mask = Integer
    mB Integer -> Integer -> Integer
    forall a. Num a => a -> a -> a
    - Integer
    1

    -- See: https://github.com/clash-lang/clash-compiler/pull/2511
    {-# CLASH_OPAQUE or# #-}
    {-# ANN or# hasBlackBox #-}
    or# :: forall (n :: Nat). KnownNat n => Signed n -> Signed n -> Signed n
    or# = \(S Integer
    a) (S Integer
    b) -> Int -> Integer -> Integer -> Integer -> Signed n
    forall (n :: Nat). Int -> Integer -> Integer -> Integer -> Signed n
    fromInteger_INLINE Int
    sz Integer
    mB Integer
    mask (Integer
    a Integer -> Integer -> Integer
    forall a. Bits a => a -> a -> a
    .|. Integer
    b)
      where sz :: Int
    sz   = Integer -> Int
    forall a. Num a => Integer -> a
    fromInteger (Proxy n -> Integer
    forall (n :: Nat) (proxy :: Nat -> Type).
    KnownNat n =>
    proxy n -> Integer
    natVal (forall (t :: Nat). Proxy t
    forall {k} (t :: k). Proxy t
    Proxy @n)) Int -> Int -> Int
    forall a. Num a => a -> a -> a
    - Int
    1
            mB :: Integer
    mB   = Integer
    1 Integer -> Int -> Integer
    forall a. Bits a => a -> Int -> a
    `shiftL` Int
    sz
            mask :: Integer
    mask = Integer
    mB Integer -> Integer -> Integer
    forall a. Num a => a -> a -> a
    - Integer
    1

    -- See: https://github.com/clash-lang/clash-compiler/pull/2511
    {-# CLASH_OPAQUE xor# #-}
    {-# ANN xor# hasBlackBox #-}
    xor# :: forall (n :: Nat). KnownNat n => Signed n -> Signed n -> Signed n
    xor# = \(S Integer
    a) (S Integer
    b) -> Int -> Integer -> Integer -> Integer -> Signed n
    forall (n :: Nat). Int -> Integer -> Integer -> Integer -> Signed n
    fromInteger_INLINE Int
    sz Integer
    mB Integer
    mask (Integer -> Integer -> Integer
    forall a. Bits a => a -> a -> a
    xor Integer
    a Integer
    b)
      where sz :: Int
    sz   = Integer -> Int
    forall a. Num a => Integer -> a
    fromInteger (Proxy n -> Integer
    forall (n :: Nat) (proxy :: Nat -> Type).
    KnownNat n =>
    proxy n -> Integer
    natVal (forall (t :: Nat). Proxy t
    forall {k} (t :: k). Proxy t
    Proxy @n)) Int -> Int -> Int
    forall a. Num a => a -> a -> a
    - Int
    1
            mB :: Integer
    mB   = Integer
    1 Integer -> Int -> Integer
    forall a. Bits a => a -> Int -> a
    `shiftL` Int
    sz
            mask :: Integer
    mask = Integer
    mB Integer -> Integer -> Integer
    forall a. Num a => a -> a -> a
    - Integer
    1

    -- See: https://github.com/clash-lang/clash-compiler/pull/2511
    {-# CLASH_OPAQUE complement# #-}
    {-# ANN complement# hasBlackBox #-}
    complement# :: forall n . KnownNat n => Signed n -> Signed n
    complement# :: forall (n :: Nat). KnownNat n => Signed n -> Signed n
    complement# = \(S Integer
    a) -> Int -> Integer -> Integer -> Integer -> Signed n
    forall (n :: Nat). Int -> Integer -> Integer -> Integer -> Signed n
    fromInteger_INLINE Int
    sz Integer
    mB Integer
    mask (Integer -> Integer
    forall a. Bits a => a -> a
    complement Integer
    a)
      where sz :: Int
    sz   = Integer -> Int
    forall a. Num a => Integer -> a
    fromInteger (Proxy n -> Integer
    forall (n :: Nat) (proxy :: Nat -> Type).
    KnownNat n =>
    proxy n -> Integer
    natVal (forall (t :: Nat). Proxy t
    forall {k} (t :: k). Proxy t
    Proxy @n)) Int -> Int -> Int
    forall a. Num a => a -> a -> a
    - Int
    1
            mB :: Integer
    mB   = Integer
    1 Integer -> Int -> Integer
    forall a. Bits a => a -> Int -> a
    `shiftL` Int
    sz
            mask :: Integer
    mask = Integer
    mB Integer -> Integer -> Integer
    forall a. Num a => a -> a -> a
    - Integer
    1

    shiftL#,shiftR#,rotateL#,rotateR# :: forall n . KnownNat n => Signed n -> Int -> Signed n
    -- See: https://github.com/clash-lang/clash-compiler/pull/2511
    {-# CLASH_OPAQUE shiftL# #-}
    {-# ANN shiftL# hasBlackBox #-}
    shiftL# :: forall (n :: Nat). KnownNat n => Signed n -> Int -> Signed n
    shiftL# = \(S Integer
    n) Int
    b ->
      if | Int
    b Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    < Int
    0     -> String -> Signed n
    forall a. HasCallStack => String -> a
    error (String -> Signed n) -> String -> Signed n
    forall a b. (a -> b) -> a -> b
    $ String
    "'shiftL' undefined for negative number: " String -> ShowS
    forall a. [a] -> [a] -> [a]
    ++ Int -> String
    forall a. Show a => a -> String
    show Int
    b
         | Int
    b Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    > Int
    sz    -> Integer -> Signed n
    forall (n :: Nat). Integer -> Signed n
    S Integer
    0
         | Bool
    otherwise -> Int -> Integer -> Integer -> Integer -> Signed n
    forall (n :: Nat). Int -> Integer -> Integer -> Integer -> Signed n
    fromInteger_INLINE Int
    sz Integer
    mB Integer
    mask (Integer -> Int -> Integer
    forall a. Bits a => a -> Int -> a
    shiftL Integer
    n Int
    b)
     where
      sz :: Int
    sz   = Integer -> Int
    forall a. Num a => Integer -> a
    fromInteger (Proxy n -> Integer
    forall (n :: Nat) (proxy :: Nat -> Type).
    KnownNat n =>
    proxy n -> Integer
    natVal (forall (t :: Nat). Proxy t
    forall {k} (t :: k). Proxy t
    Proxy @n)) Int -> Int -> Int
    forall a. Num a => a -> a -> a
    - Int
    1
      mB :: Integer
    mB   = Integer
    1 Integer -> Int -> Integer
    forall a. Bits a => a -> Int -> a
    `shiftL` Int
    sz
      mask :: Integer
    mask = Integer
    mB Integer -> Integer -> Integer
    forall a. Num a => a -> a -> a
    - Integer
    1

    -- See: https://github.com/clash-lang/clash-compiler/pull/2511
    {-# CLASH_OPAQUE shiftR# #-}
    {-# ANN shiftR# hasBlackBox #-}
    shiftR# :: forall (n :: Nat). KnownNat n => Signed n -> Int -> Signed n
    shiftR# =
      \(S Integer
    n) Int
    b ->
        if Int
    b Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    >= Int
    0 then
          Int -> Integer -> Integer -> Integer -> Signed n
    forall (n :: Nat). Int -> Integer -> Integer -> Integer -> Signed n
    fromInteger_INLINE Int
    sz Integer
    mB Integer
    mask (Integer -> Int -> Integer
    forall a. Bits a => a -> Int -> a
    shiftR Integer
    n Int
    b)
        else
          String -> Signed n
    forall a. HasCallStack => String -> a
    error (String -> Signed n) -> String -> Signed n
    forall a b. (a -> b) -> a -> b
    $ String
    "'shiftR' undefined for negative number: " String -> ShowS
    forall a. [a] -> [a] -> [a]
    ++ Int -> String
    forall a. Show a => a -> String
    show Int
    b
     where
      sz :: Int
    sz   = Integer -> Int
    forall a. Num a => Integer -> a
    fromInteger (Proxy n -> Integer
    forall (n :: Nat) (proxy :: Nat -> Type).
    KnownNat n =>
    proxy n -> Integer
    natVal (forall (t :: Nat). Proxy t
    forall {k} (t :: k). Proxy t
    Proxy @n)) Int -> Int -> Int
    forall a. Num a => a -> a -> a
    - Int
    1
      mB :: Integer
    mB   = Integer
    1 Integer -> Int -> Integer
    forall a. Bits a => a -> Int -> a
    `shiftL` Int
    sz
      mask :: Integer
    mask = Integer
    mB Integer -> Integer -> Integer
    forall a. Num a => a -> a -> a
    - Integer
    1

    -- See: https://github.com/clash-lang/clash-compiler/pull/2511
    {-# CLASH_OPAQUE rotateL# #-}
    {-# ANN rotateL# hasBlackBox #-}
    rotateL# :: forall (n :: Nat). KnownNat n => Signed n -> Int -> Signed n
    rotateL# =
      \(S Integer
    n) Int
    b ->
        if Int
    b Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    >= Int
    0 then
          let l :: Integer
    l    = Integer -> Int -> Integer
    forall a. Bits a => a -> Int -> a
    shiftL Integer
    n Int
    b'
              r :: Integer
    r    = Integer -> Int -> Integer
    forall a. Bits a => a -> Int -> a
    shiftR Integer
    n Int
    b'' Integer -> Integer -> Integer
    forall a. Bits a => a -> a -> a
    .&. Integer
    mask
              mask :: Integer
    mask = Integer
    2 Integer -> Int -> Integer
    forall a b. (Num a, Integral b) => a -> b -> a
    ^ Int
    b' Integer -> Integer -> Integer
    forall a. Num a => a -> a -> a
    - Integer
    1

              b' :: Int
    b'   = Int
    b Int -> Int -> Int
    forall a. Integral a => a -> a -> a
    `mod` Int
    sz
              b'' :: Int
    b''  = Int
    sz Int -> Int -> Int
    forall a. Num a => a -> a -> a
    - Int
    b'
          in  Int -> Integer -> Integer -> Integer -> Signed n
    forall (n :: Nat). Int -> Integer -> Integer -> Integer -> Signed n
    fromInteger_INLINE Int
    sz1 Integer
    mB Integer
    maskM (Integer
    l Integer -> Integer -> Integer
    forall a. Bits a => a -> a -> a
    .|. Integer
    r)
        else
          String -> Signed n
    forall a. HasCallStack => String -> a
    error (String -> Signed n) -> String -> Signed n
    forall a b. (a -> b) -> a -> b
    $ String
    "'rotateL undefined for negative number: " String -> ShowS
    forall a. [a] -> [a] -> [a]
    ++ Int -> String
    forall a. Show a => a -> String
    show Int
    b
     where
      sz :: Int
    sz    = Integer -> Int
    forall a. Num a => Integer -> a
    fromInteger (Proxy n -> Integer
    forall (n :: Nat) (proxy :: Nat -> Type).
    KnownNat n =>
    proxy n -> Integer
    natVal (forall (t :: Nat). Proxy t
    forall {k} (t :: k). Proxy t
    Proxy @n))
      sz1 :: Int
    sz1   = Int
    szInt -> Int -> Int
    forall a. Num a => a -> a -> a
    -Int
    1
      mB :: Integer
    mB    = Integer
    1 Integer -> Int -> Integer
    forall a. Bits a => a -> Int -> a
    `shiftL` Int
    sz1
      maskM :: Integer
    maskM = Integer
    mB Integer -> Integer -> Integer
    forall a. Num a => a -> a -> a
    - Integer
    1

    -- See: https://github.com/clash-lang/clash-compiler/pull/2511
    {-# CLASH_OPAQUE rotateR# #-}
    {-# ANN rotateR# hasBlackBox #-}
    rotateR# :: forall (n :: Nat). KnownNat n => Signed n -> Int -> Signed n
    rotateR# =
      \(S Integer
    n) Int
    b ->
        if Int
    b Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    >= Int
    0 then
          let l :: Integer
    l    = Integer -> Int -> Integer
    forall a. Bits a => a -> Int -> a
    shiftR Integer
    n Int
    b' Integer -> Integer -> Integer
    forall a. Bits a => a -> a -> a
    .&. Integer
    mask
              r :: Integer
    r    = Integer -> Int -> Integer
    forall a. Bits a => a -> Int -> a
    shiftL Integer
    n Int
    b''
              mask :: Integer
    mask = Integer
    2 Integer -> Int -> Integer
    forall a b. (Num a, Integral b) => a -> b -> a
    ^ Int
    b'' Integer -> Integer -> Integer
    forall a. Num a => a -> a -> a
    - Integer
    1

              b' :: Int
    b'  = Int
    b Int -> Int -> Int
    forall a. Integral a => a -> a -> a
    `mod` Int
    sz
              b'' :: Int
    b'' = Int
    sz Int -> Int -> Int
    forall a. Num a => a -> a -> a
    - Int
    b'
          in  Int -> Integer -> Integer -> Integer -> Signed n
    forall (n :: Nat). Int -> Integer -> Integer -> Integer -> Signed n
    fromInteger_INLINE Int
    sz1 Integer
    mB Integer
    maskM (Integer
    l Integer -> Integer -> Integer
    forall a. Bits a => a -> a -> a
    .|. Integer
    r)
        else
          String -> Signed n
    forall a. HasCallStack => String -> a
    error (String -> Signed n) -> String -> Signed n
    forall a b. (a -> b) -> a -> b
    $ String
    "'rotateR' undefined for negative number: " String -> ShowS
    forall a. [a] -> [a] -> [a]
    ++ Int -> String
    forall a. Show a => a -> String
    show Int
    b
     where
      sz :: Int
    sz    = Integer -> Int
    forall a. Num a => Integer -> a
    fromInteger (Proxy n -> Integer
    forall (n :: Nat) (proxy :: Nat -> Type).
    KnownNat n =>
    proxy n -> Integer
    natVal (forall (t :: Nat). Proxy t
    forall {k} (t :: k). Proxy t
    Proxy @n))
      sz1 :: Int
    sz1   = Int
    sz Int -> Int -> Int
    forall a. Num a => a -> a -> a
    - Int
    1
      mB :: Integer
    mB    = Integer
    1 Integer -> Int -> Integer
    forall a. Bits a => a -> Int -> a
    `shiftL` Int
    sz1
      maskM :: Integer
    maskM = Integer
    mB Integer -> Integer -> Integer
    forall a. Num a => a -> a -> a
    - Integer
    1

    instance KnownNat n => FiniteBits (Signed n) where
      finiteBitSize :: Signed n -> Int
    finiteBitSize        = Signed n -> Int
    forall (n :: Nat). KnownNat n => Signed n -> Int
    size#
      countLeadingZeros :: Signed n -> Int
    countLeadingZeros  Signed n
    s = BitVector n -> Int
    forall b. FiniteBits b => b -> Int
    countLeadingZeros  (Signed n -> BitVector n
    forall (n :: Nat). KnownNat n => Signed n -> BitVector n
    pack# Signed n
    s)
      countTrailingZeros :: Signed n -> Int
    countTrailingZeros Signed n
    s = BitVector n -> Int
    forall b. FiniteBits b => b -> Int
    countTrailingZeros (Signed n -> BitVector n
    forall (n :: Nat). KnownNat n => Signed n -> BitVector n
    pack# Signed n
    s)

    instance Resize Signed where
      resize :: forall (a :: Nat) (b :: Nat).
    (KnownNat a, KnownNat b) =>
    Signed a -> Signed b
    resize       = Signed a -> Signed b
    forall (m :: Nat) (n :: Nat).
    (KnownNat n, KnownNat m) =>
    Signed n -> Signed m
    resize#
      zeroExtend :: forall (a :: Nat) (b :: Nat).
    (KnownNat a, KnownNat b) =>
    Signed a -> Signed (b + a)
    zeroExtend Signed a
    s = BitVector (b + a) -> Signed (b + a)
    forall (n :: Nat). KnownNat n => BitVector n -> Signed n
    unpack# (BitVector b
    0 BitVector b -> BitVector a -> BitVector (b + a)
    forall (m :: Nat) (n :: Nat).
    KnownNat m =>
    BitVector n -> BitVector m -> BitVector (n + m)
    ++# Signed a -> BitVector (BitSize (Signed a))
    forall a. BitPack a => a -> BitVector (BitSize a)
    pack Signed a
    s)
      truncateB :: forall (a :: Nat) (b :: Nat).
    KnownNat a =>
    Signed (a + b) -> Signed a
    truncateB    = Signed (a + b) -> Signed a
    forall (a :: Nat) (b :: Nat).
    KnownNat a =>
    Signed (a + b) -> Signed a
    truncateB#

    -- See: https://github.com/clash-lang/clash-compiler/pull/2511
    {-# CLASH_OPAQUE resize# #-}
    {-# ANN resize# hasBlackBox #-}
    resize# :: forall m n . (KnownNat n, KnownNat m) => Signed n -> Signed m
    resize# :: forall (m :: Nat) (n :: Nat).
    (KnownNat n, KnownNat m) =>
    Signed n -> Signed m
    resize# s :: Signed n
    s@(S Integer
    i)
      | forall (n :: Nat). KnownNat n => Nat
    natToNatural @m Nat -> Nat -> Bool
    forall a. Eq a => a -> a -> Bool
    == Nat
    0 = Integer -> Signed m
    forall (n :: Nat). Integer -> Signed n
    S Integer
    0
      | Integer
    n' Integer -> Integer -> Bool
    forall a. Ord a => a -> a -> Bool
    <= Integer
    m'  = Signed m
    extended
      | Bool
    otherwise = Signed m
    truncated
      where
        n :: Int
    n  = Integer -> Int
    forall a. Num a => Integer -> a
    fromInteger (Signed n -> Integer
    forall (n :: Nat) (proxy :: Nat -> Type).
    KnownNat n =>
    proxy n -> Integer
    natVal Signed n
    s)
        n' :: Integer
    n' = Integer -> Int -> Integer
    forall a. Bits a => a -> Int -> a
    shiftL Integer
    1 Int
    n
        m' :: Integer
    m' = Integer -> Int -> Integer
    forall a. Bits a => a -> Int -> a
    shiftL Integer
    mask Int
    1
        extended :: Signed m
    extended = Integer -> Signed m
    forall (n :: Nat). Integer -> Signed n
    S Integer
    i

        mask :: Integer
    mask      = Integer
    1 Integer -> Int -> Integer
    forall a. Bits a => a -> Int -> a
    `shiftL` Integer -> Int
    forall a. Num a => Integer -> a
    fromInteger (Proxy m -> Integer
    forall (n :: Nat) (proxy :: Nat -> Type).
    KnownNat n =>
    proxy n -> Integer
    natVal (forall (t :: Nat). Proxy t
    forall {k} (t :: k). Proxy t
    Proxy @m) Integer -> Integer -> Integer
    forall a. Num a => a -> a -> a
    -Integer
    1)
        i' :: Integer
    i'        = Integer
    i Integer -> Integer -> Integer
    forall a. Integral a => a -> a -> a
    `mod` Integer
    mask
        truncated :: Signed m
    truncated = if Integer -> Int -> Bool
    forall a. Bits a => a -> Int -> Bool
    testBit Integer
    i (Int
    nInt -> Int -> Int
    forall a. Num a => a -> a -> a
    -Int
    1)
                       then Integer -> Signed m
    forall (n :: Nat). Integer -> Signed n
    S (Integer
    i' Integer -> Integer -> Integer
    forall a. Num a => a -> a -> a
    - Integer
    mask)
                       else Integer -> Signed m
    forall (n :: Nat). Integer -> Signed n
    S Integer
    i'

    -- See: https://github.com/clash-lang/clash-compiler/pull/2511
    {-# CLASH_OPAQUE truncateB# #-}
    {-# ANN truncateB# hasBlackBox #-}
    truncateB# :: forall m n . KnownNat m => Signed (m + n) -> Signed m
    truncateB# :: forall (a :: Nat) (b :: Nat).
    KnownNat a =>
    Signed (a + b) -> Signed a
    truncateB# = \(S Integer
    n) -> Int -> Integer -> Integer -> Integer -> Signed m
    forall (n :: Nat). Int -> Integer -> Integer -> Integer -> Signed n
    fromInteger_INLINE Int
    sz Integer
    mB Integer
    mask Integer
    n
      where sz :: Int
    sz   = Integer -> Int
    forall a. Num a => Integer -> a
    fromInteger (Proxy m -> Integer
    forall (n :: Nat) (proxy :: Nat -> Type).
    KnownNat n =>
    proxy n -> Integer
    natVal (forall (t :: Nat). Proxy t
    forall {k} (t :: k). Proxy t
    Proxy @m)) Int -> Int -> Int
    forall a. Num a => a -> a -> a
    - Int
    1
            mB :: Integer
    mB   = Integer
    1 Integer -> Int -> Integer
    forall a. Bits a => a -> Int -> a
    `shiftL` Int
    sz
            mask :: Integer
    mask = Integer
    mB Integer -> Integer -> Integer
    forall a. Num a => a -> a -> a
    - Integer
    1

    instance KnownNat n => Default (Signed n) where
      def :: Signed n
    def = Integer -> Signed n
    forall (n :: Nat). KnownNat n => Integer -> Signed n
    fromInteger# Integer
    0

    instance KnownNat n => Lift (Signed n) where
      lift :: forall (m :: Type -> Type). Quote m => Signed n -> m Exp
    lift s :: Signed n
    s@(S Integer
    i) = m Exp -> m Type -> m Exp
    forall (m :: Type -> Type). Quote m => m Exp -> m Type -> m Exp
    sigE [| fromInteger# i |] (Integer -> m Type
    forall (m :: Type -> Type). Quote m => Integer -> m Type
    decSigned (Signed n -> Integer
    forall (n :: Nat) (proxy :: Nat -> Type).
    KnownNat n =>
    proxy n -> Integer
    natVal Signed n
    s))
      {-# NOINLINE lift #-}
    #if MIN_VERSION_template_haskell(2,16,0)
      liftTyped :: forall (m :: Type -> Type).
    Quote m =>
    Signed n -> Code m (Signed n)
    liftTyped = Signed n -> Code m (Signed n)
    forall a (m :: Type -> Type). (Lift a, Quote m) => a -> Code m a
    liftTypedFromUntyped
    #endif

    #if MIN_VERSION_template_haskell(2,17,0)
    decSigned :: Quote m => Integer -> m Type
    #else
    decSigned :: Integer -> TypeQ
    #endif
    decSigned :: forall (m :: Type -> Type). Quote m => Integer -> m Type
    decSigned Integer
    n = m Type -> m Type -> m Type
    forall (m :: Type -> Type). Quote m => m Type -> m Type -> m Type
    appT (Name -> m Type
    forall (m :: Type -> Type). Quote m => Name -> m Type
    conT ''Signed) (m TyLit -> m Type
    forall (m :: Type -> Type). Quote m => m TyLit -> m Type
    litT (m TyLit -> m Type) -> m TyLit -> m Type
    forall a b. (a -> b) -> a -> b
    $ Integer -> m TyLit
    forall (m :: Type -> Type). Quote m => Integer -> m TyLit
    numTyLit Integer
    n)

    instance KnownNat n => SaturatingNum (Signed n) where
      satAdd :: SaturationMode -> Signed n -> Signed n -> Signed n
    satAdd SaturationMode
    SatWrap  Signed n
    a Signed n
    b = Signed n
    a Signed n -> Signed n -> Signed n
    forall (n :: Nat). KnownNat n => Signed n -> Signed n -> Signed n
    +# Signed n
    b
      satAdd SaturationMode
    SatBound Signed n
    a Signed n
    b =
        let r :: Signed (Max n n + 1)
    r      = Signed n -> Signed n -> Signed (Max n n + 1)
    forall (m :: Nat) (n :: Nat).
    Signed m -> Signed n -> Signed (Max m n + 1)
    plus# Signed n
    a Signed n
    b
            (BitVector 1
    _,BitVector n
    r') = Signed (n + 1) -> (BitVector 1, BitVector n)
    forall a (m :: Nat) (n :: Nat).
    (BitPack a, BitSize a ~ (m + n), KnownNat n) =>
    a -> (BitVector m, BitVector n)
    split Signed (n + 1)
    Signed (Max n n + 1)
    r
        in  case Signed (n + 1) -> Bit
    forall a. BitPack a => a -> Bit
    msb Signed (n + 1)
    Signed (Max n n + 1)
    r Bit -> Bit -> Bit
    forall a. Bits a => a -> a -> a
    `xor` BitVector n -> Bit
    forall a. BitPack a => a -> Bit
    msb BitVector n
    r' of
              Bit
    0 -> BitVector n -> Signed n
    forall (n :: Nat). KnownNat n => BitVector n -> Signed n
    unpack# BitVector n
    r'
              Bit
    _ -> case Signed n -> Bit
    forall a. BitPack a => a -> Bit
    msb Signed n
    a Bit -> Bit -> Bit
    forall a. Bits a => a -> a -> a
    .&. Signed n -> Bit
    forall a. BitPack a => a -> Bit
    msb Signed n
    b of
                Bit
    0 -> Signed n
    forall (n :: Nat). KnownNat n => Signed n
    maxBound#
                Bit
    _ -> Signed n
    forall (n :: Nat). KnownNat n => Signed n
    minBound#
      satAdd SaturationMode
    SatZero Signed n
    a Signed n
    b =
        let r :: Signed (Max n n + 1)
    r      = Signed n -> Signed n -> Signed (Max n n + 1)
    forall (m :: Nat) (n :: Nat).
    Signed m -> Signed n -> Signed (Max m n + 1)
    plus# Signed n
    a Signed n
    b
            (BitVector 1
    _,BitVector n
    r') = Signed (n + 1) -> (BitVector 1, BitVector n)
    forall a (m :: Nat) (n :: Nat).
    (BitPack a, BitSize a ~ (m + n), KnownNat n) =>
    a -> (BitVector m, BitVector n)
    split Signed (n + 1)
    Signed (Max n n + 1)
    r
        in  case Signed (n + 1) -> Bit
    forall a. BitPack a => a -> Bit
    msb Signed (n + 1)
    Signed (Max n n + 1)
    r Bit -> Bit -> Bit
    forall a. Bits a => a -> a -> a
    `xor` BitVector n -> Bit
    forall a. BitPack a => a -> Bit
    msb BitVector n
    r' of
              Bit
    0 -> BitVector n -> Signed n
    forall (n :: Nat). KnownNat n => BitVector n -> Signed n
    unpack# BitVector n
    r'
              Bit
    _ -> Integer -> Signed n
    forall (n :: Nat). KnownNat n => Integer -> Signed n
    fromInteger# Integer
    0
      satAdd SaturationMode
    SatError Signed n
    a Signed n
    b =
        let r :: Signed (Max n n + 1)
    r      = Signed n -> Signed n -> Signed (Max n n + 1)
    forall (m :: Nat) (n :: Nat).
    Signed m -> Signed n -> Signed (Max m n + 1)
    plus# Signed n
    a Signed n
    b
            (BitVector 1
    _,BitVector n
    r') = Signed (n + 1) -> (BitVector 1, BitVector n)
    forall a (m :: Nat) (n :: Nat).
    (BitPack a, BitSize a ~ (m + n), KnownNat n) =>
    a -> (BitVector m, BitVector n)
    split Signed (n + 1)
    Signed (Max n n + 1)
    r
        in  case Signed (n + 1) -> Bit
    forall a. BitPack a => a -> Bit
    msb Signed (n + 1)
    Signed (Max n n + 1)
    r Bit -> Bit -> Bit
    forall a. Bits a => a -> a -> a
    `xor` BitVector n -> Bit
    forall a. BitPack a => a -> Bit
    msb BitVector n
    r' of
              Bit
    0 -> BitVector n -> Signed n
    forall (n :: Nat). KnownNat n => BitVector n -> Signed n
    unpack# BitVector n
    r'
              Bit
    _ -> String -> Signed n
    forall a. HasCallStack => String -> a
    errorX String
    "Signed.satAdd: overflow/underflow"
      satAdd SaturationMode
    SatSymmetric Signed n
    a Signed n
    b =
        let r :: Signed (Max n n + 1)
    r      = Signed n -> Signed n -> Signed (Max n n + 1)
    forall (m :: Nat) (n :: Nat).
    Signed m -> Signed n -> Signed (Max m n + 1)
    plus# Signed n
    a Signed n
    b
            (BitVector 1
    _,BitVector n
    r') = Signed (n + 1) -> (BitVector 1, BitVector n)
    forall a (m :: Nat) (n :: Nat).
    (BitPack a, BitSize a ~ (m + n), KnownNat n) =>
    a -> (BitVector m, BitVector n)
    split Signed (n + 1)
    Signed (Max n n + 1)
    r
        in  case Signed (n + 1) -> Bit
    forall a. BitPack a => a -> Bit
    msb Signed (n + 1)
    Signed (Max n n + 1)
    r Bit -> Bit -> Bit
    forall a. Bits a => a -> a -> a
    `xor` BitVector n -> Bit
    forall a. BitPack a => a -> Bit
    msb BitVector n
    r' of
              Bit
    0 -> BitVector n -> Signed n
    forall (n :: Nat). KnownNat n => BitVector n -> Signed n
    unpack# BitVector n
    r'
              Bit
    _ -> case Signed n -> Bit
    forall a. BitPack a => a -> Bit
    msb Signed n
    a Bit -> Bit -> Bit
    forall a. Bits a => a -> a -> a
    .&. Signed n -> Bit
    forall a. BitPack a => a -> Bit
    msb Signed n
    b of
                Bit
    0 -> Signed n
    forall (n :: Nat). KnownNat n => Signed n
    maxBound#
                Bit
    _ -> Signed n
    forall (n :: Nat). KnownNat n => Signed n
    minBoundSym#

      satSub :: SaturationMode -> Signed n -> Signed n -> Signed n
    satSub SaturationMode
    SatWrap Signed n
    a Signed n
    b = Signed n
    a Signed n -> Signed n -> Signed n
    forall (n :: Nat). KnownNat n => Signed n -> Signed n -> Signed n
    -# Signed n
    b
      satSub SaturationMode
    SatBound Signed n
    a Signed n
    b =
        let r :: Signed (Max n n + 1)
    r      = Signed n -> Signed n -> Signed (Max n n + 1)
    forall (m :: Nat) (n :: Nat).
    Signed m -> Signed n -> Signed (Max m n + 1)
    minus# Signed n
    a Signed n
    b
            (BitVector 1
    _,BitVector n
    r') = Signed (n + 1) -> (BitVector 1, BitVector n)
    forall a (m :: Nat) (n :: Nat).
    (BitPack a, BitSize a ~ (m + n), KnownNat n) =>
    a -> (BitVector m, BitVector n)
    split Signed (n + 1)
    Signed (Max n n + 1)
    r
        in  case Signed (n + 1) -> Bit
    forall a. BitPack a => a -> Bit
    msb Signed (n + 1)
    Signed (Max n n + 1)
    r Bit -> Bit -> Bit
    forall a. Bits a => a -> a -> a
    `xor` BitVector n -> Bit
    forall a. BitPack a => a -> Bit
    msb BitVector n
    r' of
              Bit
    0 -> BitVector n -> Signed n
    forall (n :: Nat). KnownNat n => BitVector n -> Signed n
    unpack# BitVector n
    r'
              Bit
    _ -> case Bit -> BitVector 1
    BV.pack# (Signed n -> Bit
    forall a. BitPack a => a -> Bit
    msb Signed n
    a) BitVector 1 -> BitVector 1 -> BitVector (1 + 1)
    forall (m :: Nat) (n :: Nat).
    KnownNat m =>
    BitVector n -> BitVector m -> BitVector (n + m)
    ++# Bit -> BitVector 1
    BV.pack# (Signed n -> Bit
    forall a. BitPack a => a -> Bit
    msb Signed n
    b) of
                BitVector (1 + 1)
    2 -> Signed n
    forall (n :: Nat). KnownNat n => Signed n
    minBound#
                BitVector (1 + 1)
    _ -> Signed n
    forall (n :: Nat). KnownNat n => Signed n
    maxBound#
      satSub SaturationMode
    SatZero Signed n
    a Signed n
    b =
        let r :: Signed (Max n n + 1)
    r      = Signed n -> Signed n -> Signed (Max n n + 1)
    forall (m :: Nat) (n :: Nat).
    Signed m -> Signed n -> Signed (Max m n + 1)
    minus# Signed n
    a Signed n
    b
            (BitVector 1
    _,BitVector n
    r') = Signed (n + 1) -> (BitVector 1, BitVector n)
    forall a (m :: Nat) (n :: Nat).
    (BitPack a, BitSize a ~ (m + n), KnownNat n) =>
    a -> (BitVector m, BitVector n)
    split Signed (n + 1)
    Signed (Max n n + 1)
    r
        in  case Signed (n + 1) -> Bit
    forall a. BitPack a => a -> Bit
    msb Signed (n + 1)
    Signed (Max n n + 1)
    r Bit -> Bit -> Bit
    forall a. Bits a => a -> a -> a
    `xor` BitVector n -> Bit
    forall a. BitPack a => a -> Bit
    msb BitVector n
    r' of
              Bit
    0 -> BitVector n -> Signed n
    forall (n :: Nat). KnownNat n => BitVector n -> Signed n
    unpack# BitVector n
    r'
              Bit
    _ -> Integer -> Signed n
    forall (n :: Nat). KnownNat n => Integer -> Signed n
    fromInteger# Integer
    0
      satSub SaturationMode
    SatError Signed n
    a Signed n
    b =
        let r :: Signed (Max n n + 1)
    r      = Signed n -> Signed n -> Signed (Max n n + 1)
    forall (m :: Nat) (n :: Nat).
    Signed m -> Signed n -> Signed (Max m n + 1)
    minus# Signed n
    a Signed n
    b
            (BitVector 1
    _,BitVector n
    r') = Signed (n + 1) -> (BitVector 1, BitVector n)
    forall a (m :: Nat) (n :: Nat).
    (BitPack a, BitSize a ~ (m + n), KnownNat n) =>
    a -> (BitVector m, BitVector n)
    split Signed (n + 1)
    Signed (Max n n + 1)
    r
        in  case Signed (n + 1) -> Bit
    forall a. BitPack a => a -> Bit
    msb Signed (n + 1)
    Signed (Max n n + 1)
    r Bit -> Bit -> Bit
    forall a. Bits a => a -> a -> a
    `xor` BitVector n -> Bit
    forall a. BitPack a => a -> Bit
    msb BitVector n
    r' of
              Bit
    0 -> BitVector n -> Signed n
    forall (n :: Nat). KnownNat n => BitVector n -> Signed n
    unpack# BitVector n
    r'
              Bit
    _ -> String -> Signed n
    forall a. HasCallStack => String -> a
    errorX String
    "Signed.satSub: overflow/underflow"
      satSub SaturationMode
    SatSymmetric Signed n
    a Signed n
    b =
        let r :: Signed (Max n n + 1)
    r      = Signed n -> Signed n -> Signed (Max n n + 1)
    forall (m :: Nat) (n :: Nat).
    Signed m -> Signed n -> Signed (Max m n + 1)
    minus# Signed n
    a Signed n
    b
            (BitVector 1
    _,BitVector n
    r') = Signed (n + 1) -> (BitVector 1, BitVector n)
    forall a (m :: Nat) (n :: Nat).
    (BitPack a, BitSize a ~ (m + n), KnownNat n) =>
    a -> (BitVector m, BitVector n)
    split Signed (n + 1)
    Signed (Max n n + 1)
    r
        in  case Signed (n + 1) -> Bit
    forall a. BitPack a => a -> Bit
    msb Signed (n + 1)
    Signed (Max n n + 1)
    r Bit -> Bit -> Bit
    forall a. Bits a => a -> a -> a
    `xor` BitVector n -> Bit
    forall a. BitPack a => a -> Bit
    msb BitVector n
    r' of
              Bit
    0 -> BitVector n -> Signed n
    forall (n :: Nat). KnownNat n => BitVector n -> Signed n
    unpack# BitVector n
    r'
              Bit
    _ -> case Bit -> BitVector 1
    BV.pack# (Signed n -> Bit
    forall a. BitPack a => a -> Bit
    msb Signed n
    a) BitVector 1 -> BitVector 1 -> BitVector (1 + 1)
    forall (m :: Nat) (n :: Nat).
    KnownNat m =>
    BitVector n -> BitVector m -> BitVector (n + m)
    ++# Bit -> BitVector 1
    BV.pack# (Signed n -> Bit
    forall a. BitPack a => a -> Bit
    msb Signed n
    b) of
                BitVector (1 + 1)
    2 -> Signed n
    forall (n :: Nat). KnownNat n => Signed n
    minBoundSym#
                BitVector (1 + 1)
    _ -> Signed n
    forall (n :: Nat). KnownNat n => Signed n
    maxBound#

      satMul :: SaturationMode -> Signed n -> Signed n -> Signed n
    satMul SaturationMode
    SatWrap Signed n
    a Signed n
    b = Signed n
    a Signed n -> Signed n -> Signed n
    forall (n :: Nat). KnownNat n => Signed n -> Signed n -> Signed n
    *# Signed n
    b
      satMul SaturationMode
    SatBound Signed n
    a Signed n
    b =
        let r :: Signed (n + n)
    r        = Signed n -> Signed n -> Signed (n + n)
    forall (m :: Nat) (n :: Nat).
    Signed m -> Signed n -> Signed (m + n)
    times# Signed n
    a Signed n
    b
            (BitVector n
    rL,BitVector n
    rR)  = Signed (n + n) -> (BitVector n, BitVector n)
    forall a (m :: Nat) (n :: Nat).
    (BitPack a, BitSize a ~ (m + n), KnownNat n) =>
    a -> (BitVector m, BitVector n)
    split Signed (n + n)
    r
            overflow :: Bit
    overflow = Bit -> Bit
    forall a. Bits a => a -> a
    complement (BitVector (1 + n) -> Bit
    forall a. BitPack a => a -> Bit
    reduceOr (Bit -> BitVector 1
    BV.pack# (BitVector n -> Bit
    forall a. BitPack a => a -> Bit
    msb BitVector n
    rR) BitVector 1 -> BitVector n -> BitVector (1 + n)
    forall (m :: Nat) (n :: Nat).
    KnownNat m =>
    BitVector n -> BitVector m -> BitVector (n + m)
    ++# BitVector n -> BitVector (BitSize (BitVector n))
    forall a. BitPack a => a -> BitVector (BitSize a)
    pack BitVector n
    rL)) Bit -> Bit -> Bit
    forall a. Bits a => a -> a -> a
    .|.
                       BitVector (1 + n) -> Bit
    forall a. BitPack a => a -> Bit
    reduceAnd (Bit -> BitVector 1
    BV.pack# (BitVector n -> Bit
    forall a. BitPack a => a -> Bit
    msb BitVector n
    rR) BitVector 1 -> BitVector n -> BitVector (1 + n)
    forall (m :: Nat) (n :: Nat).
    KnownNat m =>
    BitVector n -> BitVector m -> BitVector (n + m)
    ++# BitVector n -> BitVector (BitSize (BitVector n))
    forall a. BitPack a => a -> BitVector (BitSize a)
    pack BitVector n
    rL)
        in  case Bit
    overflow of
              Bit
    1 -> BitVector n -> Signed n
    forall (n :: Nat). KnownNat n => BitVector n -> Signed n
    unpack# BitVector n
    rR
              Bit
    _ -> case BitVector n -> Bit
    forall a. BitPack a => a -> Bit
    msb BitVector n
    rL of
                Bit
    0 -> Signed n
    forall (n :: Nat). KnownNat n => Signed n
    maxBound#
                Bit
    _ -> Signed n
    forall (n :: Nat). KnownNat n => Signed n
    minBound#
      satMul SaturationMode
    SatZero Signed n
    a Signed n
    b =
        let r :: Signed (n + n)
    r        = Signed n -> Signed n -> Signed (n + n)
    forall (m :: Nat) (n :: Nat).
    Signed m -> Signed n -> Signed (m + n)
    times# Signed n
    a Signed n
    b
            (BitVector n
    rL,BitVector n
    rR)  = Signed (n + n) -> (BitVector n, BitVector n)
    forall a (m :: Nat) (n :: Nat).
    (BitPack a, BitSize a ~ (m + n), KnownNat n) =>
    a -> (BitVector m, BitVector n)
    split Signed (n + n)
    r
            overflow :: Bit
    overflow = Bit -> Bit
    forall a. Bits a => a -> a
    complement (BitVector (1 + n) -> Bit
    forall a. BitPack a => a -> Bit
    reduceOr (Bit -> BitVector 1
    BV.pack# (BitVector n -> Bit
    forall a. BitPack a => a -> Bit
    msb BitVector n
    rR) BitVector 1 -> BitVector n -> BitVector (1 + n)
    forall (m :: Nat) (n :: Nat).
    KnownNat m =>
    BitVector n -> BitVector m -> BitVector (n + m)
    ++# BitVector n -> BitVector (BitSize (BitVector n))
    forall a. BitPack a => a -> BitVector (BitSize a)
    pack BitVector n
    rL)) Bit -> Bit -> Bit
    forall a. Bits a => a -> a -> a
    .|.
                       BitVector (1 + n) -> Bit
    forall a. BitPack a => a -> Bit
    reduceAnd (Bit -> BitVector 1
    BV.pack# (BitVector n -> Bit
    forall a. BitPack a => a -> Bit
    msb BitVector n
    rR) BitVector 1 -> BitVector n -> BitVector (1 + n)
    forall (m :: Nat) (n :: Nat).
    KnownNat m =>
    BitVector n -> BitVector m -> BitVector (n + m)
    ++# BitVector n -> BitVector (BitSize (BitVector n))
    forall a. BitPack a => a -> BitVector (BitSize a)
    pack BitVector n
    rL)
        in  case Bit
    overflow of
              Bit
    1 -> BitVector n -> Signed n
    forall (n :: Nat). KnownNat n => BitVector n -> Signed n
    unpack# BitVector n
    rR
              Bit
    _ -> Integer -> Signed n
    forall (n :: Nat). KnownNat n => Integer -> Signed n
    fromInteger# Integer
    0
      satMul SaturationMode
    SatError Signed n
    a Signed n
    b =
        let r :: Signed (n + n)
    r        = Signed n -> Signed n -> Signed (n + n)
    forall (m :: Nat) (n :: Nat).
    Signed m -> Signed n -> Signed (m + n)
    times# Signed n
    a Signed n
    b
            (BitVector n
    rL,BitVector n
    rR)  = Signed (n + n) -> (BitVector n, BitVector n)
    forall a (m :: Nat) (n :: Nat).
    (BitPack a, BitSize a ~ (m + n), KnownNat n) =>
    a -> (BitVector m, BitVector n)
    split Signed (n + n)
    r
            overflow :: Bit
    overflow = Bit -> Bit
    forall a. Bits a => a -> a
    complement (BitVector (1 + n) -> Bit
    forall a. BitPack a => a -> Bit
    reduceOr (Bit -> BitVector 1
    BV.pack# (BitVector n -> Bit
    forall a. BitPack a => a -> Bit
    msb BitVector n
    rR) BitVector 1 -> BitVector n -> BitVector (1 + n)
    forall (m :: Nat) (n :: Nat).
    KnownNat m =>
    BitVector n -> BitVector m -> BitVector (n + m)
    ++# BitVector n -> BitVector (BitSize (BitVector n))
    forall a. BitPack a => a -> BitVector (BitSize a)
    pack BitVector n
    rL)) Bit -> Bit -> Bit
    forall a. Bits a => a -> a -> a
    .|.
                       BitVector (1 + n) -> Bit
    forall a. BitPack a => a -> Bit
    reduceAnd (Bit -> BitVector 1
    BV.pack# (BitVector n -> Bit
    forall a. BitPack a => a -> Bit
    msb BitVector n
    rR) BitVector 1 -> BitVector n -> BitVector (1 + n)
    forall (m :: Nat) (n :: Nat).
    KnownNat m =>
    BitVector n -> BitVector m -> BitVector (n + m)
    ++# BitVector n -> BitVector (BitSize (BitVector n))
    forall a. BitPack a => a -> BitVector (BitSize a)
    pack BitVector n
    rL)
        in  case Bit
    overflow of
              Bit
    1 -> BitVector n -> Signed n
    forall (n :: Nat). KnownNat n => BitVector n -> Signed n
    unpack# BitVector n
    rR
              Bit
    _ -> String -> Signed n
    forall a. HasCallStack => String -> a
    errorX String
    "Signed.satMul: overflow/underflow"
      satMul SaturationMode
    SatSymmetric Signed n
    a Signed n
    b =
        let r :: Signed (n + n)
    r        = Signed n -> Signed n -> Signed (n + n)
    forall (m :: Nat) (n :: Nat).
    Signed m -> Signed n -> Signed (m + n)
    times# Signed n
    a Signed n
    b
            (BitVector n
    rL,BitVector n
    rR)  = Signed (n + n) -> (BitVector n, BitVector n)
    forall a (m :: Nat) (n :: Nat).
    (BitPack a, BitSize a ~ (m + n), KnownNat n) =>
    a -> (BitVector m, BitVector n)
    split Signed (n + n)
    r
            overflow :: Bit
    overflow = Bit -> Bit
    forall a. Bits a => a -> a
    complement (BitVector (1 + n) -> Bit
    forall a. BitPack a => a -> Bit
    reduceOr (Bit -> BitVector 1
    BV.pack# (BitVector n -> Bit
    forall a. BitPack a => a -> Bit
    msb BitVector n
    rR) BitVector 1 -> BitVector n -> BitVector (1 + n)
    forall (m :: Nat) (n :: Nat).
    KnownNat m =>
    BitVector n -> BitVector m -> BitVector (n + m)
    ++# BitVector n -> BitVector (BitSize (BitVector n))
    forall a. BitPack a => a -> BitVector (BitSize a)
    pack BitVector n
    rL)) Bit -> Bit -> Bit
    forall a. Bits a => a -> a -> a
    .|.
                       BitVector (1 + n) -> Bit
    forall a. BitPack a => a -> Bit
    reduceAnd (Bit -> BitVector 1
    BV.pack# (BitVector n -> Bit
    forall a. BitPack a => a -> Bit
    msb BitVector n
    rR) BitVector 1 -> BitVector n -> BitVector (1 + n)
    forall (m :: Nat) (n :: Nat).
    KnownNat m =>
    BitVector n -> BitVector m -> BitVector (n + m)
    ++# BitVector n -> BitVector (BitSize (BitVector n))
    forall a. BitPack a => a -> BitVector (BitSize a)
    pack BitVector n
    rL)
        in  case Bit
    overflow of
              Bit
    1 -> BitVector n -> Signed n
    forall (n :: Nat). KnownNat n => BitVector n -> Signed n
    unpack# BitVector n
    rR
              Bit
    _ -> case BitVector n -> Bit
    forall a. BitPack a => a -> Bit
    msb BitVector n
    rL of
                Bit
    0 -> Signed n
    forall (n :: Nat). KnownNat n => Signed n
    maxBound#
                Bit
    _ -> Signed n
    forall (n :: Nat). KnownNat n => Signed n
    minBoundSym#

      satSucc :: SaturationMode -> Signed n -> Signed n
    satSucc SaturationMode
    SatError Signed n
    a
        | Signed n
    a Signed n -> Signed n -> Bool
    forall a. Eq a => a -> a -> Bool
    == Signed n
    forall a. Bounded a => a
    maxBound = String -> Signed n
    forall a. HasCallStack => String -> a
    errorX String
    "Signed.satSucc: overflow"
      satSucc SaturationMode
    satMode Signed n
    a = SaturationMode -> Signed n -> Signed n -> Signed n
    forall a. SaturatingNum a => SaturationMode -> a -> a -> a
    satSub SaturationMode
    satMode Signed n
    a (Signed n -> Signed n) -> Signed n -> Signed n
    forall a b. (a -> b) -> a -> b
    $ Integer -> Signed n
    forall (n :: Nat). KnownNat n => Integer -> Signed n
    fromInteger# (-Integer
    1)
      {-# INLINE satSucc #-}

      satPred :: SaturationMode -> Signed n -> Signed n
    satPred SaturationMode
    SatError Signed n
    a
        | Signed n
    a Signed n -> Signed n -> Bool
    forall a. Eq a => a -> a -> Bool
    == Signed n
    forall a. Bounded a => a
    minBound = String -> Signed n
    forall a. HasCallStack => String -> a
    errorX String
    "Signed.satPred: underflow"
      satPred SaturationMode
    satMode Signed n
    a = SaturationMode -> Signed n -> Signed n -> Signed n
    forall a. SaturatingNum a => SaturationMode -> a -> a -> a
    satAdd SaturationMode
    satMode Signed n
    a (Signed n -> Signed n) -> Signed n -> Signed n
    forall a b. (a -> b) -> a -> b
    $ Integer -> Signed n
    forall (n :: Nat). KnownNat n => Integer -> Signed n
    fromInteger# (-Integer
    1)
      {-# INLINE satPred #-}

    minBoundSym# :: KnownNat n => Signed n
    minBoundSym# :: forall (n :: Nat). KnownNat n => Signed n
    minBoundSym# = Signed n
    forall (n :: Nat). KnownNat n => Signed n
    minBound# Signed n -> Signed n -> Signed n
    forall (n :: Nat). KnownNat n => Signed n -> Signed n -> Signed n
    +# Integer -> Signed n
    forall (n :: Nat). KnownNat n => Integer -> Signed n
    fromInteger# Integer
    1

    instance KnownNat n => Arbitrary (Signed n) where
      arbitrary :: Gen (Signed n)
    arbitrary = Gen (Signed n)
    forall a. (Bounded a, Integral a) => Gen a
    arbitraryBoundedIntegral
      shrink :: Signed n -> [Signed n]
    shrink    = Signed n -> [Signed n]
    forall (n :: Nat) (p :: Nat -> Type).
    (KnownNat n, Integral (p n)) =>
    p n -> [p n]
    shrinkSizedSigned

    shrinkSizedSigned :: (KnownNat n, Integral (p n)) => p n -> [p n]
    shrinkSizedSigned :: forall (n :: Nat) (p :: Nat -> Type).
    (KnownNat n, Integral (p n)) =>
    p n -> [p n]
    shrinkSizedSigned p n
    x | p n -> Integer
    forall (n :: Nat) (proxy :: Nat -> Type).
    KnownNat n =>
    proxy n -> Integer
    natVal p n
    x Integer -> Integer -> Bool
    forall a. Ord a => a -> a -> Bool
    < Integer
    2 = case p n -> Integer
    forall a. Integral a => a -> Integer
    toInteger p n
    x of
                                           Integer
    0 -> []
                                           Integer
    _ -> [p n
    0]
                        -- 'shrinkIntegral' uses "`quot` 2", which for sized types
                        -- less than 2 bits wide results in a division by zero.
                        --
                        -- See: https://github.com/clash-lang/clash-compiler/issues/153
                        | Bool
    otherwise    = p n -> [p n]
    forall a. Integral a => a -> [a]
    shrinkIntegral p n
    x
    {-# INLINE shrinkSizedSigned #-}

    instance KnownNat n => CoArbitrary (Signed n) where
      coarbitrary :: forall b. Signed n -> Gen b -> Gen b
    coarbitrary = Signed n -> Gen b -> Gen b
    forall a b. Integral a => a -> Gen b -> Gen b
    coarbitraryIntegral

    type instance Index   (Signed n) = Int
    type instance IxValue (Signed n) = Bit
    instance KnownNat n => Ixed (Signed n) where
      ix :: Index (Signed n) -> Traversal' (Signed n) (IxValue (Signed n))
    ix Index (Signed n)
    i IxValue (Signed n) -> f (IxValue (Signed n))
    f Signed n
    s = BitVector n -> Signed n
    forall (n :: Nat). KnownNat n => BitVector n -> Signed n
    unpack# (BitVector n -> Signed n)
    -> (Bit -> BitVector n) -> Bit -> Signed n
    forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
    <$> BitVector n -> Int -> Bit -> BitVector n
    forall (n :: Nat).
    KnownNat n =>
    BitVector n -> Int -> Bit -> BitVector n
    BV.replaceBit# (Signed n -> BitVector n
    forall (n :: Nat). KnownNat n => Signed n -> BitVector n
    pack# Signed n
    s) Int
    Index (Signed n)
    i
                         (Bit -> Signed n) -> f Bit -> f (Signed n)
    forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
    <$> IxValue (Signed n) -> f (IxValue (Signed n))
    f (BitVector n -> Int -> Bit
    forall (n :: Nat). KnownNat n => BitVector n -> Int -> Bit
    BV.index# (Signed n -> BitVector n
    forall (n :: Nat). KnownNat n => Signed n -> BitVector n
    pack# Signed n
    s) Int
    Index (Signed n)
    i)

    instance (KnownNat n) => Ix (Signed n) where
      range :: (Signed n, Signed n) -> [Signed n]
    range (Signed n
    a, Signed n
    b) = [Signed n
    a..Signed n
    b]
      index :: (Signed n, Signed n) -> Signed n -> Int
    index ab :: (Signed n, Signed n)
    ab@(Signed n
    a, Signed n
    b) Signed n
    x
        | (Signed n, Signed n) -> Signed n -> Bool
    forall a. Ix a => (a, a) -> a -> Bool
    inRange (Signed n, Signed n)
    ab Signed n
    x = Signed n -> Int
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (Signed n -> Int) -> Signed n -> Int
    forall a b. (a -> b) -> a -> b
    $ Signed n
    x Signed n -> Signed n -> Signed n
    forall a. Num a => a -> a -> a
    - Signed n
    a
        | Bool
    otherwise = String -> Int
    forall a. HasCallStack => String -> a
    error (String -> Int) -> String -> Int
    forall a b. (a -> b) -> a -> b
    $ String -> Signed n -> Signed n -> Signed n -> String
    forall r. PrintfType r => String -> r
    printf String
    "Index (%d) out of range ((%d, %d))" Signed n
    x Signed n
    a Signed n
    b
      inRange :: (Signed n, Signed n) -> Signed n -> Bool
    inRange (Signed n
    a, Signed n
    b) Signed n
    x = Signed n
    a Signed n -> Signed n -> Bool
    forall a. Ord a => a -> a -> Bool
    <= Signed n
    x Bool -> Bool -> Bool
    && Signed n
    x Signed n -> Signed n -> Bool
    forall a. Ord a => a -> a -> Bool
    <= Signed n
    b

    -- | Shift left that ties to zero on negative shifts
    shiftL0 :: Integer -> Int -> Integer
    #if MIN_VERSION_base(4,15,0)
    shiftL0 :: Integer -> Int -> Integer
    shiftL0 = \Integer
    a Int
    sh -> if Int
    sh Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    >= Int
    0 then Integer -> Int -> Integer
    forall a. Bits a => a -> Int -> a
    shiftL Integer
    a Int
    sh else Integer
    0
    #else
    shiftL0 = shiftL -- True for use with this module
    #endif
    {-# INLINE shiftL0 #-}