simplify Sel lowering code
[libfirm] / ir / tv / IeeeCC754 / src / hex.cc
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 #include <Hex.h>
59
60 #ifdef IntelPentium
61 #include <string.h>
62 #endif
63
64 /*class implementation*/
65
66 /***********************************************************************
67 * Member:  GetBin(char v)
68 * Purpose: returns a Bitstring with the corresponding binary value of "v"
69 * Return:  Bitstring with binary value of "v"
70 ***********************************************************************/
71 void Hex::GetBin(char v)
72 {
73   // cout << "char = " << v << endl;
74   switch (v)
75     {
76     case '0':
77       bitstr[0] = 0;
78       // Bitstring::StringToBitstr("0000");
79       break;
80     case '1':
81       bitstr[0] = 1;
82       // StringToBitstr("0001");
83       break;
84     case '2':
85       bitstr[0] = 2;
86       // StringToBitstr("0010");
87       break;
88     case '3':
89       bitstr[0] = 3;
90       // StringToBitstr("0011");
91       break;
92     case '4':
93       bitstr[0] = 4;
94       // StringToBitstr("0100");
95       break;
96     case '5':
97       bitstr[0] = 5;
98       // StringToBitstr("0101");
99       break;
100     case '6':
101       bitstr[0] = 6;
102       // StringToBitstr("0110");
103       break;
104     case '7':
105       bitstr[0] = 7;
106       // StringToBitstr("0111");
107       break;
108     case '8':
109       bitstr[0] = 8;
110       // StringToBitstr("1000");
111       break;
112     case '9':
113       bitstr[0] = 9;
114       // StringToBitstr("1001");
115       break;
116     case 'A':
117     case 'a':
118       bitstr[0] = 10;
119       // StringToBitstr("1010");
120       break;
121     case 'B':
122     case 'b':
123       bitstr[0] = 11;
124       // StringToBitstr("1011");
125       break;
126     case 'C':
127     case 'c':
128       bitstr[0] = 12;
129       // StringToBitstr("1100");
130       break;
131     case 'D':
132     case 'd':
133       bitstr[0] = 13;
134       // StringToBitstr("1101");
135       break;
136     case 'E':
137     case 'e':
138       bitstr[0] = 14;
139       // StringToBitstr("1110");
140       break;
141     case 'F':
142     case 'f':
143       bitstr[0] = 15;
144       // StringToBitstr("1111");
145       break;
146     }
147 }
148
149
150 /***********************************************************************
151 * Member:  GetHex(Bitstring &b)
152 * Purpose: It gives the character that is represented in "b"
153 * Return:  returns b as a character
154 ***********************************************************************/
155 char Hex::GetHex(Bitstring &b) const
156 {
157
158   Bitstring temp(4);
159
160
161   temp.StringToBitstr("0000");
162   if(temp == b)
163     return '0';
164
165   temp.StringToBitstr("0001");
166   if(temp == b)
167     return '1';
168
169   temp.StringToBitstr("0010");
170   if(temp== b)
171     return '2';
172
173   temp.StringToBitstr("0011");
174   if(temp == b)
175     return '3';
176
177   temp.StringToBitstr("0100");
178   if(temp == b)
179     return '4';
180
181   temp.StringToBitstr("0101");
182   if(temp == b)
183     return '5';
184
185   temp.StringToBitstr("0110");
186   if(temp == b)
187     return '6';
188
189   temp.StringToBitstr("0111");
190   if(temp == b)
191     return '7';
192
193   temp.StringToBitstr("1000");
194   if(temp == b)
195     return '8';
196
197   temp.StringToBitstr("1001");
198   if(temp == b)
199     return '9';
200
201   temp.StringToBitstr("1010");
202   if(temp == b)
203     return 'A';
204
205   temp.StringToBitstr("1011");
206   if(temp == b)
207     return 'B';
208
209   temp.StringToBitstr("1100") ;
210   if(temp == b)
211     return 'C';
212
213   temp.StringToBitstr("1101");
214   if(temp == b)
215     return 'D';
216
217   temp.StringToBitstr("1110");
218   if(temp == b)
219     return 'E';
220
221   temp.StringToBitstr("1111");
222   if(temp == b)
223     return 'F';
224
225   return '\0';
226
227 }
228
229
230 /***********************************************************************
231 * Member:  Hex()  Constructor
232 * Purpose: Create empty hexadecimal bitstring
233 * Return:  Nothing
234 ***********************************************************************/
235 Hex::Hex()
236 { }
237
238
239 /***********************************************************************
240 * Member:  Bitstring(unsigned long size)  Constructor
241 * Purpose: Create a hexadecimal bitstring of size "size"*4
242 * Return:  Nothing
243 ***********************************************************************/
244 Hex::Hex(unsigned long size)
245 {
246   Resize(size);
247 }
248
249
250 /***********************************************************************
251 * Member:  Hex(char * str)  Constructor
252 * Purpose: Create hexadecimal bitstring with "str" as initiale value
253 * Return:  Nothing
254 ***********************************************************************/
255 Hex::Hex(char *hstr)
256 {
257   long i,j;
258   Hex t1(4),t2(4);
259
260   length = strlen(hstr)*4;
261   lengthT= sizeof(unsigned long) * 8;
262   lengthBlock = (length/lengthT)+1;
263
264   bitstr = new unsigned long [lengthBlock];
265
266   for (i=0;i<lengthBlock;i++)
267     bitstr[i]=0;
268
269   j=0;
270   for (i= (length/4) -1; i >= 0; i--)
271     {
272       t1.GetBin(hstr[i--]);
273       if (i >= 0)
274   {
275     t2.GetBin(hstr[i]);
276           t2.Concat(t1);
277     PutByte(j,t2);
278   }
279       else
280     PutByte(j,t1);
281       j++;
282     }
283 }
284
285
286 /***********************************************************************
287 * Member:  Hex(Bitstring & copy)
288 * Purpose: Create a hexadecimal bitstring with initiale value the bitstring
289 *          "copy"
290 * Return:  Nothing
291 ***********************************************************************/
292 Hex::Hex(const Bitstring &copy)
293 {
294   int i=0;
295
296   length = copy.Length();
297   lengthT = sizeof(unsigned long)*8;
298   lengthBlock = (length /lengthT) +1;
299
300   bitstr = new unsigned long [lengthBlock];
301
302   for (i=0 ; i< lengthBlock; i++)
303     bitstr[i]= copy[i];
304
305   if ((length%4)!= 0)
306     Resize(length/4 +1);
307
308 }
309
310
311 /***********************************************************************
312 * Member:  Resize (unsigned long len)
313 * Purpose: Change the length of the bitstring to "len"*4. If the new length is
314 *          larger then the length, the bitstring is appended with 0, else
315 *          the bitstring is truncated to the new length "len"*4
316 * Return:  Previous length
317 ***********************************************************************/
318 unsigned long Hex::Resize(unsigned long len)
319 {
320   return Bitstring::Resize(len*4);
321 }
322
323 /***********************************************************************
324 * Member:  BitstrToString()
325 * Purpose: Converts a hexadecimal bitstring to a C-string
326 * Return:  length >0   ->  The C-string
327 *          else        ->  "Bitstring is empty"
328 ***********************************************************************/
329 void Hex::BitstrToString(char* out) const
330 {
331   Bitstring temp(4);
332   int i;
333
334   out= new char[(length/4)+2];
335   for (i = 0 ;i< length/4; i++)
336     {
337       // cout << "i = " << i << endl << flush;
338       SubBitstring(i*4,temp);
339       // cout << "temp = " << temp << endl << flush;
340       out[length/4 -i -1]=GetHex(temp);
341     }
342   out[(length/4)]='\0';
343 }
344
345 /***********************************************************************
346 * Member:  StringToBitstr(char *str)
347 * Purpose: Converts a C-string to a hexadecimal bitstring
348 * Return:  converted bitstring
349 ***********************************************************************/
350 void Hex::StringToBitstr(char *hstr)
351 {
352   long i,j,k;
353   Bitstring t1(4),t2(4);
354   unsigned long tmp;
355
356   length = strlen(hstr)*4;
357   lengthT= sizeof(unsigned long) * 8;
358   if (length % lengthT == 0)
359      lengthBlock = length/lengthT;
360   else
361      lengthBlock = (length/lengthT)+1;
362   if (bitstr)
363     delete [] bitstr;
364   // cout << "lengthBlock = " << lengthBlock << endl << flush;
365   bitstr = new unsigned long [lengthBlock];
366   for (i=0;i<lengthBlock;i++)
367     bitstr[i]=0;
368   j=0;
369   for (i= 0; i < length/4;)  {
370     // cout << "hstr[i] = " << hstr[i] << endl << flush;
371     tmp = 0;
372     for (k = 0; k < 7;k++) {
373         if (hstr[i] <= '9')
374       tmp += hstr[i++] - '0';
375         else
376             tmp += hstr[i++] - 'a' + 10;
377         // cout << hex << tmp << endl;
378         tmp *= 16;
379     }
380     if (hstr[i] <= '9')
381         tmp += hstr[i++] - '0';
382     else
383         tmp += hstr[i++] - 'a' + 10;
384     // cout << "tmp = " << hex << tmp << endl;
385     bitstr[j] = tmp;
386     // cout << hex << bitstr[j] << " " << flush;
387     j++;
388   }
389 /*
390 for (i= (length/4) -1; i >= 0; i--)
391     {
392       t1.GetBin(hstr[i--]);
393       // cout << "t1 = " << t1 << endl << flush;
394       if (i >= 0)
395   {
396     t2.GetBin(hstr[i]);
397           // cout << "t2 = " << t2 << endl << flush;
398           t2.Concat(t1);
399           // cout << "t2 = " << t2 << endl << flush;
400     PutByte(j,t2);
401   }
402       else
403     PutByte(j,t1);
404       j++;
405     }
406 */
407 }