decompiler  1.0.0
Public Types | Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | List of all members
ghidra::FloatFormat Class Reference

Encoding information for a single floating-point format. More...

#include <float.hh>

Public Types

enum  floatclass {
  normalized = 0, infinity = 1, zero = 2, nan = 3,
  denormalized = 4
}
 The various classes of floating-point encodings. More...
 

Public Member Functions

 FloatFormat (void)
 Construct for use with restoreXml()
 
 FloatFormat (int4 sz)
 Construct default IEEE 754 standard settings. More...
 
int4 getSize (void) const
 Get the size of the encoding in bytes.
 
double getHostFloat (uintb encoding, floatclass *type) const
 Convert an encoding into host's double. More...
 
uintb getEncoding (double host) const
 Convert host's double into this encoding. More...
 
int4 getDecimalPrecision (void) const
 Get number of digits of precision.
 
uintb convertEncoding (uintb encoding, const FloatFormat *formin) const
 Convert between two different formats. More...
 
uintb extractFractionalCode (uintb x) const
 Extract the fractional part of the encoding. More...
 
bool extractSign (uintb x) const
 Extract the sign bit from the encoding. More...
 
int4 extractExponentCode (uintb x) const
 Extract the exponent from the encoding. More...
 
uintb opEqual (uintb a, uintb b) const
 Equality comparison (==) More...
 
uintb opNotEqual (uintb a, uintb b) const
 Inequality comparison (!=) More...
 
uintb opLess (uintb a, uintb b) const
 Less-than comparison (<) More...
 
uintb opLessEqual (uintb a, uintb b) const
 Less-than-or-equal comparison (<=) More...
 
uintb opNan (uintb a) const
 Test if Not-a-Number (NaN) More...
 
uintb opAdd (uintb a, uintb b) const
 Addition (+) More...
 
uintb opDiv (uintb a, uintb b) const
 Division (/) More...
 
uintb opMult (uintb a, uintb b) const
 Multiplication (*) More...
 
uintb opSub (uintb a, uintb b) const
 Subtraction (-) More...
 
uintb opNeg (uintb a) const
 Unary negate. More...
 
uintb opAbs (uintb a) const
 Absolute value (abs) More...
 
uintb opSqrt (uintb a) const
 Square root (sqrt) More...
 
uintb opTrunc (uintb a, int4 sizeout) const
 Convert floating-point to integer. More...
 
uintb opCeil (uintb a) const
 Ceiling (ceil) More...
 
uintb opFloor (uintb a) const
 Floor (floor) More...
 
uintb opRound (uintb a) const
 Round. More...
 
uintb opInt2Float (uintb a, int4 sizein) const
 Convert integer to floating-point. More...
 
uintb opFloat2Float (uintb a, const FloatFormat &outformat) const
 Convert between floating-point precisions. More...
 
void saveXml (ostream &s) const
 Save the format to an XML stream. More...
 
void restoreXml (const Element *el)
 Restore the format from XML. More...
 

Private Member Functions

uintb setFractionalCode (uintb x, uintb code) const
 Set the fractional part of an encoded value. More...
 
uintb setSign (uintb x, bool sign) const
 Set the sign bit of an encoded value. More...
 
uintb setExponentCode (uintb x, uintb code) const
 Set the exponent of an encoded value. More...
 
uintb getZeroEncoding (bool sgn) const
 Get an encoded zero value. More...
 
uintb getInfinityEncoding (bool sgn) const
 Get an encoded infinite value. More...
 
uintb getNaNEncoding (bool sgn) const
 Get an encoded NaN value. More...
 
void calcPrecision (void)
 Calculate the decimal precision of this format.
 

Static Private Member Functions

static double createFloat (bool sign, uintb signif, int4 exp)
 Create a double given sign, fractional, and exponent. More...
 
static floatclass extractExpSig (double x, bool *sgn, uintb *signif, int4 *exp)
 Extract the sign, fractional, and exponent from a given floating-point value. More...
 
static bool roundToNearestEven (uintb &signif, int4 lowbitpos)
 Round a floating point value to the nearest even. More...
 

