Ghidra Decompiler Analysis Engine
float.hh
Go to the documentation of this file.
1 /* ###
2  * IP: GHIDRA
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
18 
19 #ifndef __CPUI_FLOATFORMAT__
20 #define __CPUI_FLOATFORMAT__
21 
22 #include "xml.hh"
23 
30 class FloatFormat {
31 public:
33  enum floatclass {
34  normalized = 0,
35  infinity = 1,
36  zero = 2,
37  nan = 3,
39  };
40 private:
41  int4 size;
42  int4 signbit_pos;
43  int4 frac_pos;
44  int4 frac_size;
45  int4 exp_pos;
46  int4 exp_size;
47  int4 bias;
48  int4 maxexponent;
49  int4 decimal_precision;
50  bool jbitimplied;
51  static double createFloat(bool sign,uintb signif,int4 exp);
52  static floatclass extractExpSig(double x,bool *sgn,uintb *signif,int4 *exp);
53  static bool roundToNearestEven(uintb &signif, int4 lowbitpos);
54  uintb setFractionalCode(uintb x,uintb code) const;
55  uintb setSign(uintb x,bool sign) const;
56  uintb setExponentCode(uintb x,uintb code) const;
57  uintb getZeroEncoding(bool sgn) const;
58  uintb getInfinityEncoding(bool sgn) const;
59  uintb getNaNEncoding(bool sgn) const;
60  void calcPrecision(void);
61 public:
62  FloatFormat(void) {}
63  FloatFormat(int4 sz);
64  int4 getSize(void) const { return size; }
65  double getHostFloat(uintb encoding,floatclass *type) const;
66  uintb getEncoding(double host) const;
67  int4 getDecimalPrecision(void) const { return decimal_precision; }
68  uintb convertEncoding(uintb encoding,const FloatFormat *formin) const;
69 
70  uintb extractFractionalCode(uintb x) const;
71  bool extractSign(uintb x) const;
72  int4 extractExponentCode(uintb x) const;
73 
74  // Operations on floating point values
75 
76  uintb opEqual(uintb a,uintb b) const;
77  uintb opNotEqual(uintb a,uintb b) const;
78  uintb opLess(uintb a,uintb b) const;
79  uintb opLessEqual(uintb a,uintb b) const;
80  uintb opNan(uintb a) const;
81  uintb opAdd(uintb a,uintb b) const;
82  uintb opDiv(uintb a,uintb b) const;
83  uintb opMult(uintb a,uintb b) const;
84  uintb opSub(uintb a,uintb b) const;
85  uintb opNeg(uintb a) const;
86  uintb opAbs(uintb a) const;
87  uintb opSqrt(uintb a) const;
88  uintb opTrunc(uintb a,int4 sizeout) const;
89  uintb opCeil(uintb a) const;
90  uintb opFloor(uintb a) const;
91  uintb opRound(uintb a) const;
92  uintb opInt2Float(uintb a,int4 sizein) const;
93  uintb opFloat2Float(uintb a,const FloatFormat &outformat) const;
94 
95  void saveXml(ostream &s) const;
96  void restoreXml(const Element *el);
97 };
98 
99 #endif
FloatFormat::normalized
@ normalized
A normal floating-point number.
Definition: float.hh:34
FloatFormat::opNotEqual
uintb opNotEqual(uintb a, uintb b) const
Inequality comparison (!=)
Definition: float.cc:427
FloatFormat::opLessEqual
uintb opLessEqual(uintb a, uintb b) const
Less-than-or-equal comparison (<=)
Definition: float.cc:453
FloatFormat::opAdd
uintb opAdd(uintb a, uintb b) const
Addition (+)
Definition: float.cc:477
FloatFormat::restoreXml
void restoreXml(const Element *el)
Restore the format from XML.
Definition: float.cc:637
FloatFormat::extractSign
bool extractSign(uintb x) const
Extract the sign bit from the encoding.
Definition: float.cc:111
FloatFormat
Encoding information for a single floating-point format.
Definition: float.hh:30
FloatFormat::opFloor
uintb opFloor(uintb a) const
Floor (floor)
Definition: float.cc:599
FloatFormat::opMult
uintb opMult(uintb a, uintb b) const
Multiplication (*)
Definition: float.cc:501
FloatFormat::opInt2Float
uintb opInt2Float(uintb a, int4 sizein) const
Convert integer to floating-point.
Definition: float.cc:555
FloatFormat::saveXml
void saveXml(ostream &s) const
Save the format to an XML stream.
Definition: float.cc:620
Element
An XML element. A node in the DOM tree.
Definition: xml.hh:150
FloatFormat::floatclass
floatclass
The various classes of floating-point encodings.
Definition: float.hh:33
FloatFormat::opDiv
uintb opDiv(uintb a, uintb b) const
Division (/)
Definition: float.cc:489
FloatFormat::opEqual
uintb opEqual(uintb a, uintb b) const
Equality comparison (==)
Definition: float.cc:414
FloatFormat::opNeg
uintb opNeg(uintb a) const
Unary negate.
Definition: float.cc:524
FloatFormat::opAbs
uintb opAbs(uintb a) const
Absolute value (abs)
Definition: float.cc:534
FloatFormat::extractFractionalCode
uintb extractFractionalCode(uintb x) const
Extract the fractional part of the encoding.
Definition: float.cc:101
FloatFormat::opLess
uintb opLess(uintb a, uintb b) const
Less-than comparison (<)
Definition: float.cc:440
FloatFormat::opRound
uintb opRound(uintb a) const
Round.
Definition: float.cc:609
FloatFormat::opSub
uintb opSub(uintb a, uintb b) const
Subtraction (-)
Definition: float.cc:513
FloatFormat::infinity
@ infinity
An encoding representing an infinite value.
Definition: float.hh:35
FloatFormat::nan
@ nan
An invalid encoding, Not-a-Number.
Definition: float.hh:37
FloatFormat::opTrunc
uintb opTrunc(uintb a, int4 sizeout) const
Convert floating-point to integer.
Definition: float.cc:576
FloatFormat::zero
@ zero
An encoding of the value zero.
Definition: float.hh:36
FloatFormat::denormalized
@ denormalized
A denormalized encoding (for very small values)
Definition: float.hh:38
FloatFormat::getHostFloat
double getHostFloat(uintb encoding, floatclass *type) const
Convert an encoding into host's double.
Definition: float.cc:215
FloatFormat::opNan
uintb opNan(uintb a) const
Test if Not-a-Number (NaN)
Definition: float.cc:465
FloatFormat::opFloat2Float
uintb opFloat2Float(uintb a, const FloatFormat &outformat) const
Convert between floating-point precisions.
Definition: float.cc:567
FloatFormat::opSqrt
uintb opSqrt(uintb a) const
Square root (sqrt)
Definition: float.cc:544
xml.hh
Lightweight (and incomplete) XML parser for marshaling data to and from the decompiler.
FloatFormat::getEncoding
uintb getEncoding(double host) const
Convert host's double into this encoding.
Definition: float.cc:278
FloatFormat::convertEncoding
uintb convertEncoding(uintb encoding, const FloatFormat *formin) const
Convert between two different formats.
Definition: float.cc:337
FloatFormat::FloatFormat
FloatFormat(int4 sz)
Construct default IEEE 754 standard settings.
Definition: float.cc:24
FloatFormat::opCeil
uintb opCeil(uintb a) const
Ceiling (ceil)
Definition: float.cc:589
FloatFormat::extractExponentCode
int4 extractExponentCode(uintb x) const
Extract the exponent from the encoding.
Definition: float.cc:120