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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
|
/* -----------------------------------------------------------------------------
*
* (c) The GHC Team, 1998-2000
*
* Miscellaneous support for floating-point primitives
*
* ---------------------------------------------------------------------------*/
#include "PosixSource.h"
#include "Rts.h"
#include <math.h>
/*
* Encoding and decoding Doubles. Code based on the HBC code
* (lib/fltcode.c).
*/
#ifdef _SHORT_LIMB
#define SIZEOF_LIMB_T SIZEOF_UNSIGNED_INT
#else
#ifdef _LONG_LONG_LIMB
#define SIZEOF_LIMB_T SIZEOF_UNSIGNED_LONG_LONG
#else
#define SIZEOF_LIMB_T SIZEOF_UNSIGNED_LONG
#endif
#endif
#if SIZEOF_LIMB_T == 4
#define GMP_BASE 4294967296.0
#elif SIZEOF_LIMB_T == 8
#define GMP_BASE 18446744073709551616.0
#else
#error Cannot cope with SIZEOF_LIMB_T -- please add definition of GMP_BASE
#endif
#define DNBIGIT ((SIZEOF_DOUBLE+SIZEOF_LIMB_T-1)/SIZEOF_LIMB_T)
#define FNBIGIT ((SIZEOF_FLOAT +SIZEOF_LIMB_T-1)/SIZEOF_LIMB_T)
#if IEEE_FLOATING_POINT
#define MY_DMINEXP ((DBL_MIN_EXP) - (DBL_MANT_DIG) - 1)
/* DMINEXP is defined in values.h on Linux (for example) */
#define DHIGHBIT 0x00100000
#define DMSBIT 0x80000000
#define MY_FMINEXP ((FLT_MIN_EXP) - (FLT_MANT_DIG) - 1)
#define FHIGHBIT 0x00800000
#define FMSBIT 0x80000000
#endif
#if defined(WORDS_BIGENDIAN) || defined(FLOAT_WORDS_BIGENDIAN)
#define L 1
#define H 0
#else
#define L 0
#define H 1
#endif
#define __abs(a) (( (a) >= 0 ) ? (a) : (-(a)))
StgDouble
__encodeDouble (I_ size, StgByteArray ba, I_ e) /* result = s * 2^e */
{
StgDouble r;
const mp_limb_t *const arr = (const mp_limb_t *)ba;
I_ i;
/* Convert MP_INT to a double; knows a lot about internal rep! */
for(r = 0.0, i = __abs(size)-1; i >= 0; i--)
r = (r * GMP_BASE) + arr[i];
/* Now raise to the exponent */
if ( r != 0.0 ) /* Lennart suggests this avoids a bug in MIPS's ldexp */
r = ldexp(r, e);
/* sign is encoded in the size */
if (size < 0)
r = -r;
return r;
}
StgDouble
__2Int_encodeDouble (I_ j_high, I_ j_low, I_ e)
{
StgDouble r;
/* assuming 32 bit ints */
ASSERT(sizeof(int ) == 4 );
r = (StgDouble)((unsigned int)j_high);
r *= 4294967296.0; /* exp2f(32); */
r += (StgDouble)((unsigned int)j_low);
/* Now raise to the exponent */
if ( r != 0.0 ) /* Lennart suggests this avoids a bug in MIPS's ldexp */
r = ldexp(r, e);
/* sign is encoded in the size */
if (j_high < 0)
r = -r;
return r;
}
/* Special version for words */
StgDouble
__word_encodeDouble (W_ j, I_ e)
{
StgDouble r;
r = (StgDouble)j;
/* Now raise to the exponent */
if ( r != 0.0 ) /* Lennart suggests this avoids a bug in MIPS's ldexp */
r = ldexp(r, e);
return r;
}
/* Special version for small Integers */
StgDouble
__int_encodeDouble (I_ j, I_ e)
{
StgDouble r;
r = (StgDouble)__abs(j);
/* Now raise to the exponent */
if ( r != 0.0 ) /* Lennart suggests this avoids a bug in MIPS's ldexp */
r = ldexp(r, e);
/* sign is encoded in the size */
if (j < 0)
r = -r;
return r;
}
StgFloat
__encodeFloat (I_ size, StgByteArray ba, I_ e) /* result = s * 2^e */
{
StgFloat r;
const mp_limb_t *arr = (const mp_limb_t *)ba;
I_ i;
/* Convert MP_INT to a float; knows a lot about internal rep! */
for(r = 0.0, i = __abs(size)-1; i >= 0; i--)
r = (r * GMP_BASE) + arr[i];
/* Now raise to the exponent */
if ( r != 0.0 ) /* Lennart suggests this avoids a bug in MIPS's ldexp */
r = ldexp(r, e);
/* sign is encoded in the size */
if (size < 0)
r = -r;
return r;
}
/* Special version for small Integers */
StgFloat
__int_encodeFloat (I_ j, I_ e)
{
StgFloat r;
r = (StgFloat)__abs(j);
/* Now raise to the exponent */
if ( r != 0.0 ) /* Lennart suggests this avoids a bug in MIPS's ldexp */
r = ldexp(r, e);
/* sign is encoded in the size */
if (j < 0)
r = -r;
return r;
}
/* Special version for small positive Integers */
StgFloat
__word_encodeFloat (W_ j, I_ e)
{
StgFloat r;
r = (StgFloat)j;
/* Now raise to the exponent */
if ( r != 0.0 ) /* Lennart suggests this avoids a bug in MIPS's ldexp */
r = ldexp(r, e);
return r;
}
/* This only supports IEEE floating point */
void
__decodeDouble (MP_INT *man, I_ *exp, StgDouble dbl)
{
/* Do some bit fiddling on IEEE */
unsigned int low, high; /* assuming 32 bit ints */
int sign, iexp;
union { double d; unsigned int i[2]; } u; /* assuming 32 bit ints, 64 bit double */
ASSERT(sizeof(unsigned int ) == 4 );
ASSERT(sizeof(dbl ) == SIZEOF_DOUBLE);
ASSERT(sizeof(man->_mp_d[0]) == SIZEOF_LIMB_T);
ASSERT(DNBIGIT*SIZEOF_LIMB_T >= SIZEOF_DOUBLE);
u.d = dbl; /* grab chunks of the double */
low = u.i[L];
high = u.i[H];
/* we know the MP_INT* passed in has size zero, so we realloc
no matter what.
*/
man->_mp_alloc = DNBIGIT;
if (low == 0 && (high & ~DMSBIT) == 0) {
man->_mp_size = 0;
*exp = 0L;
} else {
man->_mp_size = DNBIGIT;
iexp = ((high >> 20) & 0x7ff) + MY_DMINEXP;
sign = high;
high &= DHIGHBIT-1;
if (iexp != MY_DMINEXP) /* don't add hidden bit to denorms */
high |= DHIGHBIT;
else {
iexp++;
/* A denorm, normalize the mantissa */
while (! (high & DHIGHBIT)) {
high <<= 1;
if (low & DMSBIT)
high++;
low <<= 1;
iexp--;
}
}
*exp = (I_) iexp;
#if DNBIGIT == 2
man->_mp_d[0] = (mp_limb_t)low;
man->_mp_d[1] = (mp_limb_t)high;
#else
#if DNBIGIT == 1
man->_mp_d[0] = ((mp_limb_t)high) << 32 | (mp_limb_t)low;
#else
#error Cannot cope with DNBIGIT
#endif
#endif
if (sign < 0)
man->_mp_size = -man->_mp_size;
}
}
void
__decodeDouble_2Int (I_ *man_sign, W_ *man_high, W_ *man_low, I_ *exp, StgDouble dbl)
{
/* Do some bit fiddling on IEEE */
unsigned int low, high; /* assuming 32 bit ints */
int sign, iexp;
union { double d; unsigned int i[2]; } u; /* assuming 32 bit ints, 64 bit double */
ASSERT(sizeof(unsigned int ) == 4 );
ASSERT(sizeof(dbl ) == 8 );
ASSERT(sizeof(dbl ) == SIZEOF_DOUBLE);
u.d = dbl; /* grab chunks of the double */
low = u.i[L];
high = u.i[H];
if (low == 0 && (high & ~DMSBIT) == 0) {
*man_low = 0;
*man_high = 0;
*exp = 0L;
} else {
iexp = ((high >> 20) & 0x7ff) + MY_DMINEXP;
sign = high;
high &= DHIGHBIT-1;
if (iexp != MY_DMINEXP) /* don't add hidden bit to denorms */
high |= DHIGHBIT;
else {
iexp++;
/* A denorm, normalize the mantissa */
while (! (high & DHIGHBIT)) {
high <<= 1;
if (low & DMSBIT)
high++;
low <<= 1;
iexp--;
}
}
*exp = (I_) iexp;
*man_low = low;
*man_high = high;
*man_sign = (sign < 0) ? -1 : 1;
}
}
void
__decodeFloat (MP_INT *man, I_ *exp, StgFloat flt)
{
/* Do some bit fiddling on IEEE */
int high, sign; /* assuming 32 bit ints */
union { float f; int i; } u; /* assuming 32 bit float and int */
ASSERT(sizeof(int ) == 4 );
ASSERT(sizeof(flt ) == SIZEOF_FLOAT );
ASSERT(sizeof(man->_mp_d[0]) == SIZEOF_LIMB_T);
ASSERT(FNBIGIT*SIZEOF_LIMB_T >= SIZEOF_FLOAT );
u.f = flt; /* grab the float */
high = u.i;
/* we know the MP_INT* passed in has size zero, so we realloc
no matter what.
*/
man->_mp_alloc = FNBIGIT;
if ((high & ~FMSBIT) == 0) {
man->_mp_size = 0;
*exp = 0;
} else {
man->_mp_size = FNBIGIT;
*exp = ((high >> 23) & 0xff) + MY_FMINEXP;
sign = high;
high &= FHIGHBIT-1;
if (*exp != MY_FMINEXP) /* don't add hidden bit to denorms */
high |= FHIGHBIT;
else {
(*exp)++;
/* A denorm, normalize the mantissa */
while (! (high & FHIGHBIT)) {
high <<= 1;
(*exp)--;
}
}
#if FNBIGIT == 1
man->_mp_d[0] = (mp_limb_t)high;
#else
#error Cannot cope with FNBIGIT
#endif
if (sign < 0)
man->_mp_size = -man->_mp_size;
}
}
/* Convenient union types for checking the layout of IEEE 754 types -
based on defs in GNU libc <ieee754.h>
*/
void
__decodeFloat_Int (I_ *man, I_ *exp, StgFloat flt)
{
/* Do some bit fiddling on IEEE */
int high, sign; /* assuming 32 bit ints */
union { float f; int i; } u; /* assuming 32 bit float and int */
ASSERT(sizeof(int ) == 4 );
ASSERT(sizeof(flt ) == 4 );
ASSERT(sizeof(flt ) == SIZEOF_FLOAT );
u.f = flt; /* grab the float */
high = u.i;
if ((high & ~FMSBIT) == 0) {
*man = 0;
*exp = 0;
} else {
*exp = ((high >> 23) & 0xff) + MY_FMINEXP;
sign = high;
high &= FHIGHBIT-1;
if (*exp != MY_FMINEXP) /* don't add hidden bit to denorms */
high |= FHIGHBIT;
else {
(*exp)++;
/* A denorm, normalize the mantissa */
while (! (high & FHIGHBIT)) {
high <<= 1;
(*exp)--;
}
}
*man = high;
if (sign < 0)
*man = - *man;
}
}
union stg_ieee754_flt
{
float f;
struct {
#if WORDS_BIGENDIAN
unsigned int negative:1;
unsigned int exponent:8;
unsigned int mantissa:23;
#else
unsigned int mantissa:23;
unsigned int exponent:8;
unsigned int negative:1;
#endif
} ieee;
struct {
#if WORDS_BIGENDIAN
unsigned int negative:1;
unsigned int exponent:8;
unsigned int quiet_nan:1;
unsigned int mantissa:22;
#else
unsigned int mantissa:22;
unsigned int quiet_nan:1;
unsigned int exponent:8;
unsigned int negative:1;
#endif
} ieee_nan;
};
/*
To recap, here's the representation of a double precision
IEEE floating point number:
sign 63 sign bit (0==positive, 1==negative)
exponent 62-52 exponent (biased by 1023)
fraction 51-0 fraction (bits to right of binary point)
*/
union stg_ieee754_dbl
{
double d;
struct {
#if WORDS_BIGENDIAN
unsigned int negative:1;
unsigned int exponent:11;
unsigned int mantissa0:20;
unsigned int mantissa1:32;
#else
#if FLOAT_WORDS_BIGENDIAN
unsigned int mantissa0:20;
unsigned int exponent:11;
unsigned int negative:1;
unsigned int mantissa1:32;
#else
unsigned int mantissa1:32;
unsigned int mantissa0:20;
unsigned int exponent:11;
unsigned int negative:1;
#endif
#endif
} ieee;
/* This format makes it easier to see if a NaN is a signalling NaN. */
struct {
#if WORDS_BIGENDIAN
unsigned int negative:1;
unsigned int exponent:11;
unsigned int quiet_nan:1;
unsigned int mantissa0:19;
unsigned int mantissa1:32;
#else
#if FLOAT_WORDS_BIGENDIAN
unsigned int mantissa0:19;
unsigned int quiet_nan:1;
unsigned int exponent:11;
unsigned int negative:1;
unsigned int mantissa1:32;
#else
unsigned int mantissa1:32;
unsigned int mantissa0:19;
unsigned int quiet_nan:1;
unsigned int exponent:11;
unsigned int negative:1;
#endif
#endif
} ieee_nan;
};
/*
* Predicates for testing for extreme IEEE fp values. Used
* by the bytecode evaluator and the Prelude.
*
*/
/* In case you don't suppport IEEE, you'll just get dummy defs.. */
#ifdef IEEE_FLOATING_POINT
StgInt
isDoubleNaN(StgDouble d)
{
union stg_ieee754_dbl u;
u.d = d;
return (
u.ieee.exponent == 2047 /* 2^11 - 1 */ && /* Is the exponent all ones? */
(u.ieee.mantissa0 != 0 || u.ieee.mantissa1 != 0)
/* and the mantissa non-zero? */
);
}
StgInt
isDoubleInfinite(StgDouble d)
{
union stg_ieee754_dbl u;
u.d = d;
/* Inf iff exponent is all ones, mantissa all zeros */
return (
u.ieee.exponent == 2047 /* 2^11 - 1 */ &&
u.ieee.mantissa0 == 0 &&
u.ieee.mantissa1 == 0
);
}
StgInt
isDoubleDenormalized(StgDouble d)
{
union stg_ieee754_dbl u;
u.d = d;
/* A (single/double/quad) precision floating point number
is denormalised iff:
- exponent is zero
- mantissa is non-zero.
- (don't care about setting of sign bit.)
*/
return (
u.ieee.exponent == 0 &&
(u.ieee.mantissa0 != 0 ||
u.ieee.mantissa1 != 0)
);
}
StgInt
isDoubleNegativeZero(StgDouble d)
{
union stg_ieee754_dbl u;
u.d = d;
/* sign (bit 63) set (only) => negative zero */
return (
u.ieee.negative == 1 &&
u.ieee.exponent == 0 &&
u.ieee.mantissa0 == 0 &&
u.ieee.mantissa1 == 0);
}
/* Same tests, this time for StgFloats. */
/*
To recap, here's the representation of a single precision
IEEE floating point number:
sign 31 sign bit (0 == positive, 1 == negative)
exponent 30-23 exponent (biased by 127)
fraction 22-0 fraction (bits to right of binary point)
*/
StgInt
isFloatNaN(StgFloat f)
{
union stg_ieee754_flt u;
u.f = f;
/* Floating point NaN iff exponent is all ones, mantissa is
non-zero (but see below.) */
return (
u.ieee.exponent == 255 /* 2^8 - 1 */ &&
u.ieee.mantissa != 0);
}
StgInt
isFloatInfinite(StgFloat f)
{
union stg_ieee754_flt u;
u.f = f;
/* A float is Inf iff exponent is max (all ones),
and mantissa is min(all zeros.) */
return (
u.ieee.exponent == 255 /* 2^8 - 1 */ &&
u.ieee.mantissa == 0);
}
StgInt
isFloatDenormalized(StgFloat f)
{
union stg_ieee754_flt u;
u.f = f;
/* A (single/double/quad) precision floating point number
is denormalised iff:
- exponent is zero
- mantissa is non-zero.
- (don't care about setting of sign bit.)
*/
return (
u.ieee.exponent == 0 &&
u.ieee.mantissa != 0);
}
StgInt
isFloatNegativeZero(StgFloat f)
{
union stg_ieee754_flt u;
u.f = f;
/* sign (bit 31) set (only) => negative zero */
return (
u.ieee.negative &&
u.ieee.exponent == 0 &&
u.ieee.mantissa == 0);
}
#else /* ! IEEE_FLOATING_POINT */
/* Dummy definitions of predicates - they all return false */
StgInt isDoubleNaN(d) StgDouble d; { return 0; }
StgInt isDoubleInfinite(d) StgDouble d; { return 0; }
StgInt isDoubleDenormalized(d) StgDouble d; { return 0; }
StgInt isDoubleNegativeZero(d) StgDouble d; { return 0; }
StgInt isFloatNaN(f) StgFloat f; { return 0; }
StgInt isFloatInfinite(f) StgFloat f; { return 0; }
StgInt isFloatDenormalized(f) StgFloat f; { return 0; }
StgInt isFloatNegativeZero(f) StgFloat f; { return 0; }
#endif /* ! IEEE_FLOATING_POINT */
|