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