Private Attributes

int4 size
 Size of float in bytes (this format)
 
int4 signbit_pos
 Bit position of sign bit.
 
int4 frac_pos
 (lowest) bit position of fractional part
 
int4 frac_size
 Number of bits in fractional part.
 
int4 exp_pos
 (lowest) bit position of exponent
 
int4 exp_size
 Number of bits in exponent.
 
int4 bias
 What to add to real exponent to get encoding.
 
int4 maxexponent
 Maximum possible exponent.
 
int4 decimal_precision
 Number of decimal digits of precision.
 
bool jbitimplied
 Set to true if integer bit of 1 is assumed.
 

Detailed Description

Encoding information for a single floating-point format.

This class supports manipulation of a single floating-point encoding. An encoding can be converted to and from the host format and convenience methods allow p-code floating-point operations to be performed on natively encoded operands. This follows the IEEE754 standards.

Member Enumeration Documentation

◆ floatclass

The various classes of floating-point encodings.

Enumerator
normalized 

A normal floating-point number.

infinity 

An encoding representing an infinite value.

zero 

An encoding of the value zero.

nan 

An invalid encoding, Not-a-Number.

denormalized 

A denormalized encoding (for very small values)

Constructor & Destructor Documentation

◆ FloatFormat()

ghidra::FloatFormat::FloatFormat ( int4  sz)

Construct default IEEE 754 standard settings.

Set format for a given encoding size according to IEEE 754 standards

Parameters
szis the size of the encoding in bytes

References bias, calcPrecision(), exp_pos, exp_size, frac_pos, frac_size, jbitimplied, maxexponent, signbit_pos, and size.

Member Function Documentation

◆ convertEncoding()

uintb ghidra::FloatFormat::convertEncoding ( uintb  encoding,
const FloatFormat formin 
) const

Convert between two different formats.

Parameters
encodingis the value in the other FloatFormat
forminis the other FloatFormat
Returns
the equivalent value in this FloatFormat

References bias, extractExponentCode(), extractFractionalCode(), extractSign(), frac_size, getInfinityEncoding(), getNaNEncoding(), getZeroEncoding(), jbitimplied, maxexponent, roundToNearestEven(), setExponentCode(), setFractionalCode(), and setSign().

Referenced by getDecimalPrecision(), and opFloat2Float().

◆ createFloat()

double ghidra::FloatFormat::createFloat ( bool  sign,
uintb  signif,
int4  exp 
)
staticprivate

Create a double given sign, fractional, and exponent.

Parameters
signis set to true if the value should be negative
signifis the fractional part
expis the exponent
Returns
the constructed floating-point value

Referenced by getHostFloat().

◆ extractExponentCode()

int4 ghidra::FloatFormat::extractExponentCode ( uintb  x) const

Extract the exponent from the encoding.

Parameters
xis an encoded floating-point value
Returns
the (signed) exponent

References exp_pos, and exp_size.

Referenced by convertEncoding(), getDecimalPrecision(), getHostFloat(), and ghidra::ScoreUnionFields::scoreConstantFit().

◆ extractExpSig()

FloatFormat::floatclass ghidra::FloatFormat::extractExpSig ( double  x,
bool *  sgn,
uintb *  signif,
int4 *  exp 
)
staticprivate

Extract the sign, fractional, and exponent from a given floating-point value.

Parameters
xis the given value
sgnpasses back the sign
signifpasses back the fractional part
exppasses back the exponent
Returns
the floating-point class of the value

References infinity, nan, normalized, and zero.

Referenced by getEncoding().

◆ extractFractionalCode()

uintb ghidra::FloatFormat::extractFractionalCode ( uintb  x) const

Extract the fractional part of the encoding.

Parameters
xis an encoded floating-point value
Returns
the fraction part of the value aligned to the top of the word

References frac_pos, and frac_size.

Referenced by convertEncoding(), getDecimalPrecision(), and getHostFloat().

◆ extractSign()

bool ghidra::FloatFormat::extractSign ( uintb  x) const

