[project @ 1999-02-05 16:02:18 by simonm]
[ghc-hetmet.git] / ghc / rts / StgPrimFloat.c
1 /* -----------------------------------------------------------------------------
2  * $Id: StgPrimFloat.c,v 1.3 1999/02/05 16:02:59 simonm Exp $
3  *
4  * (c) The GHC Team, 1998-1999
5  *
6  * Miscellaneous support for floating-point primitives
7  *
8  * ---------------------------------------------------------------------------*/
9
10 #include "Rts.h"
11
12 /*
13  * Encoding and decoding Doubles.  Code based on the HBC code
14  * (lib/fltcode.c).
15  */
16
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 */
20 #else
21 #define DNBIGIT  2  /* mantissa of a double will fit in two longs */
22 #endif
23 #define FNBIGIT  1  /* for float, one long */
24
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
30
31 #define MY_FMINEXP  ((FLT_MIN_EXP) - (FLT_MANT_DIG) - 1)
32 #define FHIGHBIT 0x00800000
33 #define FMSBIT   0x80000000
34 #endif
35
36 #ifdef WORDS_BIGENDIAN
37 #define L 1
38 #define H 0
39 #else
40 #define L 0
41 #define H 1
42 #endif
43
44 #define __abs(a)                (( (a) >= 0 ) ? (a) : (-(a)))
45
46 StgDouble
47 __encodeDouble (MP_INT *s, I_ e) /* result = s * 2^e */
48 {
49     StgDouble r;
50     I_ i;
51
52     /* Convert MP_INT to a double; knows a lot about internal rep! */
53     i = __abs(s->_mp_size)-1;
54     if (i < 0) {
55       r = 0.0;
56     } else {
57       for (r = s->_mp_d[i], i--; i >= 0; i--)
58         r = r * GMP_BASE + s->_mp_d[i];
59     }
60
61     /* Now raise to the exponent */
62     if ( r != 0.0 ) /* Lennart suggests this avoids a bug in MIPS's ldexp */
63         r = ldexp(r, e);
64
65     /* sign is encoded in the size */
66     if (s->_mp_size < 0)
67         r = -r;
68
69     return r;
70 }
71
72 #if ! FLOATS_AS_DOUBLES
73 StgFloat
74 __encodeFloat (MP_INT *s, I_ e) /* result = s * 2^e */
75 {
76     StgFloat r;
77     I_ i;
78
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];
82
83     /* Now raise to the exponent */
84     if ( r != 0.0 ) /* Lennart suggests this avoids a bug in MIPS's ldexp */
85         r = ldexp(r, e);
86
87     /* sign is encoded in the size */
88     if (s->_mp_size < 0)
89         r = -r;
90
91     return r;
92 }
93 #endif  /* FLOATS_AS_DOUBLES */
94
95 /* This only supports IEEE floating point */
96
97 void
98 __decodeDouble (MP_INT *man, I_ *exp, StgDouble dbl)
99 {
100     /* Do some bit fiddling on IEEE */
101     nat low, high;              /* assuming 32 bit ints */
102     int sign, iexp;
103     union { double d; int i[2]; } u;    /* assuming 32 bit ints, 64 bit double */
104
105     u.d = dbl;      /* grab chunks of the double */
106     low = u.i[L];
107     high = u.i[H];
108
109     /* we know the MP_INT* passed in has size zero, so we realloc
110         no matter what.
111     */
112     man->_mp_alloc = DNBIGIT;
113
114     if (low == 0 && (high & ~DMSBIT) == 0) {
115         man->_mp_size = 0;
116         *exp = 0L;
117     } else {
118         man->_mp_size = DNBIGIT;
119         iexp = ((high >> 20) & 0x7ff) + MY_DMINEXP;
120         sign = high;
121
122         high &= DHIGHBIT-1;
123         if (iexp != MY_DMINEXP) /* don't add hidden bit to denorms */
124             high |= DHIGHBIT;
125         else {
126             iexp++;
127             /* A denorm, normalize the mantissa */
128             while (! (high & DHIGHBIT)) {
129                 high <<= 1;
130                 if (low & DMSBIT)
131                     high++;
132                 low <<= 1;
133                 iexp--;
134             }
135         }
136         *exp = (I_) iexp;
137 #if DNBIGIT == 2
138         man->_mp_d[0] = low;
139         man->_mp_d[1] = high;
140 #else
141 #if DNBIGIT == 1
142         man->_mp_d[0] = ((unsigned long)high) << 32 | (unsigned long)low;
143 #else
144         error : error : error : Cannae cope with DNBIGIT
145 #endif
146 #endif
147         if (sign < 0)
148             man->_mp_size = -man->_mp_size;
149     }
150 }
151
152 #if ! FLOATS_AS_DOUBLES
153 void
154 __decodeFloat (MP_INT *man, I_ *exp, StgFloat flt)
155 {
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 */
159
160     u.f = flt;      /* grab the float */
161     high = u.i;
162
163     /* we know the MP_INT* passed in has size zero, so we realloc
164         no matter what.
165     */
166     man->_mp_alloc = FNBIGIT;
167
168     if ((high & ~FMSBIT) == 0) {
169         man->_mp_size = 0;
170         *exp = 0;
171     } else {
172         man->_mp_size = FNBIGIT;
173         *exp = ((high >> 23) & 0xff) + MY_FMINEXP;
174         sign = high;
175
176         high &= FHIGHBIT-1;
177         if (*exp != MY_FMINEXP) /* don't add hidden bit to denorms */
178             high |= FHIGHBIT;
179         else {
180             (*exp)++;
181             /* A denorm, normalize the mantissa */
182             while (! (high & FHIGHBIT)) {
183                 high <<= 1;
184                 (*exp)--;
185             }
186         }
187 #if FNBIGIT == 1
188         man->_mp_d[0] = high;
189 #else
190         error : error : error : Cannae cope with FNBIGIT
191 #endif
192         if (sign < 0)
193             man->_mp_size = -man->_mp_size;
194     }
195 }
196 #endif  /* FLOATS_AS_DOUBLES */
197
198 /* Convenient union types for checking the layout of IEEE 754 types -
199    based on defs in GNU libc <ieee754.h>
200 */
201
202 union stg_ieee754_flt
203 {
204    float f;
205    struct {
206
207 #if WORDS_BIGENDIAN
208         unsigned int negative:1;
209         unsigned int exponent:8;
210         unsigned int mantissa:23;
211 #else
212         unsigned int mantissa:23;
213         unsigned int exponent:8;
214         unsigned int negative:1;
215 #endif
216    } ieee;
217    struct {
218
219 #if WORDS_BIGENDIAN
220         unsigned int negative:1;
221         unsigned int exponent:8;
222         unsigned int quiet_nan:1;
223         unsigned int mantissa:22;
224 #else
225         unsigned int mantissa:22;
226         unsigned int quiet_nan:1;
227         unsigned int exponent:8;
228         unsigned int negative:1;
229 #endif
230    } ieee_nan;
231 };
232
233 /*
234  
235  To recap, here's the representation of a double precision
236  IEEE floating point number:
237
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)
241 */
242
243 union stg_ieee754_dbl
244 {
245    double d;
246    struct {
247
248 #if WORDS_BIGENDIAN
249         unsigned int negative:1;
250         unsigned int exponent:11;
251         unsigned int mantissa0:20;
252         unsigned int mantissa1:32;
253 #else
254         unsigned int mantissa1:32;
255         unsigned int mantissa0:20;
256         unsigned int exponent:11;
257         unsigned int negative:1;
258 #endif
259    } ieee;
260     /* This format makes it easier to see if a NaN is a signalling NaN.  */
261    struct {
262
263 #if WORDS_BIGENDIAN
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;
269 #else
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;
275 #endif
276    } ieee_nan;
277 };
278
279 /*
280  * Predicates for testing for extreme IEEE fp values. Used
281  * by the bytecode evaluator and the Prelude.
282  *
283  */ 
284
285 /* In case you don't suppport IEEE, you'll just get dummy defs.. */
286 #ifdef IEEE_FLOATING_POINT
287
288 StgInt
289 isDoubleNaN(d)
290 StgDouble d;
291 {
292   union stg_ieee754_dbl u;
293   
294   u.d = d;
295
296   return (
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? */
300     );
301 }
302
303 StgInt
304 isDoubleInfinite(d)
305 StgDouble d;
306 {
307     union stg_ieee754_dbl u;
308
309     u.d = d;
310
311     /* Inf iff exponent is all ones, mantissa all zeros */
312     return (
313         u.ieee.exponent  == 2047 /* 2^11 - 1 */ &&
314         u.ieee.mantissa0 == 0                   &&
315         u.ieee.mantissa1 == 0
316       );
317 }
318
319 StgInt
320 isDoubleDenormalized(d) 
321 StgDouble d;
322 {
323     union stg_ieee754_dbl u;
324
325     u.d = d;
326
327     /* A (single/double/quad) precision floating point number
328        is denormalised iff:
329         - exponent is zero
330         - mantissa is non-zero.
331         - (don't care about setting of sign bit.)
332
333     */
334     return (  
335         u.ieee.exponent  == 0 &&
336         (u.ieee.mantissa0 != 0 ||
337          u.ieee.mantissa1 != 0)
338       );
339          
340 }
341
342 StgInt
343 isDoubleNegativeZero(d) 
344 StgDouble d;
345 {
346     union stg_ieee754_dbl u;
347
348     u.d = d;
349     /* sign (bit 63) set (only) => negative zero */
350
351     return (
352         u.ieee.negative  == 1 &&
353         u.ieee.exponent  == 0 &&
354         u.ieee.mantissa0 == 0 &&
355         u.ieee.mantissa1 == 0);
356 }
357
358 /* Same tests, this time for StgFloats. */
359
360 /*
361  To recap, here's the representation of a single precision
362  IEEE floating point number:
363
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)
367 */
368
369
370 StgInt
371 isFloatNaN(f) 
372 StgFloat f;
373 {
374 # ifdef FLOATS_AS_DOUBLES
375     return (isDoubleNaN(f));
376 # else
377     union stg_ieee754_flt u;
378     u.f = f;
379
380    /* Floating point NaN iff exponent is all ones, mantissa is
381       non-zero (but see below.) */
382    return (
383         u.ieee.exponent == 255 /* 2^8 - 1 */ &&
384         u.ieee.mantissa != 0);
385
386 # endif /* !FLOATS_AS_DOUBLES */
387 }
388
389 StgInt
390 isFloatInfinite(f) 
391 StgFloat f;
392 {
393 # ifdef FLOATS_AS_DOUBLES
394     return (isDoubleInfinite(f));
395 # else
396     union stg_ieee754_flt u;
397     u.f = f;
398   
399     /* A float is Inf iff exponent is max (all ones),
400        and mantissa is min(all zeros.) */
401     return (
402         u.ieee.exponent == 255 /* 2^8 - 1 */ &&
403         u.ieee.mantissa == 0);
404 # endif /* !FLOATS_AS_DOUBLES */
405 }
406
407 StgInt
408 isFloatDenormalized(f) 
409 StgFloat f;
410 {
411 # ifdef FLOATS_AS_DOUBLES
412     return (isDoubleDenormalized(f));
413 # else
414     union stg_ieee754_flt u;
415     u.f = f;
416
417     /* A (single/double/quad) precision floating point number
418        is denormalised iff:
419         - exponent is zero
420         - mantissa is non-zero.
421         - (don't care about setting of sign bit.)
422
423     */
424     return (
425         u.ieee.exponent == 0 &&
426         u.ieee.mantissa != 0);
427 #endif /* !FLOATS_AS_DOUBLES */
428 }
429
430 StgInt
431 isFloatNegativeZero(f) 
432 StgFloat f;
433 {
434 #ifdef FLOATS_AS_DOUBLES
435     return (isDoubleNegativeZero(f));
436 # else
437     union stg_ieee754_flt u;
438     u.f = f;
439
440     /* sign (bit 31) set (only) => negative zero */
441     return (
442         u.ieee.negative      &&
443         u.ieee.exponent == 0 &&
444         u.ieee.mantissa == 0);
445 # endif /* !FLOATS_AS_DOUBLES */
446 }
447
448 #else /* ! IEEE_FLOATING_POINT */
449
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; }
459
460 #endif /* ! IEEE_FLOATING_POINT */