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