17 for(std::size_t i=0; i<width; i++)
27 tmp.erase(tmp.begin(), tmp.begin()+1);
34 for(std::size_t i=0; i<a.size(); i++)
46 result.resize(last+1);
48 result.erase(result.begin(), result.begin()+first);
60 result.erase(result.begin(), result.begin()+(result.size()-n));
80 result.resize(a.size()+b.size());
82 for(std::size_t i=0; i<a.size(); i++)
85 for(std::size_t i=0; i<b.size(); i++)
86 result[i+a.size()]=b[i];
98 result.resize(a.size());
99 for(std::size_t i=0; i<result.size(); i++)
107 std::size_t new_size,
110 std::size_t old_size=bv.size();
114 result.resize(new_size);
120 for(std::size_t i=old_size; i<new_size; i++)
121 result[i]=extend_with;
134 #define OPTIMAL_FULL_ADDER
142 #ifdef OPTIMAL_FULL_ADDER
147 int constantProp = -1;
153 constantProp = (a.
is_true()) ? 1 : 0;
159 constantProp = (b.
is_true()) ? 1 : 0;
165 constantProp = (carry_in.
is_true()) ? 1 : 0;
171 if(constantProp == 1)
177 else if(constantProp == 0)
225 #define COMPACT_CARRY
233 const auto const_count =
303 for(std::size_t i=0; i<sum.size(); i++)
318 for(std::size_t i=0; i<op0.size(); i++)
352 const bvt op1_sign_applied=
371 literalt old_sign=op0[op0.size()-1];
396 literalt op0_is_negative=op0[op0.size()-1];
425 literalt old_sign=sum[sum.size()-1];
427 prop.
lequal(sum[sum.size()-1], tmp_op[tmp_op.size()-1]);
440 "representation has either value signed or unsigned");
458 std::size_t d=1, width=op.size();
461 for(std::size_t stage=0; stage<dist.size(); stage++)
467 for(std::size_t i=0; i<width; i++)
468 result[i]=
prop.
lselect(dist[stage], tmp[i], result[i]);
480 result.resize(src.size());
486 for(std::size_t i=0; i<src.size(); i++)
500 l=(dist<src.size()-i?src[i+dist]:src[src.size()-1]);
511 l=src[(src.size()+i-(dist%src.size()))%src.size()];
516 l=src[(i+(dist%src.size()))%src.size()];
561 for(
auto &literal : bv)
580 for(
auto &literal : result)
591 else if(pps.size()==2)
592 return add(pps[0], pps[1]);
595 std::vector<bvt> new_pps;
596 std::size_t no_full_adders=pps.size()/3;
599 for(std::size_t i=0; i<no_full_adders; i++)
601 const bvt &a=pps[i*3+0],
605 INVARIANT(a.size() == b.size(),
"groups should be of equal size");
606 INVARIANT(a.size() == c.size(),
"groups should be of equal size");
608 bvt s(a.size()), t(a.size());
610 for(std::size_t bit=0; bit<a.size(); bit++)
615 carry(a[bit-1], b[bit-1], c[bit-1]);
618 new_pps.push_back(s);
619 new_pps.push_back(t);
623 for(std::size_t i=no_full_adders*3; i<pps.size(); i++)
624 new_pps.push_back(pps[i]);
634 bvt op0=_op0, op1=_op1;
640 product.resize(op0.size());
642 for(std::size_t i=0; i<product.size(); i++)
645 for(std::size_t sum=0; sum<op0.size(); sum++)
650 tmpop.reserve(op0.size());
652 for(std::size_t idx=0; idx<sum; idx++)
655 for(std::size_t idx=sum; idx<op0.size(); idx++)
656 tmpop.push_back(
prop.
land(op1[idx-sum], op0[sum]));
658 product=
add(product, tmpop);
668 bvt op0=_op0, op1=_op1;
673 std::vector<bvt> pps;
674 pps.reserve(op0.size());
676 for(std::size_t bit=0; bit<op0.size(); bit++)
681 pp.reserve(op0.size());
684 for(std::size_t idx=0; idx<bit; idx++)
687 for(std::size_t idx=bit; idx<op0.size(); idx++)
688 pp.push_back(
prop.
land(op1[idx-bit], op0[bit]));
694 return zeros(op0.size());
705 bvt _op0=op0, _op1=op1;
713 product.resize(_op0.size());
715 for(std::size_t i=0; i<product.size(); i++)
718 for(std::size_t sum=0; sum<op0.size(); sum++)
723 tmpop.reserve(product.size());
725 for(std::size_t idx=0; idx<sum; idx++)
728 for(std::size_t idx=sum; idx<product.size(); idx++)
729 tmpop.push_back(
prop.
land(op1[idx-sum], op0[sum]));
733 for(std::size_t idx=op1.size()-sum; idx<op1.size(); idx++)
742 if(op0.empty() || op1.empty())
763 result.resize(bv.size());
765 for(std::size_t i=0; i<bv.size(); i++)
788 if(op0.empty() || op1.empty())
842 if(op0.empty() || op1.empty())
845 bvt _op0(op0), _op1(op1);
847 literalt sign_0=_op0[_op0.size()-1];
848 literalt sign_1=_op1[_op1.size()-1];
852 for(std::size_t i=0; i<_op0.size(); i++)
855 for(std::size_t i=0; i<_op1.size(); i++)
864 for(std::size_t i=0; i<res.size(); i++)
865 res[i]=
prop.
lselect(result_sign, neg_res[i], res[i]);
867 for(std::size_t i=0; i<res.size(); i++)
895 std::size_t width=op0.size();
900 std::size_t one_count=0, non_const_count=0, one_pos=0;
902 for(std::size_t i=0; i<op1.size(); i++)
914 if(non_const_count==0 && one_count==1 && one_pos!=0)
917 res=
shift(op0, LRIGHT, one_pos);
921 for(std::size_t i=one_pos; i<rem.size(); i++)
969 #ifdef COMPACT_EQUAL_CONST
975 void bv_utilst::equal_const_register(
const bvt &var)
978 equal_const_registered.insert(var);
990 std::size_t size = var.size();
1000 constant.pop_back();
1005 var_constant_pairt index(var, constant);
1007 equal_const_cachet::iterator entry = equal_const_cache.find(index);
1009 if(entry != equal_const_cache.end())
1011 return entry->second;
1017 constant.pop_back();
1019 literalt rec = equal_const_rec(var, constant);
1022 equal_const_cache.insert(
1023 std::pair<var_constant_pairt, literalt>(index, compare));
1038 literalt bv_utilst::equal_const(
const bvt &var,
const bvt &constant)
1040 std::size_t size = constant.size();
1058 literalt top_bit = constant[size - 1];
1060 std::size_t split = size - 1;
1061 var_upper.push_back(var[size - 1]);
1062 constant_upper.push_back(constant[size - 1]);
1064 for(split = size - 2; split != 0; --split)
1066 if(constant[split] != top_bit)
1072 var_upper.push_back(var[split]);
1073 constant_upper.push_back(constant[split]);
1077 for(std::size_t i = 0; i <= split; ++i)
1079 var_lower.push_back(var[i]);
1080 constant_lower.push_back(constant[i]);
1085 var_upper.size() + var_lower.size() == size,
1086 "lower size plus upper size should equal the total size");
1088 constant_upper.size() + constant_lower.size() == size,
1089 "lower size plus upper size should equal the total size");
1091 literalt top_comparison = equal_const_rec(var_upper, constant_upper);
1092 literalt bottom_comparison = equal_const_rec(var_lower, constant_lower);
1094 return prop.
land(top_comparison, bottom_comparison);
1107 #ifdef COMPACT_EQUAL_CONST
1111 equal_const_registered.find(op1) != equal_const_registered.end())
1112 return equal_const(op1, op0);
1114 equal_const_registered.find(op0) != equal_const_registered.end())
1115 return equal_const(op0, op1);
1119 equal_bv.resize(op0.size());
1121 for(std::size_t i=0; i<op0.size(); i++)
1151 top1=bv1[bv1.size()-1];
1153 #ifdef COMPACT_LT_OR_LE
1167 bv0.size() >= 2,
"signed bitvectors should have at least two bits");
1168 start = compareBelow.size() - 2;
1170 literalt firstComp=compareBelow[start];
1173 #ifdef INCLUDE_REDUNDANT_CLAUSES
1181 prop.
lcnf(!top0, !top1, firstComp);
1183 #ifdef INCLUDE_REDUNDANT_CLAUSES
1184 prop.
lcnf(top0, !top1, !firstComp);
1185 prop.
lcnf(!top0, top1, !firstComp);
1191 start = compareBelow.size() - 1;
1201 prop.
lcnf(!compareBelow[i], bv0[i], !bv1[i], result);
1202 prop.
lcnf(!compareBelow[i], !bv0[i], bv1[i], !result);
1209 for(i = start; i > 0; i--)
1211 prop.
lcnf(!compareBelow[i], !bv0[i], !bv1[i], compareBelow[i-1]);
1212 prop.
lcnf(!compareBelow[i], bv0[i], bv1[i], compareBelow[i-1]);
1216 #ifdef INCLUDE_REDUNDANT_CLAUSES
1221 for(i = start; i > 0; i--)
1223 prop.
lcnf(compareBelow[i], !compareBelow[i-1]);
1224 prop.
lcnf(!compareBelow[i], bv0[i], !bv1[i], !compareBelow[i-1]);
1225 prop.
lcnf(!compareBelow[i], !bv0[i], bv1[i], !compareBelow[i-1]);
1230 prop.
lcnf(!compareBelow[0], !bv0[0], !bv1[0], (or_equal)?result:!result);
1231 prop.
lcnf(!compareBelow[0], bv0[0], bv1[0], (or_equal)?result:!result);
1249 "representation has either value signed or unsigned");
1264 #ifdef COMPACT_LT_OR_LE
1265 return lt_or_le(
false, op0, op1, UNSIGNED);
1286 return equal(bv0, bv1);
1287 else if(
id==ID_notequal)
1288 return !
equal(bv0, bv1);
1290 return lt_or_le(
true, bv0, bv1, rep);
1292 return lt_or_le(
false, bv0, bv1, rep);
1294 return lt_or_le(
true, bv1, bv0, rep);
1296 return lt_or_le(
false, bv1, bv0, rep);
1303 for(
const auto &literal : bv)
1305 if(!literal.is_constant())
1321 for(std::size_t i=0; i<a.size(); i++)
1338 odd_bits.reserve(src.size()/2);
1341 for(std::size_t i=0; i<src.size(); i++)
1344 odd_bits.push_back(src[i]);
1353 even_bits.reserve(src.size()/2);
1356 for(std::size_t i=0; i<src.size(); i++)
1359 even_bits.push_back(src[i]);
static bvt inverted(const bvt &op)
literalt signed_less_than(const bvt &bv0, const bvt &bv1)
void adder_no_overflow(bvt &sum, const bvt &op, bool subtract, representationt rep)
static bool is_constant(const bvt &bv)
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)
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)
void adder(bvt &sum, const bvt &op, literalt carry_in, literalt &carry_out)
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)
To provide a bitwise model of < or <=.
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)
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)
literalt rel(const bvt &bv0, irep_idt id, const bvt &bv1, representationt rep)
bvt zeros(std::size_t new_size) const
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
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 POSTCONDITION(CONDITION)