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