Skip to content

Commit 51844c7

Browse files
committed
btorsim: implement smod
Smod implementation taken from boolector (src/btorexp.c:1528) Signed-off-by: Ferdinand Bachmann <[email protected]>
1 parent 37e4beb commit 51844c7

File tree

3 files changed

+75
-1
lines changed

3 files changed

+75
-1
lines changed

src/btorsim/btorsim.cpp

+8-1
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ parse_model_line (Btor2Line *l)
267267
case BTOR2_TAG_sll:
268268
case BTOR2_TAG_slt:
269269
case BTOR2_TAG_slte:
270+
case BTOR2_TAG_smod:
270271
case BTOR2_TAG_sra:
271272
case BTOR2_TAG_srem:
272273
case BTOR2_TAG_srl:
@@ -288,7 +289,6 @@ parse_model_line (Btor2Line *l)
288289
case BTOR2_TAG_justice:
289290
case BTOR2_TAG_saddo:
290291
case BTOR2_TAG_sdivo:
291-
case BTOR2_TAG_smod:
292292
case BTOR2_TAG_smulo:
293293
case BTOR2_TAG_ssubo:
294294
case BTOR2_TAG_uaddo:
@@ -624,6 +624,13 @@ simulate (int64_t id)
624624
assert (args[1].type == BtorSimState::Type::BITVEC);
625625
res.bv_state = btorsim_bv_sll (args[0].bv_state, args[1].bv_state);
626626
break;
627+
case BTOR2_TAG_smod:
628+
assert (l->nargs == 2);
629+
assert (res.type == BtorSimState::Type::BITVEC);
630+
assert (args[0].type == BtorSimState::Type::BITVEC);
631+
assert (args[1].type == BtorSimState::Type::BITVEC);
632+
res.bv_state = btorsim_bv_smod (args[0].bv_state, args[1].bv_state);
633+
break;
627634
case BTOR2_TAG_srl:
628635
assert (l->nargs == 2);
629636
assert (res.type == BtorSimState::Type::BITVEC);

src/btorsim/btorsimbv.c

+64
Original file line numberDiff line numberDiff line change
@@ -2033,6 +2033,70 @@ btorsim_bv_srem (const BtorSimBitVector *a, const BtorSimBitVector *b)
20332033
return res;
20342034
}
20352035

2036+
BtorSimBitVector *
2037+
btorsim_bv_smod (const BtorSimBitVector *a, const BtorSimBitVector *b)
2038+
{
2039+
assert (a);
2040+
assert (b);
2041+
assert (a->len == b->len);
2042+
assert (a->width == b->width);
2043+
2044+
BtorSimBitVector *res = 0, *not_b, *sign_a, *sign_b, *neg_a, *neg_b, *add;
2045+
BtorSimBitVector *cond_a, *cond_b, *urem, *neg_urem, *add_urem, *add_neg_urem;
2046+
bool a_positive, b_positive;
2047+
2048+
if (a->width == 1)
2049+
{
2050+
not_b = btorsim_bv_not (b);
2051+
res = btorsim_bv_and (a, not_b);
2052+
btorsim_bv_free (not_b);
2053+
}
2054+
else
2055+
{
2056+
sign_a = btorsim_bv_slice (a, a->width - 1, a->width - 1);
2057+
sign_b = btorsim_bv_slice (b, b->width - 1, b->width - 1);
2058+
a_positive = !btorsim_bv_is_true (sign_a);
2059+
b_positive = !btorsim_bv_is_true (sign_b);
2060+
2061+
neg_a = btorsim_bv_neg (a);
2062+
neg_b = btorsim_bv_neg (b);
2063+
cond_a = a_positive ? btorsim_bv_copy (a)
2064+
: btorsim_bv_copy (neg_a);
2065+
cond_b = b_positive ? btorsim_bv_copy (b)
2066+
: btorsim_bv_copy (neg_b);
2067+
2068+
2069+
urem = btorsim_bv_urem (cond_a, cond_b);
2070+
add = btorsim_bv_is_zero(urem) ? btorsim_bv_zero(b->width)
2071+
: btorsim_bv_copy(b);
2072+
2073+
neg_urem = btorsim_bv_neg (urem);
2074+
add_urem = btorsim_bv_add(urem, add);
2075+
add_neg_urem = btorsim_bv_add(neg_urem, add);
2076+
2077+
res = a_positive && b_positive ? btorsim_bv_copy(urem)
2078+
: !a_positive && b_positive ? btorsim_bv_copy(add_neg_urem)
2079+
: a_positive && !b_positive ? btorsim_bv_copy(add_urem)
2080+
: btorsim_bv_copy(neg_urem);
2081+
2082+
btorsim_bv_free (sign_a);
2083+
btorsim_bv_free (sign_b);
2084+
btorsim_bv_free (neg_a);
2085+
btorsim_bv_free (neg_b);
2086+
btorsim_bv_free (cond_a);
2087+
btorsim_bv_free (cond_b);
2088+
btorsim_bv_free (urem);
2089+
btorsim_bv_free (add);
2090+
btorsim_bv_free (neg_urem);
2091+
btorsim_bv_free (add_urem);
2092+
btorsim_bv_free (add_neg_urem);
2093+
}
2094+
2095+
assert (res);
2096+
assert (rem_bits_zero_dbg (res));
2097+
return res;
2098+
}
2099+
20362100
BtorSimBitVector *
20372101
btorsim_bv_concat (const BtorSimBitVector *a, const BtorSimBitVector *b)
20382102
{

src/btorsim/btorsimbv.h

+3
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,9 @@ BtorSimBitVector *btorsim_bv_urem (const BtorSimBitVector *a,
212212
BtorSimBitVector *btorsim_bv_srem (const BtorSimBitVector *a,
213213
const BtorSimBitVector *b);
214214

215+
BtorSimBitVector *btorsim_bv_smod (const BtorSimBitVector *a,
216+
const BtorSimBitVector *b);
217+
215218
BtorSimBitVector *btorsim_bv_ite (const BtorSimBitVector *c,
216219
const BtorSimBitVector *t,
217220
const BtorSimBitVector *e);

0 commit comments

Comments
 (0)