1 /* -----------------------------------------------------------------------------
2 * $Id: StgPrimFloat.c,v 1.4 1999/02/18 12:26:12 simonm Exp $
4 * (c) The GHC Team, 1998-1999
6 * Miscellaneous support for floating-point primitives
8 * ---------------------------------------------------------------------------*/
13 * Encoding and decoding Doubles. Code based on the HBC code
17 #define GMP_BASE 4294967296.0
18 #if FLOATS_AS_DOUBLES /* defined in StgTypes.h */
19 #define DNBIGIT 1 /* mantissa of a double will fit in one long */
21 #define DNBIGIT 2 /* mantissa of a double will fit in two longs */
23 #define FNBIGIT 1 /* for float, one long */
25 #if IEEE_FLOATING_POINT
26 #define MY_DMINEXP ((DBL_MIN_EXP) - (DBL_MANT_DIG) - 1)
27 /* DMINEXP is defined in values.h on Linux (for example) */
28 #define DHIGHBIT 0x00100000
29 #define DMSBIT 0x80000000
31 #define MY_FMINEXP ((FLT_MIN_EXP) - (FLT_MANT_DIG) - 1)
32 #define FHIGHBIT 0x00800000
33 #define FMSBIT 0x80000000
36 #ifdef WORDS_BIGENDIAN
44 #define __abs(a) (( (a) >= 0 ) ? (a) : (-(a)))
47 __encodeDouble (I_ size, StgByteArray ba, I_ e) /* result = s * 2^e */
53 /* Convert MP_INT to a double; knows a lot about internal rep! */
58 for (r = arr[i], i--; i >= 0; i--)
59 r = r * GMP_BASE + arr[i];
62 /* Now raise to the exponent */
63 if ( r != 0.0 ) /* Lennart suggests this avoids a bug in MIPS's ldexp */
66 /* sign is encoded in the size */
73 /* Special version for small Integers */
75 __int_encodeDouble (I_ j, I_ e)
79 r = (StgDouble)__abs(j);
81 /* Now raise to the exponent */
82 if ( r != 0.0 ) /* Lennart suggests this avoids a bug in MIPS's ldexp */
85 /* sign is encoded in the size */
92 #if ! FLOATS_AS_DOUBLES
94 __encodeFloat (I_ size, StgByteArray ba, I_ e) /* result = s * 2^e */
100 /* Convert MP_INT to a float; knows a lot about internal rep! */
101 for(r = 0.0, i = __abs(size); i >= 0; i--)
102 r = (r * GMP_BASE) + arr[i];
104 /* Now raise to the exponent */
105 if ( r != 0.0 ) /* Lennart suggests this avoids a bug in MIPS's ldexp */
108 /* sign is encoded in the size */
115 /* Special version for small Integers */
117 __int_encodeFloat (I_ j, I_ e)
121 r = (StgFloat)__abs(j);
123 /* Now raise to the exponent */
124 if ( r != 0.0 ) /* Lennart suggests this avoids a bug in MIPS's ldexp */
127 /* sign is encoded in the size */
134 #endif /* FLOATS_AS_DOUBLES */
136 /* This only supports IEEE floating point */
139 __decodeDouble (MP_INT *man, I_ *exp, StgDouble dbl)
141 /* Do some bit fiddling on IEEE */
142 nat low, high; /* assuming 32 bit ints */
144 union { double d; int i[2]; } u; /* assuming 32 bit ints, 64 bit double */
146 u.d = dbl; /* grab chunks of the double */
150 /* we know the MP_INT* passed in has size zero, so we realloc
153 man->_mp_alloc = DNBIGIT;
155 if (low == 0 && (high & ~DMSBIT) == 0) {
159 man->_mp_size = DNBIGIT;
160 iexp = ((high >> 20) & 0x7ff) + MY_DMINEXP;
164 if (iexp != MY_DMINEXP) /* don't add hidden bit to denorms */
168 /* A denorm, normalize the mantissa */
169 while (! (high & DHIGHBIT)) {
180 man->_mp_d[1] = high;
183 man->_mp_d[0] = ((unsigned long)high) << 32 | (unsigned long)low;
185 error : error : error : Cannae cope with DNBIGIT
189 man->_mp_size = -man->_mp_size;
193 #if ! FLOATS_AS_DOUBLES
195 __decodeFloat (MP_INT *man, I_ *exp, StgFloat flt)
197 /* Do some bit fiddling on IEEE */
198 int high, sign; /* assuming 32 bit ints */
199 union { float f; int i; } u; /* assuming 32 bit float and int */
201 u.f = flt; /* grab the float */
204 /* we know the MP_INT* passed in has size zero, so we realloc
207 man->_mp_alloc = FNBIGIT;
209 if ((high & ~FMSBIT) == 0) {
213 man->_mp_size = FNBIGIT;
214 *exp = ((high >> 23) & 0xff) + MY_FMINEXP;
218 if (*exp != MY_FMINEXP) /* don't add hidden bit to denorms */
222 /* A denorm, normalize the mantissa */
223 while (! (high & FHIGHBIT)) {
229 man->_mp_d[0] = high;
231 error : error : error : Cannae cope with FNBIGIT
234 man->_mp_size = -man->_mp_size;
237 #endif /* FLOATS_AS_DOUBLES */
239 /* Convenient union types for checking the layout of IEEE 754 types -
240 based on defs in GNU libc <ieee754.h>
243 union stg_ieee754_flt
249 unsigned int negative:1;
250 unsigned int exponent:8;
251 unsigned int mantissa:23;
253 unsigned int mantissa:23;
254 unsigned int exponent:8;
255 unsigned int negative:1;
261 unsigned int negative:1;
262 unsigned int exponent:8;
263 unsigned int quiet_nan:1;
264 unsigned int mantissa:22;
266 unsigned int mantissa:22;
267 unsigned int quiet_nan:1;
268 unsigned int exponent:8;
269 unsigned int negative:1;
276 To recap, here's the representation of a double precision
277 IEEE floating point number:
279 sign 63 sign bit (0==positive, 1==negative)
280 exponent 62-52 exponent (biased by 1023)
281 fraction 51-0 fraction (bits to right of binary point)
284 union stg_ieee754_dbl
290 unsigned int negative:1;
291 unsigned int exponent:11;
292 unsigned int mantissa0:20;
293 unsigned int mantissa1:32;
295 unsigned int mantissa1:32;
296 unsigned int mantissa0:20;
297 unsigned int exponent:11;
298 unsigned int negative:1;
301 /* This format makes it easier to see if a NaN is a signalling NaN. */
305 unsigned int negative:1;
306 unsigned int exponent:11;
307 unsigned int quiet_nan:1;
308 unsigned int mantissa0:19;
309 unsigned int mantissa1:32;
311 unsigned int mantissa1:32;
312 unsigned int mantissa0:19;
313 unsigned int quiet_nan:1;
314 unsigned int exponent:11;
315 unsigned int negative:1;
321 * Predicates for testing for extreme IEEE fp values. Used
322 * by the bytecode evaluator and the Prelude.
326 /* In case you don't suppport IEEE, you'll just get dummy defs.. */
327 #ifdef IEEE_FLOATING_POINT
333 union stg_ieee754_dbl u;
338 u.ieee.exponent == 2047 /* 2^11 - 1 */ && /* Is the exponent all ones? */
339 (u.ieee.mantissa0 != 0 || u.ieee.mantissa1 != 0)
340 /* and the mantissa non-zero? */
348 union stg_ieee754_dbl u;
352 /* Inf iff exponent is all ones, mantissa all zeros */
354 u.ieee.exponent == 2047 /* 2^11 - 1 */ &&
355 u.ieee.mantissa0 == 0 &&
356 u.ieee.mantissa1 == 0
361 isDoubleDenormalized(d)
364 union stg_ieee754_dbl u;
368 /* A (single/double/quad) precision floating point number
371 - mantissa is non-zero.
372 - (don't care about setting of sign bit.)
376 u.ieee.exponent == 0 &&
377 (u.ieee.mantissa0 != 0 ||
378 u.ieee.mantissa1 != 0)
384 isDoubleNegativeZero(d)
387 union stg_ieee754_dbl u;
390 /* sign (bit 63) set (only) => negative zero */
393 u.ieee.negative == 1 &&
394 u.ieee.exponent == 0 &&
395 u.ieee.mantissa0 == 0 &&
396 u.ieee.mantissa1 == 0);
399 /* Same tests, this time for StgFloats. */
402 To recap, here's the representation of a single precision
403 IEEE floating point number:
405 sign 31 sign bit (0 == positive, 1 == negative)
406 exponent 30-23 exponent (biased by 127)
407 fraction 22-0 fraction (bits to right of binary point)
415 # ifdef FLOATS_AS_DOUBLES
416 return (isDoubleNaN(f));
418 union stg_ieee754_flt u;
421 /* Floating point NaN iff exponent is all ones, mantissa is
422 non-zero (but see below.) */
424 u.ieee.exponent == 255 /* 2^8 - 1 */ &&
425 u.ieee.mantissa != 0);
427 # endif /* !FLOATS_AS_DOUBLES */
434 # ifdef FLOATS_AS_DOUBLES
435 return (isDoubleInfinite(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);
445 # endif /* !FLOATS_AS_DOUBLES */
449 isFloatDenormalized(f)
452 # ifdef FLOATS_AS_DOUBLES
453 return (isDoubleDenormalized(f));
455 union stg_ieee754_flt u;
458 /* A (single/double/quad) precision floating point number
461 - mantissa is non-zero.
462 - (don't care about setting of sign bit.)
466 u.ieee.exponent == 0 &&
467 u.ieee.mantissa != 0);
468 #endif /* !FLOATS_AS_DOUBLES */
472 isFloatNegativeZero(f)
475 #ifdef FLOATS_AS_DOUBLES
476 return (isDoubleNegativeZero(f));
478 union stg_ieee754_flt u;
481 /* sign (bit 31) set (only) => negative zero */
484 u.ieee.exponent == 0 &&
485 u.ieee.mantissa == 0);
486 # endif /* !FLOATS_AS_DOUBLES */
489 #else /* ! IEEE_FLOATING_POINT */
491 /* Dummy definitions of predicates - they all return false */
492 StgInt isDoubleNaN(d) StgDouble d; { return 0; }
493 StgInt isDoubleInfinite(d) StgDouble d; { return 0; }
494 StgInt isDoubleDenormalized(d) StgDouble d; { return 0; }
495 StgInt isDoubleNegativeZero(d) StgDouble d; { return 0; }
496 StgInt isFloatNaN(f) StgFloat f; { return 0; }
497 StgInt isFloatInfinite(f) StgFloat f; { return 0; }
498 StgInt isFloatDenormalized(f) StgFloat f; { return 0; }
499 StgInt isFloatNegativeZero(f) StgFloat f; { return 0; }
501 #endif /* ! IEEE_FLOATING_POINT */