Merge remote-tracking branch 'origin/patch'

This commit is contained in:
ghidra1 2021-01-26 10:46:52 -05:00
commit eda9127c26
36 changed files with 3492 additions and 1126 deletions

View file

@ -0,0 +1,993 @@
/* ###
* IP: GHIDRA
*
* 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.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ghidra.pcode.floatformat;
import java.math.*;
/**
* An IEEE 754 floating point class.
*
* <p>Values represented:
* <ul>
* <li>QUIET_NAN, SIGNALED_NAN</li>
* <li>-INF, +INF</li>
* <li>value = sign * unscaled * 2 ^ (scale-fracbits)</li>
* </ul>
* sign = -1 or +1, unscaled has at most fracbits+1 bits, and scale is at most expbits bits.
*
* <p>Operations compute exact result then round to nearest even.
*/
public strictfp class BigFloat implements Comparable<BigFloat> {
final int fracbits; // there are fracbits+1 significant bits.
final int expbits; // # bits used for exponent
final int maxScale;
final int minScale;
FloatKind kind;
// -1, +1
int sign;
// normal numbers have unscaled.bitLength() = fracbits+1
// subnormal numbers have scale=0 and unscaled.bitLength() <= fracbits
BigInteger unscaled;
int scale;
/**
* Construct a BigFloat. If kind is FINITE, the value is <code>sign*unscaled*2^(scale-fracbits)</code>
*
* @param fracbits number of fractional bits
* @param expbits maximum number of bits in exponent
* @param kind the Kind, FINITE, INFINITE, ...
* @param sign +1 or -1
* @param unscaled the value's mantissa
* @param scale value's scale
*/
public BigFloat(int fracbits, int expbits, FloatKind kind, int sign, BigInteger unscaled,
int scale) {
this.fracbits = fracbits;
this.expbits = expbits;
this.kind = kind;
this.sign = sign;
this.unscaled = unscaled;
this.scale = scale;
this.maxScale = (1 << (expbits - 1)) - 1;
this.minScale = 1 - this.maxScale;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + expbits;
result = prime * result + fracbits;
result = prime * result + kind.hashCode();
switch (kind) {
case FINITE:
result = prime * result + sign;
result = prime * result + scale;
result = prime * result + unscaled.hashCode();
break;
case INFINITE:
result = prime * result + sign;
break;
case QUIET_NAN:
case SIGNALING_NAN:
break;
}
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
BigFloat other = (BigFloat) obj;
if (expbits != other.expbits) {
return false;
}
if (fracbits != other.fracbits) {
return false;
}
if (kind != other.kind) {
return false;
}
switch (kind) {
case FINITE:
if (sign != other.sign) {
return false;
}
if (scale != other.scale) {
return false;
}
if (!unscaled.equals(other.unscaled)) {
return false;
}
break;
case INFINITE:
if (sign != other.sign) {
return false;
}
break;
case QUIET_NAN:
case SIGNALING_NAN:
break;
}
return true;
}
/**
* Return the BigFloat with the given number of bits representing the given BigInteger.
*
* @param fracbits number of fractional bits
* @param expbits number of bits in the exponent
* @param i an integer
* @return a BigFloat representing i
*/
public static BigFloat valueOf(int fracbits, int expbits, BigInteger i) {
BigFloat f = new BigFloat(fracbits, expbits, FloatKind.FINITE, i.signum() >= 0 ? +1 : -1,
i.abs(), fracbits);
f.scaleUpTo(fracbits + 1);
return f;
}
/**
* Return the BigFloat with the given number of bits representing zero.
*
* @param fracbits number of fractional bits
* @param expbits number of bits in the exponent
* @param sign +1 or -1
* @return a BigFloat representing +zero or -zero
*/
public static BigFloat zero(int fracbits, int expbits, int sign) {
return new BigFloat(fracbits, expbits, FloatKind.FINITE, sign, BigInteger.ZERO,
2 - (1 << (expbits - 1)));
}
/**
* Return the BigFloat with the given number of bits representing (positive) zero.
*
* @param fracbits number of fractional bits
* @param expbits number of bits in the exponent
* @return a BigFloat representing +zero
*/
public static BigFloat zero(int fracbits, int expbits) {
return zero(fracbits, expbits, +1);
}
/**
* @param fracbits number of fractional bits
* @param expbits number of bits in the exponent
* @param sign +1 or -1
* @return +inf or -inf
*/
public static BigFloat infinity(int fracbits, int expbits, int sign) {
return new BigFloat(fracbits, expbits, FloatKind.INFINITE, sign,
BigInteger.ONE.shiftLeft(fracbits), (1 << (expbits - 1)) - 1);
}
/**
* Return the BigFloat with the given number of bits representing (quiet) NaN.
*
* @param fracbits number of fractional bits
* @param expbits number of bits in the exponent
* @param sign +1 or -1
* @return a BigFloat representing (quiet) NaN
*/
public static BigFloat quietNaN(int fracbits, int expbits, int sign) {
return new BigFloat(fracbits, expbits, FloatKind.QUIET_NAN, sign, BigInteger.ZERO,
(1 << (expbits - 1)) - 1);
}
private void upscale(int nbits) {
unscaled = unscaled.shiftLeft(nbits);
scale -= nbits;
}
// guarantee at least significant bits (fractbits+1) plus one for rounding
protected void scaleUpTo(int newLength) {
if (kind != FloatKind.FINITE) {
throw new AssertionError("scaling of non-finite float!");
}
int d = newLength - this.unscaled.bitLength();
if (d > 0) {
this.upscale(d);
}
}
/**
* @return {@code true} if this BigFloat is FINITE and normal
*/
public boolean isNormal() {
return kind == FloatKind.FINITE && unscaled.bitLength() >= fracbits + 1;
}
/**
* This function is used internally to round after a computation.
*
* <p>Assume that the true value is
* <code>sign * (unscaled + eps) * 2 ^ (scale-fracbits)</code>
* and
* <code>unscaled.bitLength() > fracbits+1</code>
* (or the value is subnormal with at least 1 bit of extra precision)
*
* @param eps &lt; 1
*/
protected void internalRound(boolean eps) {
if (kind != FloatKind.FINITE) {
throw new AssertionError("Rounding non-finite float");
}
if (unscaled.signum() == 0) {
if (eps) {
throw new AssertionError("Rounding zero + epsilon, need bit length");
}
makeZero();
return;
}
int extrabits = Math.max(unscaled.bitLength() - (fracbits + 1), minScale - scale);
if (extrabits <= 0) {
throw new AssertionError("Rounding with no extra bits of precision");
}
int midbit = extrabits - 1;
boolean midbitset = this.unscaled.testBit(midbit);
eps |= unscaled.getLowestSetBit() < midbit;
unscaled = unscaled.shiftRight(extrabits);
scale += extrabits;
boolean odd = unscaled.testBit(0);
if (midbitset && (eps || odd)) {
unscaled = unscaled.add(BigInteger.ONE);
// handle overflowing carry
if (unscaled.bitLength() > fracbits + 1) {
assert (unscaled.bitLength() == unscaled.getLowestSetBit() + 1);
unscaled = unscaled.shiftRight(1);
scale += 1;
}
}
if (scale > maxScale) {
kind = FloatKind.INFINITE;
}
}
protected int getLeadBitPos() {
if (kind != FloatKind.FINITE || unscaled.signum() == 0) {
throw new AssertionError("lead bit of non-finite or zero");
}
return unscaled.bitLength() - fracbits + scale;
}
/**
* If finite, the returned BigDecimal is exactly equal to this. If not finite, one of the
* FloatFormat.BIG_* constants is returned.
*
* @return a BigDecimal
*/
public BigDecimal toBigDecimal() {
switch (kind) {
case FINITE:
// sign * unscaled * 2^(scale-fracbits)
int iscale = scale - fracbits;
BigDecimal x;
if (iscale >= 0) {
x = new BigDecimal(unscaled.shiftLeft(iscale));
}
else {
x = new BigDecimal(unscaled.multiply(BigInteger.valueOf(5).pow(-iscale)),
-iscale);
}
if (sign < 0) {
x = x.negate();
}
return x;
case INFINITE:
return sign < 0 ? FloatFormat.BIG_NEGATIVE_INFINITY
: FloatFormat.BIG_POSITIVE_INFINITY;
case QUIET_NAN:
case SIGNALING_NAN:
return FloatFormat.BIG_NaN;
default:
throw new AssertionError("unknown BigFloat kind: " + kind);
}
}
public String toBinaryString() {
switch (kind) {
case QUIET_NAN:
return "qNaN";
case SIGNALING_NAN:
return "sNaN";
case INFINITE:
if (sign < 0)
return "-inf";
return "+inf";
case FINITE:
String s = (sign < 0) ? "-" : "";
int ascale = scale;
String binary;
if (this.isNormal()) {
binary = "1." + unscaled.toString(2).substring(1);
ascale += (unscaled.bitLength() - (fracbits + 1));
}
else { // subnormal
assert (unscaled.bitLength() <= fracbits);
if (unscaled.equals(BigInteger.ZERO)) {
return String.format("%s0b0.0", s);
}
binary =
"0." + "0".repeat(fracbits - unscaled.bitLength()) + unscaled.toString(2);
}
binary = binary.replaceAll("0*$", "");
if (binary.endsWith(".")) {
binary += "0";
}
return String.format("%s0b%s * 2^%d", s, binary, ascale);
default:
throw new AssertionError("unexpected kind " + kind);
}
}
protected void makeSignalingNaN() {
this.kind = FloatKind.SIGNALING_NAN;
}
protected void makeQuietNaN() {
this.kind = FloatKind.QUIET_NAN;
}
/**
* @return {@code true} if this BigFloat is NaN
*/
public boolean isNaN() {
return kind == FloatKind.QUIET_NAN || kind == FloatKind.SIGNALING_NAN;
}
protected void makeZero() {
this.kind = FloatKind.FINITE;
this.unscaled = BigInteger.ZERO;
this.scale = minScale;
}
/**
* @return {@code true} if this BigFloat is infinite
*/
public boolean isInfinite() {
return kind == FloatKind.INFINITE;
}
/**
* @return {@code true} if this BigFloat is zero
*/
public boolean isZero() {
return this.kind == FloatKind.FINITE && unscaled.equals(BigInteger.ZERO);
}
/**
* @return a copy of this BigFloat
*/
public BigFloat copy() {
return new BigFloat(fracbits, expbits, kind, sign, unscaled, scale);
}
// assuming same fracbits and expbits...
protected void copyFrom(BigFloat other) {
this.kind = other.kind;
this.sign = other.sign;
this.unscaled = other.unscaled;
this.scale = other.scale;
}
/**
* @param a a BigFloat
* @param b a BigFloat
* @return {@code a/b}
*/
public static BigFloat div(BigFloat a, BigFloat b) {
BigFloat c = a.copy();
c.div(b);
return c;
}
/**
* {@code this/=other}
*
* @param other a BigFloat
*/
public void div(BigFloat other) {
if (this.isNaN() || other.isNaN()) {
makeQuietNaN();
return;
}
if (this.isInfinite()) {
if (other.isInfinite()) {
makeQuietNaN();
}
else {
sign *= other.sign;
}
return;
}
// this is finite
switch (other.kind) {
case QUIET_NAN:
case SIGNALING_NAN:
this.makeQuietNaN();
return;
case INFINITE:
makeZero();
sign *= other.sign;
return;
case FINITE:
break;
default:
throw new AssertionError("unexpected kind " + other.kind);
}
if (other.isZero()) {
if (this.isZero()) {
makeQuietNaN();
}
else {
this.kind = FloatKind.INFINITE;
this.sign *= other.sign;
}
return;
}
// this is finite, other is finite non zero
// update representations so that this.unscaled has at fracbits+2 -- normal precision plus one for rounding.
//
// for numbers a,b
// floor(a)-floor(b)-1 <= floor(a-b) <= floor(a)-floor(b)
// nbits(x) = floor(log_2(x))+1, so
// nbits(x) - nbits(y) <= nbits(x/y) <= nbits(x) - nbits(y) + 1
// so
// this + lshift - other = fracbits+2 =>
int lshift = fracbits + 2 + other.unscaled.bitLength() - this.unscaled.bitLength();
this.upscale(lshift);
BigInteger qr[] = this.unscaled.divideAndRemainder(other.unscaled);
BigInteger q = qr[0];
BigInteger r = qr[1];
this.sign *= other.sign;
this.scale -= other.scale - fracbits;
this.unscaled = q;
this.internalRound(r.signum() != 0);
}
/**
* @param a a BigFloat
* @param b a BigFloat
* @return {@code a*b}
*/
public static BigFloat mul(BigFloat a, BigFloat b) {
BigFloat c = a.copy();
c.mul(b);
return c;
}
/**
* {@code this*=other}
*
* @param other a BigFloat
*/
public void mul(BigFloat other) {
if (this.isNaN() || other.isNaN()) {
this.makeQuietNaN();
return;
}
if ((this.isZero() && other.isInfinite()) || (this.isInfinite() && other.isZero())) {
this.makeQuietNaN();
return;
}
if (this.isInfinite() || other.isInfinite()) {
this.kind = FloatKind.INFINITE;
this.sign *= other.sign;
return;
}
// this and other are finite
this.sign *= other.sign;
this.unscaled = this.unscaled.multiply(other.unscaled);
this.scale += other.scale - fracbits;
this.scaleUpTo(fracbits + 2);
this.internalRound(false);
}
/**
* @param a a BigFloat
* @param b a BigFloat
* @return {@code a+b}
*/
public static BigFloat add(BigFloat a, BigFloat b) {
BigFloat c = a.copy();
c.add(b);
return c;
}
/**
* {@code this+=other}
*
* @param other a BigFloat
*/
public void add(BigFloat other) {
if (this.isNaN() || other.isNaN()) {
this.makeQuietNaN();
return;
}
if (this.isInfinite() && other.isInfinite()) {
if (this.sign != other.sign) {
this.makeQuietNaN();
}
return;
}
if (this.isInfinite()) {
return;
}
if (other.isInfinite()) {
copyFrom(other);
return;
}
if (other.isZero()) {
if (this.isZero()) {
this.sign = (this.sign < 0 && other.sign < 0) ? -1 : 1;
}
return;
}
if (this.isZero()) {
copyFrom(other);
return;
}
if (this.sign == other.sign) {
add0(other);
}
else {
sub0(other);
}
}
/**
* @param a a BigFloat
* @param b a BigFloat
* @return {@code a-b}
*/
public static BigFloat sub(BigFloat a, BigFloat b) {
BigFloat c = b.copy();
c.sign *= -1;
c.add(a);
if (c.isZero()) {
c.sign = (a.sign < 0 && b.sign > 0) ? -1 : 1;
}
return c;
}
/**
* {@code this-=other}
*
* @param other a BigFloat
*/
public void sub(BigFloat other) {
int thissign = this.sign;
BigFloat nother = other.copy();
nother.sign *= -1;
this.add(nother);
if (this.isZero()) {
this.sign = (thissign < 0 && nother.sign < 0) ? -1 : 1;
}
}
// assume this and other are finite with the same sign, neither is zero
protected void add0(BigFloat other) {
int d = this.scale - other.scale;
if (d > fracbits + 1) {
return;
}
else if (d < -(fracbits + 1)) {
this.copyFrom(other);
return;
}
boolean residue;
BigFloat a;
BigFloat b;
if (d >= 0) {
a = this;
b = other;
}
else {
d = -d;
a = other;
b = this;
}
residue = b.unscaled.getLowestSetBit() < d - 1;
this.scale = a.scale - 1;
this.unscaled = a.unscaled.shiftLeft(1).add(b.unscaled.shiftRight(d - 1));
scaleUpTo(fracbits + 2);
internalRound(residue);
}
// assume this and other are finite with the opposite sign, neither is zero
protected void sub0(BigFloat other) {
int d = this.scale - other.scale;
if (d > fracbits + 2) {
return;
}
else if (d < -(fracbits + 2)) {
this.copyFrom(other);
return;
}
boolean residue;
BigFloat a;
BigFloat b;
if (d >= 0) {
a = this;
b = other;
}
else {
d = -d;
a = other;
b = this;
}
// d <= 0 is ok.. no residue and right shift will become left shift
residue = b.unscaled.getLowestSetBit() < d - 2;
this.sign = a.sign;
this.scale = a.scale - 2;
BigInteger x = b.unscaled;
x = x.shiftRight(d - 2);
if (residue) {
x = x.add(BigInteger.ONE);
}
this.unscaled = a.unscaled.shiftLeft(2).subtract(x);
if (this.unscaled.signum() == 0) {
this.sign = 1; // cancellation results in positive 0.
}
else if (this.unscaled.signum() < 0) {
this.sign *= -1;
this.unscaled = this.unscaled.negate();
}
scaleUpTo(fracbits + 2);
internalRound(residue);
}
/**
* @param a a BigFloat
* @return the square root of {@code a}
*/
public static BigFloat sqrt(BigFloat a) {
BigFloat c = a.copy();
c.sqrt();
return c;
}
/**
* {@code this=sqrt(this)}
*
* <p>Square root by abacus algorithm, Martin Guy @ UKC, June 1985.
* From a book on programming abaci by Mr C. Woo.
* Argument is a positive integer, as is result.
*
* <p>adapted from http://medialab.freaknet.org/martin/src/sqrt/sqrt.c
*/
public void sqrt() {
if (this.isZero()) {
return;
}
if (this.isNaN() || this.sign == -1) {
makeQuietNaN();
return;
}
if (this.isInfinite()) {
return;
}
BigInteger residue;
BigInteger result;
BigInteger bit;
//// force at least fracbits+2 bits of precision in the result
int sigbits = 2 * fracbits + 3;
this.scaleUpTo(sigbits);
// scale+fracbits needs to be even for the sqrt computation
if (((scale + fracbits) & 1) != 0) {
upscale(1);
}
residue = unscaled;
result = BigInteger.ZERO;
/* "bit" starts at the highest 4 power <= n. */
int pow = residue.bitLength() - 1; // highest 2 power <= n
pow -= pow & 1; // highest 4 power
bit = BigInteger.ONE.shiftLeft(pow);
while (bit.signum() != 0) {
BigInteger resp1 = result.add(bit);
if (residue.compareTo(resp1) >= 0) {
residue = residue.subtract(resp1);
result = result.add(bit.shiftLeft(1));
}
result = result.shiftRight(1);
bit = bit.shiftRight(2);
}
unscaled = result;
scale = (scale + fracbits) / 2;
internalRound(residue.signum() != 0);
}
// floor, ignoring sign
private void floor0() {
// value = unscaled * 2^(scale-fracbits)
if (scale < 0) {
makeZero();
return;
}
int nbitsUnderOne = fracbits - scale;
unscaled = unscaled.shiftRight(nbitsUnderOne).shiftLeft(nbitsUnderOne);
}
// sign is not set
private void makeOne() {
kind = FloatKind.FINITE;
scale = 0;
unscaled = BigInteger.ONE.shiftLeft(fracbits);
}
// ceil, ignoring sign
private void ceil0() {
if (isZero()) {
return;
}
else if (scale < 0) {
makeOne();
return;
}
int nbitsUnderOne = fracbits - scale;
boolean increment = unscaled.getLowestSetBit() < nbitsUnderOne;
unscaled = unscaled.shiftRight(nbitsUnderOne).shiftLeft(nbitsUnderOne);
if (increment) {
unscaled = unscaled.add(BigInteger.ONE.shiftLeft(nbitsUnderOne));
}
// if we carry to a new bit, change the scale
if (unscaled.bitLength() > fracbits + 1) {
upscale(-1);
}
}
/**
* @param a a BigFloat
* @return {@code floor(a)}
*/
public static BigFloat floor(BigFloat a) {
BigFloat b = a.copy();
b.floor();
return b;
}
/**
* {@code this=floor(this)}
*/
public void floor() {
switch (kind) {
case INFINITE:
return;
case SIGNALING_NAN:
makeQuietNaN();
case QUIET_NAN:
return;
case FINITE:
break;
}
if (sign >= 0) {
floor0();
}
else {
ceil0();
}
}
/**
* @param a a BigFloat
* @return {@code ceil(a)}
*/
public static BigFloat ceil(BigFloat a) {
BigFloat b = a.copy();
b.ceil();
return b;
}
/**
* {@code this=ceil(this)}
*/
public void ceil() {
switch (kind) {
case INFINITE:
return;
case SIGNALING_NAN:
makeQuietNaN();
case QUIET_NAN:
return;
case FINITE:
break;
}
if (sign >= 0) {
ceil0();
}
else {
floor0();
}
}
/**
* @param a a BigFloat
* @return {@code trunc(a)} (round toward zero)
*/
public static BigFloat trunc(BigFloat a) {
BigFloat b = a.copy();
b.trunc();
return b;
}
/**
* {@code this=trunc(this)} (round toward zero)
*/
public void trunc() {
floor0();
}
/**
* {@code this*=-1}
*/
public void negate() {
this.sign *= -1;
}
/**
* @param a a BigFloat
* @return {@code -a}
*/
public static BigFloat negate(BigFloat a) {
BigFloat b = a.copy();
b.negate();
return b;
}
/**
* @param a a BigFloat
* @return {@code abs(a)}
*/
public static BigFloat abs(BigFloat a) {
BigFloat b = a.copy();
b.abs();
return b;
}
/**
* {@code this=abs(this)}
*/
public void abs() {
this.sign = 1;
}
/**
* @return the truncated integer form of this BigFloat
*/
public BigInteger toBigInteger() {
BigInteger res = unscaled.shiftRight(fracbits - scale);
if (sign < 0) {
return res.negate();
}
return res;
}
/**
* @param a a BigFloat
* @return {@code round(a)}
*/
public static BigFloat round(BigFloat a) {
BigFloat b = a.copy();
b.round();
return b;
}
/**
* {@code this=round(this)}
*/
public void round() {
BigFloat half = new BigFloat(fracbits, expbits, FloatKind.FINITE, +1,
BigInteger.ONE.shiftLeft(fracbits), -1);
add(half);
floor();
}
@Override
public int compareTo(BigFloat other) {
// this == NaN
if (isNaN()) {
if (other.isNaN()) {
return 0;
}
return 1;
}
// this != NaN
if (other.isNaN()) {
return -1;
}
if (isInfinite()) {
// this == -inf
if (sign < 0) {
if (other.isInfinite() && other.sign < 0) {
return 0;
}
return -1;
}
// this == +inf
if (other.isInfinite() && other.sign > 0) {
return 0;
}
return 1;
}
// this is finite
if (other.isInfinite()) {
return -other.sign;
}
// other is finite
if (this.sign != other.sign) {
return this.sign;
}
// both finite, same sign
int c = Integer.compare(this.scale, other.scale);
if (c != 0) {
return c * this.sign;
}
return this.sign * this.unscaled.compareTo(other.unscaled);
}
}

