1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team, 1998-2000
5 * Miscellaneous support for floating-point primitives
7 * ---------------------------------------------------------------------------*/
9 #include "PosixSource.h"
15 * Encoding and decoding Doubles. Code based on the HBC code
20 #define SIZEOF_LIMB_T SIZEOF_UNSIGNED_INT
22 #ifdef _LONG_LONG_LIMB
23 #define SIZEOF_LIMB_T SIZEOF_UNSIGNED_LONG_LONG
25 #define SIZEOF_LIMB_T SIZEOF_UNSIGNED_LONG
29 #if SIZEOF_LIMB_T == 4
30 #define GMP_BASE 4294967296.0
31 #elif SIZEOF_LIMB_T == 8
32 #define GMP_BASE 18446744073709551616.0
34 #error Cannot cope with SIZEOF_LIMB_T -- please add definition of GMP_BASE
37 #define DNBIGIT ((SIZEOF_DOUBLE+SIZEOF_LIMB_T-1)/SIZEOF_LIMB_T)
38 #define FNBIGIT ((SIZEOF_FLOAT +SIZEOF_LIMB_T-1)/SIZEOF_LIMB_T)
40 #if IEEE_FLOATING_POINT
41 #define MY_DMINEXP ((DBL_MIN_EXP) - (DBL_MANT_DIG) - 1)
42 /* DMINEXP is defined in values.h on Linux (for example) */
43 #define DHIGHBIT 0x00100000
44 #define DMSBIT 0x80000000
46 #define MY_FMINEXP ((FLT_MIN_EXP) - (FLT_MANT_DIG) - 1)
47 #define FHIGHBIT 0x00800000
48 #define FMSBIT 0x80000000
51 #if defined(WORDS_BIGENDIAN) || defined(FLOAT_WORDS_BIGENDIAN)
59 #define __abs(a) (( (a) >= 0 ) ? (a) : (-(a)))
62 __encodeDouble (I_ size, StgByteArray ba, I_ e) /* result = s * 2^e */
65 const mp_limb_t *const arr = (const mp_limb_t *)ba;
68 /* Convert MP_INT to a double; knows a lot about internal rep! */
69 for(r = 0.0, i = __abs(size)-1; i >= 0; i--)
70 r = (r * GMP_BASE) + arr[i];
72 /* Now raise to the exponent */
73 if ( r != 0.0 ) /* Lennart suggests this avoids a bug in MIPS's ldexp */
76 /* sign is encoded in the size */
83 /* Special version for small Integers */
85 __int_encodeDouble (I_ j, I_ e)
89 r = (StgDouble)__abs(j);
91 /* Now raise to the exponent */
92 if ( r != 0.0 ) /* Lennart suggests this avoids a bug in MIPS's ldexp */
95 /* sign is encoded in the size */
103 __encodeFloat (I_ size, StgByteArray ba, I_ e) /* result = s * 2^e */
106 const mp_limb_t *arr = (const mp_limb_t *)ba;
109 /* Convert MP_INT to a float; knows a lot about internal rep! */
110 for(r = 0.0, i = __abs(size)-1; i >= 0; i--)
111 r = (r * GMP_BASE) + arr[i];
113 /* Now raise to the exponent */
114 if ( r != 0.0 ) /* Lennart suggests this avoids a bug in MIPS's ldexp */
117 /* sign is encoded in the size */
124 /* Special version for small Integers */
126 __int_encodeFloat (I_ j, I_ e)
130 r = (StgFloat)__abs(j);
132 /* Now raise to the exponent */
133 if ( r != 0.0 ) /* Lennart suggests this avoids a bug in MIPS's ldexp */
136 /* sign is encoded in the size */
143 /* This only supports IEEE floating point */
146 __decodeDouble (MP_INT *man, I_ *exp, StgDouble dbl)
148 /* Do some bit fiddling on IEEE */
149 unsigned int low, high; /* assuming 32 bit ints */
151 union { double d; unsigned int i[2]; } u; /* assuming 32 bit ints, 64 bit double */
153 ASSERT(sizeof(unsigned int ) == 4 );
154 ASSERT(sizeof(dbl ) == SIZEOF_DOUBLE);
155 ASSERT(sizeof(man->_mp_d[0]) == SIZEOF_LIMB_T);
156 ASSERT(DNBIGIT*SIZEOF_LIMB_T >= SIZEOF_DOUBLE);
158 u.d = dbl; /* grab chunks of the double */
162 /* we know the MP_INT* passed in has size zero, so we realloc
165 man->_mp_alloc = DNBIGIT;
167 if (low == 0 && (high & ~DMSBIT) == 0) {
171 man->_mp_size = DNBIGIT;
172 iexp = ((high >> 20) & 0x7ff) + MY_DMINEXP;
176 if (iexp != MY_DMINEXP) /* don't add hidden bit to denorms */
180 /* A denorm, normalize the mantissa */
181 while (! (high & DHIGHBIT)) {
191 man->_mp_d[0] = (mp_limb_t)low;
192 man->_mp_d[1] = (mp_limb_t)high;
195 man->_mp_d[0] = ((mp_limb_t)high) << 32 | (mp_limb_t)low;
197 #error Cannot cope with DNBIGIT
201 man->_mp_size = -man->_mp_size;
206 __decodeFloat (MP_INT *man, I_ *exp, StgFloat flt)
208 /* Do some bit fiddling on IEEE */
209 int high, sign; /* assuming 32 bit ints */
210 union { float f; int i; } u; /* assuming 32 bit float and int */
212 ASSERT(sizeof(int ) == 4 );
213 ASSERT(sizeof(flt ) == SIZEOF_FLOAT );
214 ASSERT(sizeof(man->_mp_d[0]) == SIZEOF_LIMB_T);
215 ASSERT(FNBIGIT*SIZEOF_LIMB_T >= SIZEOF_FLOAT );
217 u.f = flt; /* grab the float */
220 /* we know the MP_INT* passed in has size zero, so we realloc
223 man->_mp_alloc = FNBIGIT;
225 if ((high & ~FMSBIT) == 0) {
229 man->_mp_size = FNBIGIT;
230 *exp = ((high >> 23) & 0xff) + MY_FMINEXP;
234 if (*exp != MY_FMINEXP) /* don't add hidden bit to denorms */
238 /* A denorm, normalize the mantissa */
239 while (! (high & FHIGHBIT)) {
245 man->_mp_d[0] = (mp_limb_t)high;
247 #error Cannot cope with FNBIGIT
250 man->_mp_size = -man->_mp_size;
254 /* Convenient union types for checking the layout of IEEE 754 types -
255 based on defs in GNU libc <ieee754.h>
258 union stg_ieee754_flt
264 unsigned int negative:1;
265 unsigned int exponent:8;
266 unsigned int mantissa:23;
268 unsigned int mantissa:23;
269 unsigned int exponent:8;
270 unsigned int negative:1;
276 unsigned int negative:1;
277 unsigned int exponent:8;
278 unsigned int quiet_nan:1;
279 unsigned int mantissa:22;
281 unsigned int mantissa:22;
282 unsigned int quiet_nan:1;
283 unsigned int exponent:8;
284 unsigned int negative:1;
291 To recap, here's the representation of a double precision
292 IEEE floating point number:
294 sign 63 sign bit (0==positive, 1==negative)
295 exponent 62-52 exponent (biased by 1023)
296 fraction 51-0 fraction (bits to right of binary point)
299 union stg_ieee754_dbl
305 unsigned int negative:1;
306 unsigned int exponent:11;
307 unsigned int mantissa0:20;
308 unsigned int mantissa1:32;
310 #if FLOAT_WORDS_BIGENDIAN
311 unsigned int mantissa0:20;
312 unsigned int exponent:11;
313 unsigned int negative:1;
314 unsigned int mantissa1:32;
316 unsigned int mantissa1:32;
317 unsigned int mantissa0:20;
318 unsigned int exponent:11;
319 unsigned int negative:1;
323 /* This format makes it easier to see if a NaN is a signalling NaN. */
327 unsigned int negative:1;
328 unsigned int exponent:11;
329 unsigned int quiet_nan:1;
330 unsigned int mantissa0:19;
331 unsigned int mantissa1:32;
333 #if FLOAT_WORDS_BIGENDIAN
334 unsigned int mantissa0:19;
335 unsigned int quiet_nan:1;
336 unsigned int exponent:11;
337 unsigned int negative:1;
338 unsigned int mantissa1:32;
340 unsigned int mantissa1:32;
341 unsigned int mantissa0:19;
342 unsigned int quiet_nan:1;
343 unsigned int exponent:11;
344 unsigned int negative:1;
351 * Predicates for testing for extreme IEEE fp values. Used
352 * by the bytecode evaluator and the Prelude.
356 /* In case you don't suppport IEEE, you'll just get dummy defs.. */
357 #ifdef IEEE_FLOATING_POINT
360 isDoubleNaN(StgDouble d)
362 union stg_ieee754_dbl u;
367 u.ieee.exponent == 2047 /* 2^11 - 1 */ && /* Is the exponent all ones? */
368 (u.ieee.mantissa0 != 0 || u.ieee.mantissa1 != 0)
369 /* and the mantissa non-zero? */
374 isDoubleInfinite(StgDouble d)
376 union stg_ieee754_dbl u;
380 /* Inf iff exponent is all ones, mantissa all zeros */
382 u.ieee.exponent == 2047 /* 2^11 - 1 */ &&
383 u.ieee.mantissa0 == 0 &&
384 u.ieee.mantissa1 == 0
389 isDoubleDenormalized(StgDouble d)
391 union stg_ieee754_dbl u;
395 /* A (single/double/quad) precision floating point number
398 - mantissa is non-zero.
399 - (don't care about setting of sign bit.)
403 u.ieee.exponent == 0 &&
404 (u.ieee.mantissa0 != 0 ||
405 u.ieee.mantissa1 != 0)
411 isDoubleNegativeZero(StgDouble d)
413 union stg_ieee754_dbl u;
416 /* sign (bit 63) set (only) => negative zero */
419 u.ieee.negative == 1 &&
420 u.ieee.exponent == 0 &&
421 u.ieee.mantissa0 == 0 &&
422 u.ieee.mantissa1 == 0);
425 /* Same tests, this time for StgFloats. */
428 To recap, here's the representation of a single precision
429 IEEE floating point number:
431 sign 31 sign bit (0 == positive, 1 == negative)
432 exponent 30-23 exponent (biased by 127)
433 fraction 22-0 fraction (bits to right of binary point)
438 isFloatNaN(StgFloat f)
440 union stg_ieee754_flt u;
443 /* Floating point NaN iff exponent is all ones, mantissa is
444 non-zero (but see below.) */
446 u.ieee.exponent == 255 /* 2^8 - 1 */ &&
447 u.ieee.mantissa != 0);
451 isFloatInfinite(StgFloat f)
453 union stg_ieee754_flt u;
456 /* A float is Inf iff exponent is max (all ones),
457 and mantissa is min(all zeros.) */
459 u.ieee.exponent == 255 /* 2^8 - 1 */ &&
460 u.ieee.mantissa == 0);
464 isFloatDenormalized(StgFloat f)
466 union stg_ieee754_flt u;
469 /* A (single/double/quad) precision floating point number
472 - mantissa is non-zero.
473 - (don't care about setting of sign bit.)
477 u.ieee.exponent == 0 &&
478 u.ieee.mantissa != 0);
482 isFloatNegativeZero(StgFloat f)
484 union stg_ieee754_flt u;
487 /* sign (bit 31) set (only) => negative zero */
490 u.ieee.exponent == 0 &&
491 u.ieee.mantissa == 0);
494 #else /* ! IEEE_FLOATING_POINT */
496 /* Dummy definitions of predicates - they all return false */
497 StgInt isDoubleNaN(d) StgDouble d; { return 0; }
498 StgInt isDoubleInfinite(d) StgDouble d; { return 0; }
499 StgInt isDoubleDenormalized(d) StgDouble d; { return 0; }
500 StgInt isDoubleNegativeZero(d) StgDouble d; { return 0; }
501 StgInt isFloatNaN(f) StgFloat f; { return 0; }
502 StgInt isFloatInfinite(f) StgFloat f; { return 0; }
503 StgInt isFloatDenormalized(f) StgFloat f; { return 0; }
504 StgInt isFloatNegativeZero(f) StgFloat f; { return 0; }
506 #endif /* ! IEEE_FLOATING_POINT */