-
Notifications
You must be signed in to change notification settings - Fork 54
Fix Floating-Point Mantissa Not Including Sign Bit #513
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 13 commits
f99f39c
737718c
d50ad4c
a4240e6
c452a8f
a6f92b7
ca4a3f7
7bed3f5
73c1451
1ca40f9
6d80321
44310e0
475ce68
bb3760d
31b0b38
4b07b9b
e444b52
f1ff09f
eddaf77
1dae5d1
7755fe6
83cd136
96a2565
885bb68
59713fe
05b6c40
a08a811
a2a7810
f37786e
0bd0aa7
7331cd1
031a900
3d17811
55089a0
d836779
0885fd0
23cdc67
d67273c
b7b8aff
225ba7a
9df8b66
7207b71
5086ae7
6cb15ea
f760eb1
f7c5916
fbea69f
e043988
d9d2b3e
1aba81d
dd83ed4
c83f96f
e8f44d8
00dc033
20a8f48
6769850
015c145
9aff78a
ddac078
52d6e30
5dd10ca
e08a75a
bd8c6bd
55901fc
b835125
056ca12
234452d
80c480c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -205,14 +205,61 @@ public String toSMTLIBString() { | |
| } | ||
| } | ||
|
|
||
| public static FloatingPointType getFloatingPointType(int exponentSize, int mantissaSize) { | ||
| return new FloatingPointType(exponentSize, mantissaSize); | ||
| /** | ||
| * Constructs a new IEEE-754 {@link FloatingPointType} with the given exponent and mantissa sizes. | ||
| * The mantissa size is expected to not include the sign bit. | ||
| * | ||
| * @param exponentSize size of the exponent for the base of the floating-point | ||
| * @param mantissaSizeWithoutSignBit size of the mantissa (also called a coefficient or | ||
| * significant), excluding the sign bit. | ||
| * @return the newly constructed {@link FloatingPointType}. | ||
| */ | ||
| // TODO: mark as soon to be deprecated | ||
| public static FloatingPointType getFloatingPointType( | ||
| int exponentSize, int mantissaSizeWithoutSignBit) { | ||
| return new FloatingPointType(exponentSize, mantissaSizeWithoutSignBit); | ||
PhilippWendler marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /** | ||
| * Constructs a new IEEE-754 {@link FloatingPointType} with the given exponent and mantissa sizes. | ||
| * The mantissa size is expected to not include the sign bit. | ||
| * | ||
| * @param exponentSize size of the exponent for the base of the floating-point | ||
| * @param mantissaSizeWithoutSignBit size of the mantissa (also called a coefficient or | ||
| * significant), excluding the sign bit. | ||
| * @return the newly constructed {@link FloatingPointType}. | ||
| */ | ||
| public static FloatingPointType getFloatingPointTypeWithoutSignBit( | ||
| int exponentSize, int mantissaSizeWithoutSignBit) { | ||
| return new FloatingPointType(exponentSize, mantissaSizeWithoutSignBit); | ||
| } | ||
|
|
||
| /** | ||
| * Constructs a new IEEE-754 {@link FloatingPointType} with the given exponent and mantissa sizes. | ||
| * The mantissa size is expected to not include the sign bit. | ||
| * | ||
| * @param exponentSize size of the exponent for the base of the floating-point | ||
| * @param mantissaSizeWithSignBit size of the mantissa (also called a coefficient or significant), | ||
| * including the sign bit. | ||
| * @return the newly constructed {@link FloatingPointType}. | ||
| */ | ||
| public static FloatingPointType getFloatingPointTypeWithSignBit( | ||
PhilippWendler marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| int exponentSize, int mantissaSizeWithSignBit) { | ||
| return new FloatingPointType(exponentSize, mantissaSizeWithSignBit); | ||
PhilippWendler marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /** | ||
| * @return single precision {@link FloatingPointType} with exponent sized 8, and mantissa sized 24 | ||
| * (including the sign bit). | ||
| */ | ||
| public static FloatingPointType getSinglePrecisionFloatingPointType() { | ||
| return FloatingPointType.SINGLE_PRECISION_FP_TYPE; | ||
| } | ||
|
|
||
| /** | ||
| * @return double precision {@link FloatingPointType} with exponent sized 11, and mantissa sized | ||
| * 53 (including the sign bit). | ||
| */ | ||
| public static FloatingPointType getDoublePrecisionFloatingPointType() { | ||
| return FloatingPointType.DOUBLE_PRECISION_FP_TYPE; | ||
| } | ||
|
|
@@ -226,6 +273,8 @@ public static final class FloatingPointType extends FormulaType<FloatingPointFor | |
| new FloatingPointType(DOUBLE_PRECISION_EXPONENT_SIZE, DOUBLE_PRECISION_MANTISSA_SIZE); | ||
|
|
||
| private final int exponentSize; | ||
| // The SMTLib2 standard defines the mantissa size as including the sign bit. We do not include | ||
| // it here though. | ||
| private final int mantissaSize; | ||
|
|
||
| private FloatingPointType(int pExponentSize, int pMantissaSize) { | ||
PhilippWendler marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
@@ -238,15 +287,40 @@ public boolean isFloatingPointType() { | |
| return true; | ||
| } | ||
|
|
||
| /** Returns the size of the exponent */ | ||
| public int getExponentSize() { | ||
| return exponentSize; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the size of the mantissa (also called a coefficient or significant), excluding the | ||
| * sign bit. | ||
| */ | ||
| // TODO: mark as soon to be deprecated | ||
| public int getMantissaSize() { | ||
| return mantissaSize; | ||
| } | ||
|
|
||
| /** Return the total size of a value of this type in bits. */ | ||
| /** | ||
| * Returns the size of the mantissa (also called a coefficient or significant), excluding the | ||
| * sign bit. | ||
| */ | ||
| public int getMantissaSizeWithoutSignBit() { | ||
| return mantissaSize; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the size of the mantissa (also called a coefficient or significant), including the | ||
| * sign bit. | ||
| */ | ||
| public int getMantissaSizeWithSignBit() { | ||
| return mantissaSize + 1; | ||
| } | ||
|
|
||
| /** | ||
| * Return the total size of a value of this type in bits. Equal to exponent + mantissa | ||
| * (including the sign bit). | ||
| */ | ||
| public int getTotalSize() { | ||
| return exponentSize + mantissaSize + 1; | ||
| } | ||
|
|
@@ -270,12 +344,12 @@ public boolean equals(Object obj) { | |
|
|
||
| @Override | ||
| public String toString() { | ||
| return "FloatingPoint<exp=" + exponentSize + ",mant=" + mantissaSize + ">"; | ||
| return "FloatingPoint<exp=" + exponentSize + ",mant=" + getMantissaSizeWithSignBit() + ">"; | ||
| } | ||
|
|
||
| @Override | ||
| public String toSMTLIBString() { | ||
| return "(_ FloatingPoint " + exponentSize + " " + mantissaSize + ")"; | ||
| return "(_ FloatingPoint " + exponentSize + " " + getMantissaSizeWithSignBit() + ")"; | ||
|
||
| } | ||
| } | ||
|
|
||
|
|
@@ -478,7 +552,7 @@ public static FormulaType<?> fromString(String t) { | |
| } else if (t.startsWith("FloatingPoint<")) { | ||
| // FloatingPoint<exp=11,mant=52> | ||
| List<String> exman = Splitter.on(',').limit(2).splitToList(t.substring(14, t.length() - 1)); | ||
| return FormulaType.getFloatingPointType( | ||
| return FormulaType.getFloatingPointTypeWithoutSignBit( | ||
PhilippWendler marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Integer.parseInt(exman.get(0).substring(4)), Integer.parseInt(exman.get(1).substring(5))); | ||
| } else if (t.startsWith("Bitvector<")) { | ||
| // Bitvector<32> | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think that documenting this here is enough?
I have already recommend to use
FloatingPointTypeinstead of the two ints when this was added, which unfortunately was rejected. I want to renew this recommendation.Note that even the current state of this PR calls
FloatingPointNumber.of()with the wrong values, at least inZ3FormulaCreator, because it is forced to extract the two numbers from aFloatingPointTypeinstance that it already has. If there would have been a way to create aFloatingPointNumberinstance based on aFloatingPointType, the code there would have simply used that and would now not have needed to be touched nor would the bug have been introduced.Same for the other method below.