forked from cseagle/blc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
float.cc
616 lines (527 loc) · 16.1 KB
/
float.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
/* ###
* IP: GHIDRA
* NOTE: uses some windows and sparc specific floating point definitions
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "float.hh"
#include <sstream>
#include <cmath>
#include "address.hh"
#if defined(_WINDOWS) && !defined(INFINITY)
// Some definitions for Windows floating point stuff
#include <cfloat>
inline int4 signbit(double x) {
return (((_fpclass(x)& (_FPCLASS_NINF | _FPCLASS_NN
| _FPCLASS_ND | _FPCLASS_NZ))!=0) ? 1 : 0);
}
inline int4 isnan(double x) {
return (((_fpclass(x)& (_FPCLASS_SNAN | _FPCLASS_QNAN))!=0) ? 1 : 0);
}
inline int4 isinf(double x) {
int4 classify = _fpclass(x);
if (classify == _FPCLASS_NINF) return -1;
if (classify == _FPCLASS_PINF) return 1;
return 0;
}
#define INFINITY HUGE_VAL
#define NAN (INFINITY/INFINITY)
#endif
/// Set format for a given encoding size according to IEEE 754 standards
/// \param sz is the size of the encoding in bytes
FloatFormat::FloatFormat(int4 sz)
{
size = sz;
if (size == 4) {
signbit_pos = 31;
exp_pos = 23;
exp_size = 8;
frac_pos = 0;
frac_size = 23;
bias = 127;
jbitimplied = true;
}
else if (size == 8) {
signbit_pos = 63;
exp_pos = 52;
exp_size = 11;
frac_pos = 0;
frac_size = 52;
bias = 1023;
jbitimplied = true;
}
maxexponent = (1<<exp_size)-1;
}
/// \param sign is set to \b true if the value should be negative
/// \param signif is the fractional part
/// \param exp is the exponent
/// \return the constructed floating-point value
double FloatFormat::createFloat(bool sign,uintb signif,int4 exp)
{
signif >>= 1; // Throw away 1 bit of precision we will
// lose anyway, to make sure highbit is 0
int4 precis = 8*sizeof(uintb) - 1; // fullword - 1 we threw away
double res = (double)signif;
int4 expchange = exp - precis + 1; // change in exponent is precis
// -1 integer bit
res = ldexp(res,expchange);
if (sign)
res = res * -1.0;
return res;
}
/// \brief Extract the sign, fractional, and exponent from a given floating-point value
///
/// \param x is the given value
/// \param sgn passes back the sign
/// \param signif passes back the fractional part
/// \param exp passes back the exponent
/// \return the floating-point class of the value
FloatFormat::floatclass FloatFormat::extractExpSig(double x,bool *sgn,uintb *signif,int4 *exp)
{
int4 e;
*sgn = (signbit(x) != 0);
if (x == 0.0) return zero;
if (isinf(x)!=0) return infinity;
if (isnan(x)!=0) return nan;
if (*sgn)
x = -x;
double norm = frexp(x,&e); // norm is between 1/2 and 1
norm = ldexp(norm,8*sizeof(uintb)-1); // norm between 2^62 and 2^63
*signif = (uintb)norm; // Convert to normalized integer
*signif <<= 1;
e -= 1; // Consider normalization between 1 and 2
*exp = e;
return normalized;
}
/// \param x is an encoded floating-point value
/// \return the fraction part of the value aligned to the top of the word
uintb FloatFormat::extractFractionalCode(uintb x) const
{
x >>= frac_pos; // Eliminate bits below
x <<= 8*sizeof(uintb) - frac_size; // Align with top of word
return x;
}
/// \param x is an encoded floating-point value
/// \return the sign bit
bool FloatFormat::extractSign(uintb x) const
{
x >>= signbit_pos;
return ((x&1)!=0);
}
/// \param x is an encoded floating-point value
/// \return the (signed) exponent
int4 FloatFormat::extractExponentCode(uintb x) const
{
x >>= exp_pos;
uintb mask = 1;
mask = (mask<<exp_size) - 1;
return (int4)(x & mask);
}
/// \param x is an encoded value (with fraction part set to zero)
/// \param code is the new fractional value to set
/// \return the encoded value with the fractional filled in
uintb FloatFormat::setFractionalCode(uintb x,uintb code) const
{
// Align with bottom of word, also drops bits of precision
// we don't have room for
code >>= 8*sizeof(uintb) - frac_size;
code <<= frac_pos; // Move bits into position;
x |= code;
return x;
}
/// \param x is an encoded value (with sign set to zero)
/// \param sign is the sign bit to set
/// \return the encoded value with the sign bit set
uintb FloatFormat::setSign(uintb x,bool sign) const
{
if (!sign) return x; // Assume bit is already zero
uintb mask = 1;
mask <<= signbit_pos;
x |= mask; // Stick in the bit
return x;
}
/// \param x is an encoded value (with exponent set to zero)
/// \param code is the exponent to set
/// \return the encoded value with the new exponent
uintb FloatFormat::setExponentCode(uintb x,uintb code) const
{
code <<= exp_pos; // Move bits into position
x |= code;
return x;
}
/// \param sgn is set to \b true for negative zero, \b false for positive
/// \return the encoded zero
uintb FloatFormat::getZeroEncoding(bool sgn) const
{
uintb res = 0;
// Use IEEE 754 standard for zero encoding
res = setFractionalCode(res,0);
res = setExponentCode(res,0);
return setSign(res,sgn);
}
/// \param sgn is set to \b true for negative infinity, \b false for positive
/// \return the encoded infinity
uintb FloatFormat::getInfinityEncoding(bool sgn) const
{
uintb res = 0;
// Use IEEE 754 standard for infinity encoding
res = setFractionalCode(res,0);
res = setExponentCode(res,(uintb)maxexponent);
return setSign(res,sgn);
}
/// \param sgn is set to \b true for negative NaN, \b false for positive
/// \return the encoded NaN
uintb FloatFormat::getNaNEncoding(bool sgn) const
{
uintb res = 0;
// Use IEEE 754 standard for NaN encoding
uintb mask = 1;
mask <<= 8*sizeof(uintb)-1; // Create "quiet" NaN
res = setFractionalCode(res,mask);
res = setExponentCode(res,(uintb)maxexponent);
return setSign(res,sgn);
}
/// \param encoding is the encoding value
/// \param type points to the floating-point class, which is passed back
/// \return the equivalent double value
double FloatFormat::getHostFloat(uintb encoding,floatclass *type) const
{
bool sgn = extractSign(encoding);
uintb frac = extractFractionalCode(encoding);
int4 exp = extractExponentCode(encoding);
bool normal = true;
if (exp == 0) {
if ( frac == 0 ) { // Floating point zero
*type = zero;
// FIXME: add on sign-bit for +0 or -0 allowed by standard
return sgn ? -0.0 : +0.0;
}
*type = denormalized;
// Number is denormalized
normal = false;
}
else if (exp == maxexponent) {
if ( frac == 0 ) { // Floating point infinity
*type = infinity;
// FIXME: add on sign-bit for +inf or -inf allowed by standard
return sgn ? -INFINITY : +INFINITY;
}
*type = nan;
// encoding is "Not a Number" NaN
return sgn ? -NAN : +NAN; // Sign is usually ignored
}
else
*type = normalized;
// Get "true" exponent and fractional
exp -= bias;
if (normal && jbitimplied) {
frac >>= 1; // Make room for 1 jbit
uintb highbit = 1;
highbit <<= 8*sizeof(uintb)-1;
frac |= highbit; // Stick bit in at top
}
return createFloat(sgn,frac,exp);
}
/// \param host is the double value to convert
/// \return the equivalent encoded value
uintb FloatFormat::getEncoding(double host) const
{
floatclass type;
bool sgn;
uintb signif;
int4 exp;
type = extractExpSig(host,&sgn,&signif,&exp);
if (type == zero)
return getZeroEncoding(sgn);
else if (type == infinity)
return getInfinityEncoding(sgn);
else if (type == nan)
return getNaNEncoding(sgn);
// convert exponent and fractional to their encodings
exp += bias;
if (exp < 0) // Exponent is too small to represent
return getZeroEncoding(sgn);
if (exp > maxexponent) // Exponent is too big to represent
return getInfinityEncoding(sgn);
if (jbitimplied && (exp !=0))
signif <<= 1; // Cut off top bit (which should be 1)
uintb res = 0;
res = setFractionalCode(res,signif);
res = setExponentCode(res,(uintb)exp);
return setSign(res,sgn);
}
/// \param encoding is the value in the \e other FloatFormat
/// \param formin is the \e other FloatFormat
/// \return the equivalent value in \b this FloatFormat
uintb FloatFormat::convertEncoding(uintb encoding,const FloatFormat *formin) const
{
bool sgn = formin->extractSign(encoding);
uintb frac = formin->extractFractionalCode(encoding);
int4 exp = formin->extractExponentCode(encoding);
if (exp == formin->maxexponent) { // NaN or INFINITY encoding
exp = maxexponent;
}
else {
exp -= formin->bias;
exp += bias;
if (exp < 0)
return getZeroEncoding(sgn);
if (exp > maxexponent)
return getInfinityEncoding(sgn);
}
if (jbitimplied && !formin->jbitimplied)
frac <<= 1; // Cut off top bit (which should be 1)
else if (formin->jbitimplied && !jbitimplied) {
frac >>= 1; // Make room for 1 jbit
uintb highbit = 1;
highbit <<= 8*sizeof(uintb)-1;
frac |= highbit; // Stick bit in at top
}
uintb res = 0;
res = setFractionalCode(res,frac);
res = setExponentCode(res,(uintb)exp);
return setSign(res,sgn);
}
// Currently we emulate floating point operations on the target
// By converting the encoding to the host's encoding and then
// performing the operation using the host's floating point unit
// then the host's encoding is converted back to the targets encoding
/// \param a is the first floating-point value
/// \param b is the second floating-point value
/// \return \b true if (a == b)
uintb FloatFormat::opEqual(uintb a,uintb b) const
{
floatclass type;
double val1 = getHostFloat(a,&type);
double val2 = getHostFloat(b,&type);
uintb res = (val1 == val2) ? 1 : 0;
return res;
}
/// \param a is the first floating-point value
/// \param b is the second floating-point value
/// \return \b true if (a != b)
uintb FloatFormat::opNotEqual(uintb a,uintb b) const
{
floatclass type;
double val1 = getHostFloat(a,&type);
double val2 = getHostFloat(b,&type);
uintb res = (val1 != val2) ? 1 : 0;
return res;
}
/// \param a is the first floating-point value
/// \param b is the second floating-point value
/// \return \b true if (a < b)
uintb FloatFormat::opLess(uintb a,uintb b) const
{
floatclass type;
double val1 = getHostFloat(a,&type);
double val2 = getHostFloat(b,&type);
uintb res = (val1 < val2) ? 1 : 0;
return res;
}
/// \param a is the first floating-point value
/// \param b is the second floating-point value
/// \return \b true if (a <= b)
uintb FloatFormat::opLessEqual(uintb a,uintb b) const
{
floatclass type;
double val1 = getHostFloat(a,&type);
double val2 = getHostFloat(b,&type);
uintb res = (val1 <= val2) ? 1 : 0;
return res;
}
/// \param a is an encoded floating-point value
/// \return \b true if a is Not-a-Number
uintb FloatFormat::opNan(uintb a) const
{
floatclass type;
getHostFloat(a,&type);
uintb res = (type == FloatFormat::nan) ? 1 : 0;
return res;
}
/// \param a is the first floating-point value
/// \param b is the second floating-point value
/// \return a + b
uintb FloatFormat::opAdd(uintb a,uintb b) const
{
floatclass type;
double val1 = getHostFloat(a,&type);
double val2 = getHostFloat(b,&type);
return getEncoding(val1 + val2);
}
/// \param a is the first floating-point value
/// \param b is the second floating-point value
/// \return a / b
uintb FloatFormat::opDiv(uintb a,uintb b) const
{
floatclass type;
double val1 = getHostFloat(a,&type);
double val2 = getHostFloat(b,&type);
return getEncoding(val1 / val2);
}
/// \param a is the first floating-point value
/// \param b is the second floating-point value
/// \return a * b
uintb FloatFormat::opMult(uintb a,uintb b) const
{
floatclass type;
double val1 = getHostFloat(a,&type);
double val2 = getHostFloat(b,&type);
return getEncoding(val1 * val2);
}
/// \param a is the first floating-point value
/// \param b is the second floating-point value
/// \return a - b
uintb FloatFormat::opSub(uintb a,uintb b) const
{
floatclass type;
double val1 = getHostFloat(a,&type);
double val2 = getHostFloat(b,&type);
return getEncoding(val1 - val2);
}
/// \param a is an encoded floating-point value
/// \return -a
uintb FloatFormat::opNeg(uintb a) const
{
floatclass type;
double val = getHostFloat(a,&type);
return getEncoding(-val);
}
/// \param a is an encoded floating-point value
/// \return abs(a)
uintb FloatFormat::opAbs(uintb a) const
{
floatclass type;
double val = getHostFloat(a,&type);
return getEncoding(fabs(val));
}
/// \param a is an encoded floating-point value
/// \return sqrt(a)
uintb FloatFormat::opSqrt(uintb a) const
{
floatclass type;
double val = getHostFloat(a,&type);
return getEncoding(sqrt(val));
}
/// \param a is a signed integer value
/// \param sizein is the number of bytes in the integer encoding
/// \return a converted to an encoded floating-point value
uintb FloatFormat::opInt2Float(uintb a,int4 sizein) const
{
intb ival = (intb)a;
sign_extend(ival,8*sizein-1);
double val = (double) ival; // Convert integer to float
return getEncoding(val);
}
/// \param a is an encoded floating-point value
/// \param outformat is the desired output FloatFormat
/// \return a converted to the output FloatFormat
uintb FloatFormat::opFloat2Float(uintb a,const FloatFormat &outformat) const
{
floatclass type;
double val = getHostFloat(a,&type);
return outformat.getEncoding(val);
}
/// \param a is an encoded floating-point value
/// \param sizeout is the desired encoding size of the output
/// \return an integer encoding of a
uintb FloatFormat::opTrunc(uintb a,int4 sizeout) const
{
floatclass type;
double val = getHostFloat(a,&type);
intb ival = (intb) val; // Convert to integer
uintb res = (uintb) ival; // Convert to unsigned
res &= calc_mask(sizeout); // Truncate to proper size
return res;
}
/// \param a is an encoded floating-point value
/// \return ceil(a)
uintb FloatFormat::opCeil(uintb a) const
{
floatclass type;
double val = getHostFloat(a,&type);
return getEncoding(ceil(val));
}
/// \param a is an encoded floating-point value
/// \return floor(a)
uintb FloatFormat::opFloor(uintb a) const
{
floatclass type;
double val = getHostFloat(a,&type);
return getEncoding(floor(val));
}
/// \param a is an encoded floating-point value
/// \return round(a)
uintb FloatFormat::opRound(uintb a) const
{
floatclass type;
double val = getHostFloat(a,&type);
return getEncoding(floor(val+0.5));
}
/// Write the format out to a \<floatformat> XML tag.
/// \param s is the output stream
void FloatFormat::saveXml(ostream &s) const
{
s << "<floatformat";
a_v_i(s,"size",size);
a_v_i(s,"signpos",signbit_pos);
a_v_i(s,"fracpos",frac_pos);
a_v_i(s,"fracsize",frac_size);
a_v_i(s,"exppos",exp_pos);
a_v_i(s,"expsize",exp_size);
a_v_i(s,"bias",bias);
a_v_b(s,"jbitimplied",jbitimplied);
s << "/>\n";
}
/// Restore \b object from a \<floatformat> XML tag
/// \param el is the element
void FloatFormat::restoreXml(const Element *el)
{
{
istringstream s(el->getAttributeValue("size"));
s.unsetf(ios::dec | ios::hex | ios::oct);
s >> size;
}
{
istringstream s(el->getAttributeValue("signpos"));
s.unsetf(ios::dec | ios::hex | ios::oct);
s >> signbit_pos;
}
{
istringstream s(el->getAttributeValue("fracpos"));
s.unsetf(ios::dec | ios::hex | ios::oct);
s >> frac_pos;
}
{
istringstream s(el->getAttributeValue("fracsize"));
s.unsetf(ios::dec | ios::hex | ios::oct);
s >> frac_size;
}
{
istringstream s(el->getAttributeValue("exppos"));
s.unsetf(ios::dec | ios::hex | ios::oct);
s >> exp_pos;
}
{
istringstream s(el->getAttributeValue("expsize"));
s.unsetf(ios::dec | ios::hex | ios::oct);
s >> exp_size;
}
{
istringstream s(el->getAttributeValue("bias"));
s.unsetf(ios::dec | ios::hex | ios::oct);
s >> bias;
}
jbitimplied = xml_readbool(el->getAttributeValue("jbitimplied"));
maxexponent = (1<<exp_size)-1;
}