View file

@ -1,6 +1,5 @@
/* ###
* IP: GHIDRA
* REVIEWED: YES
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -16,11 +15,9 @@
*/
package ghidra.pcode.floatformat;
public enum Floatclass {
normalized,
infinity,
zero,
nan,
denormalized;
enum FloatKind {
FINITE, /* includes both normal and subnormal */
INFINITE,
QUIET_NAN,
SIGNALING_NAN;
}

View file

@ -85,8 +85,7 @@ public abstract class AbstractFloatDataType extends BuiltIn {
return doubleValue;
}
BigInteger value = Utils.bytesToBigInteger(bytes, len, buf.isBigEndian(), false);
BigDecimal decValue = floatFormat.getHostFloat(value);
// TODO: adjust scale for improved display value ??
BigDecimal decValue = floatFormat.round(floatFormat.getHostFloat(value));
return decValue;
}
catch (UnsupportedFloatFormatException e) {
@ -197,7 +196,8 @@ public abstract class AbstractFloatDataType extends BuiltIn {
}
for (int size : floatMap.keySet()) {
if (!newFloatMap.containsKey(size)) {
newFloatMap.put(size, (AbstractFloatDataType) floatMap.get(size).clone(dtm));
newFloatMap.put(size,
(AbstractFloatDataType) floatMap.get(size).clone(dtm));
}
}
}

View file

