GP-1379/3022 FloatFormat and BigFloat refactor in support of string

parsing and 80-bit float format.  Change float datatype naming to use
number of bits instead of bytes.
This commit is contained in:
ghidra1 2023-03-03 11:47:37 -05:00
parent ff5ef27231
commit b9202411b9
116 changed files with 2122 additions and 1757 deletions

View file

@ -20,6 +20,9 @@ import static org.junit.Assert.*;
import org.junit.Test;
import generic.test.AbstractGTest;
import ghidra.pcode.floatformat.*;
import ghidra.program.model.data.floats.Float32DataType;
import ghidra.program.model.data.floats.Float64DataType;
import ghidra.program.model.mem.ByteMemBufferImpl;
import ghidra.util.LittleEndianDataConverter;
@ -34,106 +37,110 @@ public class FloatDataTypeTest extends AbstractGTest {
@Test
public void testFloat4Extremes() {
FloatFormat ff = FloatFormatFactory.getFloatFormat(4);
int bits = Float.floatToRawIntBits(Float.NaN);
byte[] bytes = getBytes(bits, 4);
Object value =
Float4DataType.dataType.getValue(new ByteMemBufferImpl(null, bytes, false), null, 10);
assertEquals(Float.NaN, value);
Float32DataType.dataType.getValue(new ByteMemBufferImpl(null, bytes, false), null, 10);
assertEquals(ff.getBigNaN(false), value);
bits = Float.floatToRawIntBits(Float.POSITIVE_INFINITY);
bytes = getBytes(bits, 4);
value =
Float4DataType.dataType.getValue(new ByteMemBufferImpl(null, bytes, false), null, 10);
assertEquals(Float.POSITIVE_INFINITY, value);
Float32DataType.dataType.getValue(new ByteMemBufferImpl(null, bytes, false), null, 10);
assertEquals(ff.getBigInfinity(false), value);
bits = Float.floatToRawIntBits(Float.NEGATIVE_INFINITY);
bytes = getBytes(bits, 4);
value =
Float4DataType.dataType.getValue(new ByteMemBufferImpl(null, bytes, false), null, 10);
assertEquals(Float.NEGATIVE_INFINITY, value);
Float32DataType.dataType.getValue(new ByteMemBufferImpl(null, bytes, false), null, 10);
assertEquals(ff.getBigInfinity(true), value);
bits = Float.floatToRawIntBits(0F);
bytes = getBytes(bits, 4);
value =
Float4DataType.dataType.getValue(new ByteMemBufferImpl(null, bytes, false), null, 10);
assertEquals(0F, value);
Float32DataType.dataType.getValue(new ByteMemBufferImpl(null, bytes, false), null, 10);
assertEquals("0.0", ff.toDecimalString((BigFloat) value));
bits = Float.floatToRawIntBits(1F);
bytes = getBytes(bits, 4);
value =
Float4DataType.dataType.getValue(new ByteMemBufferImpl(null, bytes, false), null, 10);
assertEquals(1F, value);
Float32DataType.dataType.getValue(new ByteMemBufferImpl(null, bytes, false), null, 10);
assertEquals("1.0", ff.toDecimalString((BigFloat) value));
bits = Float.floatToRawIntBits(-1F);
bytes = getBytes(bits, 4);
value =
Float4DataType.dataType.getValue(new ByteMemBufferImpl(null, bytes, false), null, 10);
assertEquals(-1F, value);
Float32DataType.dataType.getValue(new ByteMemBufferImpl(null, bytes, false), null, 10);
assertEquals("-1.0", ff.toDecimalString((BigFloat) value));
bits = Float.floatToRawIntBits(555.555F);
bytes = getBytes(bits, 4);
value =
Float4DataType.dataType.getValue(new ByteMemBufferImpl(null, bytes, false), null, 10);
assertEquals(555.555F, value);
Float32DataType.dataType.getValue(new ByteMemBufferImpl(null, bytes, false), null, 10);
assertEquals("555.55499", ff.toDecimalString((BigFloat) value, true));
bits = Float.floatToRawIntBits(-555.555F);
bytes = getBytes(bits, 4);
value =
Float4DataType.dataType.getValue(new ByteMemBufferImpl(null, bytes, false), null, 10);
assertEquals(-555.555F, value);
Float32DataType.dataType.getValue(new ByteMemBufferImpl(null, bytes, false), null, 10);
assertEquals("-555.55499", ff.toDecimalString((BigFloat) value, true));
}
@Test
public void testFloat8Extremes() {
FloatFormat ff = FloatFormatFactory.getFloatFormat(8);
long bits = Double.doubleToRawLongBits(Double.NaN);
byte[] bytes = getBytes(bits, 8);
Object value =
Float8DataType.dataType.getValue(new ByteMemBufferImpl(null, bytes, false), null, 10);
assertEquals(Double.NaN, value);
Float64DataType.dataType.getValue(new ByteMemBufferImpl(null, bytes, false), null, 10);
assertEquals(ff.getBigNaN(false), value);
bits = Double.doubleToRawLongBits(Double.POSITIVE_INFINITY);
bytes = getBytes(bits, 8);
value =
Float8DataType.dataType.getValue(new ByteMemBufferImpl(null, bytes, false), null, 10);
assertEquals(Double.POSITIVE_INFINITY, value);
Float64DataType.dataType.getValue(new ByteMemBufferImpl(null, bytes, false), null, 10);
assertEquals(ff.getBigInfinity(false), value);
bits = Double.doubleToRawLongBits(Double.NEGATIVE_INFINITY);
bytes = getBytes(bits, 8);
value =
Float8DataType.dataType.getValue(new ByteMemBufferImpl(null, bytes, false), null, 10);
assertEquals(Double.NEGATIVE_INFINITY, value);
Float64DataType.dataType.getValue(new ByteMemBufferImpl(null, bytes, false), null, 10);
assertEquals(ff.getBigInfinity(true), value);
bits = Double.doubleToRawLongBits(0D);
bytes = getBytes(bits, 8);
value =
Float8DataType.dataType.getValue(new ByteMemBufferImpl(null, bytes, false), null, 10);
assertEquals(0D, value);
Float64DataType.dataType.getValue(new ByteMemBufferImpl(null, bytes, false), null, 10);
assertEquals("0.0", ff.toDecimalString((BigFloat) value));
bits = Double.doubleToRawLongBits(1D);
bytes = getBytes(bits, 8);
value =
Float8DataType.dataType.getValue(new ByteMemBufferImpl(null, bytes, false), null, 10);
assertEquals(1D, value);
Float64DataType.dataType.getValue(new ByteMemBufferImpl(null, bytes, false), null, 10);
assertEquals("1.0", ff.toDecimalString((BigFloat) value));
bits = Double.doubleToRawLongBits(-1D);
bytes = getBytes(bits, 8);
value =
Float8DataType.dataType.getValue(new ByteMemBufferImpl(null, bytes, false), null, 10);
assertEquals(-1D, value);
Float64DataType.dataType.getValue(new ByteMemBufferImpl(null, bytes, false), null, 10);
assertEquals("-1.0", ff.toDecimalString((BigFloat) value));
bits = Double.doubleToRawLongBits(555.555D);
bytes = getBytes(bits, 8);
value =
Float8DataType.dataType.getValue(new ByteMemBufferImpl(null, bytes, false), null, 10);
assertEquals(555.555D, value);
Float64DataType.dataType.getValue(new ByteMemBufferImpl(null, bytes, false), null, 10);
assertEquals("555.5549999999999", ff.toDecimalString((BigFloat) value, true));
bits = Double.doubleToRawLongBits(-555.555D);
bytes = getBytes(bits, 8);
value =
Float8DataType.dataType.getValue(new ByteMemBufferImpl(null, bytes, false), null, 10);
assertEquals(-555.555D, value);
Float64DataType.dataType.getValue(new ByteMemBufferImpl(null, bytes, false), null, 10);
assertEquals("-555.5549999999999", ff.toDecimalString((BigFloat) value, true));
}