1 /* -----------------------------------------------------------------------------
2 * $Id: StgPrimFloat.c,v 1.2 1998/12/02 13:28:53 simonm Exp $
4 * Miscellaneous support for floating-point primitives
6 * ---------------------------------------------------------------------------*/
11 * Encoding and decoding Doubles. Code based on the HBC code
15 #define GMP_BASE 4294967296.0
16 #if FLOATS_AS_DOUBLES /* defined in StgTypes.h */
17 #define DNBIGIT 1 /* mantissa of a double will fit in one long */
19 #define DNBIGIT 2 /* mantissa of a double will fit in two longs */
21 #define FNBIGIT 1 /* for float, one long */
23 #if IEEE_FLOATING_POINT
24 #define MY_DMINEXP ((DBL_MIN_EXP) - (DBL_MANT_DIG) - 1)
25 /* DMINEXP is defined in values.h on Linux (for example) */
26 #define DHIGHBIT 0x00100000
27 #define DMSBIT 0x80000000
29 #define MY_FMINEXP ((FLT_MIN_EXP) - (FLT_MANT_DIG) - 1)
30 #define FHIGHBIT 0x00800000
31 #define FMSBIT 0x80000000
34 #ifdef WORDS_BIGENDIAN
42 #define __abs(a) (( (a) >= 0 ) ? (a) : (-(a)))
45 __encodeDouble (MP_INT *s, I_ e) /* result = s * 2^e */
50 /* Convert MP_INT to a double; knows a lot about internal rep! */
51 i = __abs(s->_mp_size)-1;
55 for (r = s->_mp_d[i], i--; i >= 0; i--)
56 r = r * GMP_BASE + s->_mp_d[i];
59 /* Now raise to the exponent */
60 if ( r != 0.0 ) /* Lennart suggests this avoids a bug in MIPS's ldexp */
63 /* sign is encoded in the size */
70 #if ! FLOATS_AS_DOUBLES
72 __encodeFloat (MP_INT *s, I_ e) /* result = s * 2^e */
77 /* Convert MP_INT to a float; knows a lot about internal rep! */
78 for(r = 0.0, i = __abs(s->_mp_size)-1; i >= 0; i--)
79 r = (r * GMP_BASE) + s->_mp_d[i];
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 */
91 #endif /* FLOATS_AS_DOUBLES */
93 /* This only supports IEEE floating point */
96 __decodeDouble (MP_INT *man, I_ *exp, StgDouble dbl)
98 /* Do some bit fiddling on IEEE */
99 nat low, high; /* assuming 32 bit ints */
101 union { double d; int i[2]; } u; /* assuming 32 bit ints, 64 bit double */
103 u.d = dbl; /* grab chunks of the double */
107 /* we know the MP_INT* passed in has size zero, so we realloc
110 man->_mp_alloc = DNBIGIT;
112 if (low == 0 && (high & ~DMSBIT) == 0) {
116 man->_mp_size = DNBIGIT;
117 iexp = ((high >> 20) & 0x7ff) + MY_DMINEXP;
121 if (iexp != MY_DMINEXP) /* don't add hidden bit to denorms */
125 /* A denorm, normalize the mantissa */
126 while (! (high & DHIGHBIT)) {
137 man->_mp_d[1] = high;
140 man->_mp_d[0] = ((unsigned long)high) << 32 | (unsigned long)low;
142 error : error : error : Cannae cope with DNBIGIT
146 man->_mp_size = -man->_mp_size;
150 #if ! FLOATS_AS_DOUBLES
152 __decodeFloat (MP_INT *man, I_ *exp, StgFloat flt)
154 /* Do some bit fiddling on IEEE */
155 int high, sign; /* assuming 32 bit ints */
156 union { float f; int i; } u; /* assuming 32 bit float and int */
158 u.f = flt; /* grab the float */
161 /* we know the MP_INT* passed in has size zero, so we realloc
164 man->_mp_alloc = FNBIGIT;
166 if ((high & ~FMSBIT) == 0) {
170 man->_mp_size = FNBIGIT;
171 *exp = ((high >> 23) & 0xff) + MY_FMINEXP;
175 if (*exp != MY_FMINEXP) /* don't add hidden bit to denorms */
179 /* A denorm, normalize the mantissa */
180 while (! (high & FHIGHBIT)) {
186 man->_mp_d[0] = high;
188 error : error : error : Cannae cope with FNBIGIT
191 man->_mp_size = -man->_mp_size;
194 #endif /* FLOATS_AS_DOUBLES */
196 /* Convenient union types for checking the layout of IEEE 754 types -
197 based on defs in GNU libc <ieee754.h>
200 union stg_ieee754_flt
206 unsigned int negative:1;
207 unsigned int exponent:8;
208 unsigned int mantissa:23;
210 unsigned int mantissa:23;
211 unsigned int exponent:8;
212 unsigned int negative:1;
218 unsigned int negative:1;
219 unsigned int exponent:8;
220 unsigned int quiet_nan:1;
221 unsigned int mantissa:22;
223 unsigned int mantissa:22;
224 unsigned int quiet_nan:1;
225 unsigned int exponent:8;
226 unsigned int negative:1;
233 To recap, here's the representation of a double precision
234 IEEE floating point number:
236 sign 63 sign bit (0==positive, 1==negative)
237 exponent 62-52 exponent (biased by 1023)
238 fraction 51-0 fraction (bits to right of binary point)
241 union stg_ieee754_dbl
247 unsigned int negative:1;
248 unsigned int exponent:11;
249 unsigned int mantissa0:20;
250 unsigned int mantissa1:32;
252 unsigned int mantissa1:32;
253 unsigned int mantissa0:20;
254 unsigned int exponent:11;
255 unsigned int negative:1;
258 /* This format makes it easier to see if a NaN is a signalling NaN. */
262 unsigned int negative:1;
263 unsigned int exponent:11;
264 unsigned int quiet_nan:1;
265 unsigned int mantissa0:19;
266 unsigned int mantissa1:32;
268 unsigned int mantissa1:32;
269 unsigned int mantissa0:19;
270 unsigned int quiet_nan:1;
271 unsigned int exponent:11;
272 unsigned int negative:1;
278 * Predicates for testing for extreme IEEE fp values. Used
279 * by the bytecode evaluator and the Prelude.
283 /* In case you don't suppport IEEE, you'll just get dummy defs.. */
284 #ifdef IEEE_FLOATING_POINT
290 union stg_ieee754_dbl u;
295 u.ieee.exponent == 2047 /* 2^11 - 1 */ && /* Is the exponent all ones? */
296 (u.ieee.mantissa0 != 0 || u.ieee.mantissa1 != 0)
297 /* and the mantissa non-zero? */
305 union stg_ieee754_dbl u;
309 /* Inf iff exponent is all ones, mantissa all zeros */
311 u.ieee.exponent == 2047 /* 2^11 - 1 */ &&
312 u.ieee.mantissa0 == 0 &&
313 u.ieee.mantissa1 == 0
318 isDoubleDenormalized(d)
321 union stg_ieee754_dbl u;
325 /* A (single/double/quad) precision floating point number
328 - mantissa is non-zero.
329 - (don't care about setting of sign bit.)
333 u.ieee.exponent == 0 &&
334 (u.ieee.mantissa0 != 0 ||
335 u.ieee.mantissa1 != 0)
341 isDoubleNegativeZero(d)
344 union stg_ieee754_dbl u;
347 /* sign (bit 63) set (only) => negative zero */
350 u.ieee.negative == 1 &&
351 u.ieee.exponent == 0 &&
352 u.ieee.mantissa0 == 0 &&
353 u.ieee.mantissa1 == 0);
356 /* Same tests, this time for StgFloats. */
359 To recap, here's the representation of a single precision
360 IEEE floating point number:
362 sign 31 sign bit (0 == positive, 1 == negative)
363 exponent 30-23 exponent (biased by 127)
364 fraction 22-0 fraction (bits to right of binary point)
372 # ifdef FLOATS_AS_DOUBLES
373 return (isDoubleNaN(f));
375 union stg_ieee754_flt u;
378 /* Floating point NaN iff exponent is all ones, mantissa is
379 non-zero (but see below.) */
381 u.ieee.exponent == 255 /* 2^8 - 1 */ &&
382 u.ieee.mantissa != 0);
384 # endif /* !FLOATS_AS_DOUBLES */
391 # ifdef FLOATS_AS_DOUBLES
392 return (isDoubleInfinite(f));
394 union stg_ieee754_flt u;
397 /* A float is Inf iff exponent is max (all ones),
398 and mantissa is min(all zeros.) */
400 u.ieee.exponent == 255 /* 2^8 - 1 */ &&
401 u.ieee.mantissa == 0);
402 # endif /* !FLOATS_AS_DOUBLES */
406 isFloatDenormalized(f)
409 # ifdef FLOATS_AS_DOUBLES
410 return (isDoubleDenormalized(f));
412 union stg_ieee754_flt u;
415 /* A (single/double/quad) precision floating point number
418 - mantissa is non-zero.
419 - (don't care about setting of sign bit.)
423 u.ieee.exponent == 0 &&
424 u.ieee.mantissa != 0);
425 #endif /* !FLOATS_AS_DOUBLES */
429 isFloatNegativeZero(f)
432 #ifdef FLOATS_AS_DOUBLES
433 return (isDoubleNegativeZero(f));
435 union stg_ieee754_flt u;
438 /* sign (bit 31) set (only) => negative zero */
441 u.ieee.exponent == 0 &&
442 u.ieee.mantissa == 0);
443 # endif /* !FLOATS_AS_DOUBLES */
446 #else /* ! IEEE_FLOATING_POINT */
448 /* Dummy definitions of predicates - they all return false */
449 StgInt isDoubleNaN(d) StgDouble d; { return 0; }
450 StgInt isDoubleInfinite(d) StgDouble d; { return 0; }
451 StgInt isDoubleDenormalized(d) StgDouble d; { return 0; }
452 StgInt isDoubleNegativeZero(d) StgDouble d; { return 0; }
453 StgInt isFloatNaN(f) StgFloat f; { return 0; }
454 StgInt isFloatInfinite(f) StgFloat f; { return 0; }
455 StgInt isFloatDenormalized(f) StgFloat f; { return 0; }
456 StgInt isFloatNegativeZero(f) StgFloat f; { return 0; }
458 #endif /* ! IEEE_FLOATING_POINT */