@ -0,0 +1,361 @@
/* ###
* IP: GHIDRA
*
* 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.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ghidra.pcode.floatformat;
import static org.junit.Assert.*;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.junit.Assert;
import org.junit.Test;
import generic.test.AbstractGenericTest;
public class BigFloatTest extends AbstractGenericTest {
public BigFloatTest() {
super();
}
@Test
public void testIEEEFloatRepresentation() {
Assert.assertEquals("0b0.0", FloatFormat.toBinaryString(0.0f));
Assert.assertEquals("0b1.0 * 2^0", FloatFormat.toBinaryString(1.0f));
Assert.assertEquals("0b1.0 * 2^1", FloatFormat.toBinaryString(2.0f));
Assert.assertEquals("0b1.0 * 2^-1", FloatFormat.toBinaryString(0.5f));
Assert.assertEquals("-0b1.0 * 2^1", FloatFormat.toBinaryString(-2.0f));
}
@Test
public void testIEEEFloatAsBigFloat() {
Assert.assertEquals(FloatFormat.toBigFloat(0.0f).toBinaryString(),
FloatFormat.toBinaryString(0.0f));
Assert.assertEquals(FloatFormat.toBigFloat(1.0f).toBinaryString(),
FloatFormat.toBinaryString(1.0f));
Assert.assertEquals(FloatFormat.toBigFloat(2.0f).toBinaryString(),
FloatFormat.toBinaryString(2.0f));
Assert.assertEquals(FloatFormat.toBigFloat(0.5f).toBinaryString(),
FloatFormat.toBinaryString(0.5f));
Assert.assertEquals(FloatFormat.toBigFloat(-2.0f).toBinaryString(),
FloatFormat.toBinaryString(-2.0f));
}
@Test
public void testIEEEFloatAsBigFloatRandom() {
Random rand = new Random(1);
for (int i = 0; i < 100; ++i) {
float f = Float.intBitsToFloat(rand.nextInt());
Assert.assertEquals(FloatFormat.toBigFloat(f).toBinaryString(),
FloatFormat.toBinaryString(f));
}
}
@Test
public void testIEEEDoubleRepresentation() {
Assert.assertEquals("0b0.0", FloatFormat.toBinaryString(0.0));
Assert.assertEquals("0b1.0 * 2^0", FloatFormat.toBinaryString(1.0));
Assert.assertEquals("0b1.0 * 2^1", FloatFormat.toBinaryString(2.0));
Assert.assertEquals("0b1.0 * 2^-1", FloatFormat.toBinaryString(0.5));
Assert.assertEquals("-0b1.0 * 2^1", FloatFormat.toBinaryString(-2.0));
}
@Test
public void testIEEEDoubleAsBigFloat() {
Assert.assertEquals(FloatFormat.toBigFloat(0.0).toBinaryString(),
FloatFormat.toBinaryString(0.0));
Assert.assertEquals(FloatFormat.toBigFloat(1.0).toBinaryString(),
FloatFormat.toBinaryString(1.0));
Assert.assertEquals(FloatFormat.toBigFloat(2.0).toBinaryString(),
FloatFormat.toBinaryString(2.0));
Assert.assertEquals(FloatFormat.toBigFloat(0.5).toBinaryString(),
FloatFormat.toBinaryString(0.5));
Assert.assertEquals(FloatFormat.toBigFloat(-2.0).toBinaryString(),
FloatFormat.toBinaryString(-2.0));
}
@Test
public void testIEEEDoubleAsBigFloatRandom() {
Random rand = new Random(1);
for (int i = 0; i < 100; ++i) {
double d = Double.longBitsToDouble(rand.nextLong());
Assert.assertEquals(FloatFormat.toBigFloat(d).toBinaryString(),
FloatFormat.toBinaryString(d));
}
}
interface UnaryProc<T> {
void apply(T a);
}
interface UnaryOp<T> {
T apply(T a);
}
interface BinaryProc<T> {
void apply(T a, T b);
}
interface BinaryOp<T> {
T apply(T a, T b);
}
// used for testing one-argument operations
final static int NUM_RANDOM_TEST_VALUES_UNARY = 1000;
// used for each operand of two-argument operations
final static int NUM_RANDOM_TEST_VALUES_BINARY = 100;
final static List<Float> testFloatList;
final static List<Float> testFloatShortList;
static {
Random rand = new Random(1);
// @formatter:off
List<Float> specialValues =
List.of(
-0.0f, 0.0f,
-1.0f, 1.0f,
-Float.MIN_VALUE, Float.MIN_VALUE,
-Float.MAX_VALUE, Float.MAX_VALUE,
-Float.MIN_NORMAL - Float.MIN_VALUE, -Float.MIN_NORMAL, -Float.MIN_NORMAL + Float.MIN_VALUE,
Float.MIN_NORMAL - Float.MIN_VALUE, Float.MIN_NORMAL, Float.MIN_NORMAL + Float.MIN_VALUE,
Float.NaN,
Float.NEGATIVE_INFINITY, Float.POSITIVE_INFINITY);
// @formatter:on
Stream<Float> randStream = Stream.generate(rand::nextInt)
.limit(NUM_RANDOM_TEST_VALUES_UNARY)
.map(Float::intBitsToFloat);
testFloatList = Stream.concat(specialValues.stream(), randStream)
.collect(Collectors.toUnmodifiableList());
testFloatShortList =
testFloatList.subList(0, specialValues.size() + NUM_RANDOM_TEST_VALUES_BINARY);
}
final static List<Double> testDoubleList;
final static List<Double> testDoubleShortList;
static {
Random rand = new Random(1);
// @formatter:off
List<Double> specialValues =
List.of(
-0.0, 0.0,
-1.0, 1.0,
-Double.MIN_VALUE, Double.MIN_VALUE,
-Double.MAX_VALUE, Double.MAX_VALUE,
-Double.MIN_NORMAL - Double.MIN_VALUE, -Double.MIN_NORMAL, -Double.MIN_NORMAL + Double.MIN_VALUE,
Double.MIN_NORMAL - Double.MIN_VALUE, Double.MIN_NORMAL, Double.MIN_NORMAL + Double.MIN_VALUE,
Double.NaN,
Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY);
// @formatter:on
Stream<Double> randStream = Stream.generate(rand::nextLong)
.limit(NUM_RANDOM_TEST_VALUES_UNARY)
.map(Double::longBitsToDouble);
testDoubleList = Stream.concat(specialValues.stream(), randStream)
.collect(Collectors.toUnmodifiableList());
testDoubleShortList =
testDoubleList.subList(0, specialValues.size() + NUM_RANDOM_TEST_VALUES_BINARY);
}
public void unaryDoubleOpTest(UnaryOp<Double> op, UnaryProc<BigFloat> bproc) {
int i = 0;
for (double fa : testDoubleList) {
BigFloat bfa = FloatFormat.toBigFloat(fa);
double fb = op.apply(fa);
bproc.apply(bfa);
assertEquals("case #" + Integer.toString(i), Double.isNaN(fb), bfa.isNaN());
if (!Double.isNaN(fb)) {
assertEquals("case #" + Integer.toString(i),
FloatFormat.toBinaryString(op.apply(fa)), bfa.toBinaryString());
}
++i;
}
}
public void binaryDoubleOpTest(BinaryOp<Double> op, BinaryProc<BigFloat> bproc) {
int i = 0;
for (double fa : testDoubleShortList) {
int j = 0;
for (double fb : testDoubleShortList) {
BigFloat bfa = FloatFormat.toBigFloat(fa);
BigFloat bfb = FloatFormat.toBigFloat(fb);
double fc = op.apply(fa, fb);
bproc.apply(bfa, bfb);
assertEquals(String.format("case #%d,%d", i, j), Double.isNaN(fc), bfa.isNaN());
if (!Double.isNaN(fc)) {
assertEquals(String.format("case #%d,%d", i, j), FloatFormat.toBinaryString(fc),
bfa.toBinaryString());
}
++j;
}
++i;
}
}
public void unaryFloatOpTest(UnaryOp<Float> op, UnaryProc<BigFloat> bproc) {
int i = 0;
for (float fa : testFloatList) {
BigFloat bfa = FloatFormat.toBigFloat(fa);
float fb = op.apply(fa);
bproc.apply(bfa);
assertEquals("case #" + Integer.toString(i), Float.isNaN(fb), bfa.isNaN());
if (!Float.isNaN(fb)) {
assertEquals("case #" + Integer.toString(i), FloatFormat.toBinaryString(fb),
bfa.toBinaryString());
}
++i;
}
}
public void binaryFloatOpTest(BinaryOp<Float> op, BinaryProc<BigFloat> bproc) {
int i = 0;
for (float fa : testFloatShortList) {
int j = 0;
for (float fb : testFloatShortList) {
BigFloat bfa = FloatFormat.toBigFloat(fa);
BigFloat bfb = FloatFormat.toBigFloat(fb);
float fc = op.apply(fa, fb);
bproc.apply(bfa, bfb);
assertEquals(String.format("case #%d,%d", i, j), Float.isNaN(fc), bfa.isNaN());
if (!Float.isNaN(fc)) {
assertEquals(String.format("case #%d,%d", i, j), FloatFormat.toBinaryString(fc),
bfa.toBinaryString());
}
++j;
}
++i;
}
}
@Test
public void testFloatAdd() {
binaryFloatOpTest((a, b) -> a + b, (a, b) -> a.add(b));
}
@Test
public void testFloatSubstract() {
binaryFloatOpTest((a, b) -> a - b, (a, b) -> a.sub(b));
}
@Test
public void testFloatMultiply() {
binaryFloatOpTest((a, b) -> a * b, (a, b) -> a.mul(b));
}
@Test
public void testFloatDivide() {
binaryFloatOpTest((a, b) -> a / b, (a, b) -> a.div(b));
}
@Test
public void testFloatCompare() {
int i = 0;
for (float a : testFloatShortList) {
int j = 0;
BigFloat fa = FloatFormat.toBigFloat(a);
for (float b : testFloatShortList) {
BigFloat fb = FloatFormat.toBigFloat(b);
assertEquals(String.format("case #%d,%d", i, j), Float.compare(a, b),
fa.compareTo(fb));
++j;
}
++i;
}
}
@Test
public void testFloatSqrt() {
unaryFloatOpTest(a -> (float) Math.sqrt(a), a -> a.sqrt());
}
@Test
public void testFloatFloor() {
unaryFloatOpTest(a -> (float) Math.floor(a), a -> a.floor());
}
@Test
public void testFloatCeil() {
unaryFloatOpTest(a -> (float) Math.ceil(a), a -> a.ceil());
}
@Test
public void testDoubleAdd() {
binaryDoubleOpTest((a, b) -> a + b, (a, b) -> a.add(b));
}
@Test
public void testDoubleSubstract() {
binaryDoubleOpTest((a, b) -> a - b, (a, b) -> a.sub(b));
}
@Test
public void testDoubleMultiply() {
binaryDoubleOpTest((a, b) -> a * b, (a, b) -> a.mul(b));
}
@Test
public void testDoubleDivide() {
binaryDoubleOpTest((a, b) -> a / b, (a, b) -> a.div(b));
}
@Test
public void testDoubleCompare() {
int i = 0;
for (double a : testDoubleShortList) {
int j = 0;
BigFloat fa = FloatFormat.toBigFloat(a);
for (double b : testDoubleShortList) {
BigFloat fb = FloatFormat.toBigFloat(b);
assertEquals(String.format("case #%d,%d", i, j), Double.compare(a, b),
fa.compareTo(fb));
++j;
}
++i;
}
}
@Test
public void testDoubleSqrt() {
unaryDoubleOpTest(a -> Math.sqrt(a), a -> a.sqrt());
}
@Test
public void testDoubleFloor() {
unaryDoubleOpTest(a -> Math.floor(a), a -> a.floor());
}
@Test
public void testDoubleCeil() {
unaryDoubleOpTest(a -> Math.ceil(a), a -> a.ceil());
}
}

View file

@ -15,7 +15,6 @@
*/
package ghidra.pcode.opbehavior;
import java.math.BigDecimal;
import java.math.BigInteger;
import org.junit.Assert;
@ -65,25 +64,25 @@ public class OpBehaviorFloatAbsTest extends AbstractOpBehaviorTest {
FloatFormat ff = FloatFormatFactory.getFloatFormat(8);
BigInteger a = ff.getEncoding(BigDecimal.valueOf(2.5d));
BigInteger a = ff.getEncoding(ff.getBigFloat(2.5d));
BigInteger result = op.evaluateUnary(8, 8, a);
Assert.assertEquals(BigDecimal.valueOf(2.5d), ff.getHostFloat(result));
Assert.assertEquals(ff.getBigFloat(2.5d), ff.getHostFloat(result));
a = ff.getEncoding(BigDecimal.valueOf(-2.5d));
a = ff.getEncoding(ff.getBigFloat(-2.5d));
result = op.evaluateUnary(8, 8, a);
Assert.assertEquals(BigDecimal.valueOf(2.5d), ff.getHostFloat(result));
Assert.assertEquals(ff.getBigFloat(2.5d), ff.getHostFloat(result));
a = ff.getEncoding(FloatFormat.BIG_POSITIVE_INFINITY);
a = ff.getBigInfinityEncoding(false);
result = op.evaluateUnary(8, 8, a);
Assert.assertEquals(FloatFormat.BIG_POSITIVE_INFINITY, ff.getHostFloat(result));
Assert.assertEquals(ff.getBigInfinity(false), ff.getHostFloat(result));
a = ff.getEncoding(FloatFormat.BIG_NEGATIVE_INFINITY);
a = ff.getBigInfinityEncoding(true);
result = op.evaluateUnary(8, 8, a);
Assert.assertEquals(FloatFormat.BIG_POSITIVE_INFINITY, ff.getHostFloat(result));
Assert.assertEquals(ff.getBigInfinity(false), ff.getHostFloat(result));
a = ff.getEncoding(FloatFormat.BIG_NaN);
a = ff.getBigNaNEncoding(false);
result = op.evaluateUnary(8, 8, a);
Assert.assertEquals(FloatFormat.BIG_NaN, ff.getHostFloat(result));
Assert.assertEquals(ff.getBigNaN(false), ff.getHostFloat(result));
}
}

View file

