1 /* -----------------------------------------------------------------------------
3 * (c) Lennart Augustsson
4 * (c) The GHC Team, 1998-2000
6 * Miscellaneous support for floating-point primitives
8 * ---------------------------------------------------------------------------*/
10 #include "PosixSource.h"
16 * Encoding and decoding Doubles. Code based on the HBC code
21 #define SIZEOF_LIMB_T SIZEOF_UNSIGNED_INT
23 #ifdef _LONG_LONG_LIMB
24 #define SIZEOF_LIMB_T SIZEOF_UNSIGNED_LONG_LONG
26 #define SIZEOF_LIMB_T SIZEOF_UNSIGNED_LONG
30 #if SIZEOF_LIMB_T == 4
31 #define GMP_BASE 4294967296.0
32 #elif SIZEOF_LIMB_T == 8
33 #define GMP_BASE 18446744073709551616.0
35 #error Cannot cope with SIZEOF_LIMB_T -- please add definition of GMP_BASE
38 #define DNBIGIT ((SIZEOF_DOUBLE+SIZEOF_LIMB_T-1)/SIZEOF_LIMB_T)
39 #define FNBIGIT ((SIZEOF_FLOAT +SIZEOF_LIMB_T-1)/SIZEOF_LIMB_T)
41 #if IEEE_FLOATING_POINT
42 #define MY_DMINEXP ((DBL_MIN_EXP) - (DBL_MANT_DIG) - 1)
43 /* DMINEXP is defined in values.h on Linux (for example) */
44 #define DHIGHBIT 0x00100000
45 #define DMSBIT 0x80000000
47 #define MY_FMINEXP ((FLT_MIN_EXP) - (FLT_MANT_DIG) - 1)
48 #define FHIGHBIT 0x00800000
49 #define FMSBIT 0x80000000
52 #if defined(WORDS_BIGENDIAN) || defined(FLOAT_WORDS_BIGENDIAN)
60 #define __abs(a) (( (a) >= 0 ) ? (a) : (-(a)))
63 __2Int_encodeDouble (I_ j_high, I_ j_low, I_ e)
67 /* assuming 32 bit ints */
68 ASSERT(sizeof(int ) == 4 );
70 r = (StgDouble)((unsigned int)j_high);
71 r *= 4294967296.0; /* exp2f(32); */
72 r += (StgDouble)((unsigned int)j_low);
74 /* Now raise to the exponent */
75 if ( r != 0.0 ) /* Lennart suggests this avoids a bug in MIPS's ldexp */
78 /* sign is encoded in the size */
85 /* Special version for words */
87 __word_encodeDouble (W_ j, I_ e)
93 /* Now raise to the exponent */
94 if ( r != 0.0 ) /* Lennart suggests this avoids a bug in MIPS's ldexp */
100 /* Special version for small Integers */
102 __int_encodeDouble (I_ j, I_ e)
106 r = (StgDouble)__abs(j);
108 /* Now raise to the exponent */
109 if ( r != 0.0 ) /* Lennart suggests this avoids a bug in MIPS's ldexp */
112 /* sign is encoded in the size */
119 /* Special version for small Integers */
121 __int_encodeFloat (I_ j, I_ e)
125 r = (StgFloat)__abs(j);
127 /* Now raise to the exponent */
128 if ( r != 0.0 ) /* Lennart suggests this avoids a bug in MIPS's ldexp */
131 /* sign is encoded in the size */
138 /* Special version for small positive Integers */
140 __word_encodeFloat (W_ j, I_ e)
146 /* Now raise to the exponent */
147 if ( r != 0.0 ) /* Lennart suggests this avoids a bug in MIPS's ldexp */
153 /* This only supports IEEE floating point */
156 __decodeDouble (MP_INT *man, I_ *exp, StgDouble dbl)
158 /* Do some bit fiddling on IEEE */
159 unsigned int low, high; /* assuming 32 bit ints */
161 union { double d; unsigned int i[2]; } u; /* assuming 32 bit ints, 64 bit double */
163 ASSERT(sizeof(unsigned int ) == 4 );
164 ASSERT(sizeof(dbl ) == SIZEOF_DOUBLE);
165 ASSERT(sizeof(man->_mp_d[0]) == SIZEOF_LIMB_T);
166 ASSERT(DNBIGIT*SIZEOF_LIMB_T >= SIZEOF_DOUBLE);
168 u.d = dbl; /* grab chunks of the double */
172 /* we know the MP_INT* passed in has size zero, so we realloc
175 man->_mp_alloc = DNBIGIT;
177 if (low == 0 && (high & ~DMSBIT) == 0) {
181 man->_mp_size = DNBIGIT;
182 iexp = ((high >> 20) & 0x7ff) + MY_DMINEXP;
186 if (iexp != MY_DMINEXP) /* don't add hidden bit to denorms */
190 /* A denorm, normalize the mantissa */
191 while (! (high & DHIGHBIT)) {
201 man->_mp_d[0] = (mp_limb_t)low;
202 man->_mp_d[1] = (mp_limb_t)high;
205 man->_mp_d[0] = ((mp_limb_t)high) << 32 | (mp_limb_t)low;
207 #error Cannot cope with DNBIGIT
211 man->_mp_size = -man->_mp_size;
216 __decodeDouble_2Int (I_ *man_sign, W_ *man_high, W_ *man_low, I_ *exp, StgDouble dbl)
218 /* Do some bit fiddling on IEEE */
219 unsigned int low, high; /* assuming 32 bit ints */
221 union { double d; unsigned int i[2]; } u; /* assuming 32 bit ints, 64 bit double */
223 ASSERT(sizeof(unsigned int ) == 4 );
224 ASSERT(sizeof(dbl ) == 8 );
225 ASSERT(sizeof(dbl ) == SIZEOF_DOUBLE);
227 u.d = dbl; /* grab chunks of the double */
231 if (low == 0 && (high & ~DMSBIT) == 0) {
236 iexp = ((high >> 20) & 0x7ff) + MY_DMINEXP;
240 if (iexp != MY_DMINEXP) /* don't add hidden bit to denorms */
244 /* A denorm, normalize the mantissa */
245 while (! (high & DHIGHBIT)) {
256 *man_sign = (sign < 0) ? -1 : 1;
260 /* Convenient union types for checking the layout of IEEE 754 types -
261 based on defs in GNU libc <ieee754.h>
265 __decodeFloat_Int (I_ *man, I_ *exp, StgFloat flt)
267 /* Do some bit fiddling on IEEE */
268 int high, sign; /* assuming 32 bit ints */
269 union { float f; int i; } u; /* assuming 32 bit float and int */
271 ASSERT(sizeof(int ) == 4 );
272 ASSERT(sizeof(flt ) == 4 );
273 ASSERT(sizeof(flt ) == SIZEOF_FLOAT );
275 u.f = flt; /* grab the float */
278 if ((high & ~FMSBIT) == 0) {
282 *exp = ((high >> 23) & 0xff) + MY_FMINEXP;
286 if (*exp != MY_FMINEXP) /* don't add hidden bit to denorms */
290 /* A denorm, normalize the mantissa */
291 while (! (high & FHIGHBIT)) {
302 union stg_ieee754_flt
308 unsigned int negative:1;
309 unsigned int exponent:8;
310 unsigned int mantissa:23;
312 unsigned int mantissa:23;
313 unsigned int exponent:8;
314 unsigned int negative:1;
320 unsigned int negative:1;
321 unsigned int exponent:8;
322 unsigned int quiet_nan:1;
323 unsigned int mantissa:22;
325 unsigned int mantissa:22;
326 unsigned int quiet_nan:1;
327 unsigned int exponent:8;
328 unsigned int negative:1;
335 To recap, here's the representation of a double precision
336 IEEE floating point number:
338 sign 63 sign bit (0==positive, 1==negative)
339 exponent 62-52 exponent (biased by 1023)
340 fraction 51-0 fraction (bits to right of binary point)
343 union stg_ieee754_dbl
349 unsigned int negative:1;
350 unsigned int exponent:11;
351 unsigned int mantissa0:20;
352 unsigned int mantissa1:32;
354 #if FLOAT_WORDS_BIGENDIAN
355 unsigned int mantissa0:20;
356 unsigned int exponent:11;
357 unsigned int negative:1;
358 unsigned int mantissa1:32;
360 unsigned int mantissa1:32;
361 unsigned int mantissa0:20;
362 unsigned int exponent:11;
363 unsigned int negative:1;
367 /* This format makes it easier to see if a NaN is a signalling NaN. */
371 unsigned int negative:1;
372 unsigned int exponent:11;
373 unsigned int quiet_nan:1;
374 unsigned int mantissa0:19;
375 unsigned int mantissa1:32;
377 #if FLOAT_WORDS_BIGENDIAN
378 unsigned int mantissa0:19;
379 unsigned int quiet_nan:1;
380 unsigned int exponent:11;
381 unsigned int negative:1;
382 unsigned int mantissa1:32;
384 unsigned int mantissa1:32;
385 unsigned int mantissa0:19;
386 unsigned int quiet_nan:1;
387 unsigned int exponent:11;
388 unsigned int negative:1;
395 * Predicates for testing for extreme IEEE fp values. Used
396 * by the bytecode evaluator and the Prelude.
400 /* In case you don't suppport IEEE, you'll just get dummy defs.. */
401 #ifdef IEEE_FLOATING_POINT
404 isDoubleNaN(StgDouble d)
406 union stg_ieee754_dbl u;
411 u.ieee.exponent == 2047 /* 2^11 - 1 */ && /* Is the exponent all ones? */
412 (u.ieee.mantissa0 != 0 || u.ieee.mantissa1 != 0)
413 /* and the mantissa non-zero? */
418 isDoubleInfinite(StgDouble d)
420 union stg_ieee754_dbl u;
424 /* Inf iff exponent is all ones, mantissa all zeros */
426 u.ieee.exponent == 2047 /* 2^11 - 1 */ &&
427 u.ieee.mantissa0 == 0 &&
428 u.ieee.mantissa1 == 0
433 isDoubleDenormalized(StgDouble d)
435 union stg_ieee754_dbl u;
439 /* A (single/double/quad) precision floating point number
442 - mantissa is non-zero.
443 - (don't care about setting of sign bit.)
447 u.ieee.exponent == 0 &&
448 (u.ieee.mantissa0 != 0 ||
449 u.ieee.mantissa1 != 0)
455 isDoubleNegativeZero(StgDouble d)
457 union stg_ieee754_dbl u;
460 /* sign (bit 63) set (only) => negative zero */
463 u.ieee.negative == 1 &&
464 u.ieee.exponent == 0 &&
465 u.ieee.mantissa0 == 0 &&
466 u.ieee.mantissa1 == 0);
469 /* Same tests, this time for StgFloats. */
472 To recap, here's the representation of a single precision
473 IEEE floating point number:
475 sign 31 sign bit (0 == positive, 1 == negative)
476 exponent 30-23 exponent (biased by 127)
477 fraction 22-0 fraction (bits to right of binary point)
482 isFloatNaN(StgFloat f)
484 union stg_ieee754_flt u;
487 /* Floating point NaN iff exponent is all ones, mantissa is
488 non-zero (but see below.) */
490 u.ieee.exponent == 255 /* 2^8 - 1 */ &&
491 u.ieee.mantissa != 0);
495 isFloatInfinite(StgFloat f)
497 union stg_ieee754_flt u;
500 /* A float is Inf iff exponent is max (all ones),
501 and mantissa is min(all zeros.) */
503 u.ieee.exponent == 255 /* 2^8 - 1 */ &&
504 u.ieee.mantissa == 0);
508 isFloatDenormalized(StgFloat f)
510 union stg_ieee754_flt u;
513 /* A (single/double/quad) precision floating point number
516 - mantissa is non-zero.
517 - (don't care about setting of sign bit.)
521 u.ieee.exponent == 0 &&
522 u.ieee.mantissa != 0);
526 isFloatNegativeZero(StgFloat f)
528 union stg_ieee754_flt u;
531 /* sign (bit 31) set (only) => negative zero */
534 u.ieee.exponent == 0 &&
535 u.ieee.mantissa == 0);
538 #else /* ! IEEE_FLOATING_POINT */
540 /* Dummy definitions of predicates - they all return false */
541 StgInt isDoubleNaN(d) StgDouble d; { return 0; }
542 StgInt isDoubleInfinite(d) StgDouble d; { return 0; }
543 StgInt isDoubleDenormalized(d) StgDouble d; { return 0; }
544 StgInt isDoubleNegativeZero(d) StgDouble d; { return 0; }
545 StgInt isFloatNaN(f) StgFloat f; { return 0; }
546 StgInt isFloatInfinite(f) StgFloat f; { return 0; }
547 StgInt isFloatDenormalized(f) StgFloat f; { return 0; }
548 StgInt isFloatNegativeZero(f) StgFloat f; { return 0; }
550 #endif /* ! IEEE_FLOATING_POINT */