we can do without the odd align.h
[libfirm] / ir / tv / IeeeCC754 / include / Fp.h
1 /*
2 ##########################################################################
3 #                                                                        #
4 # Program: IeeeCC754                                                     #
5 #                                                                        #
6 # Description:                                                           #
7 #   IeeeCC754 or IEEE 754 Compliance Checker is a precision and range    #
8 #   independent tool to test whether an implementation of                #
9 #   floating-point arithmetic (in hardware or software) is compliant     #
10 #   with the principles of the IEEE 754-854 floating-point standards.    #
11 #   You can find out more about the testing tool IeeeCC754 at            #
12 #                                                                        #
13 #         http://win-www.uia.ac.be/u/cant/ieeecc754.html                 #
14 #                                                                        #
15 #   This tool is in parts based on and greatly benefited from the        #
16 #   the program FPTEST developed by Jerome Coonen. For a full            #
17 #   description of the extensions to FPTEST and a reference to           #
18 #   the original Coonen program, please refer to the URL given above.    #
19 #   For the options available with the program IeeeCC754 and its         #
20 #   compatibility with David Hough's hexadecimal UCB format, we          #
21 #   also refer to the file readme.usage.                                 #
22 #                                                                        #
23 #  Usage: see readme.usage                                               #
24 #                                                                        #
25 #  Responsible authors:                                                  #
26 #         Brigitte Verdonk                                               #
27 #         Annie Cuyt                                                     #
28 #                                                                        #
29 #  Contributors:                                                         #
30 #         Johan Bogo (1998-1999)                                         #
31 #         Tim Gevers (10-12/2000)                                        #
32 #         Debby Ooms (1996-1997)                                         #
33 #         Geert Vermuyten (1996-1997)                                    #
34 #         Dennis Verschaeren (09/1996-06/2000)                           #
35 #                                                                        #
36 #  Copyright (C) 2000  University of Antwerp                             #
37 #                                                                        #
38 #  This program can be obtained from the authors, free, but WITHOUT ANY  #
39 #  WARRANTY; without even the implied warranty of MERCHANTABILITY or     #
40 #  FITNESS FOR A PARTICULAR PURPOSE.                                     #
41 #                                                                        #
42 #  Contact:                                                              #
43 #       Brigitte.Verdonk@uia.ua.ac.be                                    #
44 #       Department of Mathematics and Computer Science                   #
45 #       University of Antwerp (UIA)                                      #
46 #       Universiteitsplein 1                                             #
47 #       B2610 Antwerp, BELGIUM                                           #
48 #                                                                        #
49 ##########################################################################
50
51 Filename:
52        $RCSfile$
53
54 Last updated:
55        $Date$
56
57 */
58
59 /***************************************************************************
60 * This is a class definition for floating point numbers (IEEE 754).
61 * It uses the bitstring class for storing the exponent and the mantissa.
62 ************************************************************************** */
63
64 #ifndef _FP_H
65 #define _FP_H
66 #include <stdio.h>
67
68 #include <Bitstring.h>
69 #include <Hex.h>
70
71 #define RM_NEAR   0
72 #define RM_UP     2
73 #define RM_DOWN   1
74 #define RM_ZERO   3
75
76
77 #define NOT_KNOWN        100
78
79 #ifndef MYBIG_ENDIAN
80   #define MYBIG_ENDIAN     101
81 #endif
82
83 #ifndef MYLITTLE_ENDIAN
84   #define MYLITTLE_ENDIAN  102
85 #endif
86
87 #define maxstr 5000
88 /*  maximum chars for binary to decimal conversion */
89
90 /**This is an abstract class defining floating-points as described in the IEEE 754 standard. It uses the Bitstring class to represent the mantissa and exponent
91 separately. The hidden bit is always present in the mantissa and needs to be defined for input and output in a hexadecimal form.\\
92 \\
93 There are three functions that need to be overloaded: SetRound(), GetExceptions(), SetEnvironment(). The first one must set the rounding mode of the library,
94 according to the rounding mode stored in the environment \textit{fpEnv}. This
95 can be done by calling the function GetFPRound() which returns the value actual rounding mode. GetExceptions() needs to convert the exceptions stored in the
96 testing library to the exceptions located in the environment \textit{fpEnv}.
97 To set the exceptions in the environment the functions: SetFPDivByZero(), SetFPInvalid(), SetFPUnderflow(), SetFPOverflow(), SetFPInexact() can be used.
98 The last function SetEnvironment() must set the library default environment.
99
100
101 @author Bogo Johan*/
102
103 class FP
104 {
105
106 protected:
107   /* * Little or Big Endian */ */
108   static int Endian;
109
110   /**The representation of a floating-point environment. \\
111   Bit positions and values:  \begin{itemize}
112       \item 0: divide by zero
113       \item 1: invalid
114       \item 2: underflow
115       \item 3: overflow
116       \item 4: inexact
117       \item 21 - 22 : rounding mode
118        \begin{itemize}
119         \item 00: round to nearest
120         \item 01: round up
121         \item 10: round down
122         \item 11: round to zero
123       \end{itemize}
124     \end{itemize}  */
125   static Bitstring fpEnv;
126
127   /* /The sign of the floating-point number */
128   int sign;
129   /* /Hidden bit (yes/no) */
130   int hidden;
131   /* * The size of the mantissa */ */
132   int sizeMant;
133   /* * The mantissa */ */
134   Bitstring mant;
135   /* * The exponent */ */
136   Bitstring exp;
137
138
139   /**Checks if the hardware is big or little endian. This function is only
140     needed to correctly convert a FP to a hardware floating-point*/
141   static void CheckEndian();
142
143   /**Set the library rounding mode. This member function needs to be
144     overloaded when testing a floating-point implementation. The tester should
145     call his library function(s) to set the rounding mode corresponding the
146     one saved in fpEnv.*/
147   void SetLibRound(){};
148   /**Get library exceptions. This member functions needs to be overloaded
149     when testing a floating-point implementation. The tester should call his
150     function(s) to get the occured exceptions in his library and set them in
151     the fpEnv with SetFPDivByZero(), SetFPInvalid(),...*/
152   void GetLibExceptions(){};
153   /**Get FP rounding mode stored in fpEnv
154    @return the rounding mode \\ \begin{tabular}{ccl}  0 &:& round to nearest $(RM\_NEAR)$  \\
155                                   1 &:& round up (RM\_UP) \\
156                 2 &:& round down $(RM\_DOWN)$ \\
157           3 &:& round to zero $(RM\_ZERO)$
158                               \end{tabular}
159    */
160   int GetFPRound();
161
162   /* * Sets the divide by zero exception in fpEnv */ */
163   void SetFPDivByZero(){fpEnv.Set(0);}
164   /* * Sets the invalid exception in fpEnv */ */
165   void SetFPInvalid()  {fpEnv.Set(1);}
166   /* * Sets the underflow exception in fpEnv */ */
167   void SetFPUnderflow(){fpEnv.Set(2);}
168   /* * Sets the overflow exception in fpEnv */ */
169   void SetFPOverflow() {fpEnv.Set(3);}
170   /* * Sets the inexact exception in fpEnv */ */
171   void SetFPInexact()  {fpEnv.Set(4);}
172
173
174
175 public:
176
177   /* * The size of the exponent */ */
178   int sizeExp;
179   /* * decimal */ */
180   char *decimal; /*  binary to decimal conversion */
181
182   /* * Constructor */ */
183   FP();
184   /**Constructor setting the size of the exponent, the mantissa and the
185     hidden bit
186     @param sizeM  the size of the mantissa
187     @param sizeE  the size of the exponent
188     @param hiddenbit if there is a hidden bit (0: false / 1: true)*/
189   FP(int sizeM, int sizeE,int hiddenbit=0);
190   /**Constructor setting the size of the exponent,the mantissa and the
191     hidden bit. It initializes the floating-point number with a bitstring.
192     The bitstring must have the following form (sign exp (h) mant)
193     @param fp  a bitstring containing the hardware reprecentation
194                of a floating-point
195     @param sizeM  the size of the mantissa
196     @param sizeE  the size of the exponent
197     @param hiddenbit if there is a hidden bit (0: false / 1: true) */
198   FP(Bitstring &fp,int sizeM, int sizeE,int hiddenbit=0);
199   /**Copy constructor
200    @param copy  an other FP object*/
201   FP(FP & copy);
202
203   /* Destructor */
204   virtual~FP(){};
205
206   /* * returns true (1) if there is a hidden bit */ */
207   int Hidden(){return hidden;};
208   /**returns or sets the sign
209     @param  sgn  the new sign \\ \begin{tabular}{rcl} 1 &:& negative \\
210                                                    0 &:& positive \\
211                -1 &:& no change
212                               \end{tabular}
213    @return the current sign or the previous depending \it{sgn}
214                */
215   int Sign(int sgn=-1);
216
217   /**Returns the mantissa
218    @return the mantissa hidden bit included (if defined)*/
219   Bitstring & GetMantissa();
220   /**Returns the exponent
221    @return the exponent*/
222   Bitstring & GetExponent();
223   /**Sets the mantissa
224     @param mantissa the new mantissa including the hidden bit (if defined)
225     @return the previous mantissa*/
226   Bitstring   PutMantissa(Bitstring &mantissa);
227   /**Sets the exponent
228     @param exponent the new exponent.
229     @return the previous exponent.*/
230   Bitstring   PutExponent(Bitstring &exponent);
231
232   /**Checks if the floating-point is a Zero
233     @return  \begin{tabular}{rcl} 1 &:& is a Zero\\
234                                    0 &:& is not a Zero
235     \end{tabular}*/
236   int IsZero();
237
238   /**Checks if the floating-point is a negative Zero
239     @return  \begin{tabular}{rcl} 1 &:& is a neg Zero\\
240                                    0 &:& is not a neg Zero
241     \end{tabular}*/
242   int IsNegZero();
243
244   /**Checks if the floating-point is a NaN
245     @return  \begin{tabular}{rcl} 1 &:& is a NaN \\
246                                    0 &:& is not a NaN
247     \end{tabular}*/
248   int IsNaN();
249   /**Creates a quiet NaN   */
250   void CreateQFPNan();
251
252   /**Assignment operator
253    @param copy an other FP object to be copied
254    @return *this*/
255   FP& operator = (const FP &copy);
256   /**Assignment operator
257     @param copy an Bitstring object to be converted to a FP object
258     @return *this*/
259   FP& operator = (const Bitstring &copy);
260
261   /**Set the default library environment rounding mode. This member function
262     needs to be overloaded when testing a floating-point implementation. The
263     tester should call his library function(s) to set the default library
264     environment. Normaly this means to clear the exception flags and restoring
265     the rounding mode to round to nearest.*/
266   void SetLibEnvironment(){};
267
268   /** Sets the rounding mode in the fpEnv to rm
269     @param rm next values a defined \\ \begin{tabular}{rcl} 0 &:& round to nearest $(RM\_NEAR)$  \\
270                                   1 &:& round up (RM\_UP) \\
271                 2 &:& round down $(RM\_DOWN)$ \\
272           3 &:& round to zero $(RM\_ZERO)$
273                               \end{tabular}
274     */
275   void SetFPRound (int rm);
276   /* * Clears the environment (fpEnv) */ */
277   void ClearFPEnvironment();
278   /**Returns the exceptions stored in fpEnv as a Bitstring
279    @return a Bitstring with next bit positions set, depending the exception \\\begin{tabular}{rcl}
280        0 &:& divide by zero\\
281        1 &:& invalid\\
282        2 &:& underflow\\
283              3 &:& overflow\\
284        4 &:& inexact
285     \end{tabular} */
286   void GetFPExceptions(Bitstring);
287
288
289   /**Chekcs if there has occured a divide by zero exception
290     @return\begin{tabular}{rcl} 1 &:& divide by zero exception\\
291                                 0 &:& no divide by zero exception
292     \end{tabular}*/
293   int GetFPDivByZero(){return fpEnv.GetBit(0);}
294   /**chekcs if there has occured a invalid exception
295     @return\begin{tabular}{rcl} 1 &:& invalid exception  \\
296                                  0 &:& no invalid exception
297     \end{tabular}*/
298   int GetFPInvalid()  {/* cout << "fpEnv.GetBit(1)" << fpEnv.GetBit(1); */
299    return fpEnv.GetBit(1);}
300   /**chekcs if there has occured a underflow exception
301     @return\begin{tabular}{rcl} 1 &:& underflow exception \\
302                                 0 &:& no underflow exception
303     \end{tabular}*/
304   int GetFPUnderflow(){return fpEnv.GetBit(2);}
305   /**chekcs if there has occured a overflow exception
306    @return\begin{tabular}{rcl} 1 &:& overflow exception \\
307                                0 &:& no overflow exception
308     \end{tabular}*/
309   int GetFPOverflow() {return fpEnv.GetBit(3);}
310   /**chekcs if there has occured a inexact exception
311    @return\begin{tabular}{rcl} 1 &:& inexact exception \\
312                                0 &:& no inexact exception
313     \end{tabular}*/
314   int GetFPInexact()  {return fpEnv.GetBit(4);}
315   int istiny();
316   int isInf();
317   int isNan();
318
319   /* * Overloaded output operator */ */
320   friend ostream& operator << (ostream& outs, FP &outstr);
321
322   friend istream& operator >> (istream& ins, FP &instr);
323
324 };
325
326 /* @Include: MyFloat.h MyDouble.h MyQuad.h FpSim.h Bitstring.h UCB.h dlist.h stack.h ../Calculator/FPcalculator.h */
327
328 #endif