Search in sources :

Example 1 with FloatingPoint

use of edu.stanford.CVC4.FloatingPoint in project java-smt by sosy-lab.

the class CVC4FloatingPointFormulaManager method makeNumberAndRound.

@Override
protected Expr makeNumberAndRound(String pN, FloatingPointType pType, Expr pRoundingMode) {
    try {
        if (isNegativeZero(Double.valueOf(pN))) {
            return negate(exprManager.mkConst(new FloatingPoint(getFPSize(pType), pRoundingMode.getConstRoundingMode(), Rational.fromDecimal(pN))));
        }
    } catch (NumberFormatException e) {
    // ignore and fallback to floating point from rational numbers
    }
    final Rational rat = toRational(pN);
    final BigInteger upperBound = getBiggestNumberBeforeInf(pType.getMantissaSize(), pType.getExponentSize());
    if (rat.greater(Rational.fromDecimal(upperBound.negate().toString())) && rat.less(Rational.fromDecimal(upperBound.toString()))) {
        return exprManager.mkConst(new FloatingPoint(getFPSize(pType), pRoundingMode.getConstRoundingMode(), rat));
    } else {
        // out of range
        if (rat.greater(Rational.fromDecimal("0"))) {
            return makePlusInfinityImpl(pType);
        } else {
            return makeMinusInfinityImpl(pType);
        }
    }
}
Also used : Rational(edu.stanford.CVC4.Rational) FloatingPoint(edu.stanford.CVC4.FloatingPoint) FloatingPointToFPFloatingPoint(edu.stanford.CVC4.FloatingPointToFPFloatingPoint) BigInteger(java.math.BigInteger)

Example 2 with FloatingPoint

use of edu.stanford.CVC4.FloatingPoint in project java-smt by sosy-lab.

the class CVC4NativeAPITest method checkSimpleFPUnsat.

@Test
public void checkSimpleFPUnsat() {
    // x * y = 1/4 AND x > 0 AND y < 0
    RoundingMode rm = RoundingMode.roundNearestTiesToAway;
    Expr rmExpr = exprMgr.mkConst(rm);
    Expr oneFourth = exprMgr.mkConst(new FloatingPoint(new FloatingPointSize(8, 24), rm, new Rational(1, 4)));
    Expr zero = exprMgr.mkConst(new FloatingPoint(new FloatingPointSize(8, 24), RoundingMode.roundNearestTiesToAway, new Rational(0)));
    Expr varX = exprMgr.mkVar("x", exprMgr.mkFloatingPointType(8, 24));
    Expr varY = exprMgr.mkVar("y", exprMgr.mkFloatingPointType(8, 24));
    Expr assertion1 = exprMgr.mkExpr(Kind.FLOATINGPOINT_EQ, exprMgr.mkExpr(Kind.FLOATINGPOINT_MULT, rmExpr, varX, varY), oneFourth);
    Expr assertion2 = exprMgr.mkExpr(Kind.FLOATINGPOINT_GT, varX, zero);
    Expr assertion3 = exprMgr.mkExpr(Kind.FLOATINGPOINT_LT, varY, zero);
    smtEngine.assertFormula(assertion1);
    smtEngine.assertFormula(assertion2);
    smtEngine.assertFormula(assertion3);
    Result satCheck = smtEngine.checkSat();
    assertThat(satCheck.isSat()).isEqualTo(Sat.UNSAT);
}
Also used : FloatingPointSize(edu.stanford.CVC4.FloatingPointSize) CVC4.vectorExpr(edu.stanford.CVC4.vectorExpr) SExpr(edu.stanford.CVC4.SExpr) Expr(edu.stanford.CVC4.Expr) Rational(edu.stanford.CVC4.Rational) RoundingMode(edu.stanford.CVC4.RoundingMode) FloatingPoint(edu.stanford.CVC4.FloatingPoint) Result(edu.stanford.CVC4.Result) Test(org.junit.Test)

Example 3 with FloatingPoint

use of edu.stanford.CVC4.FloatingPoint in project java-smt by sosy-lab.

the class CVC4NativeAPITest method checkSimpleFPSat.

/**
 * Do not ever try to use decimals in a new Rational that has only a 0 as decimal. This will
 * SIGSEV CVC4!
 */
@Test
public void checkSimpleFPSat() {
    // x * y = 1/4
    RoundingMode rm = RoundingMode.roundNearestTiesToAway;
    Expr rmExpr = exprMgr.mkConst(rm);
    Expr oneFourth = exprMgr.mkConst(new FloatingPoint(new FloatingPointSize(8, 24), rm, new Rational(1, 4)));
    Expr varX = exprMgr.mkVar("x", exprMgr.mkFloatingPointType(8, 24));
    Expr varY = exprMgr.mkVar("y", exprMgr.mkFloatingPointType(8, 24));
    Expr assertion1 = exprMgr.mkExpr(Kind.FLOATINGPOINT_EQ, exprMgr.mkExpr(Kind.FLOATINGPOINT_MULT, rmExpr, varX, varY), oneFourth);
    smtEngine.assertFormula(assertion1);
    Result satCheck = smtEngine.checkSat();
    assertThat(satCheck.isSat()).isEqualTo(Sat.SAT);
}
Also used : FloatingPointSize(edu.stanford.CVC4.FloatingPointSize) CVC4.vectorExpr(edu.stanford.CVC4.vectorExpr) SExpr(edu.stanford.CVC4.SExpr) Expr(edu.stanford.CVC4.Expr) Rational(edu.stanford.CVC4.Rational) RoundingMode(edu.stanford.CVC4.RoundingMode) FloatingPoint(edu.stanford.CVC4.FloatingPoint) Result(edu.stanford.CVC4.Result) Test(org.junit.Test)