@ -15,7 +15,6 @@
*/
package ghidra.pcode.opbehavior;
import java.math.BigDecimal;
import java.math.BigInteger;
import org.junit.Assert;
@ -75,35 +74,35 @@ public class OpBehaviorFloatAddTest extends AbstractOpBehaviorTest {
FloatFormat ff = FloatFormatFactory.getFloatFormat(8);
BigInteger a = ff.getEncoding(BigDecimal.valueOf(1.234d));
BigInteger b = ff.getEncoding(BigDecimal.valueOf(1.123d));
BigInteger a = ff.getEncoding(ff.getBigFloat(1.234d));
BigInteger b = ff.getEncoding(ff.getBigFloat(1.123d));
BigInteger result = op.evaluateBinary(8, 8, a, b);// 1.234 + 1.123
Assert.assertEquals(BigDecimal.valueOf(2.357), ff.getHostFloat(result));
Assert.assertEquals(ff.getBigFloat(2.357), ff.getHostFloat(result));
a = ff.getEncoding(BigDecimal.valueOf(-1.123d));
a = ff.getEncoding(ff.getBigFloat(-1.123d));
result = op.evaluateBinary(8, 8, a, b);// -1.123 + 1.123
Assert.assertEquals(BigDecimal.ZERO, ff.getHostFloat(result));
Assert.assertEquals(ff.getBigZero(false), ff.getHostFloat(result));
a = ff.getEncoding(FloatFormat.BIG_POSITIVE_INFINITY);
a = ff.getEncoding(ff.getBigInfinity(false));
result = op.evaluateBinary(8, 8, a, b);// +INFINITY + 1.123
Assert.assertEquals(FloatFormat.BIG_POSITIVE_INFINITY, ff.getHostFloat(result));
Assert.assertEquals(ff.getBigInfinity(false), ff.getHostFloat(result));
a = ff.getEncoding(FloatFormat.BIG_NEGATIVE_INFINITY);
a = ff.getBigInfinityEncoding(true);
result = op.evaluateBinary(8, 8, a, b);// -INFINITY + 1.123
Assert.assertEquals(FloatFormat.BIG_NEGATIVE_INFINITY, ff.getHostFloat(result));
Assert.assertEquals(ff.getBigInfinity(true), ff.getHostFloat(result));
b = ff.getEncoding(FloatFormat.BIG_NEGATIVE_INFINITY);
b = ff.getBigInfinityEncoding(true);
result = op.evaluateBinary(8, 8, a, b);// -INFINITY + -INFINITY
Assert.assertEquals(FloatFormat.BIG_NEGATIVE_INFINITY, ff.getHostFloat(result));
Assert.assertEquals(ff.getBigInfinity(true), ff.getHostFloat(result));
b = ff.getEncoding(FloatFormat.BIG_POSITIVE_INFINITY);
b = ff.getEncoding(ff.getBigInfinity(false));
result = op.evaluateBinary(8, 8, a, b);// -INFINITY + +INFINITY
Assert.assertEquals(FloatFormat.BIG_NaN, ff.getHostFloat(result));
Assert.assertEquals(ff.getBigNaN(false), ff.getHostFloat(result));
a = ff.getEncoding(FloatFormat.BIG_NaN);
b = ff.getEncoding(BigDecimal.valueOf(1.123d));
a = ff.getBigNaNEncoding(false);
b = ff.getEncoding(ff.getBigFloat(1.123d));
result = op.evaluateBinary(8, 8, a, b);// NaN + 1.123
Assert.assertEquals(FloatFormat.BIG_NaN, ff.getHostFloat(result));
Assert.assertEquals(ff.getBigNaN(false), ff.getHostFloat(result));
}
}

View file

@ -15,7 +15,6 @@
*/
package ghidra.pcode.opbehavior;
import java.math.BigDecimal;
import java.math.BigInteger;
import org.junit.Assert;
@ -65,26 +64,25 @@ public class OpBehaviorFloatCeilTest extends AbstractOpBehaviorTest {
FloatFormat ff = FloatFormatFactory.getFloatFormat(8);
BigInteger a = ff.getEncoding(BigDecimal.valueOf(2.5d));
BigInteger a = ff.getEncoding(ff.getBigFloat(2.5d));
BigInteger result = op.evaluateUnary(8, 8, a);
Assert.assertEquals(BigDecimal.valueOf(3.0d).stripTrailingZeros(), ff.getHostFloat(result));
Assert.assertEquals(ff.getBigFloat(3.0d), ff.getHostFloat(result));
a = ff.getEncoding(BigDecimal.valueOf(-2.5d));
a = ff.getEncoding(ff.getBigFloat(-2.5d));
result = op.evaluateUnary(8, 8, a);
Assert.assertEquals(BigDecimal.valueOf(-2.0d).stripTrailingZeros(),
ff.getHostFloat(result));
Assert.assertEquals(ff.getBigFloat(-2.0d), ff.getHostFloat(result));
a = ff.getEncoding(FloatFormat.BIG_POSITIVE_INFINITY);
a = ff.getBigInfinityEncoding(false);
result = op.evaluateUnary(8, 8, a);
Assert.assertEquals(FloatFormat.BIG_POSITIVE_INFINITY, ff.getHostFloat(result));
Assert.assertEquals(ff.getBigInfinity(false), ff.getHostFloat(result));
a = ff.getEncoding(FloatFormat.BIG_NEGATIVE_INFINITY);
a = ff.getBigInfinityEncoding(true);
result = op.evaluateUnary(8, 8, a);
Assert.assertEquals(FloatFormat.BIG_NEGATIVE_INFINITY, ff.getHostFloat(result));
Assert.assertEquals(ff.getBigInfinity(true), ff.getHostFloat(result));
a = ff.getEncoding(FloatFormat.BIG_NaN);
a = ff.getBigNaNEncoding(false);
result = op.evaluateUnary(8, 8, a);
Assert.assertEquals(FloatFormat.BIG_NaN, ff.getHostFloat(result));
Assert.assertEquals(ff.getBigNaN(false), ff.getHostFloat(result));
}
}

View file

@ -15,7 +15,6 @@
*/
package ghidra.pcode.opbehavior;
import java.math.BigDecimal;
import java.math.BigInteger;
import org.junit.Assert;
@ -62,22 +61,22 @@ public class OpBehaviorFloatDivTest extends AbstractOpBehaviorTest {
FloatFormat ff = FloatFormatFactory.getFloatFormat(8);
BigInteger a = ff.getEncoding(BigDecimal.valueOf(3.75d));
BigInteger b = ff.getEncoding(BigDecimal.valueOf(1.5d));
BigInteger a = ff.getEncoding(ff.getBigFloat(3.75d));
BigInteger b = ff.getEncoding(ff.getBigFloat(1.5d));
BigInteger result = op.evaluateBinary(8, 8, a, b);
Assert.assertEquals(BigDecimal.valueOf(2.5d), ff.getHostFloat(result));
Assert.assertEquals(ff.getBigFloat(2.5d), ff.getHostFloat(result));
b = ff.getEncoding(BigDecimal.ZERO);
b = ff.getBigZeroEncoding(false);
result = op.evaluateBinary(8, 8, a, b);
Assert.assertEquals(FloatFormat.BIG_POSITIVE_INFINITY, ff.getHostFloat(result));
Assert.assertEquals(ff.getBigInfinity(false), ff.getHostFloat(result));
a = ff.getEncoding(BigDecimal.valueOf(-3.75d));
a = ff.getEncoding(ff.getBigFloat(-3.75d));
result = op.evaluateBinary(8, 8, a, b);
Assert.assertEquals(FloatFormat.BIG_NEGATIVE_INFINITY, ff.getHostFloat(result));
Assert.assertEquals(ff.getBigInfinity(true), ff.getHostFloat(result));
b = ff.getEncoding(FloatFormat.BIG_NaN);
b = ff.getBigNaNEncoding(false);
result = op.evaluateBinary(8, 8, a, b);
Assert.assertEquals(FloatFormat.BIG_NaN, ff.getHostFloat(result));
Assert.assertEquals(ff.getBigNaN(false), ff.getHostFloat(result));
}
}

View file

