Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-0 BigFloat and AbstractComplexDataType mods

ghizard2af59acd83b0
diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/pcode/floatformat/BigFloat.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/pcode/floatformat/BigFloat.java
index 63967c0761..259f6a666d 100644
--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/pcode/floatformat/BigFloat.java
+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/pcode/floatformat/BigFloat.java
@@ -4,9 +4,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -356,6 +356,50 @@ public class BigFloat implements Comparable<BigFloat> {
}
}
+ /**
+ * Converts this BigFloat to a primitive Java double following IEEE 754 downcasting rules.
+ *
+ * @return the double value
+ */
+ public double doubleValue() {
+ switch (kind) {
+ case FINITE:
+ if (isZero()) {
+ return sign < 0 ? -0.0 : 0.0;
+ }
+ return toBigDecimal().doubleValue();
+ case INFINITE:
+ return sign < 0 ? Double.NEGATIVE_INFINITY : Double.POSITIVE_INFINITY;
+ case QUIET_NAN:
+ case SIGNALING_NAN:
+ return Double.NaN;
+ default:
+ throw new AssertionError("unknown BigFloat kind: " + kind);
+ }
+ }
+
+ /**
+ * Converts this BigFloat to a primitive Java float following IEEE 754 downcasting rules.
+ *
+ * @return the float value
+ */
+ public float floatValue() {
+ switch (kind) {
+ case FINITE:
+ if (isZero()) {
+ return sign < 0 ? -0.0f : 0.0f;
+ }
+ return toBigDecimal().floatValue();
+ case INFINITE:
+ return sign < 0 ? Float.NEGATIVE_INFINITY : Float.POSITIVE_INFINITY;
+ case QUIET_NAN:
+ case SIGNALING_NAN:
+ return Float.NaN;
+ default:
+ throw new AssertionError("unknown BigFloat kind: " + kind);
+ }
+ }
+
public String toBinaryString() {
switch (kind) {
case QUIET_NAN:
diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/data/AbstractComplexDataType.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/data/AbstractComplexDataType.java
index 140bf38f52..fc0d43a0c4 100644
--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/data/AbstractComplexDataType.java
+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/data/AbstractComplexDataType.java
@@ -20,7 +20,6 @@ import java.math.BigDecimal;
import generic.complex.Complex;
import ghidra.docking.settings.Settings;
import ghidra.pcode.floatformat.BigFloat;
-import ghidra.pcode.floatformat.FloatFormatFactory;
import ghidra.program.model.mem.MemBuffer;
import ghidra.program.model.mem.WrappedMemBuffer;
@@ -100,22 +99,20 @@ public abstract class AbstractComplexDataType extends BuiltIn {
}
private static double toDouble(Object obj) {
- if (obj instanceof BigFloat) {
- return Double.longBitsToDouble(
- FloatFormatFactory.getFloatFormat(8).getEncoding((BigFloat) obj).longValue());
+ if (obj instanceof Double d) {
+ return d;
}
- if (obj instanceof Double) {
- return (Double) obj;
+ if (obj instanceof Float f) {
+ return f;
}
- if (obj instanceof Float) {
- return (Float) obj;
- }
- if (obj instanceof Short) {
+ if (obj instanceof Short s) {
// Looking at AbstractFloatDataType#getValue, this makes no sense to me
- return (Short) obj;
+ return s;
+ }
+ if (obj instanceof BigFloat bf) {
+ return bf.doubleValue();
}
- if (obj instanceof BigDecimal) {
- BigDecimal bd = (BigDecimal) obj;
+ if (obj instanceof BigDecimal bd) {
return bd.doubleValue();
}
throw new IllegalArgumentException(obj + "(" + obj.getClass() + ")");
diff --git a/Ghidra/Framework/SoftwareModeling/src/test/java/ghidra/pcode/floatformat/BigFloatTest.java b/Ghidra/Framework/SoftwareModeling/src/test/java/ghidra/pcode/floatformat/BigFloatTest.java
index ec1c75c324..cf0f36e2d0 100644
--- a/Ghidra/Framework/SoftwareModeling/src/test/java/ghidra/pcode/floatformat/BigFloatTest.java
+++ b/Ghidra/Framework/SoftwareModeling/src/test/java/ghidra/pcode/floatformat/BigFloatTest.java
@@ -4,9 +4,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -379,6 +379,64 @@ public class BigFloatTest extends AbstractGenericTest {
assertEquals(new BigDecimal(-2.5d), FloatFormat.toBigFloat(-2.5d).toBigDecimal());
}
+ @Test
+ public void testToDouble() {
+ // Test FINITE Kind, including signed zeros
+ Assert.assertEquals(0.0, FloatFormat.toBigFloat(0.0d).doubleValue(), 0.0);
+ Assert.assertEquals(-0.0, FloatFormat.toBigFloat(-0.0d).doubleValue(), 0.0);
+ // Check that the bit patterns of signed zeros match
+ Assert.assertEquals(Double.doubleToRawLongBits(0.0),
+ Double.doubleToRawLongBits(FloatFormat.toBigFloat(0.0d).doubleValue()));
+ Assert.assertEquals(Double.doubleToRawLongBits(-0.0),
+ Double.doubleToRawLongBits(FloatFormat.toBigFloat(-0.0d).doubleValue()));
+
+ // Test FINITE Standard Values
+ Assert.assertEquals(1.0, FloatFormat.toBigFloat(1.0d).doubleValue(), 0.0);
+ Assert.assertEquals(-2.5, FloatFormat.toBigFloat(-2.5d).doubleValue(), 0.0);
+ Assert.assertEquals(Double.MAX_VALUE,
+ FloatFormat.toBigFloat(Double.MAX_VALUE).doubleValue(), 0.0);
+ Assert.assertEquals(Double.MIN_VALUE,
+ FloatFormat.toBigFloat(Double.MIN_VALUE).doubleValue(), 0.0);
+
+ // Test INFINITE Kind
+ Assert.assertEquals(Double.POSITIVE_INFINITY,
+ FloatFormat.toBigFloat(Double.POSITIVE_INFINITY).doubleValue(), 0.0);
+ Assert.assertEquals(Double.NEGATIVE_INFINITY,
+ FloatFormat.toBigFloat(Double.NEGATIVE_INFINITY).doubleValue(), 0.0);
+
+ // Test NAN Kinds (QUIET and SIGNALING map to primitive NaN)
+ Assert.assertTrue(Double.isNaN(FloatFormat.toBigFloat(Double.NaN).doubleValue()));
+ }
+
+ @Test
+ public void testToFloat() {
+ // Test FINITE Kind, including signed zeros
+ Assert.assertEquals(0.0f, FloatFormat.toBigFloat(0.0f).floatValue(), 0.0f);
+ Assert.assertEquals(-0.0f, FloatFormat.toBigFloat(-0.0f).floatValue(), 0.0f);
+ // Check that the bit patterns of signed zeros match
+ Assert.assertEquals(Float.floatToRawIntBits(0.0f),
+ Float.floatToRawIntBits(FloatFormat.toBigFloat(0.0f).floatValue()));
+ Assert.assertEquals(Float.floatToRawIntBits(-0.0f),
+ Float.floatToRawIntBits(FloatFormat.toBigFloat(-0.0f).floatValue()));
+
+ // Test FINITE Standard Values
+ Assert.assertEquals(1.0f, FloatFormat.toBigFloat(1.0f).floatValue(), 0.0f);
+ Assert.assertEquals(-2.5f, FloatFormat.toBigFloat(-2.5f).floatValue(), 0.0f);
+ Assert.assertEquals(Float.MAX_VALUE, FloatFormat.toBigFloat(Float.MAX_VALUE).floatValue(),
+ 0.0f);
+ Assert.assertEquals(Float.MIN_VALUE, FloatFormat.toBigFloat(Float.MIN_VALUE).floatValue(),
+ 0.0f);
+
+ // Test INFINITE Kind
+ Assert.assertEquals(Float.POSITIVE_INFINITY,
+ FloatFormat.toBigFloat(Float.POSITIVE_INFINITY).floatValue(), 0.0f);
+ Assert.assertEquals(Float.NEGATIVE_INFINITY,
+ FloatFormat.toBigFloat(Float.NEGATIVE_INFINITY).floatValue(), 0.0f);
+
+ // Test NAN Kinds (QUIET and SIGNALING map to primitive NaN)
+ Assert.assertTrue(Float.isNaN(FloatFormat.toBigFloat(Float.NaN).floatValue()));
+ }
+
@Test
public void testNormalAndDenormal() {