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)
216 #endif // OPTIMAL_FULL_ADDER
225 #define COMPACT_CARRY
233 const auto const_count =
280 #endif // COMPACT_CARRY
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=
376 bvt add_sub_result = op0;
382 result.reserve(add_sub_result.size());
390 for(
const auto &literal : add_sub_result)
418 for(std::size_t i = 0; i < add_sub_result.size() - 1; ++i)
420 const auto &literal = add_sub_result[i];
422 prop.
lor(overflow_to_max_int, literal), !overflow_to_min_int));
426 prop.
lor(overflow_to_min_int, add_sub_result.back()),
427 !overflow_to_max_int));
441 literalt old_sign=op0[op0.size()-1];
466 literalt op0_is_negative=op0[op0.size()-1];
495 literalt old_sign=sum[sum.size()-1];
497 prop.
lequal(sum[sum.size()-1], tmp_op[tmp_op.size()-1]);
510 "representation has either value signed or unsigned");
528 std::size_t d=1, width=op.size();
531 for(std::size_t stage=0; stage<dist.size(); stage++)
537 for(std::size_t i=0; i<width; i++)
538 result[i]=
prop.
lselect(dist[stage], tmp[i], result[i]);
550 result.resize(src.size());
556 for(std::size_t i=0; i<src.size(); i++)
570 l=(dist<src.size()-i?src[i+dist]:src[src.size()-1]);
581 l=src[(src.size()+i-(dist%src.size()))%src.size()];
586 l=src[(i+(dist%src.size()))%src.size()];
618 bvt should_be_zeros(bv);
619 should_be_zeros.pop_back();
631 for(
auto &literal : bv)
650 for(
auto &literal : result)
661 else if(pps.size()==2)
662 return add(pps[0], pps[1]);
665 std::vector<bvt> new_pps;
666 std::size_t no_full_adders=pps.size()/3;
669 for(std::size_t i=0; i<no_full_adders; i++)
671 const bvt &a=pps[i*3+0],
675 INVARIANT(a.size() == b.size(),
"groups should be of equal size");
676 INVARIANT(a.size() == c.size(),
"groups should be of equal size");
678 bvt s(a.size()), t(a.size());
680 for(std::size_t bit=0; bit<a.size(); bit++)
685 carry(a[bit-1], b[bit-1], c[bit-1]);
688 new_pps.push_back(s);
689 new_pps.push_back(t);
693 for(std::size_t i=no_full_adders*3; i<pps.size(); i++)
694 new_pps.push_back(pps[i]);
704 bvt op0=_op0, op1=_op1;
710 product.resize(op0.size());
712 for(std::size_t i=0; i<product.size(); i++)
715 for(std::size_t sum=0; sum<op0.size(); sum++)
719 tmpop.reserve(op0.size());
721 for(std::size_t idx=sum; idx<op0.size(); idx++)
722 tmpop.push_back(
prop.
land(op1[idx-sum], op0[sum]));
724 product=
add(product, tmpop);
734 bvt op0=_op0, op1=_op1;
739 std::vector<bvt> pps;
740 pps.reserve(op0.size());
742 for(std::size_t bit=0; bit<op0.size(); bit++)
747 pp.reserve(op0.size());
749 for(std::size_t idx=bit; idx<op0.size(); idx++)
750 pp.push_back(
prop.
land(op1[idx-bit], op0[bit]));
756 return zeros(op0.size());
767 bvt _op0=op0, _op1=op1;
775 product.resize(_op0.size());
777 for(std::size_t i=0; i<product.size(); i++)
780 for(std::size_t sum=0; sum<op0.size(); sum++)
785 tmpop.reserve(product.size());
787 for(std::size_t idx=0; idx<sum; idx++)
790 for(std::size_t idx=sum; idx<product.size(); idx++)
791 tmpop.push_back(
prop.
land(op1[idx-sum], op0[sum]));
795 for(std::size_t idx=op1.size()-sum; idx<op1.size(); idx++)
804 if(op0.empty() || op1.empty())
825 result.resize(bv.size());
827 for(std::size_t i=0; i<bv.size(); i++)
850 if(op0.empty() || op1.empty())
904 if(op0.empty() || op1.empty())
907 bvt _op0(op0), _op1(op1);
909 literalt sign_0=_op0[_op0.size()-1];
910 literalt sign_1=_op1[_op1.size()-1];
914 for(std::size_t i=0; i<_op0.size(); i++)
917 for(std::size_t i=0; i<_op1.size(); i++)
926 for(std::size_t i=0; i<res.size(); i++)
927 res[i]=
prop.
lselect(result_sign, neg_res[i], res[i]);
929 for(std::size_t i=0; i<res.size(); i++)
957 std::size_t width=op0.size();
962 std::size_t one_count=0, non_const_count=0, one_pos=0;
964 for(std::size_t i=0; i<op1.size(); i++)
976 if(non_const_count==0 && one_count==1 && one_pos!=0)
979 res=
shift(op0, LRIGHT, one_pos);
983 for(std::size_t i=one_pos; i<rem.size(); i++)
1031 #ifdef COMPACT_EQUAL_CONST
1037 void bv_utilst::equal_const_register(
const bvt &var)
1040 equal_const_registered.insert(var);
1052 std::size_t size = var.size();
1062 constant.pop_back();
1067 var_constant_pairt index(var, constant);
1069 equal_const_cachet::iterator entry = equal_const_cache.find(index);
1071 if(entry != equal_const_cache.end())
1073 return entry->second;
1079 constant.pop_back();
1081 literalt rec = equal_const_rec(var, constant);
1084 equal_const_cache.insert(
1085 std::pair<var_constant_pairt, literalt>(index, compare));
1100 literalt bv_utilst::equal_const(
const bvt &var,
const bvt &constant)
1102 std::size_t size = constant.size();
1120 literalt top_bit = constant[size - 1];
1122 std::size_t split = size - 1;
1123 var_upper.push_back(var[size - 1]);
1124 constant_upper.push_back(constant[size - 1]);
1126 for(split = size - 2; split != 0; --split)
1128 if(constant[split] != top_bit)
1134 var_upper.push_back(var[split]);
1135 constant_upper.push_back(constant[split]);
1139 for(std::size_t i = 0; i <= split; ++i)
1141 var_lower.push_back(var[i]);
1142 constant_lower.push_back(constant[i]);
1147 var_upper.size() + var_lower.size() == size,
1148 "lower size plus upper size should equal the total size");
1150 constant_upper.size() + constant_lower.size() == size,
1151 "lower size plus upper size should equal the total size");
1153 literalt top_comparison = equal_const_rec(var_upper, constant_upper);
1154 literalt bottom_comparison = equal_const_rec(var_lower, constant_lower);
1156 return prop.
land(top_comparison, bottom_comparison);
1169 #ifdef COMPACT_EQUAL_CONST
1173 equal_const_registered.find(op1) != equal_const_registered.end())
1174 return equal_const(op1, op0);
1176 equal_const_registered.find(op0) != equal_const_registered.end())
1177 return equal_const(op0, op1);
1181 equal_bv.resize(op0.size());
1183 for(std::size_t i=0; i<op0.size(); i++)
1194 #define COMPACT_LT_OR_LE
1209 top1=bv1[bv1.size()-1];
1211 #ifdef COMPACT_LT_OR_LE
1225 bv0.size() >= 2,
"signed bitvectors should have at least two bits");
1226 start = compareBelow.size() - 2;
1228 literalt firstComp=compareBelow[start];
1231 #ifdef INCLUDE_REDUNDANT_CLAUSES
1239 prop.
lcnf(!top0, !top1, firstComp);
1241 #ifdef INCLUDE_REDUNDANT_CLAUSES
1242 prop.
lcnf(top0, !top1, !firstComp);
1243 prop.
lcnf(!top0, top1, !firstComp);
1249 start = compareBelow.size() - 1;
1259 prop.
lcnf(!compareBelow[i], bv0[i], !bv1[i], result);
1260 prop.
lcnf(!compareBelow[i], !bv0[i], bv1[i], !result);
1267 for(i = start; i > 0; i--)
1269 prop.
lcnf(!compareBelow[i], !bv0[i], !bv1[i], compareBelow[i-1]);
1270 prop.
lcnf(!compareBelow[i], bv0[i], bv1[i], compareBelow[i-1]);
1274 #ifdef INCLUDE_REDUNDANT_CLAUSES
1279 for(i = start; i > 0; i--)
1281 prop.
lcnf(compareBelow[i], !compareBelow[i-1]);
1282 prop.
lcnf(!compareBelow[i], bv0[i], !bv1[i], !compareBelow[i-1]);
1283 prop.
lcnf(!compareBelow[i], !bv0[i], bv1[i], !compareBelow[i-1]);
1288 prop.
lcnf(!compareBelow[0], !bv0[0], !bv1[0], (or_equal)?result:!result);
1289 prop.
lcnf(!compareBelow[0], bv0[0], bv1[0], (or_equal)?result:!result);
1307 "representation has either value signed or unsigned");
1322 #ifdef COMPACT_LT_OR_LE
1344 return equal(bv0, bv1);
1345 else if(
id==ID_notequal)
1346 return !
equal(bv0, bv1);
1348 return lt_or_le(
true, bv0, bv1, rep);
1350 return lt_or_le(
false, bv0, bv1, rep);
1352 return lt_or_le(
true, bv1, bv0, rep);
1354 return lt_or_le(
false, bv1, bv0, rep);
1361 for(
const auto &literal : bv)
1363 if(!literal.is_constant())
1379 for(std::size_t i=0; i<a.size(); i++)
1396 odd_bits.reserve(src.size()/2);
1399 for(std::size_t i=0; i<src.size(); i++)
1402 odd_bits.push_back(src[i]);
1411 even_bits.reserve(src.size()/2);
1414 for(std::size_t i=0; i<src.size(); i++)
1417 even_bits.push_back(src[i]);