1 /* -----------------------------------------------------------------------------
2 * $Id: StgPrimFloat.c,v 1.9 2002/07/17 09:21:51 simonmar Exp $
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 #ifdef WORDS_BIGENDIAN
60 #define __abs(a) (( (a) >= 0 ) ? (a) : (-(a)))
63 __encodeDouble (I_ size, StgByteArray ba, I_ e) /* result = s * 2^e */
66 const mp_limb_t *const arr = (const mp_limb_t *)ba;
69 /* Convert MP_INT to a double; knows a lot about internal rep! */
70 for(r = 0.0, i = __abs(size)-1; i >= 0; i--)
71 r = (r * GMP_BASE) + arr[i];
73 /* Now raise to the exponent */
74 if ( r != 0.0 ) /* Lennart suggests this avoids a bug in MIPS's ldexp */
77 /* sign is encoded in the size */
84 /* Special version for small Integers */
86 __int_encodeDouble (I_ j, I_ e)
90 r = (StgDouble)__abs(j);
92 /* Now raise to the exponent */
93 if ( r != 0.0 ) /* Lennart suggests this avoids a bug in MIPS's ldexp */
96 /* sign is encoded in the size */
104 __encodeFloat (I_ size, StgByteArray ba, I_ e) /* result = s * 2^e */
107 const mp_limb_t *arr = (const mp_limb_t *)ba;
110 /* Convert MP_INT to a float; knows a lot about internal rep! */
111 for(r = 0.0, i = __abs(size)-1; i >= 0; i--)
112 r = (r * GMP_BASE) + arr[i];
114 /* Now raise to the exponent */
115 if ( r != 0.0 ) /* Lennart suggests this avoids a bug in MIPS's ldexp */
118 /* sign is encoded in the size */
125 /* Special version for small Integers */
127 __int_encodeFloat (I_ j, I_ e)
131 r = (StgFloat)__abs(j);
133 /* Now raise to the exponent */
134 if ( r != 0.0 ) /* Lennart suggests this avoids a bug in MIPS's ldexp */
137 /* sign is encoded in the size */
144 /* This only supports IEEE floating point */
147 __decodeDouble (MP_INT *man, I_ *exp, StgDouble dbl)
149 /* Do some bit fiddling on IEEE */
150 unsigned int low, high; /* assuming 32 bit ints */
152 union { double d; unsigned int i[2]; } u; /* assuming 32 bit ints, 64 bit double */
154 ASSERT(sizeof(unsigned int ) == 4 );
155 ASSERT(sizeof(dbl ) == SIZEOF_DOUBLE);
156 ASSERT(sizeof(man->_mp_d[0]) == SIZEOF_LIMB_T);
157 ASSERT(DNBIGIT*SIZEOF_LIMB_T >= SIZEOF_DOUBLE);
159 u.d = dbl; /* grab chunks of the double */
163 /* we know the MP_INT* passed in has size zero, so we realloc
166 man->_mp_alloc = DNBIGIT;
168 if (low == 0 && (high & ~DMSBIT) == 0) {
172 man->_mp_size = DNBIGIT;
173 iexp = ((high >> 20) & 0x7ff) + MY_DMINEXP;
177 if (iexp != MY_DMINEXP) /* don't add hidden bit to denorms */
181 /* A denorm, normalize the mantissa */
182 while (! (high & DHIGHBIT)) {
192 man->_mp_d[0] = (mp_limb_t)low;
193 man->_mp_d[1] = (mp_limb_t)high;
196 man->_mp_d[0] = ((mp_limb_t)high) << 32 | (mp_limb_t)low;
198 #error Cannot cope with DNBIGIT
202 man->_mp_size = -man->_mp_size;
207 __decodeFloat (MP_INT *man, I_ *exp, StgFloat flt)
209 /* Do some bit fiddling on IEEE */
210 int high, sign; /* assuming 32 bit ints */
211 union { float f; int i; } u; /* assuming 32 bit float and int */
213 ASSERT(sizeof(int ) == 4 );
214 ASSERT(sizeof(flt ) == SIZEOF_FLOAT );
215 ASSERT(sizeof(man->_mp_d[0]) == SIZEOF_LIMB_T);
216 ASSERT(FNBIGIT*SIZEOF_LIMB_T >= SIZEOF_FLOAT );
218 u.f = flt; /* grab the float */
221 /* we know the MP_INT* passed in has size zero, so we realloc
224 man->_mp_alloc = FNBIGIT;
226 if ((high & ~FMSBIT) == 0) {
230 man->_mp_size = FNBIGIT;
231 *exp = ((high >> 23) & 0xff) + MY_FMINEXP;
235 if (*exp != MY_FMINEXP) /* don't add hidden bit to denorms */
239 /* A denorm, normalize the mantissa */
240 while (! (high & FHIGHBIT)) {
246 man->_mp_d[0] = (mp_limb_t)high;
248 #error Cannot cope with FNBIGIT
251 man->_mp_size = -man->_mp_size;
255 /* Convenient union types for checking the layout of IEEE 754 types -
256 based on defs in GNU libc <ieee754.h>
259 union stg_ieee754_flt
265 unsigned int negative:1;
266 unsigned int exponent:8;
267 unsigned int mantissa:23;
269 unsigned int mantissa:23;
270 unsigned int exponent:8;
271 unsigned int negative:1;
277 unsigned int negative:1;
278 unsigned int exponent:8;
279 unsigned int quiet_nan:1;
280 unsigned int mantissa:22;
282 unsigned int mantissa:22;
283 unsigned int quiet_nan:1;
284 unsigned int exponent:8;
285 unsigned int negative:1;
292 To recap, here's the representation of a double precision
293 IEEE floating point number:
295 sign 63 sign bit (0==positive, 1==negative)
296 exponent 62-52 exponent (biased by 1023)
297 fraction 51-0 fraction (bits to right of binary point)
300 union stg_ieee754_dbl
306 unsigned int negative:1;
307 unsigned int exponent:11;
308 unsigned int mantissa0:20;
309 unsigned int mantissa1:32;
311 unsigned int mantissa1:32;
312 unsigned int mantissa0:20;
313 unsigned int exponent:11;
314 unsigned int negative:1;
317 /* This format makes it easier to see if a NaN is a signalling NaN. */
321 unsigned int negative:1;
322 unsigned int exponent:11;
323 unsigned int quiet_nan:1;
324 unsigned int mantissa0:19;
325 unsigned int mantissa1:32;
327 unsigned int mantissa1:32;
328 unsigned int mantissa0:19;
329 unsigned int quiet_nan:1;
330 unsigned int exponent:11;
331 unsigned int negative:1;
337 * Predicates for testing for extreme IEEE fp values. Used
338 * by the bytecode evaluator and the Prelude.
342 /* In case you don't suppport IEEE, you'll just get dummy defs.. */
343 #ifdef IEEE_FLOATING_POINT
346 isDoubleNaN(StgDouble d)
348 union stg_ieee754_dbl u;
353 u.ieee.exponent == 2047 /* 2^11 - 1 */ && /* Is the exponent all ones? */
354 (u.ieee.mantissa0 != 0 || u.ieee.mantissa1 != 0)
355 /* and the mantissa non-zero? */
360 isDoubleInfinite(StgDouble d)
362 union stg_ieee754_dbl u;
366 /* Inf iff exponent is all ones, mantissa all zeros */
368 u.ieee.exponent == 2047 /* 2^11 - 1 */ &&
369 u.ieee.mantissa0 == 0 &&
370 u.ieee.mantissa1 == 0
375 isDoubleDenormalized(StgDouble d)
377 union stg_ieee754_dbl u;
381 /* A (single/double/quad) precision floating point number
384 - mantissa is non-zero.
385 - (don't care about setting of sign bit.)
389 u.ieee.exponent == 0 &&
390 (u.ieee.mantissa0 != 0 ||
391 u.ieee.mantissa1 != 0)
397 isDoubleNegativeZero(StgDouble d)
399 union stg_ieee754_dbl u;
402 /* sign (bit 63) set (only) => negative zero */
405 u.ieee.negative == 1 &&
406 u.ieee.exponent == 0 &&
407 u.ieee.mantissa0 == 0 &&
408 u.ieee.mantissa1 == 0);
411 /* Same tests, this time for StgFloats. */
414 To recap, here's the representation of a single precision
415 IEEE floating point number:
417 sign 31 sign bit (0 == positive, 1 == negative)
418 exponent 30-23 exponent (biased by 127)
419 fraction 22-0 fraction (bits to right of binary point)
424 isFloatNaN(StgFloat f)
426 union stg_ieee754_flt u;
429 /* Floating point NaN iff exponent is all ones, mantissa is
430 non-zero (but see below.) */
432 u.ieee.exponent == 255 /* 2^8 - 1 */ &&
433 u.ieee.mantissa != 0);
437 isFloatInfinite(StgFloat f)
439 union stg_ieee754_flt u;
442 /* A float is Inf iff exponent is max (all ones),
443 and mantissa is min(all zeros.) */
445 u.ieee.exponent == 255 /* 2^8 - 1 */ &&
446 u.ieee.mantissa == 0);
450 isFloatDenormalized(StgFloat f)
452 union stg_ieee754_flt u;
455 /* A (single/double/quad) precision floating point number
458 - mantissa is non-zero.
459 - (don't care about setting of sign bit.)
463 u.ieee.exponent == 0 &&
464 u.ieee.mantissa != 0);
468 isFloatNegativeZero(StgFloat f)
470 union stg_ieee754_flt u;
473 /* sign (bit 31) set (only) => negative zero */
476 u.ieee.exponent == 0 &&
477 u.ieee.mantissa == 0);
480 #else /* ! IEEE_FLOATING_POINT */
482 /* Dummy definitions of predicates - they all return false */
483 StgInt isDoubleNaN(d) StgDouble d; { return 0; }
484 StgInt isDoubleInfinite(d) StgDouble d; { return 0; }
485 StgInt isDoubleDenormalized(d) StgDouble d; { return 0; }
486 StgInt isDoubleNegativeZero(d) StgDouble d; { return 0; }
487 StgInt isFloatNaN(f) StgFloat f; { return 0; }
488 StgInt isFloatInfinite(f) StgFloat f; { return 0; }
489 StgInt isFloatDenormalized(f) StgFloat f; { return 0; }
490 StgInt isFloatNegativeZero(f) StgFloat f; { return 0; }
492 #endif /* ! IEEE_FLOATING_POINT */