Extract the sign bit from the encoding.

Parameters
xis an encoded floating-point value
Returns
the sign bit

References signbit_pos.

Referenced by convertEncoding(), getDecimalPrecision(), getHostFloat(), and ghidra::PrintC::push_float().

◆ getEncoding()

uintb ghidra::FloatFormat::getEncoding ( double  host) const

Convert host's double into this encoding.

Parameters
hostis the double value to convert
Returns
the equivalent encoded value

References bias, extractExpSig(), frac_size, getInfinityEncoding(), getNaNEncoding(), getZeroEncoding(), infinity, jbitimplied, maxexponent, nan, roundToNearestEven(), setExponentCode(), setFractionalCode(), setSign(), and zero.

Referenced by getSize(), opAbs(), opAdd(), opCeil(), opDiv(), opFloor(), opInt2Float(), opMult(), opNeg(), opRound(), opSqrt(), and opSub().

◆ getHostFloat()

double ghidra::FloatFormat::getHostFloat ( uintb  encoding,
floatclass type 
) const

Convert an encoding into host's double.

Parameters
encodingis the encoding value
typepoints to the floating-point class, which is passed back
Returns
the equivalent double value

References bias, createFloat(), denormalized, extractExponentCode(), extractFractionalCode(), extractSign(), infinity, jbitimplied, maxexponent, nan, normalized, and zero.

Referenced by getSize(), opAbs(), opAdd(), opCeil(), opDiv(), opEqual(), opFloor(), opLess(), opLessEqual(), opMult(), opNan(), opNeg(), opNotEqual(), opRound(), opSqrt(), opSub(), opTrunc(), and ghidra::PrintC::push_float().

◆ getInfinityEncoding()

uintb ghidra::FloatFormat::getInfinityEncoding ( bool  sgn) const
private

Get an encoded infinite value.

Parameters
sgnis set to true for negative infinity, false for positive
Returns
the encoded infinity

References maxexponent, setExponentCode(), setFractionalCode(), and setSign().

Referenced by convertEncoding(), and getEncoding().

◆ getNaNEncoding()

uintb ghidra::FloatFormat::getNaNEncoding ( bool  sgn) const
private

Get an encoded NaN value.

Parameters
sgnis set to true for negative NaN, false for positive
Returns
the encoded NaN

References maxexponent, setExponentCode(), setFractionalCode(), and setSign().

Referenced by convertEncoding(), and getEncoding().

◆ getZeroEncoding()

uintb ghidra::FloatFormat::getZeroEncoding ( bool  sgn) const
private

Get an encoded zero value.

Parameters
sgnis set to true for negative zero, false for positive
Returns
the encoded zero

References setExponentCode(), setFractionalCode(), and setSign().

Referenced by convertEncoding(), and getEncoding().

◆ opAbs()

uintb ghidra::FloatFormat::opAbs ( uintb  a) const

Absolute value (abs)

Parameters
ais an encoded floating-point value
Returns
abs(a)

References getEncoding(), and getHostFloat().

Referenced by ghidra::OpBehaviorFloatAbs::evaluateUnary(), and getDecimalPrecision().

◆ opAdd()

uintb ghidra::FloatFormat::opAdd ( uintb  a,
uintb  b 
) const

Addition (+)

Parameters
ais the first floating-point value
bis the second floating-point value
Returns
a + b

References getEncoding(), and getHostFloat().

Referenced by ghidra::OpBehaviorFloatAdd::evaluateBinary(), and getDecimalPrecision().

◆ opCeil()

uintb ghidra::FloatFormat::opCeil ( uintb  a) const

Ceiling (ceil)

Parameters
ais an encoded floating-point value
Returns
ceil(a)

References getEncoding(), and getHostFloat().

Referenced by ghidra::OpBehaviorFloatCeil::evaluateUnary(), and getDecimalPrecision().

◆ opDiv()

uintb ghidra::FloatFormat::opDiv ( uintb  a,
uintb  b 
) const

Division (/)

Parameters
ais the first floating-point value
bis the second floating-point value
Returns
a / b