@ -15,14 +15,12 @@
*/
package ghidra.pcode.opbehavior;
import java.math.BigDecimal;
import java.math.BigInteger;
import org.junit.Assert;
import org.junit.Test;
import ghidra.pcode.floatformat.FloatFormat;
import ghidra.pcode.floatformat.FloatFormatFactory;
import ghidra.pcode.floatformat.*;
public class OpBehaviorFloatEqualTest extends AbstractOpBehaviorTest {
@ -30,63 +28,53 @@ public class OpBehaviorFloatEqualTest extends AbstractOpBehaviorTest {
super();
}
@Test
public void testEvaluateBinaryLong() {
@Test
public void testEvaluateBinaryLong() {
OpBehaviorFloatEqual op = new OpBehaviorFloatEqual();
FloatFormat ff = FloatFormatFactory.getFloatFormat(8);
Assert.assertEquals(1, op.evaluateBinary(8, 8, ff.getEncoding(1.234), ff.getEncoding(1.234)));
Assert.assertEquals(1, op.evaluateBinary(8, 8, ff.getEncoding(-1.234), ff.getEncoding(-1.234)));
Assert.assertEquals(0, op.evaluateBinary(8, 8, ff.getEncoding(-1.234), ff.getEncoding(1.234)));
Assert.assertEquals(
1,
op.evaluateBinary(8, 8, ff.getEncoding(Double.POSITIVE_INFINITY),
ff.getEncoding(Double.POSITIVE_INFINITY)));
Assert.assertEquals(
0,
op.evaluateBinary(8, 8, ff.getEncoding(Double.POSITIVE_INFINITY),
ff.getEncoding(Double.NEGATIVE_INFINITY)));
Assert.assertEquals(
1,
op.evaluateBinary(8, 8, ff.getEncoding(Double.NEGATIVE_INFINITY),
ff.getEncoding(Double.NEGATIVE_INFINITY)));
Assert.assertEquals(
0,
op.evaluateBinary(8, 8, ff.getEncoding(Double.POSITIVE_INFINITY),
ff.getEncoding(Double.NaN)));
Assert.assertEquals(1,
op.evaluateBinary(8, 8, ff.getEncoding(1.234), ff.getEncoding(1.234)));
Assert.assertEquals(1,
op.evaluateBinary(8, 8, ff.getEncoding(-1.234), ff.getEncoding(-1.234)));
Assert.assertEquals(0,
op.evaluateBinary(8, 8, ff.getEncoding(-1.234), ff.getEncoding(1.234)));
Assert.assertEquals(1, op.evaluateBinary(8, 8, ff.getEncoding(Double.POSITIVE_INFINITY),
ff.getEncoding(Double.POSITIVE_INFINITY)));
Assert.assertEquals(0, op.evaluateBinary(8, 8, ff.getEncoding(Double.POSITIVE_INFINITY),
ff.getEncoding(Double.NEGATIVE_INFINITY)));
Assert.assertEquals(1, op.evaluateBinary(8, 8, ff.getEncoding(Double.NEGATIVE_INFINITY),
ff.getEncoding(Double.NEGATIVE_INFINITY)));
Assert.assertEquals(0, op.evaluateBinary(8, 8, ff.getEncoding(Double.POSITIVE_INFINITY),
ff.getEncoding(Double.NaN)));
}
@Test
public void testEvaluateBinaryBigInteger() {
@Test
public void testEvaluateBinaryBigInteger() {
OpBehaviorFloatEqual op = new OpBehaviorFloatEqual();
FloatFormat ff = FloatFormatFactory.getFloatFormat(8);
BigDecimal a = BigDecimal.valueOf(1.234d);
BigDecimal b = BigDecimal.valueOf(-1.234d);
Assert.assertEquals(BigInteger.ONE, op.evaluateBinary(8, 8, ff.getEncoding(a), ff.getEncoding(a)));
Assert.assertEquals(BigInteger.ONE, op.evaluateBinary(8, 8, ff.getEncoding(b), ff.getEncoding(b)));
Assert.assertEquals(BigInteger.ZERO, op.evaluateBinary(8, 8, ff.getEncoding(b), ff.getEncoding(a)));
Assert.assertEquals(
BigInteger.ONE,
op.evaluateBinary(8, 8, ff.getEncoding(FloatFormat.BIG_POSITIVE_INFINITY),
ff.getEncoding(FloatFormat.BIG_POSITIVE_INFINITY)));
Assert.assertEquals(
BigInteger.ZERO,
op.evaluateBinary(8, 8, ff.getEncoding(FloatFormat.BIG_POSITIVE_INFINITY),
ff.getEncoding(FloatFormat.BIG_NEGATIVE_INFINITY)));
Assert.assertEquals(
BigInteger.ONE,
op.evaluateBinary(8, 8, ff.getEncoding(FloatFormat.BIG_NEGATIVE_INFINITY),
ff.getEncoding(FloatFormat.BIG_NEGATIVE_INFINITY)));
Assert.assertEquals(
BigInteger.ZERO,
op.evaluateBinary(8, 8, ff.getEncoding(FloatFormat.BIG_POSITIVE_INFINITY),
ff.getEncoding(FloatFormat.BIG_NaN)));
BigFloat a = ff.getBigFloat(1.234d);
BigFloat b = ff.getBigFloat(-1.234d);
Assert.assertEquals(BigInteger.ONE,
op.evaluateBinary(8, 8, ff.getEncoding(a), ff.getEncoding(a)));
Assert.assertEquals(BigInteger.ONE,
op.evaluateBinary(8, 8, ff.getEncoding(b), ff.getEncoding(b)));
Assert.assertEquals(BigInteger.ZERO,
op.evaluateBinary(8, 8, ff.getEncoding(b), ff.getEncoding(a)));
Assert.assertEquals(BigInteger.ONE, op.evaluateBinary(8, 8,
ff.getBigInfinityEncoding(false), ff.getBigInfinityEncoding(false)));
Assert.assertEquals(BigInteger.ZERO, op.evaluateBinary(8, 8,
ff.getBigInfinityEncoding(false), ff.getBigInfinityEncoding(true)));
Assert.assertEquals(BigInteger.ONE, op.evaluateBinary(8, 8, ff.getBigInfinityEncoding(true),
ff.getBigInfinityEncoding(true)));
Assert.assertEquals(BigInteger.ZERO,
op.evaluateBinary(8, 8, ff.getBigInfinityEncoding(false), ff.getBigNaNEncoding(false)));
}

View file

@ -15,7 +15,6 @@
*/
package ghidra.pcode.opbehavior;
import java.math.BigDecimal;
import java.math.BigInteger;
import org.junit.Assert;
@ -67,25 +66,25 @@ public class OpBehaviorFloatFloat2FloatTest extends AbstractOpBehaviorTest {
FloatFormat ff8 = FloatFormatFactory.getFloatFormat(8);
FloatFormat ff4 = FloatFormatFactory.getFloatFormat(4);
BigInteger a = ff4.getEncoding(BigDecimal.valueOf(1.75d));
BigInteger a = ff4.getEncoding(ff4.getBigFloat(1.75d));
BigInteger result = op.evaluateUnary(8, 4, a);
Assert.assertEquals(BigDecimal.valueOf(1.75d), ff8.getHostFloat(result));
Assert.assertEquals(ff8.getBigFloat(1.75d), ff8.getHostFloat(result));
a = ff4.getEncoding(BigDecimal.valueOf(-1.75d));
a = ff4.getEncoding(ff4.getBigFloat(-1.75d));
result = op.evaluateUnary(8, 4, a);
Assert.assertEquals(BigDecimal.valueOf(-1.75d), ff8.getHostFloat(result));
Assert.assertEquals(ff8.getBigFloat(-1.75d), ff8.getHostFloat(result));
a = ff4.getEncoding(FloatFormat.BIG_POSITIVE_INFINITY);
a = ff4.getEncoding(ff4.getBigInfinity(false));
result = op.evaluateUnary(8, 4, a);
Assert.assertEquals(FloatFormat.BIG_POSITIVE_INFINITY, ff8.getHostFloat(result));
Assert.assertEquals(ff8.getBigInfinity(false), ff8.getHostFloat(result));
a = ff4.getEncoding(FloatFormat.BIG_NEGATIVE_INFINITY);
a = ff4.getEncoding(ff4.getBigInfinity(true));
result = op.evaluateUnary(8, 4, a);
Assert.assertEquals(FloatFormat.BIG_NEGATIVE_INFINITY, ff8.getHostFloat(result));
Assert.assertEquals(ff8.getBigInfinity(true), ff8.getHostFloat(result));
a = ff4.getEncoding(FloatFormat.BIG_NaN);
a = ff4.getEncoding(ff4.getBigNaN(false));
result = op.evaluateUnary(8, 4, a);
Assert.assertEquals(FloatFormat.BIG_NaN, ff8.getHostFloat(result));
Assert.assertEquals(ff8.getBigNaN(false), ff8.getHostFloat(result));
}
}

View file

@ -15,7 +15,6 @@
*/
package ghidra.pcode.opbehavior;
import java.math.BigDecimal;
import java.math.BigInteger;
import org.junit.Assert;
@ -69,31 +68,29 @@ public class OpBehaviorFloatFloorTest extends AbstractOpBehaviorTest {
FloatFormat ff = FloatFormatFactory.getFloatFormat(8);
BigInteger a = ff.getEncoding(BigDecimal.valueOf(2.5d));
BigInteger a = ff.getEncoding(ff.getBigFloat(2.5d));
BigInteger result = op.evaluateUnary(8, 8, a);
Assert.assertEquals(BigDecimal.valueOf(2.0d).stripTrailingZeros(), ff.getHostFloat(result));
Assert.assertEquals(ff.getBigFloat(2.0d), ff.getHostFloat(result));
a = ff.getEncoding(BigDecimal.valueOf(-2.0d));
a = ff.getEncoding(ff.getBigFloat(-2.0d));
result = op.evaluateUnary(8, 8, a);
Assert.assertEquals(BigDecimal.valueOf(-2.0d).stripTrailingZeros(),
ff.getHostFloat(result));
Assert.assertEquals(ff.getBigFloat(-2.0d), ff.getHostFloat(result));
a = ff.getEncoding(BigDecimal.valueOf(-2.5d));
a = ff.getEncoding(ff.getBigFloat(-2.5d));
result = op.evaluateUnary(8, 8, a);
Assert.assertEquals(BigDecimal.valueOf(-3.0d).stripTrailingZeros(),
ff.getHostFloat(result));
Assert.assertEquals(ff.getBigFloat(-3.0d), ff.getHostFloat(result));
a = ff.getEncoding(FloatFormat.BIG_POSITIVE_INFINITY);
a = ff.getBigInfinityEncoding(false);
result = op.evaluateUnary(8, 8, a);
Assert.assertEquals(FloatFormat.BIG_POSITIVE_INFINITY, ff.getHostFloat(result));
Assert.assertEquals(ff.getBigInfinity(false), ff.getHostFloat(result));
a = ff.getEncoding(FloatFormat.BIG_NEGATIVE_INFINITY);
a = ff.getBigInfinityEncoding(true);
result = op.evaluateUnary(8, 8, a);
Assert.assertEquals(FloatFormat.BIG_NEGATIVE_INFINITY, ff.getHostFloat(result));
Assert.assertEquals(ff.getBigInfinity(true), ff.getHostFloat(result));
a = ff.getEncoding(FloatFormat.BIG_NaN);
a = ff.getBigNaNEncoding(false);
result = op.evaluateUnary(8, 8, a);
Assert.assertEquals(FloatFormat.BIG_NaN, ff.getHostFloat(result));
Assert.assertEquals(ff.getBigNaN(false), ff.getHostFloat(result));
}
}

View file

@ -15,9 +15,8 @@
*/
package ghidra.pcode.opbehavior;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import org.junit.Assert;
@ -68,22 +67,20 @@ public class OpBehaviorFloatInt2FloatTest extends AbstractOpBehaviorTest {
BigInteger result = op.evaluateUnary(4, 4, BigInteger.valueOf(2));
assertTrue(result.compareTo(limit) < 0);// verify that only 4-bytes are used
Assert.assertEquals(BigDecimal.valueOf(2.0d).stripTrailingZeros(), ff.getHostFloat(result));
Assert.assertEquals(ff.getBigFloat(2.0d), ff.getHostFloat(result));
result = op.evaluateUnary(4, 4, BigInteger.valueOf(-2));
assertTrue(result.compareTo(limit) < 0);// verify that only 4-bytes are used
Assert.assertEquals(BigDecimal.valueOf(-2.0d).stripTrailingZeros(),
ff.getHostFloat(result));
Assert.assertEquals(ff.getBigFloat(-2.0d), ff.getHostFloat(result));
result = op.evaluateUnary(4, 4, BigInteger.ZERO);
assertTrue(result.compareTo(limit) < 0);// verify that only 4-bytes are used
Assert.assertEquals(BigDecimal.ZERO, ff.getHostFloat(result));
Assert.assertEquals(ff.getBigZero(false), ff.getHostFloat(result));
BigInteger NEG_ONE = Utils.bytesToBigInteger(
new byte[] { (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff }, 4, false, false);
result = op.evaluateUnary(4, 4, NEG_ONE);
Assert.assertEquals(BigDecimal.valueOf(-1.0d).stripTrailingZeros(),
ff.getHostFloat(result));
Assert.assertEquals(ff.getBigFloat(-1.0d), ff.getHostFloat(result));
}

View file

@ -15,14 +15,12 @@
*/
package ghidra.pcode.opbehavior;
import java.math.BigDecimal;
import java.math.BigInteger;
import org.junit.Assert;
import org.junit.Test;
import ghidra.pcode.floatformat.FloatFormat;
import ghidra.pcode.floatformat.FloatFormatFactory;
import ghidra.pcode.floatformat.*;
public class OpBehaviorFloatLessEqualTest extends AbstractOpBehaviorTest {
@ -30,92 +28,80 @@ public class OpBehaviorFloatLessEqualTest extends AbstractOpBehaviorTest {
super();
}
@Test
public void testEvaluateBinaryLong() {
@Test
public void testEvaluateBinaryLong() {
OpBehaviorFloatLessEqual op = new OpBehaviorFloatLessEqual();
FloatFormat ff = FloatFormatFactory.getFloatFormat(8);
Assert.assertEquals(1, op.evaluateBinary(8, 8, ff.getEncoding(1.234), ff.getEncoding(1.234)));
Assert.assertEquals(1, op.evaluateBinary(8, 8, ff.getEncoding(-1.234), ff.getEncoding(-1.234)));
Assert.assertEquals(1,
op.evaluateBinary(8, 8, ff.getEncoding(1.234), ff.getEncoding(1.234)));
Assert.assertEquals(1,
op.evaluateBinary(8, 8, ff.getEncoding(-1.234), ff.getEncoding(-1.234)));
Assert.assertEquals(0, op.evaluateBinary(8, 8, ff.getEncoding(1.234), ff.getEncoding(-1.234)));
Assert.assertEquals(0,
op.evaluateBinary(8, 8, ff.getEncoding(1.234), ff.getEncoding(-1.234)));
Assert.assertEquals(0, op.evaluateBinary(8, 8, ff.getEncoding(0), ff.getEncoding(-1.234)));
Assert.assertEquals(1, op.evaluateBinary(8, 8, ff.getEncoding(0), ff.getEncoding(1.234)));
Assert.assertEquals(1, op.evaluateBinary(8, 8, ff.getEncoding(-1.234), ff.getEncoding(1.234)));
Assert.assertEquals(1,
op.evaluateBinary(8, 8, ff.getEncoding(-1.234), ff.getEncoding(1.234)));
Assert.assertEquals(
0,
op.evaluateBinary(8, 8, ff.getEncoding(Double.POSITIVE_INFINITY), ff.getEncoding(1.234)));
Assert.assertEquals(
1,
op.evaluateBinary(8, 8, ff.getEncoding(Double.NEGATIVE_INFINITY), ff.getEncoding(1.234)));
Assert.assertEquals(
1,
op.evaluateBinary(8, 8, ff.getEncoding(1.234), ff.getEncoding(Double.POSITIVE_INFINITY)));
Assert.assertEquals(
0,
op.evaluateBinary(8, 8, ff.getEncoding(1.234), ff.getEncoding(Double.NEGATIVE_INFINITY)));
Assert.assertEquals(0, op.evaluateBinary(8, 8, ff.getEncoding(Double.POSITIVE_INFINITY),
ff.getEncoding(1.234)));
Assert.assertEquals(1, op.evaluateBinary(8, 8, ff.getEncoding(Double.NEGATIVE_INFINITY),
ff.getEncoding(1.234)));
Assert.assertEquals(1, op.evaluateBinary(8, 8, ff.getEncoding(1.234),
ff.getEncoding(Double.POSITIVE_INFINITY)));
Assert.assertEquals(0, op.evaluateBinary(8, 8, ff.getEncoding(1.234),
ff.getEncoding(Double.NEGATIVE_INFINITY)));
Assert.assertEquals(
1,
op.evaluateBinary(8, 8, ff.getEncoding(Double.POSITIVE_INFINITY),
ff.getEncoding(Double.POSITIVE_INFINITY)));
Assert.assertEquals(
1,
op.evaluateBinary(8, 8, ff.getEncoding(Double.NEGATIVE_INFINITY),
ff.getEncoding(Double.POSITIVE_INFINITY)));
Assert.assertEquals(1, op.evaluateBinary(8, 8, ff.getEncoding(Double.POSITIVE_INFINITY),
ff.getEncoding(Double.POSITIVE_INFINITY)));
Assert.assertEquals(1, op.evaluateBinary(8, 8, ff.getEncoding(Double.NEGATIVE_INFINITY),
ff.getEncoding(Double.POSITIVE_INFINITY)));
}
@Test
public void testEvaluateBinaryBigInteger() {
@Test
public void testEvaluateBinaryBigInteger() {
OpBehaviorFloatLessEqual op = new OpBehaviorFloatLessEqual();
FloatFormat ff = FloatFormatFactory.getFloatFormat(8);
BigDecimal a = BigDecimal.valueOf(1.234d);
BigDecimal b = BigDecimal.valueOf(-1.234d);
Assert.assertEquals(BigInteger.ONE, op.evaluateBinary(8, 8, ff.getEncoding(a), ff.getEncoding(a)));
Assert.assertEquals(BigInteger.ONE, op.evaluateBinary(8, 8, ff.getEncoding(b), ff.getEncoding(b)));
Assert.assertEquals(BigInteger.ZERO, op.evaluateBinary(8, 8, ff.getEncoding(a), ff.getEncoding(b)));
Assert.assertEquals(BigInteger.ZERO,
op.evaluateBinary(8, 8, ff.getEncoding(BigDecimal.ZERO), ff.getEncoding(b)));
BigFloat a = ff.getBigFloat(1.234d);
BigFloat b = ff.getBigFloat(-1.234d);
Assert.assertEquals(BigInteger.ONE,
op.evaluateBinary(8, 8, ff.getEncoding(BigDecimal.ZERO), ff.getEncoding(a)));
Assert.assertEquals(BigInteger.ONE, op.evaluateBinary(8, 8, ff.getEncoding(b), ff.getEncoding(a)));
op.evaluateBinary(8, 8, ff.getEncoding(a), ff.getEncoding(a)));
Assert.assertEquals(BigInteger.ONE,
op.evaluateBinary(8, 8, ff.getEncoding(b), ff.getEncoding(b)));
Assert.assertEquals(
BigInteger.ZERO,
op.evaluateBinary(8, 8, ff.getEncoding(FloatFormat.BIG_POSITIVE_INFINITY),
ff.getEncoding(a)));
Assert.assertEquals(
BigInteger.ONE,
op.evaluateBinary(8, 8, ff.getEncoding(FloatFormat.BIG_NEGATIVE_INFINITY),
ff.getEncoding(a)));
Assert.assertEquals(
BigInteger.ONE,
op.evaluateBinary(8, 8, ff.getEncoding(a),
ff.getEncoding(FloatFormat.BIG_POSITIVE_INFINITY)));
Assert.assertEquals(
BigInteger.ZERO,
op.evaluateBinary(8, 8, ff.getEncoding(a),
ff.getEncoding(FloatFormat.BIG_NEGATIVE_INFINITY)));
Assert.assertEquals(BigInteger.ZERO,
op.evaluateBinary(8, 8, ff.getEncoding(a), ff.getEncoding(b)));
Assert.assertEquals(BigInteger.ZERO,
op.evaluateBinary(8, 8, ff.getBigZeroEncoding(false), ff.getEncoding(b)));
Assert.assertEquals(
BigInteger.ONE,
op.evaluateBinary(8, 8, ff.getEncoding(FloatFormat.BIG_POSITIVE_INFINITY),
ff.getEncoding(FloatFormat.BIG_POSITIVE_INFINITY)));
Assert.assertEquals(
BigInteger.ONE,
op.evaluateBinary(8, 8, ff.getEncoding(FloatFormat.BIG_NEGATIVE_INFINITY),
ff.getEncoding(FloatFormat.BIG_POSITIVE_INFINITY)));
Assert.assertEquals(BigInteger.ONE,
op.evaluateBinary(8, 8, ff.getBigZeroEncoding(false), ff.getEncoding(a)));
Assert.assertEquals(BigInteger.ONE,
op.evaluateBinary(8, 8, ff.getEncoding(b), ff.getEncoding(a)));
Assert.assertEquals(BigInteger.ZERO,
op.evaluateBinary(8, 8, ff.getBigInfinityEncoding(false), ff.getEncoding(a)));
Assert.assertEquals(BigInteger.ONE,
op.evaluateBinary(8, 8, ff.getBigInfinityEncoding(true), ff.getEncoding(a)));
Assert.assertEquals(BigInteger.ONE,
op.evaluateBinary(8, 8, ff.getEncoding(a), ff.getBigInfinityEncoding(false)));
Assert.assertEquals(BigInteger.ZERO,
op.evaluateBinary(8, 8, ff.getEncoding(a), ff.getBigInfinityEncoding(true)));
Assert.assertEquals(BigInteger.ONE, op.evaluateBinary(8, 8,
ff.getBigInfinityEncoding(false), ff.getBigInfinityEncoding(false)));
Assert.assertEquals(BigInteger.ONE, op.evaluateBinary(8, 8, ff.getBigInfinityEncoding(true),
ff.getBigInfinityEncoding(false)));
}

View file

@ -15,14 +15,12 @@
*/
package ghidra.pcode.opbehavior;
import java.math.BigDecimal;
import java.math.BigInteger;
import org.junit.Assert;
import org.junit.Test;
import ghidra.pcode.floatformat.FloatFormat;
import ghidra.pcode.floatformat.FloatFormatFactory;
import ghidra.pcode.floatformat.*;
public class OpBehaviorFloatLessTest extends AbstractOpBehaviorTest {
@ -30,89 +28,77 @@ public class OpBehaviorFloatLessTest extends AbstractOpBehaviorTest {
super();
}
@Test
public void testEvaluateBinaryLong() {
@Test
public void testEvaluateBinaryLong() {
OpBehaviorFloatLess op = new OpBehaviorFloatLess();
FloatFormat ff = FloatFormatFactory.getFloatFormat(8);
Assert.assertEquals(0, op.evaluateBinary(8, 8, ff.getEncoding(1.234), ff.getEncoding(1.234)));
Assert.assertEquals(0, op.evaluateBinary(8, 8, ff.getEncoding(-1.234), ff.getEncoding(-1.234)));
Assert.assertEquals(0, op.evaluateBinary(8, 8, ff.getEncoding(1.234), ff.getEncoding(-1.234)));
Assert.assertEquals(0,
op.evaluateBinary(8, 8, ff.getEncoding(1.234), ff.getEncoding(1.234)));
Assert.assertEquals(0,
op.evaluateBinary(8, 8, ff.getEncoding(-1.234), ff.getEncoding(-1.234)));
Assert.assertEquals(0,
op.evaluateBinary(8, 8, ff.getEncoding(1.234), ff.getEncoding(-1.234)));
Assert.assertEquals(0, op.evaluateBinary(8, 8, ff.getEncoding(0), ff.getEncoding(-1.234)));
Assert.assertEquals(1, op.evaluateBinary(8, 8, ff.getEncoding(0), ff.getEncoding(1.234)));
Assert.assertEquals(1, op.evaluateBinary(8, 8, ff.getEncoding(-1.234), ff.getEncoding(1.234)));
Assert.assertEquals(1,
op.evaluateBinary(8, 8, ff.getEncoding(-1.234), ff.getEncoding(1.234)));
Assert.assertEquals(
0,
op.evaluateBinary(8, 8, ff.getEncoding(Double.POSITIVE_INFINITY), ff.getEncoding(1.234)));
Assert.assertEquals(
1,
op.evaluateBinary(8, 8, ff.getEncoding(Double.NEGATIVE_INFINITY), ff.getEncoding(1.234)));
Assert.assertEquals(
1,
op.evaluateBinary(8, 8, ff.getEncoding(1.234), ff.getEncoding(Double.POSITIVE_INFINITY)));
Assert.assertEquals(
0,
op.evaluateBinary(8, 8, ff.getEncoding(1.234), ff.getEncoding(Double.NEGATIVE_INFINITY)));
Assert.assertEquals(0, op.evaluateBinary(8, 8, ff.getEncoding(Double.POSITIVE_INFINITY),
ff.getEncoding(1.234)));
Assert.assertEquals(1, op.evaluateBinary(8, 8, ff.getEncoding(Double.NEGATIVE_INFINITY),
ff.getEncoding(1.234)));
Assert.assertEquals(1, op.evaluateBinary(8, 8, ff.getEncoding(1.234),
ff.getEncoding(Double.POSITIVE_INFINITY)));
Assert.assertEquals(0, op.evaluateBinary(8, 8, ff.getEncoding(1.234),
ff.getEncoding(Double.NEGATIVE_INFINITY)));
Assert.assertEquals(
0,
op.evaluateBinary(8, 8, ff.getEncoding(Double.POSITIVE_INFINITY),
ff.getEncoding(Double.POSITIVE_INFINITY)));
Assert.assertEquals(
1,
op.evaluateBinary(8, 8, ff.getEncoding(Double.NEGATIVE_INFINITY),
ff.getEncoding(Double.POSITIVE_INFINITY)));
Assert.assertEquals(0, op.evaluateBinary(8, 8, ff.getEncoding(Double.POSITIVE_INFINITY),
ff.getEncoding(Double.POSITIVE_INFINITY)));
Assert.assertEquals(1, op.evaluateBinary(8, 8, ff.getEncoding(Double.NEGATIVE_INFINITY),
ff.getEncoding(Double.POSITIVE_INFINITY)));
}
@Test
public void testEvaluateBinaryBigInteger() {
@Test
public void testEvaluateBinaryBigInteger() {
OpBehaviorFloatLess op = new OpBehaviorFloatLess();
FloatFormat ff = FloatFormatFactory.getFloatFormat(8);
BigDecimal a = BigDecimal.valueOf(1.234d);
BigDecimal b = BigDecimal.valueOf(-1.234d);
BigFloat a = ff.getBigFloat(1.234d);
BigFloat b = ff.getBigFloat(-1.234d);
Assert.assertEquals(BigInteger.ZERO, op.evaluateBinary(8, 8, ff.getEncoding(a), ff.getEncoding(a)));
Assert.assertEquals(BigInteger.ZERO, op.evaluateBinary(8, 8, ff.getEncoding(b), ff.getEncoding(b)));
Assert.assertEquals(BigInteger.ZERO, op.evaluateBinary(8, 8, ff.getEncoding(a), ff.getEncoding(b)));
Assert.assertEquals(BigInteger.ZERO,
op.evaluateBinary(8, 8, ff.getEncoding(BigDecimal.ZERO), ff.getEncoding(b)));
op.evaluateBinary(8, 8, ff.getEncoding(a), ff.getEncoding(a)));
Assert.assertEquals(BigInteger.ZERO,
op.evaluateBinary(8, 8, ff.getEncoding(b), ff.getEncoding(b)));
Assert.assertEquals(BigInteger.ZERO,
op.evaluateBinary(8, 8, ff.getEncoding(a), ff.getEncoding(b)));
Assert.assertEquals(BigInteger.ZERO,
op.evaluateBinary(8, 8, ff.getBigZeroEncoding(false), ff.getEncoding(b)));
Assert.assertEquals(BigInteger.ONE,
op.evaluateBinary(8, 8, ff.getEncoding(BigDecimal.ZERO), ff.getEncoding(a)));
Assert.assertEquals(BigInteger.ONE, op.evaluateBinary(8, 8, ff.getEncoding(b), ff.getEncoding(a)));
op.evaluateBinary(8, 8, ff.getBigZeroEncoding(false), ff.getEncoding(a)));
Assert.assertEquals(BigInteger.ONE,
op.evaluateBinary(8, 8, ff.getEncoding(b), ff.getEncoding(a)));
Assert.assertEquals(
BigInteger.ZERO,
op.evaluateBinary(8, 8, ff.getEncoding(FloatFormat.BIG_POSITIVE_INFINITY),
ff.getEncoding(a)));
Assert.assertEquals(
BigInteger.ONE,
op.evaluateBinary(8, 8, ff.getEncoding(FloatFormat.BIG_NEGATIVE_INFINITY),
ff.getEncoding(a)));
Assert.assertEquals(
BigInteger.ONE,
op.evaluateBinary(8, 8, ff.getEncoding(a),
ff.getEncoding(FloatFormat.BIG_POSITIVE_INFINITY)));
Assert.assertEquals(
BigInteger.ZERO,
op.evaluateBinary(8, 8, ff.getEncoding(a),
ff.getEncoding(FloatFormat.BIG_NEGATIVE_INFINITY)));
Assert.assertEquals(BigInteger.ZERO,
op.evaluateBinary(8, 8, ff.getBigInfinityEncoding(false), ff.getEncoding(a)));
Assert.assertEquals(BigInteger.ONE,
op.evaluateBinary(8, 8, ff.getBigInfinityEncoding(true), ff.getEncoding(a)));
Assert.assertEquals(BigInteger.ONE,
op.evaluateBinary(8, 8, ff.getEncoding(a), ff.getBigInfinityEncoding(false)));
Assert.assertEquals(BigInteger.ZERO,
op.evaluateBinary(8, 8, ff.getEncoding(a), ff.getBigInfinityEncoding(true)));
Assert.assertEquals(
BigInteger.ZERO,
op.evaluateBinary(8, 8, ff.getEncoding(FloatFormat.BIG_POSITIVE_INFINITY),
ff.getEncoding(FloatFormat.BIG_POSITIVE_INFINITY)));
Assert.assertEquals(
BigInteger.ONE,
op.evaluateBinary(8, 8, ff.getEncoding(FloatFormat.BIG_NEGATIVE_INFINITY),
ff.getEncoding(FloatFormat.BIG_POSITIVE_INFINITY)));
Assert.assertEquals(BigInteger.ZERO, op.evaluateBinary(8, 8,
ff.getBigInfinityEncoding(false), ff.getBigInfinityEncoding(false)));
Assert.assertEquals(BigInteger.ONE, op.evaluateBinary(8, 8, ff.getBigInfinityEncoding(true),
ff.getBigInfinityEncoding(false)));
}

View file

@ -15,7 +15,6 @@
*/
package ghidra.pcode.opbehavior;
import java.math.BigDecimal;
import java.math.BigInteger;
import org.junit.Assert;
@ -62,22 +61,22 @@ public class OpBehaviorFloatMultTest extends AbstractOpBehaviorTest {
FloatFormat ff = FloatFormatFactory.getFloatFormat(8);
BigInteger a = ff.getEncoding(BigDecimal.valueOf(2.5d));
BigInteger b = ff.getEncoding(BigDecimal.valueOf(1.5d));
BigInteger a = ff.getEncoding(ff.getBigFloat(2.5d));
BigInteger b = ff.getEncoding(ff.getBigFloat(1.5d));
BigInteger result = op.evaluateBinary(8, 8, a, b);
Assert.assertEquals(BigDecimal.valueOf(3.75d), ff.getHostFloat(result));
Assert.assertEquals(ff.getBigFloat(3.75d), ff.getHostFloat(result));
b = ff.getEncoding(FloatFormat.BIG_POSITIVE_INFINITY);
b = ff.getBigInfinityEncoding(false);
result = op.evaluateBinary(8, 8, a, b);
Assert.assertEquals(FloatFormat.BIG_POSITIVE_INFINITY, ff.getHostFloat(result));
Assert.assertEquals(ff.getBigInfinity(false), ff.getHostFloat(result));
a = ff.getEncoding(FloatFormat.BIG_NEGATIVE_INFINITY);
a = ff.getBigInfinityEncoding(true);
result = op.evaluateBinary(8, 8, a, b);
Assert.assertEquals(FloatFormat.BIG_NEGATIVE_INFINITY, ff.getHostFloat(result));
Assert.assertEquals(ff.getBigInfinity(true), ff.getHostFloat(result));
b = ff.getEncoding(FloatFormat.BIG_NaN);
b = ff.getBigNaNEncoding(false);
result = op.evaluateBinary(8, 8, a, b);
Assert.assertEquals(FloatFormat.BIG_NaN, ff.getHostFloat(result));
Assert.assertEquals(ff.getBigNaN(false), ff.getHostFloat(result));
}
}

View file

@ -15,7 +15,6 @@
*/
package ghidra.pcode.opbehavior;
import java.math.BigDecimal;
import java.math.BigInteger;
import org.junit.Assert;
@ -30,8 +29,8 @@ public class OpBehaviorFloatNanTest extends AbstractOpBehaviorTest {
super();
}
@Test
public void testEvaluateBinaryLong() {
@Test
public void testEvaluateBinaryLong() {
OpBehaviorFloatNan op = new OpBehaviorFloatNan();
@ -42,17 +41,17 @@ public class OpBehaviorFloatNanTest extends AbstractOpBehaviorTest {
Assert.assertEquals(0, op.evaluateUnary(1, 8, ff.getEncoding(1.234)));
}
@Test
public void testEvaluateBinaryBigInteger() {
@Test
public void testEvaluateBinaryBigInteger() {
OpBehaviorFloatNan op = new OpBehaviorFloatNan();
FloatFormat ff = FloatFormatFactory.getFloatFormat(8);
Assert.assertEquals(BigInteger.ONE, op.evaluateUnary(1, 8, ff.getEncoding(FloatFormat.BIG_NaN)));
Assert.assertEquals(BigInteger.ZERO, op.evaluateUnary(1, 8, ff.getEncoding(BigDecimal.ZERO)));
Assert.assertEquals(BigInteger.ONE, op.evaluateUnary(1, 8, ff.getBigNaNEncoding(false)));
Assert.assertEquals(BigInteger.ZERO, op.evaluateUnary(1, 8, ff.getBigZeroEncoding(false)));
Assert.assertEquals(BigInteger.ZERO,
op.evaluateUnary(1, 8, ff.getEncoding(BigDecimal.valueOf(1.234d))));
op.evaluateUnary(1, 8, ff.getEncoding(ff.getBigFloat(1.234d))));
}

View file

@ -15,7 +15,6 @@
*/
package ghidra.pcode.opbehavior;
import java.math.BigDecimal;
import java.math.BigInteger;
import org.junit.Assert;
@ -65,25 +64,25 @@ public class OpBehaviorFloatNegTest extends AbstractOpBehaviorTest {
FloatFormat ff = FloatFormatFactory.getFloatFormat(8);
BigInteger a = ff.getEncoding(BigDecimal.valueOf(2.5d));
BigInteger a = ff.getEncoding(ff.getBigFloat(2.5d));
BigInteger result = op.evaluateUnary(8, 8, a);
Assert.assertEquals(BigDecimal.valueOf(-2.5d), ff.getHostFloat(result));
Assert.assertEquals(ff.getBigFloat(-2.5d), ff.getHostFloat(result));
a = ff.getEncoding(BigDecimal.valueOf(-2.5d));
a = ff.getEncoding(ff.getBigFloat(-2.5d));
result = op.evaluateUnary(8, 8, a);
Assert.assertEquals(BigDecimal.valueOf(2.5d), ff.getHostFloat(result));
Assert.assertEquals(ff.getBigFloat(2.5d), ff.getHostFloat(result));
a = ff.getEncoding(FloatFormat.BIG_POSITIVE_INFINITY);
a = ff.getBigInfinityEncoding(false);
result = op.evaluateUnary(8, 8, a);
Assert.assertEquals(FloatFormat.BIG_NEGATIVE_INFINITY, ff.getHostFloat(result));
Assert.assertEquals(ff.getBigInfinity(true), ff.getHostFloat(result));
a = ff.getEncoding(FloatFormat.BIG_NEGATIVE_INFINITY);
a = ff.getBigInfinityEncoding(true);
result = op.evaluateUnary(8, 8, a);
Assert.assertEquals(FloatFormat.BIG_POSITIVE_INFINITY, ff.getHostFloat(result));
Assert.assertEquals(ff.getBigInfinity(false), ff.getHostFloat(result));
a = ff.getEncoding(FloatFormat.BIG_NaN);
a = ff.getBigNaNEncoding(false);
result = op.evaluateUnary(8, 8, a);
Assert.assertEquals(FloatFormat.BIG_NaN, ff.getHostFloat(result));
Assert.assertEquals(ff.getBigNaN(false), ff.getHostFloat(result));
}
}

View file

@ -15,14 +15,12 @@
*/
package ghidra.pcode.opbehavior;
import java.math.BigDecimal;
import java.math.BigInteger;
import org.junit.Assert;
import org.junit.Test;
import ghidra.pcode.floatformat.FloatFormat;
import ghidra.pcode.floatformat.FloatFormatFactory;
import ghidra.pcode.floatformat.*;
public class OpBehaviorFloatNotEqualTest extends AbstractOpBehaviorTest {
@ -30,63 +28,53 @@ public class OpBehaviorFloatNotEqualTest extends AbstractOpBehaviorTest {
super();
}
@Test
public void testEvaluateBinaryLong() {
@Test
public void testEvaluateBinaryLong() {
OpBehaviorFloatNotEqual op = new OpBehaviorFloatNotEqual();
FloatFormat ff = FloatFormatFactory.getFloatFormat(8);
Assert.assertEquals(0, op.evaluateBinary(1, 8, ff.getEncoding(1.234), ff.getEncoding(1.234)));
Assert.assertEquals(0, op.evaluateBinary(1, 8, ff.getEncoding(-1.234), ff.getEncoding(-1.234)));
Assert.assertEquals(1, op.evaluateBinary(1, 8, ff.getEncoding(-1.234), ff.getEncoding(1.234)));
Assert.assertEquals(
0,
op.evaluateBinary(1, 8, ff.getEncoding(Double.POSITIVE_INFINITY),
ff.getEncoding(Double.POSITIVE_INFINITY)));
Assert.assertEquals(
1,
op.evaluateBinary(1, 8, ff.getEncoding(Double.POSITIVE_INFINITY),
ff.getEncoding(Double.NEGATIVE_INFINITY)));
Assert.assertEquals(
0,
op.evaluateBinary(1, 8, ff.getEncoding(Double.NEGATIVE_INFINITY),
ff.getEncoding(Double.NEGATIVE_INFINITY)));
Assert.assertEquals(
1,
op.evaluateBinary(1, 8, ff.getEncoding(Double.POSITIVE_INFINITY),
ff.getEncoding(Double.NaN)));
Assert.assertEquals(0,
op.evaluateBinary(1, 8, ff.getEncoding(1.234), ff.getEncoding(1.234)));
Assert.assertEquals(0,
op.evaluateBinary(1, 8, ff.getEncoding(-1.234), ff.getEncoding(-1.234)));
Assert.assertEquals(1,
op.evaluateBinary(1, 8, ff.getEncoding(-1.234), ff.getEncoding(1.234)));
Assert.assertEquals(0, op.evaluateBinary(1, 8, ff.getEncoding(Double.POSITIVE_INFINITY),
ff.getEncoding(Double.POSITIVE_INFINITY)));
Assert.assertEquals(1, op.evaluateBinary(1, 8, ff.getEncoding(Double.POSITIVE_INFINITY),
ff.getEncoding(Double.NEGATIVE_INFINITY)));
Assert.assertEquals(0, op.evaluateBinary(1, 8, ff.getEncoding(Double.NEGATIVE_INFINITY),
ff.getEncoding(Double.NEGATIVE_INFINITY)));
Assert.assertEquals(1, op.evaluateBinary(1, 8, ff.getEncoding(Double.POSITIVE_INFINITY),
ff.getEncoding(Double.NaN)));
}
@Test
public void testEvaluateBinaryBigInteger() {
@Test
public void testEvaluateBinaryBigInteger() {
OpBehaviorFloatNotEqual op = new OpBehaviorFloatNotEqual();
FloatFormat ff = FloatFormatFactory.getFloatFormat(8);
BigDecimal a = BigDecimal.valueOf(1.234d);
BigDecimal b = BigDecimal.valueOf(-1.234d);
Assert.assertEquals(BigInteger.ZERO, op.evaluateBinary(1, 8, ff.getEncoding(a), ff.getEncoding(a)));
Assert.assertEquals(BigInteger.ZERO, op.evaluateBinary(1, 8, ff.getEncoding(b), ff.getEncoding(b)));
Assert.assertEquals(BigInteger.ONE, op.evaluateBinary(1, 8, ff.getEncoding(b), ff.getEncoding(a)));
Assert.assertEquals(
BigInteger.ZERO,
op.evaluateBinary(1, 8, ff.getEncoding(FloatFormat.BIG_POSITIVE_INFINITY),
ff.getEncoding(FloatFormat.BIG_POSITIVE_INFINITY)));
Assert.assertEquals(
BigInteger.ONE,
op.evaluateBinary(1, 8, ff.getEncoding(FloatFormat.BIG_POSITIVE_INFINITY),
ff.getEncoding(FloatFormat.BIG_NEGATIVE_INFINITY)));
Assert.assertEquals(
BigInteger.ZERO,
op.evaluateBinary(1, 8, ff.getEncoding(FloatFormat.BIG_NEGATIVE_INFINITY),
ff.getEncoding(FloatFormat.BIG_NEGATIVE_INFINITY)));
Assert.assertEquals(
BigInteger.ONE,
op.evaluateBinary(1, 8, ff.getEncoding(FloatFormat.BIG_POSITIVE_INFINITY),
ff.getEncoding(FloatFormat.BIG_NaN)));
BigFloat a = ff.getBigFloat(1.234d);
BigFloat b = ff.getBigFloat(-1.234d);
Assert.assertEquals(BigInteger.ZERO,
op.evaluateBinary(1, 8, ff.getEncoding(a), ff.getEncoding(a)));
Assert.assertEquals(BigInteger.ZERO,
op.evaluateBinary(1, 8, ff.getEncoding(b), ff.getEncoding(b)));
Assert.assertEquals(BigInteger.ONE,
op.evaluateBinary(1, 8, ff.getEncoding(b), ff.getEncoding(a)));
Assert.assertEquals(BigInteger.ZERO, op.evaluateBinary(1, 8,
ff.getBigInfinityEncoding(false), ff.getBigInfinityEncoding(false)));
Assert.assertEquals(BigInteger.ONE, op.evaluateBinary(1, 8,
ff.getBigInfinityEncoding(false), ff.getBigInfinityEncoding(true)));
Assert.assertEquals(BigInteger.ZERO, op.evaluateBinary(1, 8,
ff.getBigInfinityEncoding(true), ff.getBigInfinityEncoding(true)));
Assert.assertEquals(BigInteger.ONE,
op.evaluateBinary(1, 8, ff.getBigInfinityEncoding(false), ff.getBigNaNEncoding(false)));
}

View file

@ -15,7 +15,6 @@
*/
package ghidra.pcode.opbehavior;
import java.math.BigDecimal;
import java.math.BigInteger;
import org.junit.Assert;
@ -81,44 +80,41 @@ public class OpBehaviorFloatRoundTest extends AbstractOpBehaviorTest {
FloatFormat ff = FloatFormatFactory.getFloatFormat(8);
BigInteger a = ff.getEncoding(BigDecimal.valueOf(2.5d));
BigInteger a = ff.getEncoding(ff.getBigFloat(2.5d));
BigInteger result = op.evaluateUnary(8, 8, a);
Assert.assertEquals(BigDecimal.valueOf(3.0d).stripTrailingZeros(), ff.getHostFloat(result));
Assert.assertEquals(ff.getBigFloat(3.0d), ff.getHostFloat(result));
a = ff.getEncoding(BigDecimal.valueOf(2.25d));
a = ff.getEncoding(ff.getBigFloat(2.25d));
result = op.evaluateUnary(8, 8, a);
Assert.assertEquals(BigDecimal.valueOf(2.0d).stripTrailingZeros(), ff.getHostFloat(result));
Assert.assertEquals(ff.getBigFloat(2.0d), ff.getHostFloat(result));
a = ff.getEncoding(BigDecimal.valueOf(2.75d));
a = ff.getEncoding(ff.getBigFloat(2.75d));
result = op.evaluateUnary(8, 8, a);
Assert.assertEquals(BigDecimal.valueOf(3.0d).stripTrailingZeros(), ff.getHostFloat(result));
Assert.assertEquals(ff.getBigFloat(3.0d), ff.getHostFloat(result));
a = ff.getEncoding(BigDecimal.valueOf(-2.5d));
a = ff.getEncoding(ff.getBigFloat(-2.5d));
result = op.evaluateUnary(8, 8, a);
Assert.assertEquals(BigDecimal.valueOf(-2.0d).stripTrailingZeros(),
ff.getHostFloat(result));
Assert.assertEquals(ff.getBigFloat(-2.0d), ff.getHostFloat(result));
a = ff.getEncoding(BigDecimal.valueOf(-2.25d));
a = ff.getEncoding(ff.getBigFloat(-2.25d));
result = op.evaluateUnary(8, 8, a);
Assert.assertEquals(BigDecimal.valueOf(-2.0d).stripTrailingZeros(),
ff.getHostFloat(result));
Assert.assertEquals(ff.getBigFloat(-2.0d), ff.getHostFloat(result));
a = ff.getEncoding(BigDecimal.valueOf(-2.75d));
a = ff.getEncoding(ff.getBigFloat(-2.75d));
result = op.evaluateUnary(8, 8, a);
Assert.assertEquals(BigDecimal.valueOf(-3.0d).stripTrailingZeros(),
ff.getHostFloat(result));
Assert.assertEquals(ff.getBigFloat(-3.0d), ff.getHostFloat(result));
a = ff.getEncoding(FloatFormat.BIG_POSITIVE_INFINITY);
a = ff.getBigInfinityEncoding(false);
result = op.evaluateUnary(8, 8, a);
Assert.assertEquals(FloatFormat.BIG_POSITIVE_INFINITY, ff.getHostFloat(result));
Assert.assertEquals(ff.getBigInfinity(false), ff.getHostFloat(result));
a = ff.getEncoding(FloatFormat.BIG_NEGATIVE_INFINITY);
a = ff.getBigInfinityEncoding(true);
result = op.evaluateUnary(8, 8, a);
Assert.assertEquals(FloatFormat.BIG_NEGATIVE_INFINITY, ff.getHostFloat(result));
Assert.assertEquals(ff.getBigInfinity(true), ff.getHostFloat(result));
a = ff.getEncoding(FloatFormat.BIG_NaN);
a = ff.getBigNaNEncoding(false);
result = op.evaluateUnary(8, 8, a);
Assert.assertEquals(FloatFormat.BIG_NaN, ff.getHostFloat(result));
Assert.assertEquals(ff.getBigNaN(false), ff.getHostFloat(result));
}

View file

@ -15,14 +15,12 @@
*/
package ghidra.pcode.opbehavior;
import java.math.BigDecimal;
import java.math.BigInteger;
import org.junit.Assert;
import org.junit.Test;
import ghidra.pcode.floatformat.FloatFormat;
import ghidra.pcode.floatformat.FloatFormatFactory;
import ghidra.pcode.floatformat.*;
public class OpBehaviorFloatSqrtTest extends AbstractOpBehaviorTest {
@ -30,8 +28,8 @@ public class OpBehaviorFloatSqrtTest extends AbstractOpBehaviorTest {
super();
}
@Test
public void testEvaluateBinaryLong() {
@Test
public void testEvaluateBinaryLong() {
OpBehaviorFloatSqrt op = new OpBehaviorFloatSqrt();
@ -44,19 +42,18 @@ public class OpBehaviorFloatSqrtTest extends AbstractOpBehaviorTest {
}
@Test
public void testEvaluateBinaryBigInteger() {
@Test
public void testEvaluateBinaryBigInteger() {
OpBehaviorFloatSqrt op = new OpBehaviorFloatSqrt();
FloatFormat ff = FloatFormatFactory.getFloatFormat(8);
BigDecimal big = BigDecimal.valueOf(2.0);
BigFloat big = ff.getBigFloat(2.0);
BigInteger encoding = ff.getEncoding(big);
encoding = op.evaluateUnary(8, 8, encoding);
BigDecimal result = ff.getHostFloat(encoding);
Assert.assertEquals("1.414213562373095", result.toString());
BigFloat result = ff.getHostFloat(encoding);
Assert.assertEquals("1.414213562373095", ff.round(result).toString());
}
}

View file

@ -15,7 +15,6 @@
*/
package ghidra.pcode.opbehavior;
import java.math.BigDecimal;
import java.math.BigInteger;
import org.junit.Assert;
@ -75,35 +74,35 @@ public class OpBehaviorFloatSubTest extends AbstractOpBehaviorTest {
FloatFormat ff = FloatFormatFactory.getFloatFormat(8);
BigInteger a = ff.getEncoding(BigDecimal.valueOf(1.5d));
BigInteger b = ff.getEncoding(BigDecimal.valueOf(1.25d));
BigInteger a = ff.getEncoding(ff.getBigFloat(1.5d));
BigInteger b = ff.getEncoding(ff.getBigFloat(1.25d));
BigInteger result = op.evaluateBinary(8, 8, a, b);// 1.5 - 1.25
Assert.assertEquals(BigDecimal.valueOf(0.25d), ff.getHostFloat(result));
Assert.assertEquals(ff.getBigFloat(0.25d), ff.getHostFloat(result));
a = ff.getEncoding(BigDecimal.valueOf(-1.25d));
a = ff.getEncoding(ff.getBigFloat(-1.25d));
result = op.evaluateBinary(8, 8, a, b);// -1.25 - 1.25
Assert.assertEquals(BigDecimal.valueOf(-2.5d), ff.getHostFloat(result));
Assert.assertEquals(ff.getBigFloat(-2.5d), ff.getHostFloat(result));
a = ff.getEncoding(FloatFormat.BIG_POSITIVE_INFINITY);
a = ff.getBigInfinityEncoding(false);
result = op.evaluateBinary(8, 8, a, b);// +INFINITY - 1.25
Assert.assertEquals(FloatFormat.BIG_POSITIVE_INFINITY, ff.getHostFloat(result));
Assert.assertEquals(ff.getBigInfinity(false), ff.getHostFloat(result));
a = ff.getEncoding(FloatFormat.BIG_NEGATIVE_INFINITY);
a = ff.getBigInfinityEncoding(true);
result = op.evaluateBinary(8, 8, a, b);// -INFINITY - 1.25
Assert.assertEquals(FloatFormat.BIG_NEGATIVE_INFINITY, ff.getHostFloat(result));
Assert.assertEquals(ff.getBigInfinity(true), ff.getHostFloat(result));
b = ff.getEncoding(FloatFormat.BIG_NEGATIVE_INFINITY);
b = ff.getBigInfinityEncoding(true);
result = op.evaluateBinary(8, 8, a, b);// -INFINITY - -INFINITY
Assert.assertEquals(FloatFormat.BIG_NaN, ff.getHostFloat(result));
Assert.assertEquals(ff.getBigNaN(false), ff.getHostFloat(result));
b = ff.getEncoding(FloatFormat.BIG_POSITIVE_INFINITY);
b = ff.getBigInfinityEncoding(false);
result = op.evaluateBinary(8, 8, a, b);// -INFINITY - +INFINITY
Assert.assertEquals(FloatFormat.BIG_NEGATIVE_INFINITY, ff.getHostFloat(result));
Assert.assertEquals(ff.getBigInfinity(true), ff.getHostFloat(result));
a = ff.getEncoding(FloatFormat.BIG_NaN);
b = ff.getEncoding(BigDecimal.valueOf(1.25d));
a = ff.getBigNaNEncoding(false);
b = ff.getEncoding(ff.getBigFloat(1.25d));
result = op.evaluateBinary(8, 8, a, b);// NaN - 1.25
Assert.assertEquals(FloatFormat.BIG_NaN, ff.getHostFloat(result));
Assert.assertEquals(ff.getBigNaN(false), ff.getHostFloat(result));
}

View file

@ -15,7 +15,6 @@
*/
package ghidra.pcode.opbehavior;
import java.math.BigDecimal;
import java.math.BigInteger;
import org.junit.Assert;
@ -30,8 +29,8 @@ public class OpBehaviorFloatTruncTest extends AbstractOpBehaviorTest {
super();
}
@Test
public void testEvaluateBinaryLong() {
@Test
public void testEvaluateBinaryLong() {
OpBehaviorFloatTrunc op = new OpBehaviorFloatTrunc();
@ -59,31 +58,31 @@ public class OpBehaviorFloatTruncTest extends AbstractOpBehaviorTest {
Assert.assertEquals(0, result);
}
@Test
public void testEvaluateBinaryBigInteger() {
@Test
public void testEvaluateBinaryBigInteger() {
OpBehaviorFloatTrunc op = new OpBehaviorFloatTrunc();
FloatFormat ff = FloatFormatFactory.getFloatFormat(8);
BigInteger a = ff.getEncoding(BigDecimal.valueOf(2.5d));
BigInteger a = ff.getEncoding(ff.getBigFloat(2.5d));
BigInteger result = op.evaluateUnary(8, 8, a);
Assert.assertEquals(BigInteger.valueOf(2), result);
a = ff.getEncoding(BigDecimal.valueOf(-2.5d));
a = ff.getEncoding(ff.getBigFloat(-2.5d));
result = op.evaluateUnary(8, 8, a);
Assert.assertEquals(BigInteger.valueOf(-2), result);
a = ff.getEncoding(FloatFormat.BIG_POSITIVE_INFINITY);
a = ff.getBigInfinityEncoding(false);
result = op.evaluateUnary(8, 8, a);
Assert.assertEquals(BigInteger.valueOf(Long.MAX_VALUE), result);
a = ff.getEncoding(FloatFormat.BIG_NEGATIVE_INFINITY);
a = ff.getBigInfinityEncoding(true);
result = op.evaluateUnary(8, 8, a);
Assert.assertEquals(BigInteger.valueOf(Long.MIN_VALUE), result);
// TODO: What should the correct result be?
a = ff.getEncoding(FloatFormat.BIG_NaN);
a = ff.getBigNaNEncoding(false);
result = op.evaluateUnary(8, 8, a);
Assert.assertEquals(BigInteger.ZERO, result);
}

View file

@ -15,7 +15,7 @@
*/
package ghidra.program.model.data;
import java.math.BigDecimal;
import java.math.*;
import org.junit.Assert;
import org.junit.Test;
@ -46,39 +46,43 @@ public class Float10DataTypeTest extends AbstractGTest {
// Really small values
MathContext mc = new MathContext(18, RoundingMode.UP);
bytes = bytes(0, 1, 0x80, 0, 0, 0, 0, 0, 0, 0); // 0x00018000000000000000 = approaches 0
value =
Float10DataType.dataType.getValue(new ByteMemBufferImpl(null, bytes, true), null, 10);
Assert.assertEquals("5.04315471466814026E-4932", value.toString());
Assert.assertEquals("5.04315471466814026E-4932", ((BigDecimal) value).round(mc).toString());
bytes = bytes(0x80, 1, 0x80, 0, 0, 0, 0, 0, 0, 0); // 0x00018000000000000000 = approaches 0
value =
Float10DataType.dataType.getValue(new ByteMemBufferImpl(null, bytes, true), null, 10);
Assert.assertEquals("-5.04315471466814026E-4932", value.toString());
Assert.assertEquals("-5.04315471466814026E-4932",
((BigDecimal) value).round(mc).toString());
// Really big values
bytes = bytes(0x7f, 0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0); // 0x7ffe8000000000000000 = approaches +infinity
value =
Float10DataType.dataType.getValue(new ByteMemBufferImpl(null, bytes, true), null, 10);
Assert.assertEquals("8.92298621517923824E+4931", value.toString());
Assert.assertEquals("8.92298621517923824E+4931", ((BigDecimal) value).round(mc).toString());
bytes = bytes(0xff, 0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0); // 0x7ffe8000000000000000 = approaches -infinity
value =
Float10DataType.dataType.getValue(new ByteMemBufferImpl(null, bytes, true), null, 10);
Assert.assertEquals("-8.92298621517923824E+4931", value.toString());
Assert.assertEquals("-8.92298621517923824E+4931",
((BigDecimal) value).round(mc).toString());
// Values within the range of Double
bytes = bytes(0x40, 1, 0x20, 0, 0, 0, 0, 0, 0, 0); // 0x40002000000000000000 = approaches -infinity
value =
Float10DataType.dataType.getValue(new ByteMemBufferImpl(null, bytes, true), null, 10);
Assert.assertEquals(BigDecimal.valueOf(4.5), value);
Assert.assertEquals(BigDecimal.valueOf(4.5), ((BigDecimal) value).stripTrailingZeros());
bytes = bytes(0xc0, 1, 0x20, 0, 0, 0, 0, 0, 0, 0); // 0x40002000000000000000 = approaches -infinity
value =
Float10DataType.dataType.getValue(new ByteMemBufferImpl(null, bytes, true), null, 10);
Assert.assertEquals(BigDecimal.valueOf(-4.5), value);
Assert.assertEquals(BigDecimal.valueOf(-4.5), ((BigDecimal) value).stripTrailingZeros());
}