1 /* -----------------------------------------------------------------------------
2 * $Id: StgPrimFloat.c,v 1.3 1999/02/05 16:02:59 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 (MP_INT *s, I_ e) /* result = s * 2^e */
52 /* Convert MP_INT to a double; knows a lot about internal rep! */
53 i = __abs(s->_mp_size)-1;
57 for (r = s->_mp_d[i], i--; i >= 0; i--)
58 r = r * GMP_BASE + s->_mp_d[i];
61 /* Now raise to the exponent */
62 if ( r != 0.0 ) /* Lennart suggests this avoids a bug in MIPS's ldexp */
65 /* sign is encoded in the size */
72 #if ! FLOATS_AS_DOUBLES
74 __encodeFloat (MP_INT *s, I_ e) /* result = s * 2^e */
79 /* Convert MP_INT to a float; knows a lot about internal rep! */
80 for(r = 0.0, i = __abs(s->_mp_size)-1; i >= 0; i--)
81 r = (r * GMP_BASE) + s->_mp_d[i];
83 /* Now raise to the exponent */
84 if ( r != 0.0 ) /* Lennart suggests this avoids a bug in MIPS's ldexp */
87 /* sign is encoded in the size */
93 #endif /* FLOATS_AS_DOUBLES */
95 /* This only supports IEEE floating point */
98 __decodeDouble (MP_INT *man, I_ *exp, StgDouble dbl)
100 /* Do some bit fiddling on IEEE */
101 nat low, high; /* assuming 32 bit ints */
103 union { double d; int i[2]; } u; /* assuming 32 bit ints, 64 bit double */
105 u.d = dbl; /* grab chunks of the double */
109 /* we know the MP_INT* passed in has size zero, so we realloc
112 man->_mp_alloc = DNBIGIT;
114 if (low == 0 && (high & ~DMSBIT) == 0) {
118 man->_mp_size = DNBIGIT;
119 iexp = ((high >> 20) & 0x7ff) + MY_DMINEXP;
123 if (iexp != MY_DMINEXP) /* don't add hidden bit to denorms */
127 /* A denorm, normalize the mantissa */
128 while (! (high & DHIGHBIT)) {
139 man->_mp_d[1] = high;
142 man->_mp_d[0] = ((unsigned long)high) << 32 | (unsigned long)low;
144 error : error : error : Cannae cope with DNBIGIT
148 man->_mp_size = -man->_mp_size;
152 #if ! FLOATS_AS_DOUBLES
154 __decodeFloat (MP_INT *man, I_ *exp, StgFloat flt)
156 /* Do some bit fiddling on IEEE */
157 int high, sign; /* assuming 32 bit ints */
158 union { float f; int i; } u; /* assuming 32 bit float and int */
160 u.f = flt; /* grab the float */
163 /* we know the MP_INT* passed in has size zero, so we realloc
166 man->_mp_alloc = FNBIGIT;
168 if ((high & ~FMSBIT) == 0) {
172 man->_mp_size = FNBIGIT;
173 *exp = ((high >> 23) & 0xff) + MY_FMINEXP;
177 if (*exp != MY_FMINEXP) /* don't add hidden bit to denorms */
181 /* A denorm, normalize the mantissa */
182 while (! (high & FHIGHBIT)) {
188 man->_mp_d[0] = high;
190 error : error : error : Cannae cope with FNBIGIT
193 man->_mp_size = -man->_mp_size;
196 #endif /* FLOATS_AS_DOUBLES */
198 /* Convenient union types for checking the layout of IEEE 754 types -
199 based on defs in GNU libc <ieee754.h>
202 union stg_ieee754_flt
208 unsigned int negative:1;
209 unsigned int exponent:8;
210 unsigned int mantissa:23;
212 unsigned int mantissa:23;
213 unsigned int exponent:8;
214 unsigned int negative:1;
220 unsigned int negative:1;
221 unsigned int exponent:8;
222 unsigned int quiet_nan:1;
223 unsigned int mantissa:22;
225 unsigned int mantissa:22;
226 unsigned int quiet_nan:1;
227 unsigned int exponent:8;
228 unsigned int negative:1;
235 To recap, here's the representation of a double precision
236 IEEE floating point number:
238 sign 63 sign bit (0==positive, 1==negative)
239 exponent 62-52 exponent (biased by 1023)
240 fraction 51-0 fraction (bits to right of binary point)
243 union stg_ieee754_dbl
249 unsigned int negative:1;
250 unsigned int exponent:11;
251 unsigned int mantissa0:20;
252 unsigned int mantissa1:32;
254 unsigned int mantissa1:32;
255 unsigned int mantissa0:20;
256 unsigned int exponent:11;
257 unsigned int negative:1;
260 /* This format makes it easier to see if a NaN is a signalling NaN. */
264 unsigned int negative:1;
265 unsigned int exponent:11;
266 unsigned int quiet_nan:1;
267 unsigned int mantissa0:19;
268 unsigned int mantissa1:32;
270 unsigned int mantissa1:32;
271 unsigned int mantissa0:19;
272 unsigned int quiet_nan:1;
273 unsigned int exponent:11;
274 unsigned int negative:1;
280 * Predicates for testing for extreme IEEE fp values. Used
281 * by the bytecode evaluator and the Prelude.
285 /* In case you don't suppport IEEE, you'll just get dummy defs.. */
286 #ifdef IEEE_FLOATING_POINT
292 union stg_ieee754_dbl u;
297 u.ieee.exponent == 2047 /* 2^11 - 1 */ && /* Is the exponent all ones? */
298 (u.ieee.mantissa0 != 0 || u.ieee.mantissa1 != 0)
299 /* and the mantissa non-zero? */
307 union stg_ieee754_dbl u;
311 /* Inf iff exponent is all ones, mantissa all zeros */
313 u.ieee.exponent == 2047 /* 2^11 - 1 */ &&
314 u.ieee.mantissa0 == 0 &&
315 u.ieee.mantissa1 == 0
320 isDoubleDenormalized(d)
323 union stg_ieee754_dbl u;
327 /* A (single/double/quad) precision floating point number
330 - mantissa is non-zero.
331 - (don't care about setting of sign bit.)
335 u.ieee.exponent == 0 &&
336 (u.ieee.mantissa0 != 0 ||
337 u.ieee.mantissa1 != 0)
343 isDoubleNegativeZero(d)
346 union stg_ieee754_dbl u;
349 /* sign (bit 63) set (only) => negative zero */
352 u.ieee.negative == 1 &&
353 u.ieee.exponent == 0 &&
354 u.ieee.mantissa0 == 0 &&
355 u.ieee.mantissa1 == 0);
358 /* Same tests, this time for StgFloats. */
361 To recap, here's the representation of a single precision
362 IEEE floating point number:
364 sign 31 sign bit (0 == positive, 1 == negative)
365 exponent 30-23 exponent (biased by 127)
366 fraction 22-0 fraction (bits to right of binary point)
374 # ifdef FLOATS_AS_DOUBLES
375 return (isDoubleNaN(f));
377 union stg_ieee754_flt u;
380 /* Floating point NaN iff exponent is all ones, mantissa is
381 non-zero (but see below.) */
383 u.ieee.exponent == 255 /* 2^8 - 1 */ &&
384 u.ieee.mantissa != 0);
386 # endif /* !FLOATS_AS_DOUBLES */
393 # ifdef FLOATS_AS_DOUBLES
394 return (isDoubleInfinite(f));
396 union stg_ieee754_flt u;
399 /* A float is Inf iff exponent is max (all ones),
400 and mantissa is min(all zeros.) */
402 u.ieee.exponent == 255 /* 2^8 - 1 */ &&
403 u.ieee.mantissa == 0);
404 # endif /* !FLOATS_AS_DOUBLES */
408 isFloatDenormalized(f)
411 # ifdef FLOATS_AS_DOUBLES
412 return (isDoubleDenormalized(f));
414 union stg_ieee754_flt u;
417 /* A (single/double/quad) precision floating point number
420 - mantissa is non-zero.
421 - (don't care about setting of sign bit.)
425 u.ieee.exponent == 0 &&
426 u.ieee.mantissa != 0);
427 #endif /* !FLOATS_AS_DOUBLES */
431 isFloatNegativeZero(f)
434 #ifdef FLOATS_AS_DOUBLES
435 return (isDoubleNegativeZero(f));
437 union stg_ieee754_flt u;
440 /* sign (bit 31) set (only) => negative zero */
443 u.ieee.exponent == 0 &&
444 u.ieee.mantissa == 0);
445 # endif /* !FLOATS_AS_DOUBLES */
448 #else /* ! IEEE_FLOATING_POINT */
450 /* Dummy definitions of predicates - they all return false */
451 StgInt isDoubleNaN(d) StgDouble d; { return 0; }
452 StgInt isDoubleInfinite(d) StgDouble d; { return 0; }
453 StgInt isDoubleDenormalized(d) StgDouble d; { return 0; }
454 StgInt isDoubleNegativeZero(d) StgDouble d; { return 0; }
455 StgInt isFloatNaN(f) StgFloat f; { return 0; }
456 StgInt isFloatInfinite(f) StgFloat f; { return 0; }
457 StgInt isFloatDenormalized(f) StgFloat f; { return 0; }
458 StgInt isFloatNegativeZero(f) StgFloat f; { return 0; }
460 #endif /* ! IEEE_FLOATING_POINT */