use of edu.stanford.CVC4.BitVectorType in project java-smt by sosy-lab.
the class CVC4BitvectorFormulaManager method divide.
@Override
protected Expr divide(Expr numerator, Expr denumerator, boolean signed) {
final Kind operator = signed ? Kind.BITVECTOR_SDIV : Kind.BITVECTOR_UDIV;
final Expr division = exprManager.mkExpr(operator, numerator, denumerator);
// CVC4 does not align with SMTLIB standard when it comes to divide-by-zero.
// For divide-by-zero, we compute the result as: return "1" with the opposite
// sign than the numerator.
final int bitsize = ((BitvectorType) formulaCreator.getFormulaType(numerator)).getSize();
final Expr zero = makeBitvectorImpl(bitsize, 0);
final Expr one = makeBitvectorImpl(bitsize, 1);
// all bits equal "1"
final Expr maxValue = makeBitvectorImpl(bitsize, -1);
return exprManager.mkExpr(Kind.ITE, exprManager.mkExpr(Kind.EQUAL, denumerator, zero), exprManager.mkExpr(Kind.ITE, lessThan(numerator, zero, signed), one, maxValue), division);
}
use of edu.stanford.CVC4.BitVectorType in project java-smt by sosy-lab.
the class CVC4BitvectorFormulaManager method modulo.
@Override
protected Expr modulo(Expr numerator, Expr denumerator, boolean signed) {
final Kind operator = signed ? Kind.BITVECTOR_SREM : Kind.BITVECTOR_UREM;
final Expr remainder = exprManager.mkExpr(operator, numerator, denumerator);
// CVC4 does not align with SMTLIB standard when it comes to modulo-by-zero.
// For modulo-by-zero, we compute the result as: "return the numerator".
final int bitsize = ((BitvectorType) formulaCreator.getFormulaType(numerator)).getSize();
final Expr zero = makeBitvectorImpl(bitsize, 0);
return exprManager.mkExpr(Kind.ITE, exprManager.mkExpr(Kind.EQUAL, denumerator, zero), numerator, remainder);
}
use of edu.stanford.CVC4.BitVectorType in project java-smt by sosy-lab.
the class CVC4FormulaCreator method getFormulaTypeFromTermType.
private FormulaType<?> getFormulaTypeFromTermType(Type t) {
if (t.isBoolean()) {
return FormulaType.BooleanType;
} else if (t.isInteger()) {
return FormulaType.IntegerType;
} else if (t.isBitVector()) {
// not hold, hence we use the new BitVectorType(t) here as a workaround:
return FormulaType.getBitvectorTypeWithSize((int) new BitVectorType(t).getSize());
} else if (t.isFloatingPoint()) {
edu.stanford.CVC4.FloatingPointType fpType = new edu.stanford.CVC4.FloatingPointType(t);
return FormulaType.getFloatingPointType((int) fpType.getExponentSize(), // without sign bit
(int) fpType.getSignificandSize() - 1);
} else if (t.isRoundingMode()) {
return FormulaType.FloatingPointRoundingModeType;
} else if (t.isReal()) {
// As such, the theory RATIONAL is contained in REAL. TODO: find a better solution.
return FormulaType.RationalType;
} else if (t.isArray()) {
// instead of casting, create a new type.
ArrayType arrayType = new ArrayType(t);
FormulaType<?> indexType = getFormulaTypeFromTermType(arrayType.getIndexType());
FormulaType<?> elementType = getFormulaTypeFromTermType(arrayType.getConstituentType());
return FormulaType.getArrayType(indexType, elementType);
} else if (t.isString()) {
return FormulaType.StringType;
} else if (t.isRegExp()) {
return FormulaType.RegexType;
} else {
throw new AssertionError(String.format("Unhandled type '%s' with base type '%s'.", t, t.getBaseType()));
}
}
use of edu.stanford.CVC4.BitVectorType in project java-smt by sosy-lab.
the class CVC4NativeAPITest method checkInvalidTypeOperationsCheckSat.
/**
* It does not matter if you take an int or array or bv here, all result in the same error.
*/
@Test
public void checkInvalidTypeOperationsCheckSat() {
BitVectorType bvType = exprMgr.mkBitVectorType(16);
Expr bvVar = exprMgr.mkVar("bla", bvType);
Expr assertion = exprMgr.mkExpr(Kind.AND, bvVar, bvVar);
Exception e = assertThrows(edu.stanford.CVC4.Exception.class, () -> smtEngine.checkSat(assertion));
assertThat(e.toString()).contains("expecting a Boolean subexpression");
}
use of edu.stanford.CVC4.BitVectorType in project java-smt by sosy-lab.
the class CVC4BitvectorFormulaManager method toIntegerFormulaImpl.
@Override
protected Expr toIntegerFormulaImpl(Expr pBv, boolean pSigned) {
Expr intExpr = exprManager.mkExpr(Kind.BITVECTOR_TO_NAT, pBv);
// CVC4 returns unsigned int by default
if (pSigned) {
// TODO check what is cheaper for the solver:
// checking the first BV-bit or computing max-int-value for the given size
final int size = Math.toIntExact(new BitVectorType(pBv.getType()).getSize());
final BigInteger modulo = BigInteger.ONE.shiftLeft(size);
final BigInteger maxInt = BigInteger.ONE.shiftLeft(size - 1).subtract(BigInteger.ONE);
final Expr moduloExpr = exprManager.mkConst(new Rational(modulo.toString()));
final Expr maxIntExpr = exprManager.mkConst(new Rational(maxInt.toString()));
intExpr = exprManager.mkExpr(Kind.ITE, exprManager.mkExpr(Kind.GT, intExpr, maxIntExpr), exprManager.mkExpr(Kind.MINUS, intExpr, moduloExpr), intExpr);
}
return intExpr;
}
Aggregations