31 #define SMTRAT_BV_INCREMENTAL_MODE
35 #include <carl-formula/bitvector/BVConstraint.h>
36 #include <carl-formula/bitvector/BVConstraintPool.h>
45 typedef std::vector<Bit>
Bits;
65 #ifdef SMTRAT_BV_INCREMENTAL_MODE
99 #ifdef SMTRAT_BV_INCREMENTAL_MODE
109 #ifdef SMTRAT_BV_ENCODER_DEBUG
110 std::cerr <<
" -> " << _formula << std::endl;
118 for(
size_t i=0;i<_value.width();++i)
128 carl::FastMap<BitVec, Variables>::iterator it =
mBitVecBits.find(_variable);
137 boolVariables = it->second;
146 for(std::size_t i=0;i<_then.size();++i)
155 Bits concatenated(_second);
156 concatenated.insert(concatenated.end(), _first.begin(), _first.end());
162 return createBits(
Bits(&_operand[_lowest], &_operand[_highest+1]));
168 for(
const Bit& bit : _operand) {
180 carry.push_back(
const1());
182 for(
size_t i=1;i<_operand.size();++i)
189 for(
size_t i=0;i<_operand.size();++i)
200 for(std::size_t i = 0; i < _first.size(); ++i) {
209 for(std::size_t i = 0; i < _first.size(); ++i) {
218 for(std::size_t i = 0; i < _first.size(); ++i) {
227 for(std::size_t i = 0; i < _first.size(); ++i) {
236 for(std::size_t i = 0; i < _first.size(); ++i) {
245 for(std::size_t i = 0; i < _first.size(); ++i) {
267 std::size_t carryBitCount = _first.size() + ((_withCarryOut || ! _allowOverflow) ? 1 : 0);
269 for(std::size_t i=1;i<carryBitCount;++i) {
272 boolAnd(_first[i-1], _second[i-1]),
274 boolXor(_first[i-1], _second[i-1]),
281 for(std::size_t i=0;i<_first.size();++i) {
287 if(! _allowOverflow) {
291 out.push_back(carry[carry.size()-1]);
304 std::vector<Bits> summands(_first.size());
305 std::vector<Bits> sums(_first.size()-1);
307 for(std::size_t i=0;i<summands.size();++i) {
308 for(std::size_t j=0;j<_first.size();++j) {
310 summands[i].push_back(
const0());
316 if(! _allowOverflow) {
317 for(std::size_t j=_first.size();j<_first.size()+i;++j) {
323 for(std::size_t i=0;i<sums.size();++i) {
324 sums[i] =
encodeAdderNetwork((i == 0 ? summands[0] : sums[i-1]), summands[i+1],
false,
false, _allowOverflow);
327 if(sums.size() > 0) {
328 return sums[sums.size()-1];
357 Bit remainderLessThanDivisor =
encodeUlt(remainder, _second);
360 if (!_returnRemainder) {
367 Bit remainderIsFirstOp =
encodeEq(remainder, _first);
371 return (_returnRemainder ? remainder : quotient);
388 Bit msbFirst = _first[_first.size()-1];
389 Bit msbSecond = _second[_second.size()-1];
419 Bit msbFirst = _first[_first.size()-1];
420 Bit msbSecond = _second[_second.size()-1];
450 Bit msbFirst = _first[_first.size()-1];
451 Bit msbSecond = _second[_second.size()-1];
472 out.push_back(
encodeEq(_first, _second));
483 std::size_t firstSize = _first.size() - 1;
484 std::size_t highestRelevantPos = 0;
486 while((firstSize >>= 1) != 0)
487 ++highestRelevantPos;
489 Bits lastStage(_first);
490 std::size_t currentShiftBy = 1;
492 for(std::size_t stage=0;stage<=highestRelevantPos && stage<_second.size();++stage)
496 for(std::size_t pos=0;pos<lastStage.size();++pos)
498 Bit notShifted = lastStage[pos];
501 if((_left && pos >= currentShiftBy) || (! _left && pos + currentShiftBy < lastStage.size()))
503 shifted = lastStage[_left ? (pos - currentShiftBy) : (pos + currentShiftBy)];
507 shifted = _arithmetic ? _first[_first.size() - 1] :
const0();
510 currentStage.push_back(
createBit(
boolIte(_second[stage], shifted, notShifted)));
514 lastStage = currentStage;
517 if(highestRelevantPos >= _second.size() - 1)
523 Bits overshiftBits(&_second[highestRelevantPos+1], &_second[_second.size()]);
528 for(std::size_t i=0;i<_first.size();++i)
531 boolIte(overshift, (_arithmetic ? _first[_first.size()-1] :
const0()), lastStage[i])
551 Bits rotated(_operand);
552 std::rotate(rotated.begin(),
553 rotated.begin() + (Bits::difference_type)(
555 (_index % _operand.size()))
563 Bits rotated(_operand);
564 std::rotate(rotated.begin(),
565 rotated.begin() + (Bits::difference_type)(
566 _index % _operand.size()),
574 for(std::size_t i=0;i<_index;++i) {
583 for(std::size_t i=0;i<_index;++i) {
584 out.push_back(
createBit(_operand[_operand.size()-1]));
592 for(std::size_t i=0;i<_index;++i)
594 repeated.insert(repeated.end(), _operand.begin(), _operand.end());
601 #ifndef SMTRAT_BV_INCREMENTAL_MODE
610 carl::BVTermType
type = _term.type();
612 if(carl::typeIsUnary(
type) ||
type == carl::BVTermType::EXTRACT) {
615 else if(carl::typeIsBinary(
type)) {
620 #ifdef SMTRAT_BV_INCREMENTAL_MODE
631 #ifdef SMTRAT_BV_ENCODER_DEBUG
632 std::cerr <<
"[BV] Encoding term: " << _term << std::endl;
637 case carl::BVTermType::CONSTANT:
639 case carl::BVTermType::VARIABLE:
641 case carl::BVTermType::CONCAT:
643 case carl::BVTermType::EXTRACT:
644 out =
encodeExtract(subTerm1, _term.highest(), _term.lowest());
break;
647 case carl::BVTermType::NEG:
650 out =
encodeAnd(subTerm1, subTerm2);
break;
652 out =
encodeOr(subTerm1, subTerm2);
break;
654 out =
encodeXor(subTerm1, subTerm2);
break;
655 case carl::BVTermType::NAND:
657 case carl::BVTermType::NOR:
658 out =
encodeNor(subTerm1, subTerm2);
break;
659 case carl::BVTermType::XNOR:
661 case carl::BVTermType::ADD:
662 out =
encodeAdd(subTerm1, subTerm2);
break;
663 case carl::BVTermType::SUB:
664 out =
encodeSub(subTerm1, subTerm2);
break;
665 case carl::BVTermType::MUL:
666 out =
encodeMul(subTerm1, subTerm2);
break;
667 case carl::BVTermType::DIV_U:
669 case carl::BVTermType::DIV_S:
671 case carl::BVTermType::MOD_U:
673 case carl::BVTermType::MOD_S1:
675 case carl::BVTermType::MOD_S2:
679 case carl::BVTermType::LSHIFT:
681 case carl::BVTermType::RSHIFT_LOGIC:
683 case carl::BVTermType::RSHIFT_ARITH:
685 case carl::BVTermType::LROTATE:
687 case carl::BVTermType::RROTATE:
689 case carl::BVTermType::EXT_U:
690 out =
encodeExtU(subTerm1, _term.index());
break;
691 case carl::BVTermType::EXT_S:
692 out =
encodeExtS(subTerm1, _term.index());
break;
693 case carl::BVTermType::REPEAT:
702 #ifdef SMTRAT_BV_ENCODER_DEBUG
703 std::cerr <<
"Encoded into:";
704 for(
auto bIt = out.crbegin(); bIt != out.crend(); ++bIt) {
705 std::cerr <<
" <" << *bIt <<
">";
707 std::cerr << std::endl;
715 #ifndef SMTRAT_BV_INCREMENTAL_MODE
729 #ifdef SMTRAT_BV_INCREMENTAL_MODE
740 carl::BVCompareRelation relation = _constraint.relation();
743 #ifdef SMTRAT_BV_ENCODER_DEBUG
744 std::cerr <<
"[BV] Encoding constraint: " << _constraint << std::endl;
751 case carl::BVCompareRelation::NEQ:
753 case carl::BVCompareRelation::ULT:
755 case carl::BVCompareRelation::ULE:
757 case carl::BVCompareRelation::UGT:
759 case carl::BVCompareRelation::UGE:
761 case carl::BVCompareRelation::SLT:
763 case carl::BVCompareRelation::SLE:
765 case carl::BVCompareRelation::SGT:
767 case carl::BVCompareRelation::SGE:
776 #ifdef SMTRAT_BV_ENCODER_DEBUG
777 std::cerr <<
"Encoded into: <" << out <<
">" << std::endl;
787 for(std::size_t i=0;i<_lhs.size();++i)
789 comparisons.push_back(
boolIff(_lhs[i], _rhs[i]));
821 return addition[addition.size()-1];
833 Bit msbLhs = _lhs[_lhs.size()-1];
834 Bit msbRhs = _rhs[_rhs.size()-1];
852 Bit msbLhs = _lhs[_lhs.size()-1];
853 Bit msbRhs = _rhs[_rhs.size()-1];
874 if(_formula.is_atom() || (_formula.type() ==
carl::FormulaType::NOT && _formula.subformula().is_atom())) {
891 #ifndef SMTRAT_BV_ENCODER_DEBUG
899 for(std::size_t i=0;i<_n;++i) {
910 for(
const Bit& bit : _original) {
929 return Formula(carl::FormulaType::IMPLIES, {_first, _second});
976 FormulaT passedFormula = carl::visit_result(_inputFormula, encodeConstraints);
978 #ifdef SMTRAT_BV_ENCODER_DEBUG
979 std::cerr <<
"Formula encoded into: " << passedFormula << std::endl;
Bit boolIff(const Bit &_first, const Bit &_second)
Bits encodeRshiftLogic(const Bits &_first, const Bits &_second)
Bits encodeDivU(const Bits &_first, const Bits &_second)
Bits encodeRrotate(const Bits &_operand, std::size_t _index)
Bits createBits(const Bits &_original)
Bits encodeShiftNetwork(const Bits &_first, const Bits &_second, bool _left, bool _arithmetic=false)
FormulaT encodeBVConstraints(const FormulaT _original)
Bit boolNot(const Bit &_operand)
Bits encodeComp(const Bits &_first, const Bits &_second)
Variable createVariable()
Bits encodeVariable(const BitVec &_variable)
Bits encodeExtract(const Bits &_operand, std::size_t _highest, std::size_t _lowest)
Bit boolImplies(const Bit &_first, const Bit &_second)
Bits encodeAdd(const Bits &_first, const Bits &_second)
carl::FastMap< BitVecConstr, Bit > mConstraintBits
Bits createBits(const Variables &_variables)
Bits encodeConstant(const carl::BVValue &_value)
Bits encodeExtS(const Bits &_operand, std::size_t _index)
std::set< Variable > mIntroducedVariables
Bit encodeUle(const Bits &_lhs, const Bits &_rhs)
Bit encodeSgt(const Bits &_lhs, const Bits &_rhs)
const FormulaSetT & encode(const FormulaT &_inputFormula)
Bit encodeSle(const Bits &_lhs, const Bits &_rhs)
Bits encodeLrotate(const Bits &_operand, std::size_t _index)
std::optional< BitVecTerm > mCurrentTerm
Bit encodeSlt(const Bits &_lhs, const Bits &_rhs)
Bits encodeModU(const Bits &_first, const Bits &_second)
std::vector< Variable > Variables
Bits encodeNot(const Bits &_operand)
Bit encodeEq(const Bits &_lhs, const Bits &_rhs)
Bit encodeUlt(const Bits &_lhs, const Bits &_rhs)
Bit boolOr(const Bits &_operands)
const carl::FastMap< BitVec, Variables > bitvectorBlastings() const
Bits encodeNor(const Bits &_first, const Bits &_second)
Bit encodeConstraint(const BitVecConstr &_constraint)
const std::set< carl::Variable > & introducedVariables() const
Bits encodeOr(const Bits &_first, const Bits &_second)
Bits createBits(std::size_t _n)
Bits encodeRshiftArith(const Bits &_first, const Bits &_second)
Bits encodeAdderNetwork(const Bits &_first, const Bits &_second, bool _carryInValue=false, bool _withCarryOut=false, bool _allowOverflow=true)
Bits encodeNand(const Bits &_first, const Bits &_second)
Bit boolOr(const Bit &_first, const Bit &_second)
Bit encodeUgt(const Bits &_lhs, const Bits &_rhs)
Bit createBit(const FormulaT &_formula)
carl::FastMap< BitVec, Variables > mBitVecBits
FormulaSetT mCurrentEncodings
Bits encodeIte(const Bit &_condition, const Bits &_then, const Bits &_else)
Bits encodeXnor(const Bits &_first, const Bits &_second)
Bits encodeRepeat(const Bits &_operand, std::size_t _index)
carl::BVConstraint BitVecConstr
Bit boolXor(const Bit &_first, const Bit &_second)
std::optional< BitVecConstr > mCurrentConstraint
Bits encodeModS1(const Bits &_first, const Bits &_second)
Bits encodeTerm(const BitVecTerm &_term)
Bits encodeDivS(const Bits &_first, const Bits &_second)
Bit encodeUge(const Bits &_lhs, const Bits &_rhs)
Bits encodeDivisionNetwork(const Bits &_first, const Bits &_second, bool _returnRemainder=false)
Bit encodeNeq(const Bits &_lhs, const Bits &_rhs)
carl::FastMap< BitVecTerm, Bits > mTermBits
Bits encodeMul(const Bits &_first, const Bits &_second)
Bits encodeLshift(const Bits &_first, const Bits &_second)
Bits encodeMultiplicationNetwork(const Bits &_first, const Bits &_second, bool _allowOverflow=true)
Bits encodeConcat(const Bits &_first, const Bits &_second)
carl::FastMap< BitVecTerm, FormulaSetT > mTermEncodings
carl::FastMap< BitVecConstr, FormulaSetT > mConstraintEncodings
Bits encodeNeg(const Bits &_operand)
Bit boolAnd(const Bit &_first, const Bit &_second)
Bits encodeModS2(const Bits &_first, const Bits &_second)
Bit boolAnd(const Bits &_operands)
void boolAssert(const Formula &_formula)
Bits encodeSub(const Bits &_first, const Bits &_second)
Bit boolIte(const Bit &_condition, const Bit &_then, const Bit &_else)
Bits encodeAnd(const Bits &_first, const Bits &_second)
Bit encodeSge(const Bits &_lhs, const Bits &_rhs)
Bits encodeExtU(const Bits &_operand, std::size_t _index)
Variables createVariables(std::size_t _n)
Bits encodeXor(const Bits &_first, const Bits &_second)
bool substitute(const FormulaT &constr, const carl::Variable var, const carl::vs::Term< Poly > &term, FormulaT &result)
carl::BVVariable BVVariable
carl::BVConstraint BVConstraint
Typedef for bitvector constraint.
carl::BVTerm BVTerm
Typedef for bitvector term.
QuantifierType type(const std::pair< QuantifierType, std::vector< carl::Variable >> &p)
Class to create the formulas for axioms.
carl::FormulaSet< Poly > FormulaSetT
carl::Formula< Poly > FormulaT