1 /* -----------------------------------------------------------------------------
2 * $Id: StgPrimFloat.c,v 1.5 1999/02/22 10:51:18 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! */
54 for(r = 0.0, i = __abs(size)-1; i >= 0; i--)
55 r = (r * GMP_BASE) + arr[i];
57 /* Now raise to the exponent */
58 if ( r != 0.0 ) /* Lennart suggests this avoids a bug in MIPS's ldexp */
61 /* sign is encoded in the size */
68 /* Special version for small Integers */
70 __int_encodeDouble (I_ j, I_ e)
74 r = (StgDouble)__abs(j);
76 /* Now raise to the exponent */
77 if ( r != 0.0 ) /* Lennart suggests this avoids a bug in MIPS's ldexp */
80 /* sign is encoded in the size */
87 #if ! FLOATS_AS_DOUBLES
89 __encodeFloat (I_ size, StgByteArray ba, I_ e) /* result = s * 2^e */
95 /* Convert MP_INT to a float; knows a lot about internal rep! */
96 for(r = 0.0, i = __abs(size)-1; i >= 0; i--)
97 r = (r * GMP_BASE) + arr[i];
99 /* Now raise to the exponent */
100 if ( r != 0.0 ) /* Lennart suggests this avoids a bug in MIPS's ldexp */
103 /* sign is encoded in the size */
110 /* Special version for small Integers */
112 __int_encodeFloat (I_ j, I_ e)
116 r = (StgFloat)__abs(j);
118 /* Now raise to the exponent */
119 if ( r != 0.0 ) /* Lennart suggests this avoids a bug in MIPS's ldexp */
122 /* sign is encoded in the size */
129 #endif /* FLOATS_AS_DOUBLES */
131 /* This only supports IEEE floating point */
134 __decodeDouble (MP_INT *man, I_ *exp, StgDouble dbl)
136 /* Do some bit fiddling on IEEE */
137 nat low, high; /* assuming 32 bit ints */
139 union { double d; int i[2]; } u; /* assuming 32 bit ints, 64 bit double */
141 u.d = dbl; /* grab chunks of the double */
145 /* we know the MP_INT* passed in has size zero, so we realloc
148 man->_mp_alloc = DNBIGIT;
150 if (low == 0 && (high & ~DMSBIT) == 0) {
154 man->_mp_size = DNBIGIT;
155 iexp = ((high >> 20) & 0x7ff) + MY_DMINEXP;
159 if (iexp != MY_DMINEXP) /* don't add hidden bit to denorms */
163 /* A denorm, normalize the mantissa */
164 while (! (high & DHIGHBIT)) {
175 man->_mp_d[1] = high;
178 man->_mp_d[0] = ((unsigned long)high) << 32 | (unsigned long)low;
180 error : error : error : Cannae cope with DNBIGIT
184 man->_mp_size = -man->_mp_size;
188 #if ! FLOATS_AS_DOUBLES
190 __decodeFloat (MP_INT *man, I_ *exp, StgFloat flt)
192 /* Do some bit fiddling on IEEE */
193 int high, sign; /* assuming 32 bit ints */
194 union { float f; int i; } u; /* assuming 32 bit float and int */
196 u.f = flt; /* grab the float */
199 /* we know the MP_INT* passed in has size zero, so we realloc
202 man->_mp_alloc = FNBIGIT;
204 if ((high & ~FMSBIT) == 0) {
208 man->_mp_size = FNBIGIT;
209 *exp = ((high >> 23) & 0xff) + MY_FMINEXP;
213 if (*exp != MY_FMINEXP) /* don't add hidden bit to denorms */
217 /* A denorm, normalize the mantissa */
218 while (! (high & FHIGHBIT)) {
224 man->_mp_d[0] = high;
226 error : error : error : Cannae cope with FNBIGIT
229 man->_mp_size = -man->_mp_size;
232 #endif /* FLOATS_AS_DOUBLES */
234 /* Convenient union types for checking the layout of IEEE 754 types -
235 based on defs in GNU libc <ieee754.h>
238 union stg_ieee754_flt
244 unsigned int negative:1;
245 unsigned int exponent:8;
246 unsigned int mantissa:23;
248 unsigned int mantissa:23;
249 unsigned int exponent:8;
250 unsigned int negative:1;
256 unsigned int negative:1;
257 unsigned int exponent:8;
258 unsigned int quiet_nan:1;
259 unsigned int mantissa:22;
261 unsigned int mantissa:22;
262 unsigned int quiet_nan:1;
263 unsigned int exponent:8;
264 unsigned int negative:1;
271 To recap, here's the representation of a double precision
272 IEEE floating point number:
274 sign 63 sign bit (0==positive, 1==negative)
275 exponent 62-52 exponent (biased by 1023)
276 fraction 51-0 fraction (bits to right of binary point)
279 union stg_ieee754_dbl
285 unsigned int negative:1;
286 unsigned int exponent:11;
287 unsigned int mantissa0:20;
288 unsigned int mantissa1:32;
290 unsigned int mantissa1:32;
291 unsigned int mantissa0:20;
292 unsigned int exponent:11;
293 unsigned int negative:1;
296 /* This format makes it easier to see if a NaN is a signalling NaN. */
300 unsigned int negative:1;
301 unsigned int exponent:11;
302 unsigned int quiet_nan:1;
303 unsigned int mantissa0:19;
304 unsigned int mantissa1:32;
306 unsigned int mantissa1:32;
307 unsigned int mantissa0:19;
308 unsigned int quiet_nan:1;
309 unsigned int exponent:11;
310 unsigned int negative:1;
316 * Predicates for testing for extreme IEEE fp values. Used
317 * by the bytecode evaluator and the Prelude.
321 /* In case you don't suppport IEEE, you'll just get dummy defs.. */
322 #ifdef IEEE_FLOATING_POINT
328 union stg_ieee754_dbl u;
333 u.ieee.exponent == 2047 /* 2^11 - 1 */ && /* Is the exponent all ones? */
334 (u.ieee.mantissa0 != 0 || u.ieee.mantissa1 != 0)
335 /* and the mantissa non-zero? */
343 union stg_ieee754_dbl u;
347 /* Inf iff exponent is all ones, mantissa all zeros */
349 u.ieee.exponent == 2047 /* 2^11 - 1 */ &&
350 u.ieee.mantissa0 == 0 &&
351 u.ieee.mantissa1 == 0
356 isDoubleDenormalized(d)
359 union stg_ieee754_dbl u;
363 /* A (single/double/quad) precision floating point number
366 - mantissa is non-zero.
367 - (don't care about setting of sign bit.)
371 u.ieee.exponent == 0 &&
372 (u.ieee.mantissa0 != 0 ||
373 u.ieee.mantissa1 != 0)
379 isDoubleNegativeZero(d)
382 union stg_ieee754_dbl u;
385 /* sign (bit 63) set (only) => negative zero */
388 u.ieee.negative == 1 &&
389 u.ieee.exponent == 0 &&
390 u.ieee.mantissa0 == 0 &&
391 u.ieee.mantissa1 == 0);
394 /* Same tests, this time for StgFloats. */
397 To recap, here's the representation of a single precision
398 IEEE floating point number:
400 sign 31 sign bit (0 == positive, 1 == negative)
401 exponent 30-23 exponent (biased by 127)
402 fraction 22-0 fraction (bits to right of binary point)
410 # ifdef FLOATS_AS_DOUBLES
411 return (isDoubleNaN(f));
413 union stg_ieee754_flt u;
416 /* Floating point NaN iff exponent is all ones, mantissa is
417 non-zero (but see below.) */
419 u.ieee.exponent == 255 /* 2^8 - 1 */ &&
420 u.ieee.mantissa != 0);
422 # endif /* !FLOATS_AS_DOUBLES */
429 # ifdef FLOATS_AS_DOUBLES
430 return (isDoubleInfinite(f));
432 union stg_ieee754_flt u;
435 /* A float is Inf iff exponent is max (all ones),
436 and mantissa is min(all zeros.) */
438 u.ieee.exponent == 255 /* 2^8 - 1 */ &&
439 u.ieee.mantissa == 0);
440 # endif /* !FLOATS_AS_DOUBLES */
444 isFloatDenormalized(f)
447 # ifdef FLOATS_AS_DOUBLES
448 return (isDoubleDenormalized(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);
463 #endif /* !FLOATS_AS_DOUBLES */
467 isFloatNegativeZero(f)
470 #ifdef FLOATS_AS_DOUBLES
471 return (isDoubleNegativeZero(f));
473 union stg_ieee754_flt u;
476 /* sign (bit 31) set (only) => negative zero */
479 u.ieee.exponent == 0 &&
480 u.ieee.mantissa == 0);
481 # endif /* !FLOATS_AS_DOUBLES */
484 #else /* ! IEEE_FLOATING_POINT */
486 /* Dummy definitions of predicates - they all return false */
487 StgInt isDoubleNaN(d) StgDouble d; { return 0; }
488 StgInt isDoubleInfinite(d) StgDouble d; { return 0; }
489 StgInt isDoubleDenormalized(d) StgDouble d; { return 0; }
490 StgInt isDoubleNegativeZero(d) StgDouble d; { return 0; }
491 StgInt isFloatNaN(f) StgFloat f; { return 0; }
492 StgInt isFloatInfinite(f) StgFloat f; { return 0; }
493 StgInt isFloatDenormalized(f) StgFloat f; { return 0; }
494 StgInt isFloatNegativeZero(f) StgFloat f; { return 0; }
496 #endif /* ! IEEE_FLOATING_POINT */