Add primitive toSignedInteger.

This commit is contained in:
Brian Huffman 2021-09-10 16:59:54 -07:00
parent 5a668a4594
commit d1883caa6a
6 changed files with 24 additions and 0 deletions

View File

@ -734,6 +734,11 @@ primitive (>>$) : {n, ix} (fin n, n >= 1, Integral ix) => [n] -> ix -> [n]
*/
primitive lg2 : {n} (fin n) => [n] -> [n]
/**
* Convert a signed 2's complement bitvector to an integer.
*/
primitive toSignedInteger : {n} (fin n, n >= 1) => [n] -> Integer
// Rational specific operations ----------------------------------------------

View File

@ -614,6 +614,12 @@ class MonadIO (SEval sym) => Backend sym where
SWord sym ->
SEval sym (SInteger sym)
-- | Construct a signed integer value from the given packed word.
wordToSignedInt ::
sym ->
SWord sym ->
SEval sym (SInteger sym)
-- ==== Integer operations ====
-- | Addition of unbounded integers.

View File

@ -185,6 +185,7 @@ instance Backend Concrete where
integerAsLit _ = Just
wordToInt _ (BV _ x) = pure x
wordToSignedInt _ (BV w x) = pure $! signedValue w x
wordFromInt _ w x = pure $! mkBv w x
packWord _ bits = pure $! BV (toInteger w) a

View File

@ -293,6 +293,7 @@ instance Backend SBV where
wordLg2 _ a = sLg2 a
wordToInt _ x = pure $! svToInteger x
wordToSignedInt _ x = pure $! svToInteger (svSign x)
wordFromInt _ w i = pure $! svFromInteger w i
intEq _ a b = pure $! svEqual a b

View File

@ -362,6 +362,7 @@ instance W4.IsSymExprBuilder sym => Backend (What4 sym) where
liftIO (SW.bvSRem (w4 sym) x y)
wordToInt sym x = liftIO (SW.bvToInteger (w4 sym) x)
wordToSignedInt sym x = liftIO (SW.sbvToInteger (w4 sym) x)
wordFromInt sym width i = liftIO (SW.integerToBV (w4 sym) i width)
intPlus sym x y = liftIO $ W4.intAdd (w4 sym) x y

View File

@ -716,6 +716,13 @@ smodV sym =
PWordFun \y ->
PPrim (VWord w . wordVal <$> wordSignedMod sym x y)
{-# SPECIALIZE toSignedIntegerV :: Concrete -> Prim Concrete #-}
toSignedIntegerV :: Backend sym => sym -> Prim sym
toSignedIntegerV sym =
PFinPoly \_w ->
PWordFun \x ->
PPrim (VInteger <$> wordToSignedInt sym x)
-- Cmp -------------------------------------------------------------------------
{-# SPECIALIZE cmpValue ::
@ -2031,6 +2038,9 @@ genericPrimTable sym getEOpts =
unary (roundToEvenV sym))
-- Bitvector specific operations
, ("toSignedInteger"
, {-# SCC "Prelude::toSignedInteger" #-}
toSignedIntegerV sym)
, ("/$" , {-# SCC "Prelude::(/$)" #-}
sdivV sym)
, ("%$" , {-# SCC "Prelude::(%$)" #-}