References getEncoding(), and getHostFloat().

Referenced by ghidra::OpBehaviorFloatDiv::evaluateBinary(), and getDecimalPrecision().

◆ opEqual()

uintb ghidra::FloatFormat::opEqual ( uintb  a,
uintb  b 
) const

Equality comparison (==)

Parameters
ais the first floating-point value
bis the second floating-point value
Returns
true if (a == b)

References getHostFloat().

Referenced by ghidra::OpBehaviorFloatEqual::evaluateBinary(), and getDecimalPrecision().

◆ opFloat2Float()

uintb ghidra::FloatFormat::opFloat2Float ( uintb  a,
const FloatFormat outformat 
) const

Convert between floating-point precisions.

Parameters
ais an encoded floating-point value
outformatis the desired output FloatFormat
Returns
a converted to the output FloatFormat

References convertEncoding().

Referenced by ghidra::OpBehaviorFloatFloat2Float::evaluateUnary(), and getDecimalPrecision().

◆ opFloor()

uintb ghidra::FloatFormat::opFloor ( uintb  a) const

Floor (floor)

Parameters
ais an encoded floating-point value
Returns
floor(a)

References getEncoding(), and getHostFloat().

Referenced by ghidra::OpBehaviorFloatFloor::evaluateUnary(), and getDecimalPrecision().

◆ opInt2Float()

uintb ghidra::FloatFormat::opInt2Float ( uintb  a,
int4  sizein 
) const

Convert integer to floating-point.

Parameters
ais a signed integer value
sizeinis the number of bytes in the integer encoding
Returns
a converted to an encoded floating-point value

References getEncoding().

Referenced by ghidra::OpBehaviorFloatInt2Float::evaluateUnary(), and getDecimalPrecision().

◆ opLess()

uintb ghidra::FloatFormat::opLess ( uintb  a,
uintb  b 
) const

Less-than comparison (<)

Parameters
ais the first floating-point value
bis the second floating-point value
Returns
true if (a < b)

References getHostFloat().

Referenced by ghidra::OpBehaviorFloatLess::evaluateBinary(), and getDecimalPrecision().

◆ opLessEqual()

uintb ghidra::FloatFormat::opLessEqual ( uintb  a,
uintb  b 
) const

Less-than-or-equal comparison (<=)

Parameters
ais the first floating-point value
bis the second floating-point value
Returns
true if (a <= b)

References getHostFloat().

Referenced by ghidra::OpBehaviorFloatLessEqual::evaluateBinary(), and getDecimalPrecision().

◆ opMult()

uintb ghidra::FloatFormat::opMult ( uintb  a,
uintb  b 
) const

Multiplication (*)

Parameters
ais the first floating-point value
bis the second floating-point value
Returns
a * b

References getEncoding(), and getHostFloat().

Referenced by ghidra::OpBehaviorFloatMult::evaluateBinary(), and getDecimalPrecision().

◆ opNan()

uintb ghidra::FloatFormat::opNan ( uintb  a) const

Test if Not-a-Number (NaN)

Parameters
ais an encoded floating-point value
Returns
true if a is Not-a-Number

References getHostFloat(), and nan.

Referenced by ghidra::OpBehaviorFloatNan::evaluateUnary(), and getDecimalPrecision().

◆ opNeg()

uintb ghidra::FloatFormat::opNeg ( uintb  a) const

Unary negate.

Parameters
ais an encoded floating-point value
Returns
-a

References getEncoding(), and getHostFloat().

Referenced by ghidra::OpBehaviorFloatNeg::evaluateUnary(), and getDecimalPrecision().

◆ opNotEqual()

uintb ghidra::FloatFormat::opNotEqual ( uintb  a,
uintb  b 
) const

Inequality comparison (!=)

Parameters
ais the first floating-point value
bis the second floating-point value
Returns
true if (a != b)

References getHostFloat().

Referenced by ghidra::OpBehaviorFloatNotEqual::evaluateBinary(), and getDecimalPrecision().

◆ opRound()

uintb ghidra::FloatFormat::opRound ( uintb  a) const

