Class BigFloat

java.lang.Object
ghidra.pcode.floatformat.BigFloat
All Implemented Interfaces:
Comparable<BigFloat>

public class BigFloat extends Object implements Comparable<BigFloat>
An IEEE 754 floating point class.

Values represented:

  • QUIET_NAN, SIGNALED_NAN
  • -INF, +INF
  • value = sign * unscaled * 2 ^ (scale-fracbits)
sign = -1 or +1, unscaled has at most fracbits+1 bits, and scale is at most expbits bits.

Operations compute exact result then round to nearest even.

  • Field Details

  • Method Details

    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • zero

      public static BigFloat zero(int fracbits, int expbits, int sign)
      Return the BigFloat with the given number of bits representing zero.
      Parameters:
      fracbits - number of fractional bits
      expbits - number of bits in the exponent
      sign - +1 or -1
      Returns:
      a BigFloat representing +zero or -zero
    • zero

      public static BigFloat zero(int fracbits, int expbits)
      Return the BigFloat with the given number of bits representing (positive) zero.
      Parameters:
      fracbits - number of fractional bits
      expbits - number of bits in the exponent
      Returns:
      a BigFloat representing +zero
    • infinity

      public static BigFloat infinity(int fracbits, int expbits, int sign)
      Parameters:
      fracbits - number of fractional bits
      expbits - number of bits in the exponent
      sign - +1 or -1
      Returns:
      +inf or -inf
    • quietNaN

      public static BigFloat quietNaN(int fracbits, int expbits, int sign)
      Return the BigFloat with the given number of bits representing (quiet) NaN.
      Parameters:
      fracbits - number of fractional bits
      expbits - number of bits in the exponent
      sign - +1 or -1
      Returns:
      a BigFloat representing (quiet) NaN
    • scaleUpTo

      protected void scaleUpTo(int newLength)
    • isNormal

      public boolean isNormal()
      Determine if the state of this BigFloat reflects a normalized value.

      NOTE: This method relies on the manner of construction and only checks for FloatKind.FINITE and that full size of the fractional bits is used for the unscaled value.

      Returns:
      true if this BigFloat is FINITE and normal.
    • isDenormal

      public boolean isDenormal()
      Determine if the state of this BigFloat reflects a subnormal/denormal value.

      NOTE: This method relies on the manner of construction and only checks for FloatKind.FINITE and that the non-zero unscaled valued does not use all fractional bits.

      Returns:
      true if this BigFloat is FINITE and denormal
    • internalRound

      protected void internalRound(boolean eps)
      This function is used internally to round after a computation.

      Assume that the true value is

         sign * (unscaled + eps) * 2 ^ (scale-fracbits)
       and
         unscaled.bitLength() > fracbits+1 
      
       (or the value is subnormal with at least 1 bit of extra precision)
       
      Parameters:
      eps - < 1
    • getLeadBitPos

      protected int getLeadBitPos()
    • toBigDecimal

      public BigDecimal toBigDecimal()
      If finite, the returned BigDecimal is exactly equal to this. If not finite, one of the FloatFormat.BIG_* constants is returned.
      Returns:
      a BigDecimal or null if value is NaN (i.e., FloatKind.QUIET_NAN or FloatKind.SIGNALING_NAN).
    • toBinaryString

      public String toBinaryString()
    • makeSignalingNaN

      protected void makeSignalingNaN()
    • makeQuietNaN

      protected void makeQuietNaN()
    • isNaN

      public boolean isNaN()
      Returns:
      true if this BigFloat is NaN
    • makeZero

      protected void makeZero()
    • isInfinite

      public boolean isInfinite()
      Returns:
      true if this BigFloat is infinite
    • isZero

      public boolean isZero()
      Returns:
      true if this BigFloat is zero
    • copy

      public BigFloat copy()
      Returns:
      a copy of this BigFloat
    • copyFrom

      protected void copyFrom(BigFloat other)
    • div

      public static BigFloat div(BigFloat a, BigFloat b)
      Parameters:
      a - a BigFloat
      b - a BigFloat
      Returns:
      a/b
    • div

      public void div(BigFloat other)
      this/=other
      Parameters:
      other - a BigFloat
    • mul

      public static BigFloat mul(BigFloat a, BigFloat b)
      Parameters:
      a - a BigFloat
      b - a BigFloat
      Returns:
      a*b
    • mul

      public void mul(BigFloat other)
      this*=other
      Parameters:
      other - a BigFloat
    • add

      public static BigFloat add(BigFloat a, BigFloat b)
      Parameters:
      a - a BigFloat
      b - a BigFloat
      Returns:
      a+b
    • add

      public void add(BigFloat other)
      this+=other
      Parameters:
      other - a BigFloat
    • sub

      public static BigFloat sub(BigFloat a, BigFloat b)
      Parameters:
      a - a BigFloat
      b - a BigFloat
      Returns:
      a-b
    • sub

      public void sub(BigFloat other)
      this-=other
      Parameters:
      other - a BigFloat
    • add0

      protected void add0(BigFloat other)
    • sub0

      protected void sub0(BigFloat other)
    • sqrt

      public static BigFloat sqrt(BigFloat a)
      Parameters:
      a - a BigFloat
      Returns:
      the square root of a
    • sqrt

      public void sqrt()
      this=sqrt(this)

      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.

      adapted from http://medialab.freaknet.org/martin/src/sqrt/sqrt.c

    • floor

      public static BigFloat floor(BigFloat a)
      Parameters:
      a - a BigFloat
      Returns:
      floor(a)
    • floor

      public void floor()
      this=floor(this)
    • ceil

      public static BigFloat ceil(BigFloat a)
      Parameters:
      a - a BigFloat
      Returns:
      ceil(a)
    • ceil

      public void ceil()
      this=ceil(this)
    • trunc

      public static BigFloat trunc(BigFloat a)
      Parameters:
      a - a BigFloat
      Returns:
      trunc(a) (round toward zero)
    • trunc

      public void trunc()
      this=trunc(this) (round toward zero)
    • negate

      public void negate()
      this*=-1
    • negate

      public static BigFloat negate(BigFloat a)
      Parameters:
      a - a BigFloat
      Returns:
      -a
    • abs

      public static BigFloat abs(BigFloat a)
      Parameters:
      a - a BigFloat
      Returns:
      abs(a)
    • abs

      public void abs()
      this=abs(this)
    • toBigInteger

      public BigInteger toBigInteger()
      Returns:
      the truncated integer form of this BigFloat
    • round

      public static BigFloat round(BigFloat a)
      Parameters:
      a - a BigFloat
      Returns:
      round(a)
    • round

      public void round()
      Round this value to the nearest whole number
    • compareTo

      public int compareTo(BigFloat other)
      Specified by:
      compareTo in interface Comparable<BigFloat>
    • toString

      public String toString()
      Perform rounding and conversion to BigDecimal prior to generating a formatted decimal string of the specified BigFloat value. A default generated MathContext is used.
      Overrides:
      toString in class Object
      Returns:
      decimal string representation
    • toString

      public String toString(MathContext displayContext)
      Perform rounding and conversion to BigDecimal prior to generating a formatted decimal string of the specified BigFloat value.
      Parameters:
      displayContext - display context used for rounding and precision.
      Returns:
      decimal string representation
    • toString

      public String toString(FloatFormat ff, boolean compact)
      Perform appropriate rounding and conversion to BigDecimal prior to generating a formatted decimal string of the specified BigFloat value. See toString(FloatFormat, boolean), FloatFormat.toDecimalString(BigFloat) and FloatFormat.toDecimalString(BigFloat, boolean).
      Parameters:
      ff - float format
      compact - if true the precision will be reduced to a form which is still equivalent at the binary encoding level for the specified FloatFormat.
      Returns:
      decimal string representation