Example 4 with FloatingPoint

use of edu.stanford.CVC4.FloatingPoint in project java-smt by sosy-lab.

the class CVC4FormulaCreator method parseFloatingPoint.

private Object parseFloatingPoint(Expr fpExpr) {
    Matcher matcher = FLOATING_POINT_PATTERN.matcher(fpExpr.toString());
    if (!matcher.matches()) {
        throw new NumberFormatException("Unknown floating-point format: " + fpExpr);
    }
    FloatingPoint fp = fpExpr.getConstFloatingPoint();
    FloatingPointSize fpType = fp.getT();
    long expWidth = fpType.exponentWidth();
    // without sign bit
    long mantWidth = fpType.significandWidth() - 1;
    assert matcher.group("sign").length() == 1;
    assert matcher.group("exp").length() == expWidth;
    assert matcher.group("mant").length() == mantWidth;
    String str = matcher.group("sign") + matcher.group("exp") + matcher.group("mant");
    if (expWidth == 11 && mantWidth == 52) {
        return Double.longBitsToDouble(UnsignedLong.valueOf(str, 2).longValue());
    } else if (expWidth == 8 && mantWidth == 23) {
        return Float.intBitsToFloat(UnsignedInteger.valueOf(str, 2).intValue());
    }
    // TODO to be fully correct, we would need to interpret this string
    return fpExpr.toString();
}
Also used : FloatingPointSize(edu.stanford.CVC4.FloatingPointSize) Matcher(java.util.regex.Matcher) FloatingPoint(edu.stanford.CVC4.FloatingPoint)

Example 5 with FloatingPoint

use of edu.stanford.CVC4.FloatingPoint in project java-smt by sosy-lab.

the class CVC4SolverContext method create.

public static SolverContext create(LogManager pLogger, ShutdownNotifier pShutdownNotifier, int randomSeed, NonLinearArithmetic pNonLinearArithmetic, FloatingPointRoundingMode pFloatingPointRoundingMode, Consumer<String> pLoader) {
    pLoader.accept("cvc4jni");
    // ExprManager is the central class for creating expressions/terms/formulae.
    ExprManager exprManager = new ExprManager();
    CVC4FormulaCreator creator = new CVC4FormulaCreator(exprManager);
    // set common options.
    // temporary SmtEngine instance, until ExprManager.getOptions() works without SegFault.
    SmtEngine smtEngine = new SmtEngine(exprManager);
    smtEngine.setOption("output-language", new SExpr("smt2"));
    smtEngine.setOption("random-seed", new SExpr(randomSeed));
    // Set Strings option to enable all String features (such as lessOrEquals)
    smtEngine.setOption("strings-exp", new SExpr(true));
    // smtEngine.delete();
    // Create managers
    CVC4UFManager functionTheory = new CVC4UFManager(creator);
    CVC4BooleanFormulaManager booleanTheory = new CVC4BooleanFormulaManager(creator);
    CVC4IntegerFormulaManager integerTheory = new CVC4IntegerFormulaManager(creator, pNonLinearArithmetic);
    CVC4RationalFormulaManager rationalTheory = new CVC4RationalFormulaManager(creator, pNonLinearArithmetic);
    CVC4BitvectorFormulaManager bitvectorTheory = new CVC4BitvectorFormulaManager(creator, booleanTheory);
    CVC4FloatingPointFormulaManager fpTheory;
    if (Configuration.isBuiltWithSymFPU()) {
        fpTheory = new CVC4FloatingPointFormulaManager(creator, pFloatingPointRoundingMode);
    } else {
        fpTheory = null;
        pLogger.log(Level.INFO, "CVC4 was built without support for FloatingPoint theory");
    // throw new AssertionError("CVC4 was built without support for FloatingPoint theory");
    }
    CVC4QuantifiedFormulaManager qfTheory = new CVC4QuantifiedFormulaManager(creator);
    CVC4ArrayFormulaManager arrayTheory = new CVC4ArrayFormulaManager(creator);
    CVC4SLFormulaManager slTheory = new CVC4SLFormulaManager(creator);
    CVC4StringFormulaManager strTheory = new CVC4StringFormulaManager(creator);
    CVC4FormulaManager manager = new CVC4FormulaManager(creator, functionTheory, booleanTheory, integerTheory, rationalTheory, bitvectorTheory, fpTheory, qfTheory, arrayTheory, slTheory, strTheory);
    return new CVC4SolverContext(creator, manager, pShutdownNotifier, randomSeed);
}
Also used : SmtEngine(edu.stanford.CVC4.SmtEngine) ExprManager(edu.stanford.CVC4.ExprManager) SExpr(edu.stanford.CVC4.SExpr)

Aggregations

FloatingPoint (edu.stanford.CVC4.FloatingPoint)4 FloatingPointSize (edu.stanford.CVC4.FloatingPointSize)3 Rational (edu.stanford.CVC4.Rational)3 SExpr (edu.stanford.CVC4.SExpr)3 Expr (edu.stanford.CVC4.Expr)2 Result (edu.stanford.CVC4.Result)2 RoundingMode (edu.stanford.CVC4.RoundingMode)2 CVC4.vectorExpr (edu.stanford.CVC4.vectorExpr)2 Test (org.junit.Test)2 ExprManager (edu.stanford.CVC4.ExprManager)1 FloatingPointToFPFloatingPoint (edu.stanford.CVC4.FloatingPointToFPFloatingPoint)1 SmtEngine (edu.stanford.CVC4.SmtEngine)1 BigInteger (java.math.BigInteger)1 Matcher (java.util.regex.Matcher)1