Skip to content

Commit 49d5c8e

Browse files
authored
Merge pull request #514 from sosy-lab/use_solver_native_bv_to_ieee-fp_CVC4_CVC5
Use Solver Native Implementation of Bitvector to IEEE FP for CVC4 and CVC5
2 parents c6d2ca7 + 7b78a86 commit 49d5c8e

File tree

4 files changed

+48
-36
lines changed

4 files changed

+48
-36
lines changed

src/org/sosy_lab/java_smt/basicimpl/AbstractFloatingPointFormulaManager.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.math.BigInteger;
1616
import java.util.HashMap;
1717
import java.util.Map;
18+
import org.sosy_lab.common.MoreStrings;
1819
import org.sosy_lab.common.rationals.Rational;
1920
import org.sosy_lab.java_smt.api.BitvectorFormula;
2021
import org.sosy_lab.java_smt.api.BooleanFormula;
@@ -25,6 +26,7 @@
2526
import org.sosy_lab.java_smt.api.FloatingPointRoundingModeFormula;
2627
import org.sosy_lab.java_smt.api.Formula;
2728
import org.sosy_lab.java_smt.api.FormulaType;
29+
import org.sosy_lab.java_smt.api.FormulaType.BitvectorType;
2830
import org.sosy_lab.java_smt.api.FormulaType.FloatingPointType;
2931

