module Data.AIG.Operations
(
BV
, length
, at
, (!)
, (++)
, concat
, take
, drop
, slice
, zipWithM
, msb
, lsb
, generateM_msb0
, generate_msb0
, generateM_lsb0
, generate_lsb0
, replicate
, bvFromInteger
, muxInteger
, asUnsigned
, asSigned
, bvToList
, neg
, add
, addC
, sub
, subC
, mul
, squot
, srem
, uquot
, urem
, shl
, sshr
, ushr
, rol
, ror
, bvEq
, sle
, slt
, ule
, ult
, sext
, zext
, pmul
, pmod
) where
import Control.Applicative
import Control.Exception
import qualified Control.Monad
import Control.Monad.State hiding (zipWithM)
import Data.Bits ((.|.), setBit, shiftL, testBit)
import qualified Data.Vector as V
import Prelude hiding (and, concat, length, not, or, replicate, splitAt, tail, (++), take, drop)
import qualified Prelude
import Data.AIG.Interface
halfAdder :: IsAIG l g => g s -> l s -> l s -> IO (l s, l s)
halfAdder g b c = do
b_or_c <- or g b c
c_out <- and g b c
s <- and g b_or_c (not c_out)
return (s, c_out)
fullAdder :: IsAIG l g => g s -> l s -> l s -> l s -> IO (l s, l s)
fullAdder g a b c_in = do
a_xor_b <- xor g a b
s <- xor g a_xor_b c_in
a_and_b <- and g a b
c_out <- or g a_and_b =<< and g a_xor_b c_in
return (s, c_out)
newtype BV l = BV { unBV :: V.Vector l }
instance Functor BV where
fmap f (BV v) = BV (f <$> v)
length :: BV l -> Int
length (BV v) = V.length v
tail :: BV l -> BV l
tail (BV v) = BV (V.tail v)
generate_lsb0
:: Int
-> (Int -> l)
-> BV l
generate_lsb0 c f = BV (V.generate c (\i -> f ((c1)i)))
generateM_lsb0
:: Monad m
=> Int
-> (Int -> m l)
-> m (BV l)
generateM_lsb0 c f = return . BV . V.reverse =<< V.generateM c (\i -> f ((c1)i))
generate_msb0
:: Int
-> (Int -> l)
-> BV l
generate_msb0 c f = BV (V.generate c f)
generateM_msb0
:: Monad m
=> Int
-> (Int -> m l)
-> m (BV l)
generateM_msb0 c f = return . BV =<< V.generateM c f
replicate
:: Int
-> l
-> BV l
replicate c e = BV (V.replicate c e)
at :: BV l -> Int -> l
at (BV v) i = v V.! i
(++) :: BV l -> BV l -> BV l
BV x ++ BV y = BV (x V.++ y)
concat :: [BV l] -> BV l
concat v = BV (V.concat (unBV <$> v))
take :: Int -> BV l -> BV l
take i (BV v) = BV (V.take i v)
drop :: Int -> BV l -> BV l
drop i (BV v) = BV (V.drop i v)
slice :: BV l
-> Int
-> Int
-> BV l
slice (BV v) i n = BV (V.slice i n v)
zipWithM :: (l -> l -> IO l) -> BV l -> BV l -> IO (BV l)
zipWithM f (BV x) (BV y) = assert (V.length x == V.length y) $
BV <$> V.zipWithM f x y
bvToList :: BV l -> [l]
bvToList (BV v) = V.toList v
bvFromList :: [l] -> BV l
bvFromList xs = BV (V.fromList xs)
(!) :: BV l -> Int -> l
(!) v i = v `at` (length v 1 i)
bvFromInteger
:: IsAIG l g
=> g s
-> Int
-> Integer
-> BV (l s)
bvFromInteger g n v = generate_lsb0 n $ \i -> constant g (v `testBit` i)
asUnsigned :: IsAIG l g => g s -> BV (l s) -> Maybe Integer
asUnsigned g v = go 0 0
where n = length v
go x i | i >= n = return x
go x i = do
b <- asConstant g (v `at` i)
let y = if b then 1 else 0
let z = x `shiftL` 1 .|. y
seq z $ go z (i+1)
asSigned :: IsAIG l g => g s -> BV (l s) -> Maybe Integer
asSigned g v = assert (n > 0) $ go 0 1
where n = length v
m = n1
go x i | i < m = do
b <- asConstant g (v `at` i)
let y = if b then 1 else 0
let z = x `shiftL` 1 .|. y
seq z $ go z (i+1)
go x i = do
msbv <- asConstant g (v `at` i)
return $ if msbv then x 2^m
else x
msb :: BV l -> l
msb v = v `at` 0
lsb :: BV l -> l
lsb v = v ! 0
ite
:: IsAIG l g
=> g s
-> l s
-> BV (l s)
-> BV (l s)
-> IO (BV (l s))
ite g c x y = zipWithM (mux g c) x y
iteM
:: IsAIG l g
=> g s
-> l s
-> IO (BV (l s))
-> IO (BV (l s))
-> IO (BV (l s))
iteM g c x y
| c === trueLit g = x
| c === falseLit g = y
| otherwise = join $ zipWithM (mux g c) <$> x <*> y
ripple_add :: IsAIG l g
=> g s
-> BV (l s)
-> BV (l s)
-> l s
-> IO (BV (l s), l s)
ripple_add _ x _ c | length x == 0 = return (x, c)
ripple_add g x y c0 = do
let unfold i = StateT $ \c -> do
fullAdder g (x `at` i) (y `at` i) c
runStateT (generateM_lsb0 (length x) unfold) c0
fullSub :: IsAIG l g => g s -> l s -> l s -> l s -> IO (l s, l s)
fullSub g x y b_in = do
y_eq_b <- eq g y b_in
s <- eq g x y_eq_b
y_and_b <- and g y b_in
c2 <- and g (not x) =<< or g y b_in
b_out <- or g y_and_b c2
return (s, b_out)
full_sub :: IsAIG l g
=> g s
-> BV (l s)
-> BV (l s)
-> IO (BV (l s), l s)
full_sub g x _ | length x == 0 = return (x,falseLit g)
full_sub g x y = do
let unfold i = StateT $ \b -> fullSub g (x `at` i) (y `at` i) b
runStateT (generateM_lsb0 (length x) unfold) (falseLit g)
neg :: IsAIG l g => g s -> BV (l s) -> IO (BV (l s))
neg g x = evalStateT (generateM_lsb0 (length x) unfold) (trueLit g)
where unfold i = StateT $ \c -> halfAdder g (not (x `at` i)) c
add :: IsAIG l g => g s -> BV (l s) -> BV (l s) -> IO (BV (l s))
add g x y = fst <$> addC g x y
addC :: IsAIG l g => g s -> BV (l s) -> BV (l s) -> IO (BV (l s), l s)
addC g x y = ripple_add g x y (falseLit g)
sub :: IsAIG l g => g s -> BV (l s) -> BV (l s) -> IO (BV (l s))
sub g x y = fst <$> subC g x y
subC :: IsAIG l g => g s -> BV (l s) -> BV (l s) -> IO (BV (l s), l s)
subC g x y = ripple_add g x (not <$> y) (trueLit g)
mul :: IsAIG l g => g s -> BV (l s) -> BV (l s) -> IO (BV (l s))
mul g x y = do
let n = length y
let updateBits i z | i == n = return z
updateBits i z = do
z_inc <- add g z (shlC g x i)
z' <- ite g (y ! i) z_inc z
updateBits (i+1) z'
updateBits 0 $ replicate (length x) (falseLit g)
squot :: IsAIG l g => g s -> BV (l s) -> BV (l s) -> IO (BV (l s))
squot g x y = fst <$> squotRem g x y
srem :: IsAIG l g => g s -> BV (l s) -> BV (l s) -> IO (BV (l s))
srem g x y = snd <$> squotRem g x y
shiftL1 :: BV l -> l -> BV l
shiftL1 (BV v) e = assert (V.length v > 0) $ BV (V.tail v `V.snoc` e)
shiftR1 :: l -> BV l -> BV l
shiftR1 e (BV v) = assert (V.length v > 0) $ BV (e `V.cons` V.init v)
splitAt :: Int -> BV l -> (BV l, BV l)
splitAt n (BV v) = (BV x, BV y)
where (x,y) = V.splitAt n v
stepN :: Monad m => Int -> (a -> m a) -> a -> m a
stepN n f x
| n > 0 = stepN (n1) f =<< f x
| otherwise = return x
sabs :: IsAIG l g => g s -> BV (l s) -> IO (BV (l s))
sabs g x = assert (length x > 0) $ negWhen g x (msb x)
negWhen :: IsAIG l g => g s -> BV (l s) -> l s -> IO (BV (l s))
negWhen g x c = iteM g c (neg g x) (return x)
uquotRem :: IsAIG l g => g s -> BV (l s) -> BV (l s) -> IO (BV (l s), BV (l s))
uquotRem g dividend divisor = do
let n = length dividend
assert (n == length divisor) $ do
let initial = zext g dividend (2*n)
let divStep i p rr | i == n = return (q `shiftL1` p, r)
where (r,q) = splitAt n rr
divStep i p rr = do
let rs = rr `shiftL1` p
let (r,q) = splitAt n rs
(s,b) <- full_sub g r divisor
divStep (i+1) (not b) =<< ite g b rs (s ++ q)
divStep 0 (falseLit g) initial
squotRem :: IsAIG l g
=> g s
-> BV (l s)
-> BV (l s)
-> IO (BV (l s), BV (l s))
squotRem g dividend' divisor' = do
let n = length dividend'
assert (n > 0 && n == length divisor') $ do
let dsign = msb dividend'
dividend <- sabs g dividend'
divisor <- sabs g divisor'
let initial = zext g dividend (2*n)
let divStep rrOrig = do
let (r,q) = splitAt n rrOrig
s <- sub g r divisor
ite g (msb s)
(rrOrig `shiftL1` falseLit g)
((s ++ q) `shiftL1` trueLit g)
(qr,rr) <- splitAt n <$> stepN n divStep (initial `shiftL1` falseLit g)
q' <- negWhen g qr =<< xor g dsign (msb divisor')
r' <- negWhen g (falseLit g `shiftR1` rr) dsign
return (q', r')
uquot :: IsAIG l g => g s -> BV (l s) -> BV (l s) -> IO (BV (l s))
uquot g x y = fst <$> uquotRem g x y
urem :: IsAIG l g => g s -> BV (l s) -> BV (l s) -> IO (BV (l s))
urem g x y = snd <$> uquotRem g x y
bvEq :: IsAIG l g => g s -> BV (l s) -> BV (l s) -> IO (l s)
bvEq g x y = go 0 (trueLit g)
where n = length x
go i r | i == n = return r
go i r = go (i+1) =<< and g r =<< eq g (x `at` i) (y `at` i)
ult :: IsAIG l g => g s -> BV (l s) -> BV (l s) -> IO (l s)
ult g x y = snd <$> full_sub g x y
ule :: IsAIG l g => g s -> BV (l s) -> BV (l s) -> IO (l s)
ule g x y = not <$> ult g y x
slt :: IsAIG l g => g s -> BV (l s) -> BV (l s) -> IO (l s)
slt g x y = do
let xs = x `at` 0
let ys = y `at` 0
c0 <- and g xs (not ys)
c1 <- and g (not xs) ys
c2 <- and g (not c1) =<< ult g (tail x) (tail y)
or g c0 c2
sle :: IsAIG l g => g s -> BV (l s) -> BV (l s) -> IO (l s)
sle g x y = not <$> slt g y x
sext :: BV l -> Int -> BV l
sext v r = assert (r >= n && n > 0) $ replicate (r n) (msb v) ++ v
where n = length v
zext :: IsAIG l g => g s -> BV (l s) -> Int -> BV (l s)
zext g v r = assert (r >= n) $ replicate (r n) (falseLit g) ++ v
where n = length v
muxInteger :: (Integral i, Monad m)
=> (l -> m a -> m a -> m a)
-> i
-> BV l
-> (i -> m a)
-> m a
muxInteger mergeFn maxValue vx valueFn = impl (length vx) 0
where impl _ y | y >= toInteger maxValue = valueFn maxValue
impl 0 y = valueFn (fromInteger y)
impl i y = mergeFn (vx ! j) (impl j (y `setBit` j)) (impl j y)
where j = i 1
shl :: IsAIG l g
=> g s
-> BV (l s)
-> BV (l s)
-> IO (BV (l s))
shl g x y = muxInteger (iteM g) (length x) y (return . shlC g x)
shlC :: IsAIG l g => g s -> BV (l s) -> Int -> BV (l s)
shlC g x s0 = slice x j (nj) ++ replicate j (falseLit g)
where n = length x
j = min n s0
shrC :: l s -> BV (l s) -> Int -> BV (l s)
shrC c x s0 = replicate j c ++ slice x 0 (nj)
where n = length x
j = min n s0
sshr :: IsAIG l g
=> g s
-> BV (l s)
-> BV (l s)
-> IO (BV (l s))
sshr g x y = muxInteger (iteM g) (length x) y (return . shrC (msb x) x)
ushr :: IsAIG l g
=> g s
-> BV (l s)
-> BV (l s)
-> IO (BV (l s))
ushr g x y = muxInteger (iteM g) (length x) y (return . shrC (falseLit g) x)
rolC :: BV l -> Int -> BV l
rolC (BV x) i
| V.null x = BV x
| otherwise = BV (V.drop j x V.++ V.take j x)
where j = i `mod` V.length x
rorC :: BV l -> Int -> BV l
rorC x i = rolC x ( i)
rol :: IsAIG l g
=> g s
-> BV (l s)
-> BV (l s)
-> IO (BV (l s))
rol g x y = do
r <- urem g y (bvFromInteger g (length y) (toInteger (length x)))
muxInteger (iteM g) (length x 1) r (return . rolC x)
ror :: IsAIG l g
=> g s
-> BV (l s)
-> BV (l s)
-> IO (BV (l s))
ror g x y = do
r <- urem g y (bvFromInteger g (length y) (toInteger (length x)))
muxInteger (iteM g) (length x 1) r (return . rorC x)
pmul :: IsAIG l g
=> g s
-> BV (l s)
-> BV (l s)
-> IO (BV (l s))
pmul g x y = generateM_msb0 (max 0 (m + n 1)) coeff
where
m = length x
n = length y
coeff k = foldM (xor g) (falseLit g) =<<
sequence [ and g (at x i) (at y j) | i <- [0 .. k], let j = k i, i < m, j < n ]
pmod :: forall l g s. IsAIG l g => g s -> BV (l s) -> BV (l s) -> IO (BV (l s))
pmod g x y = findmsb (bvToList y)
where
findmsb :: [l s] -> IO (BV (l s))
findmsb [] = return (replicate (length y 1) (falseLit g))
findmsb (c : cs)
| c === trueLit g = usemask cs
| c === falseLit g = findmsb cs
| otherwise = do
t <- usemask cs
f <- findmsb cs
zipWithM (mux g c) t f
usemask :: [l s] -> IO (BV (l s))
usemask m = do
rs <- go 0 p0 z0
return (zext g (bvFromList rs) (length y 1))
where
msize = Prelude.length m
p0 = Prelude.replicate (msize 1) (falseLit g) Prelude.++ [trueLit g]
z0 = Prelude.replicate msize (falseLit g)
next :: [l s] -> IO [l s]
next [] = return []
next (b : bs) = do
m' <- mapM (and g b) m
let bs' = bs Prelude.++ [falseLit g]
Control.Monad.zipWithM (xor g) m' bs'
go :: Int -> [l s] -> [l s] -> IO [l s]
go i p acc
| i >= length x = return acc
| otherwise = do
px <- mapM (and g (x ! i)) p
acc' <- Control.Monad.zipWithM (xor g) px acc
p' <- next p
go (i+1) p' acc'