1 /* -----------------------------------------------------------------------------
2 * $Id: StgPrimFloat.c,v 1.8 2001/08/14 13:40:09 sewardj Exp $
4 * (c) The GHC Team, 1998-2000
6 * Miscellaneous support for floating-point primitives
8 * ---------------------------------------------------------------------------*/
10 #include "PosixSource.h"
14 * Encoding and decoding Doubles. Code based on the HBC code
19 #define SIZEOF_LIMB_T SIZEOF_UNSIGNED_INT
21 #ifdef _LONG_LONG_LIMB
22 #define SIZEOF_LIMB_T SIZEOF_UNSIGNED_LONG_LONG
24 #define SIZEOF_LIMB_T SIZEOF_UNSIGNED_LONG
28 #if SIZEOF_LIMB_T == 4
29 #define GMP_BASE 4294967296.0
30 #elif SIZEOF_LIMB_T == 8
31 #define GMP_BASE 18446744073709551616.0
33 #error Cannot cope with SIZEOF_LIMB_T -- please add definition of GMP_BASE
36 #define DNBIGIT ((SIZEOF_DOUBLE+SIZEOF_LIMB_T-1)/SIZEOF_LIMB_T)
37 #define FNBIGIT ((SIZEOF_FLOAT +SIZEOF_LIMB_T-1)/SIZEOF_LIMB_T)
39 #if IEEE_FLOATING_POINT
40 #define MY_DMINEXP ((DBL_MIN_EXP) - (DBL_MANT_DIG) - 1)
41 /* DMINEXP is defined in values.h on Linux (for example) */
42 #define DHIGHBIT 0x00100000
43 #define DMSBIT 0x80000000
45 #define MY_FMINEXP ((FLT_MIN_EXP) - (FLT_MANT_DIG) - 1)
46 #define FHIGHBIT 0x00800000
47 #define FMSBIT 0x80000000
50 #ifdef WORDS_BIGENDIAN
58 #define __abs(a) (( (a) >= 0 ) ? (a) : (-(a)))
61 __encodeDouble (I_ size, StgByteArray ba, I_ e) /* result = s * 2^e */
64 const mp_limb_t *const arr = (const mp_limb_t *)ba;
67 /* Convert MP_INT to a double; knows a lot about internal rep! */
68 for(r = 0.0, i = __abs(size)-1; i >= 0; i--)
69 r = (r * GMP_BASE) + arr[i];
71 /* Now raise to the exponent */
72 if ( r != 0.0 ) /* Lennart suggests this avoids a bug in MIPS's ldexp */
75 /* sign is encoded in the size */
82 /* Special version for small Integers */
84 __int_encodeDouble (I_ j, I_ e)
88 r = (StgDouble)__abs(j);
90 /* Now raise to the exponent */
91 if ( r != 0.0 ) /* Lennart suggests this avoids a bug in MIPS's ldexp */
94 /* sign is encoded in the size */
102 __encodeFloat (I_ size, StgByteArray ba, I_ e) /* result = s * 2^e */
105 const mp_limb_t *arr = (const mp_limb_t *)ba;
108 /* Convert MP_INT to a float; knows a lot about internal rep! */
109 for(r = 0.0, i = __abs(size)-1; i >= 0; i--)
110 r = (r * GMP_BASE) + arr[i];
112 /* Now raise to the exponent */
113 if ( r != 0.0 ) /* Lennart suggests this avoids a bug in MIPS's ldexp */
116 /* sign is encoded in the size */
123 /* Special version for small Integers */
125 __int_encodeFloat (I_ j, I_ e)
129 r = (StgFloat)__abs(j);
131 /* Now raise to the exponent */
132 if ( r != 0.0 ) /* Lennart suggests this avoids a bug in MIPS's ldexp */
135 /* sign is encoded in the size */
142 /* This only supports IEEE floating point */
145 __decodeDouble (MP_INT *man, I_ *exp, StgDouble dbl)
147 /* Do some bit fiddling on IEEE */
148 unsigned int low, high; /* assuming 32 bit ints */
150 union { double d; unsigned int i[2]; } u; /* assuming 32 bit ints, 64 bit double */
152 ASSERT(sizeof(unsigned int ) == 4 );
153 ASSERT(sizeof(dbl ) == SIZEOF_DOUBLE);
154 ASSERT(sizeof(man->_mp_d[0]) == SIZEOF_LIMB_T);
155 ASSERT(DNBIGIT*SIZEOF_LIMB_T >= SIZEOF_DOUBLE);
157 u.d = dbl; /* grab chunks of the double */
161 /* we know the MP_INT* passed in has size zero, so we realloc
164 man->_mp_alloc = DNBIGIT;
166 if (low == 0 && (high & ~DMSBIT) == 0) {
170 man->_mp_size = DNBIGIT;
171 iexp = ((high >> 20) & 0x7ff) + MY_DMINEXP;
175 if (iexp != MY_DMINEXP) /* don't add hidden bit to denorms */
179 /* A denorm, normalize the mantissa */
180 while (! (high & DHIGHBIT)) {
190 man->_mp_d[0] = (mp_limb_t)low;
191 man->_mp_d[1] = (mp_limb_t)high;
194 man->_mp_d[0] = ((mp_limb_t)high) << 32 | (mp_limb_t)low;
196 #error Cannot cope with DNBIGIT
200 man->_mp_size = -man->_mp_size;
205 __decodeFloat (MP_INT *man, I_ *exp, StgFloat flt)
207 /* Do some bit fiddling on IEEE */
208 int high, sign; /* assuming 32 bit ints */
209 union { float f; int i; } u; /* assuming 32 bit float and int */
211 ASSERT(sizeof(int ) == 4 );
212 ASSERT(sizeof(flt ) == SIZEOF_FLOAT );
213 ASSERT(sizeof(man->_mp_d[0]) == SIZEOF_LIMB_T);
214 ASSERT(FNBIGIT*SIZEOF_LIMB_T >= SIZEOF_FLOAT );
216 u.f = flt; /* grab the float */
219 /* we know the MP_INT* passed in has size zero, so we realloc
222 man->_mp_alloc = FNBIGIT;
224 if ((high & ~FMSBIT) == 0) {
228 man->_mp_size = FNBIGIT;
229 *exp = ((high >> 23) & 0xff) + MY_FMINEXP;
233 if (*exp != MY_FMINEXP) /* don't add hidden bit to denorms */
237 /* A denorm, normalize the mantissa */
238 while (! (high & FHIGHBIT)) {
244 man->_mp_d[0] = (mp_limb_t)high;
246 #error Cannot cope with FNBIGIT
249 man->_mp_size = -man->_mp_size;
253 /* Convenient union types for checking the layout of IEEE 754 types -
254 based on defs in GNU libc <ieee754.h>
257 union stg_ieee754_flt
263 unsigned int negative:1;
264 unsigned int exponent:8;
265 unsigned int mantissa:23;
267 unsigned int mantissa:23;
268 unsigned int exponent:8;
269 unsigned int negative:1;
275 unsigned int negative:1;
276 unsigned int exponent:8;
277 unsigned int quiet_nan:1;
278 unsigned int mantissa:22;
280 unsigned int mantissa:22;
281 unsigned int quiet_nan:1;
282 unsigned int exponent:8;
283 unsigned int negative:1;
290 To recap, here's the representation of a double precision
291 IEEE floating point number:
293 sign 63 sign bit (0==positive, 1==negative)
294 exponent 62-52 exponent (biased by 1023)
295 fraction 51-0 fraction (bits to right of binary point)
298 union stg_ieee754_dbl
304 unsigned int negative:1;
305 unsigned int exponent:11;
306 unsigned int mantissa0:20;
307 unsigned int mantissa1:32;
309 unsigned int mantissa1:32;
310 unsigned int mantissa0:20;
311 unsigned int exponent:11;
312 unsigned int negative:1;
315 /* This format makes it easier to see if a NaN is a signalling NaN. */
319 unsigned int negative:1;
320 unsigned int exponent:11;
321 unsigned int quiet_nan:1;
322 unsigned int mantissa0:19;
323 unsigned int mantissa1:32;
325 unsigned int mantissa1:32;
326 unsigned int mantissa0:19;
327 unsigned int quiet_nan:1;
328 unsigned int exponent:11;
329 unsigned int negative:1;
335 * Predicates for testing for extreme IEEE fp values. Used
336 * by the bytecode evaluator and the Prelude.
340 /* In case you don't suppport IEEE, you'll just get dummy defs.. */
341 #ifdef IEEE_FLOATING_POINT
344 isDoubleNaN(StgDouble d)
346 union stg_ieee754_dbl u;
351 u.ieee.exponent == 2047 /* 2^11 - 1 */ && /* Is the exponent all ones? */
352 (u.ieee.mantissa0 != 0 || u.ieee.mantissa1 != 0)
353 /* and the mantissa non-zero? */
358 isDoubleInfinite(StgDouble d)
360 union stg_ieee754_dbl u;
364 /* Inf iff exponent is all ones, mantissa all zeros */
366 u.ieee.exponent == 2047 /* 2^11 - 1 */ &&
367 u.ieee.mantissa0 == 0 &&
368 u.ieee.mantissa1 == 0
373 isDoubleDenormalized(StgDouble d)
375 union stg_ieee754_dbl u;
379 /* A (single/double/quad) precision floating point number
382 - mantissa is non-zero.
383 - (don't care about setting of sign bit.)
387 u.ieee.exponent == 0 &&
388 (u.ieee.mantissa0 != 0 ||
389 u.ieee.mantissa1 != 0)
395 isDoubleNegativeZero(StgDouble d)
397 union stg_ieee754_dbl u;
400 /* sign (bit 63) set (only) => negative zero */
403 u.ieee.negative == 1 &&
404 u.ieee.exponent == 0 &&
405 u.ieee.mantissa0 == 0 &&
406 u.ieee.mantissa1 == 0);
409 /* Same tests, this time for StgFloats. */
412 To recap, here's the representation of a single precision
413 IEEE floating point number:
415 sign 31 sign bit (0 == positive, 1 == negative)
416 exponent 30-23 exponent (biased by 127)
417 fraction 22-0 fraction (bits to right of binary point)
422 isFloatNaN(StgFloat f)
424 union stg_ieee754_flt u;
427 /* Floating point NaN iff exponent is all ones, mantissa is
428 non-zero (but see below.) */
430 u.ieee.exponent == 255 /* 2^8 - 1 */ &&
431 u.ieee.mantissa != 0);
435 isFloatInfinite(StgFloat f)
437 union stg_ieee754_flt u;
440 /* A float is Inf iff exponent is max (all ones),
441 and mantissa is min(all zeros.) */
443 u.ieee.exponent == 255 /* 2^8 - 1 */ &&
444 u.ieee.mantissa == 0);
448 isFloatDenormalized(StgFloat f)
450 union stg_ieee754_flt u;
453 /* A (single/double/quad) precision floating point number
456 - mantissa is non-zero.
457 - (don't care about setting of sign bit.)
461 u.ieee.exponent == 0 &&
462 u.ieee.mantissa != 0);
466 isFloatNegativeZero(StgFloat f)
468 union stg_ieee754_flt u;
471 /* sign (bit 31) set (only) => negative zero */
474 u.ieee.exponent == 0 &&
475 u.ieee.mantissa == 0);
478 #else /* ! IEEE_FLOATING_POINT */
480 /* Dummy definitions of predicates - they all return false */
481 StgInt isDoubleNaN(d) StgDouble d; { return 0; }
482 StgInt isDoubleInfinite(d) StgDouble d; { return 0; }
483 StgInt isDoubleDenormalized(d) StgDouble d; { return 0; }
484 StgInt isDoubleNegativeZero(d) StgDouble d; { return 0; }
485 StgInt isFloatNaN(f) StgFloat f; { return 0; }
486 StgInt isFloatInfinite(f) StgFloat f; { return 0; }
487 StgInt isFloatDenormalized(f) StgFloat f; { return 0; }
488 StgInt isFloatNegativeZero(f) StgFloat f; { return 0; }
490 #endif /* ! IEEE_FLOATING_POINT */