3032
/**
@@ -258,6 +260,14 @@ protected abstract TFormulaInfo castFromImpl(
258260
@Override
259261
public FloatingPointFormula fromIeeeBitvector(
260262
BitvectorFormula pNumber, FloatingPointType pTargetType) {
263+
BitvectorType bvType = (BitvectorType) formulaCreator.getFormulaType(pNumber);
264+
Preconditions.checkArgument(
265+
bvType.getSize() == pTargetType.getTotalSize(),
266+
MoreStrings.lazyString(
267+
() ->
268+
String.format(
269+
"The total size %s of type %s has to match the size %s of type %s.",
270+
pTargetType.getTotalSize(), pTargetType, bvType.getSize(), bvType)));
261271
return wrap(fromIeeeBitvectorImpl(extractInfo(pNumber), pTargetType));
262272
}
263273

src/org/sosy_lab/java_smt/solvers/cvc4/CVC4FloatingPointFormulaManager.java

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010

1111
import com.google.common.collect.ImmutableList;
1212
import edu.stanford.CVC4.BitVector;
13-
import edu.stanford.CVC4.BitVectorExtract;
1413
import edu.stanford.CVC4.Expr;
1514
import edu.stanford.CVC4.ExprManager;
1615
import edu.stanford.CVC4.FloatingPoint;
1716
import edu.stanford.CVC4.FloatingPointConvertSort;
1817
import edu.stanford.CVC4.FloatingPointSize;
1918
import edu.stanford.CVC4.FloatingPointToFPFloatingPoint;
19+
import edu.stanford.CVC4.FloatingPointToFPIEEEBitVector;
2020
import edu.stanford.CVC4.FloatingPointToFPSignedBitVector;
2121
import edu.stanford.CVC4.FloatingPointToFPUnsignedBitVector;
2222
import edu.stanford.CVC4.FloatingPointToSBV;
@@ -358,20 +358,10 @@ protected Expr isNegative(Expr pParam) {
358358

359359
@Override
360360
protected Expr fromIeeeBitvectorImpl(Expr bitvector, FloatingPointType pTargetType) {
361-
int mantissaSize = pTargetType.getMantissaSize();
362-
int exponentSize = pTargetType.getExponentSize();
363-
int size = pTargetType.getTotalSize();
364-
assert size == mantissaSize + exponentSize + 1;
365-
366-
Expr signExtract = exprManager.mkConst(new BitVectorExtract(size - 1, size - 1));
367-
Expr exponentExtract = exprManager.mkConst(new BitVectorExtract(size - 2, mantissaSize));
368-
Expr mantissaExtract = exprManager.mkConst(new BitVectorExtract(mantissaSize - 1, 0));
369-
370-
Expr sign = exprManager.mkExpr(Kind.BITVECTOR_EXTRACT, signExtract, bitvector);
371-
Expr exponent = exprManager.mkExpr(Kind.BITVECTOR_EXTRACT, exponentExtract, bitvector);
372-
Expr mantissa = exprManager.mkExpr(Kind.BITVECTOR_EXTRACT, mantissaExtract, bitvector);
373-
374-
return exprManager.mkExpr(Kind.FLOATINGPOINT_FP, sign, exponent, mantissa);
361+
// This is just named weird, but the CVC4 doc say this is IEEE BV -> FP
362+
FloatingPointConvertSort fpConvertSort = new FloatingPointConvertSort(getFPSize(pTargetType));
363+
Expr op = exprManager.mkConst(new FloatingPointToFPIEEEBitVector(fpConvertSort));
364+
return exprManager.mkExpr(op, bitvector);
375365
}
376366

377367
@Override

src/org/sosy_lab/java_smt/solvers/cvc5/CVC5FloatingPointFormulaManager.java

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -408,29 +408,20 @@ protected Term isNegative(Term pParam) {
408408
}
409409

410410
@Override
411-
protected Term fromIeeeBitvectorImpl(Term bitvector, FloatingPointType pTargetType) {
412-
int mantissaSize = pTargetType.getMantissaSize();
413-
int exponentSize = pTargetType.getExponentSize();
414-
int size = pTargetType.getTotalSize();
415-
assert size == mantissaSize + exponentSize + 1;
416-
417-
Op signExtract;
418-
Op exponentExtract;
419-
Op mantissaExtract;
411+
protected Term fromIeeeBitvectorImpl(Term pBitvector, FloatingPointType pTargetType) {
420412
try {
421-
signExtract = termManager.mkOp(Kind.BITVECTOR_EXTRACT, size - 1, size - 1);
422-
exponentExtract = termManager.mkOp(Kind.BITVECTOR_EXTRACT, size - 2, mantissaSize);
423-
mantissaExtract = termManager.mkOp(Kind.BITVECTOR_EXTRACT, mantissaSize - 1, 0);
424-
} catch (CVC5ApiException e) {
425-
throw new IllegalArgumentException(
426-
"You tried creating a invalid bitvector extract in term " + bitvector + ".", e);
413+
return termManager.mkTerm(
414+
termManager.mkOp(
415+
Kind.FLOATINGPOINT_TO_FP_FROM_IEEE_BV,
416+
pTargetType.getExponentSize(),
417+
pTargetType.getMantissaSize() + 1), // add sign bit
418+
pBitvector);
419+
} catch (CVC5ApiException cvc5ApiException) {
420+
// This seems to only be thrown for wrong exponent and mantissa sizes (e.g. negative
421+
// numbers, size not equal to BV size etc.). We check for this beforehand, so this should
422+
// not be thrown.
423+
throw new IllegalArgumentException(cvc5ApiException);
427424
}
428-
429-
Term sign = termManager.mkTerm(signExtract, bitvector);
430-
Term exponent = termManager.mkTerm(exponentExtract, bitvector);
431-
Term mantissa = termManager.mkTerm(mantissaExtract, bitvector);
432-
433-
return termManager.mkTerm(Kind.FLOATINGPOINT_FP, sign, exponent, mantissa);
434425
}
435426

436427
@Override

src/org/sosy_lab/java_smt/test/FloatingPointFormulaManagerTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,6 +1098,27 @@ public void checkString2FpConversion64() throws SolverException, InterruptedExce
10981098
fpmgr.makeNumber(Double.toString(pDouble), doublePrecType)));
10991099
}
11001100

1101+
@Test
1102+
public void checkErrorOnInvalidSize_IeeeBv2FpConversion() {
1103+
BitvectorFormula bv = bvmgr.makeBitvector(9, 123);
1104+
1105+
var exSingle =
1106+
assertThrows(
1107+
IllegalArgumentException.class, () -> fpmgr.fromIeeeBitvector(bv, singlePrecType));
1108+
assertThat(exSingle.getMessage())
1109+
.contains(
1110+
"The total size 32 of type FloatingPoint<exp=8,mant=23> "
1111+
+ "has to match the size 9 of type Bitvector<9>.");
1112+
1113+
var exDouble =
1114+
assertThrows(
1115+
IllegalArgumentException.class, () -> fpmgr.fromIeeeBitvector(bv, doublePrecType));
1116+
assertThat(exDouble.getMessage())
1117+
.contains(
1118+
"The total size 64 of type FloatingPoint<exp=11,mant=52> "
1119+
+ "has to match the size 9 of type Bitvector<9>.");
1120+
}
1121+
11011122
@Test
11021123
public void checkIeeeBv2FpConversion32() throws SolverException, InterruptedException {
11031124
proveForAll(

0 commit comments

Comments
 (0)