19 for(std::size_t i=0; i<width; i++)
29 tmp.erase(tmp.begin(), tmp.begin()+1);
36 for(std::size_t i=0; i<a.size(); i++)
48 result.resize(last+1);
50 result.erase(result.begin(), result.begin()+first);
62 result.erase(result.begin(), result.begin()+(result.size()-n));
82 result.resize(a.size()+b.size());
84 for(std::size_t i=0; i<a.size(); i++)
87 for(std::size_t i=0; i<b.size(); i++)
88 result[i+a.size()]=b[i];
100 result.resize(a.size());
101 for(std::size_t i=0; i<result.size(); i++)
109 std::size_t new_size,
112 std::size_t old_size=bv.size();
116 result.resize(new_size);
122 for(std::size_t i=old_size; i<new_size; i++)
123 result[i]=extend_with;
136#define OPTIMAL_FULL_ADDER
144 #ifdef OPTIMAL_FULL_ADDER
149 int constantProp = -1;
155 constantProp = (a.
is_true()) ? 1 : 0;
161 constantProp = (b.
is_true()) ? 1 : 0;
167 constantProp = (carry_in.
is_true()) ? 1 : 0;
173 if(constantProp == 1)
179 else if(constantProp == 0)
235 const auto const_count =
295std::pair<bvt, literalt>
300 std::pair<bvt, literalt> result{
bvt{}, carry_in};
301 result.first.reserve(op0.size());
304 for(std::size_t i = 0; i < op0.size(); i++)
321 for(std::size_t i=0; i<op0.size(); i++)
345 return adder(op0, tmp_op1, carry_in).first;
350 const bvt op1_sign_applied=
354 return adder(op0, op1_sign_applied, subtract).first;
371 auto add_sub_result =
adder(op0, tmp_op1, carry_in);
375 result.reserve(add_sub_result.first.size());
383 for(
const auto &literal : add_sub_result.first)
411 for(std::size_t i = 0; i < add_sub_result.first.size() - 1; ++i)
413 const auto &literal = add_sub_result.first[i];
415 prop.
lor(overflow_to_max_int, literal), !overflow_to_min_int));
420 !overflow_to_max_int));
434 literalt old_sign=op0[op0.size()-1];
459 literalt op0_is_negative=op0[op0.size()-1];
503 "representation has either value signed or unsigned");
506 return std::move(result.first);
517 std::size_t d=1, width=op.size();
520 for(std::size_t stage=0; stage<dist.size(); stage++)
526 for(std::size_t i=0; i<width; i++)
527 result[i]=
prop.
lselect(dist[stage], tmp[i], result[i]);
539 result.resize(src.size());
545 for(std::size_t i=0; i<src.size(); i++)
559 l=(dist<src.size()-i?src[i+dist]:src[src.size()-1]);
570 l=src[(src.size()+i-(dist%src.size()))%src.size()];
575 l=src[(i+(dist%src.size()))%src.size()];
607 bvt should_be_zeros(bv);
608 should_be_zeros.pop_back();
620 for(
auto &literal : bv)
639 for(
auto &literal : result)
650 else if(pps.size()==2)
651 return add(pps[0], pps[1]);
654 std::vector<bvt> new_pps;
655 std::size_t no_full_adders=pps.size()/3;
658 for(std::size_t i=0; i<no_full_adders; i++)
660 const bvt &a=pps[i*3+0],
664 INVARIANT(a.size() == b.size(),
"groups should be of equal size");
665 INVARIANT(a.size() == c.size(),
"groups should be of equal size");
670 for(std::size_t bit=0; bit<a.size(); bit++)
674 if(bit + 1 < a.size())
678 new_pps.push_back(std::move(s));
679 new_pps.push_back(std::move(t));
683 for(std::size_t i=no_full_adders*3; i<pps.size(); i++)
684 new_pps.push_back(pps[i]);
695 using columnt = std::list<literalt>;
696 std::vector<columnt> columns(pps.front().size());
697 for(
const auto &pp : pps)
700 for(std::size_t i = 0; i < pp.size(); ++i)
703 columns[i].push_back(pp[i]);
707 std::list<std::size_t> dadda_sequence;
708 for(std::size_t d = 2; d < pps.front().size(); d = (d * 3) / 2)
709 dadda_sequence.push_front(d);
711 for(
auto d : dadda_sequence)
713 for(
auto col_it = columns.begin(); col_it != columns.end();)
715 if(col_it->size() <= d)
717 else if(col_it->size() == d + 1)
724 *std::next(col_it->begin()),
729 if(std::next(col_it) != columns.end())
740 *std::next(col_it->begin()),
741 *std::next(std::next(col_it->begin())),
746 if(std::next(col_it) != columns.end())
753 a.reserve(pps.front().size());
754 b.reserve(pps.front().size());
756 for(
const auto &col : columns)
766 a.push_back(col.front());
770 a.push_back(col.front());
771 b.push_back(col.back());
920 bvt op0=_op0, op1=_op1;
926 std::vector<bvt> pps;
927 pps.reserve(op0.size());
929 for(std::size_t bit=0; bit<op0.size(); bit++)
936 pp.reserve(op0.size());
938 for(std::size_t idx = bit; idx < op0.size(); idx++)
939 pp.push_back(
prop.
land(op1[idx - bit], op0[bit]));
945 return zeros(op0.size());
950#elif defined(DADDA_TREE)
953 bvt product = pps.front();
955 for(
auto it = std::next(pps.begin()); it != pps.end(); ++it)
956 product =
add(product, *it);
967 bvt _op0=op0, _op1=op1;
975 product.resize(_op0.size());
977 for(std::size_t i=0; i<product.size(); i++)
980 for(std::size_t sum=0; sum<op0.size(); sum++)
985 tmpop.reserve(product.size());
987 for(std::size_t idx=0; idx<sum; idx++)
990 for(std::size_t idx=sum; idx<product.size(); idx++)
991 tmpop.push_back(
prop.
land(op1[idx-sum], op0[sum]));
995 for(std::size_t idx=op1.size()-sum; idx<op1.size(); idx++)
1004 if(op0.empty() || op1.empty())
1025 result.resize(bv.size());
1027 for(std::size_t i=0; i<bv.size(); i++)
1050 if(op0.empty() || op1.empty())
1104 if(op0.empty() || op1.empty())
1107 bvt _op0(op0), _op1(op1);
1109 literalt sign_0=_op0[_op0.size()-1];
1110 literalt sign_1=_op1[_op1.size()-1];
1114 for(std::size_t i=0; i<_op0.size(); i++)
1115 _op0[i]=(
prop.
lselect(sign_0, neg_0[i], _op0[i]));
1117 for(std::size_t i=0; i<_op1.size(); i++)
1118 _op1[i]=(
prop.
lselect(sign_1, neg_1[i], _op1[i]));
1126 for(std::size_t i=0; i<res.size(); i++)
1127 res[i]=
prop.
lselect(result_sign, neg_res[i], res[i]);
1129 for(std::size_t i=0; i<res.size(); i++)
1157 std::size_t width=op0.size();
1162 std::size_t one_count=0, non_const_count=0, one_pos=0;
1164 for(std::size_t i=0; i<op1.size(); i++)
1176 if(non_const_count==0 && one_count==1 && one_pos!=0)
1179 res=
shift(op0, LRIGHT, one_pos);
1183 for(std::size_t i=one_pos; i<rem.size(); i++)
1229#ifdef COMPACT_EQUAL_CONST
1235void bv_utilst::equal_const_register(
const bvt &var)
1238 equal_const_registered.insert(var);
1250 std::size_t size = var.size();
1260 constant.pop_back();
1265 var_constant_pairt index(var, constant);
1267 equal_const_cachet::iterator entry = equal_const_cache.find(index);
1269 if(entry != equal_const_cache.end())
1271 return entry->second;
1277 constant.pop_back();
1279 literalt rec = equal_const_rec(var, constant);
1282 equal_const_cache.insert(
1283 std::pair<var_constant_pairt, literalt>(index, compare));
1298literalt bv_utilst::equal_const(
const bvt &var,
const bvt &constant)
1300 std::size_t size = constant.size();
1318 literalt top_bit = constant[size - 1];
1320 std::size_t split = size - 1;
1321 var_upper.push_back(var[size - 1]);
1322 constant_upper.push_back(constant[size - 1]);
1324 for(split = size - 2; split != 0; --split)
1326 if(constant[split] != top_bit)
1332 var_upper.push_back(var[split]);
1333 constant_upper.push_back(constant[split]);
1337 for(std::size_t i = 0; i <= split; ++i)
1339 var_lower.push_back(var[i]);
1340 constant_lower.push_back(constant[i]);
1345 var_upper.size() + var_lower.size() == size,
1346 "lower size plus upper size should equal the total size");
1348 constant_upper.size() + constant_lower.size() == size,
1349 "lower size plus upper size should equal the total size");
1351 literalt top_comparison = equal_const_rec(var_upper, constant_upper);
1352 literalt bottom_comparison = equal_const_rec(var_lower, constant_lower);
1354 return prop.
land(top_comparison, bottom_comparison);
1367 #ifdef COMPACT_EQUAL_CONST
1371 equal_const_registered.find(op1) != equal_const_registered.end())
1372 return equal_const(op1, op0);
1374 equal_const_registered.find(op0) != equal_const_registered.end())
1375 return equal_const(op0, op1);
1379 equal_bv.resize(op0.size());
1381 for(std::size_t i=0; i<op0.size(); i++)
1392#define COMPACT_LT_OR_LE
1407 top1=bv1[bv1.size()-1];
1409#ifdef COMPACT_LT_OR_LE
1419 if(top0.
is_false() && top1.is_true())
1421 else if(top0.
is_true() && top1.is_false())
1425 bv0.size() >= 2,
"signed bitvectors should have at least two bits");
1427 start = compareBelow.size() - 1;
1429 literalt &firstComp = compareBelow[start];
1434 else if(top1.is_false())
1436 else if(top1.is_true())
1446 prop.
lcnf(!top0, !top1, firstComp);
1448#ifdef INCLUDE_REDUNDANT_CLAUSES
1449 prop.
lcnf(top0, !top1, !firstComp);
1450 prop.
lcnf(!top0, top1, !firstComp);
1458 start = compareBelow.size() - 1;
1470 else if(compareBelow[i].
is_true())
1478 prop.
lcnf(!compareBelow[i], bv0[i], !bv1[i], result);
1479 prop.
lcnf(!compareBelow[i], !bv0[i], bv1[i], !result);
1486 for(i = start; i > 0; i--)
1488 prop.
lcnf(!compareBelow[i], !bv0[i], !bv1[i], compareBelow[i-1]);
1489 prop.
lcnf(!compareBelow[i], bv0[i], bv1[i], compareBelow[i-1]);
1493#ifdef INCLUDE_REDUNDANT_CLAUSES
1498 for(i = start; i > 0; i--)
1500 prop.
lcnf(compareBelow[i], !compareBelow[i-1]);
1501 prop.
lcnf(!compareBelow[i], bv0[i], !bv1[i], !compareBelow[i-1]);
1502 prop.
lcnf(!compareBelow[i], !bv0[i], bv1[i], !compareBelow[i-1]);
1507 prop.
lcnf(!compareBelow[0], !bv0[0], !bv1[0], (or_equal)?result:!result);
1508 prop.
lcnf(!compareBelow[0], bv0[0], bv1[0], (or_equal)?result:!result);
1526 "representation has either value signed or unsigned");
1541#ifdef COMPACT_LT_OR_LE
1563 return equal(bv0, bv1);
1564 else if(
id==ID_notequal)
1565 return !
equal(bv0, bv1);
1567 return lt_or_le(
true, bv0, bv1, rep);
1569 return lt_or_le(
false, bv0, bv1, rep);
1571 return lt_or_le(
true, bv1, bv0, rep);
1573 return lt_or_le(
false, bv1, bv0, rep);
1580 for(
const auto &literal : bv)
1582 if(!literal.is_constant())
1598 for(std::size_t i=0; i<a.size(); i++)
1615 odd_bits.reserve(src.size()/2);
1618 for(std::size_t i=0; i<src.size(); i++)
1621 odd_bits.push_back(src[i]);
1630 even_bits.reserve(src.size()/2);
1633 for(std::size_t i=0; i<src.size(); i++)
1636 even_bits.push_back(src[i]);
static bvt inverted(const bvt &op)
literalt signed_less_than(const bvt &bv0, const bvt &bv1)
static bool is_constant(const bvt &bv)
NODISCARD std::pair< bvt, literalt > adder(const bvt &op0, const bvt &op1, literalt carry_in)
Return the sum and carry-out when adding op0 and op1 under initial carry carry_in.
bvt wallace_tree(const std::vector< bvt > &pps)
literalt is_not_zero(const bvt &op)
static bvt verilog_bv_normal_bits(const bvt &)
literalt is_int_min(const bvt &op)
static bvt extract_msb(const bvt &a, std::size_t n)
void set_equal(const bvt &a, const bvt &b)
static literalt sign_bit(const bvt &op)
bvt multiplier_no_overflow(const bvt &op0, const bvt &op1, representationt rep)
bvt add_sub_no_overflow(const bvt &op0, const bvt &op1, bool subtract, representationt rep)
bvt add(const bvt &op0, const bvt &op1)
bvt absolute_value(const bvt &op)
bvt select(literalt s, const bvt &a, const bvt &b)
If s is true, selects a otherwise selects b.
static bvt build_constant(const mp_integer &i, std::size_t width)
void signed_divider(const bvt &op0, const bvt &op1, bvt &res, bvt &rem)
literalt is_zero(const bvt &op)
literalt equal(const bvt &op0, const bvt &op1)
Bit-blasting ID_equal and use in other encodings.
bvt signed_multiplier(const bvt &op0, const bvt &op1)
literalt overflow_add(const bvt &op0, const bvt &op1, representationt rep)
literalt is_one(const bvt &op)
bvt cond_negate_no_overflow(const bvt &bv, const literalt cond)
literalt overflow_sub(const bvt &op0, const bvt &op1, representationt rep)
void cond_implies_equal(literalt cond, const bvt &a, const bvt &b)
literalt unsigned_less_than(const bvt &bv0, const bvt &bv1)
bvt signed_multiplier_no_overflow(const bvt &op0, const bvt &op1)
bvt incrementer(const bvt &op, literalt carry_in)
bvt add_sub(const bvt &op0, const bvt &op1, bool subtract)
static bvt shift(const bvt &op, const shiftt shift, std::size_t distance)
literalt lt_or_le(bool or_equal, const bvt &bv0, const bvt &bv1, representationt rep)
literalt overflow_negate(const bvt &op)
static bvt concatenate(const bvt &a, const bvt &b)
literalt full_adder(const literalt a, const literalt b, const literalt carry_in, literalt &carry_out)
bvt negate_no_overflow(const bvt &op)
static bvt extract(const bvt &a, std::size_t first, std::size_t last)
bvt unsigned_multiplier(const bvt &op0, const bvt &op1)
bvt divider(const bvt &op0, const bvt &op1, representationt rep)
void unsigned_divider(const bvt &op0, const bvt &op1, bvt &res, bvt &rem)
bvt multiplier(const bvt &op0, const bvt &op1, representationt rep)
bvt cond_negate(const bvt &bv, const literalt cond)
bvt dadda_tree(const std::vector< bvt > &pps)
literalt carry_out(const bvt &op0, const bvt &op1, literalt carry_in)
bvt negate(const bvt &op)
static bvt extension(const bvt &bv, std::size_t new_size, representationt rep)
static bvt extract_lsb(const bvt &a, std::size_t n)
literalt verilog_bv_has_x_or_z(const bvt &)
literalt carry(literalt a, literalt b, literalt c)
bvt unsigned_multiplier_no_overflow(const bvt &op0, const bvt &op1)
static bvt zeros(std::size_t new_size)
literalt rel(const bvt &bv0, irep_idt id, const bvt &bv1, representationt rep)
NODISCARD bvt adder_no_overflow(const bvt &op0, const bvt &op1, bool subtract, representationt rep)
bvt saturating_add_sub(const bvt &op0, const bvt &op1, bool subtract, representationt rep)
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
void l_set_to_true(literalt a)
virtual literalt land(literalt a, literalt b)=0
virtual literalt limplies(literalt a, literalt b)=0
virtual literalt lselect(literalt a, literalt b, literalt c)=0
virtual bool cnf_handled_well() const
virtual void l_set_to(literalt a, bool value)
virtual literalt lxor(literalt a, literalt b)=0
void lcnf(literalt l0, literalt l1)
virtual void set_equal(literalt a, literalt b)
asserts a==b in the propositional formula
virtual bvt new_variables(std::size_t width)
generates a bitvector of given width with new variables
virtual literalt new_variable()=0
virtual literalt lequal(literalt a, literalt b)=0
virtual literalt lor(literalt a, literalt b)=0
void l_set_to_false(literalt a)
virtual bool has_set_to() const
bool is_false(const literalt &l)
bool is_true(const literalt &l)
std::vector< literalt > bvt
literalt const_literal(bool value)
const std::string integer2binary(const mp_integer &n, std::size_t width)
#define CHECK_RETURN(CONDITION)
#define UNREACHABLE
This should be used to mark dead code.
#define PRECONDITION(CONDITION)
#define INVARIANT(CONDITION, REASON)
This macro uses the wrapper function 'invariant_violated_string'.
#define POSTCONDITION(CONDITION)