Round.

Parameters
ais an encoded floating-point value
Returns
round(a)

References getEncoding(), and getHostFloat().

Referenced by ghidra::OpBehaviorFloatRound::evaluateUnary(), and getDecimalPrecision().

◆ opSqrt()

uintb ghidra::FloatFormat::opSqrt ( uintb  a) const

Square root (sqrt)

Parameters
ais an encoded floating-point value
Returns
sqrt(a)

References getEncoding(), and getHostFloat().

Referenced by ghidra::OpBehaviorFloatSqrt::evaluateUnary(), and getDecimalPrecision().

◆ opSub()

uintb ghidra::FloatFormat::opSub ( uintb  a,
uintb  b 
) const

Subtraction (-)

Parameters
ais the first floating-point value
bis the second floating-point value
Returns
a - b

References getEncoding(), and getHostFloat().

Referenced by ghidra::OpBehaviorFloatSub::evaluateBinary(), and getDecimalPrecision().

◆ opTrunc()

uintb ghidra::FloatFormat::opTrunc ( uintb  a,
int4  sizeout 
) const

Convert floating-point to integer.

Parameters
ais an encoded floating-point value
sizeoutis the desired encoding size of the output
Returns
an integer encoding of a

References ghidra::calc_mask(), and getHostFloat().

Referenced by ghidra::OpBehaviorFloatTrunc::evaluateUnary(), and getDecimalPrecision().

◆ restoreXml()

void ghidra::FloatFormat::restoreXml ( const Element el)

Restore the format from XML.

Restore object from a <floatformat> XML tag

Parameters
elis the element

References bias, calcPrecision(), exp_pos, exp_size, frac_pos, frac_size, ghidra::Element::getAttributeValue(), jbitimplied, maxexponent, signbit_pos, size, and ghidra::xml_readbool().

Referenced by getDecimalPrecision().

◆ roundToNearestEven()

bool ghidra::FloatFormat::roundToNearestEven ( uintb &  signif,
int4  lowbitpos 
)
staticprivate

Round a floating point value to the nearest even.

Parameters
signifthe significant bits of a floating point value
lowbitposthe position in signif of the floating point
Returns
true if we rounded up

Referenced by convertEncoding(), and getEncoding().

◆ saveXml()

void ghidra::FloatFormat::saveXml ( ostream &  s) const

Save the format to an XML stream.

Write the format out to a <floatformat> XML tag.

Parameters
sis the output stream

References ghidra::a_v_b(), ghidra::a_v_i(), bias, exp_pos, exp_size, frac_pos, frac_size, jbitimplied, signbit_pos, and size.

Referenced by getDecimalPrecision().

◆ setExponentCode()

uintb ghidra::FloatFormat::setExponentCode ( uintb  x,
uintb  code 
) const
private

Set the exponent of an encoded value.

Parameters
xis an encoded value (with exponent set to zero)
codeis the exponent to set
Returns
the encoded value with the new exponent

References exp_pos.

Referenced by convertEncoding(), getEncoding(), getInfinityEncoding(), getNaNEncoding(), and getZeroEncoding().

◆ setFractionalCode()

uintb ghidra::FloatFormat::setFractionalCode ( uintb  x,
uintb  code 
) const
private

Set the fractional part of an encoded value.

Parameters
xis an encoded value (with fraction part set to zero)
codeis the new fractional value to set
Returns
the encoded value with the fractional filled in

References frac_pos, and frac_size.

Referenced by convertEncoding(), getEncoding(), getInfinityEncoding(), getNaNEncoding(), and getZeroEncoding().

◆ setSign()

uintb ghidra::FloatFormat::setSign ( uintb  x,
bool  sign 
) const
private

Set the sign bit of an encoded value.

Parameters
xis an encoded value (with sign set to zero)
signis the sign bit to set
Returns
the encoded value with the sign bit set

References signbit_pos.

Referenced by convertEncoding(), getEncoding(), getInfinityEncoding(), getNaNEncoding(), and getZeroEncoding().


The documentation for this class was generated from the following files: