1 /* -----------------------------------------------------------------------------
3 * (c) Lennart Augustsson
4 * (c) The GHC Team, 1998-2000
6 * Miscellaneous support for floating-point primitives
8 * ---------------------------------------------------------------------------*/
10 #include "PosixSource.h"
13 #include "StgPrimFloat.h"
18 #define IEEE_FLOATING_POINT 1
21 * Encoding and decoding Doubles. Code based on the HBC code
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
31 #define MY_FMINEXP ((FLT_MIN_EXP) - (FLT_MANT_DIG) - 1)
32 #define FHIGHBIT 0x00800000
33 #define FMSBIT 0x80000000
36 #if defined(WORDS_BIGENDIAN) || defined(FLOAT_WORDS_BIGENDIAN)
44 #define __abs(a) (( (a) >= 0 ) ? (a) : (-(a)))
47 __2Int_encodeDouble (I_ j_high, I_ j_low, I_ e)
51 /* assuming 32 bit ints */
52 ASSERT(sizeof(int ) == 4 );
54 r = (StgDouble)((unsigned int)j_high);
55 r *= 4294967296.0; /* exp2f(32); */
56 r += (StgDouble)((unsigned int)j_low);
58 /* Now raise to the exponent */
59 if ( r != 0.0 ) /* Lennart suggests this avoids a bug in MIPS's ldexp */
62 /* sign is encoded in the size */
69 /* Special version for words */
71 __word_encodeDouble (W_ j, I_ e)
77 /* Now raise to the exponent */
78 if ( r != 0.0 ) /* Lennart suggests this avoids a bug in MIPS's ldexp */
84 /* Special version for small Integers */
86 __int_encodeDouble (I_ j, I_ e)
90 r = (StgDouble)__abs(j);
92 /* Now raise to the exponent */
93 if ( r != 0.0 ) /* Lennart suggests this avoids a bug in MIPS's ldexp */
96 /* sign is encoded in the size */
103 /* Special version for small Integers */
105 __int_encodeFloat (I_ j, I_ e)
109 r = (StgFloat)__abs(j);
111 /* Now raise to the exponent */
112 if ( r != 0.0 ) /* Lennart suggests this avoids a bug in MIPS's ldexp */
115 /* sign is encoded in the size */
122 /* Special version for small positive Integers */
124 __word_encodeFloat (W_ j, I_ e)
130 /* Now raise to the exponent */
131 if ( r != 0.0 ) /* Lennart suggests this avoids a bug in MIPS's ldexp */
137 /* This only supports IEEE floating point */
140 __decodeDouble_2Int (I_ *man_sign, W_ *man_high, W_ *man_low, I_ *exp, StgDouble dbl)
142 /* Do some bit fiddling on IEEE */
143 unsigned int low, high; /* assuming 32 bit ints */
145 union { double d; unsigned int i[2]; } u; /* assuming 32 bit ints, 64 bit double */
147 ASSERT(sizeof(unsigned int ) == 4 );
148 ASSERT(sizeof(dbl ) == 8 );
149 ASSERT(sizeof(dbl ) == SIZEOF_DOUBLE);
151 u.d = dbl; /* grab chunks of the double */
155 if (low == 0 && (high & ~DMSBIT) == 0) {
160 iexp = ((high >> 20) & 0x7ff) + MY_DMINEXP;
164 if (iexp != MY_DMINEXP) /* don't add hidden bit to denorms */
168 /* A denorm, normalize the mantissa */
169 while (! (high & DHIGHBIT)) {
180 *man_sign = (sign < 0) ? -1 : 1;
184 /* Convenient union types for checking the layout of IEEE 754 types -
185 based on defs in GNU libc <ieee754.h>
189 __decodeFloat_Int (I_ *man, I_ *exp, StgFloat flt)
191 /* Do some bit fiddling on IEEE */
192 int high, sign; /* assuming 32 bit ints */
193 union { float f; int i; } u; /* assuming 32 bit float and int */
195 ASSERT(sizeof(int ) == 4 );
196 ASSERT(sizeof(flt ) == 4 );
197 ASSERT(sizeof(flt ) == SIZEOF_FLOAT );
199 u.f = flt; /* grab the float */
202 if ((high & ~FMSBIT) == 0) {
206 *exp = ((high >> 23) & 0xff) + MY_FMINEXP;
210 if (*exp != MY_FMINEXP) /* don't add hidden bit to denorms */
214 /* A denorm, normalize the mantissa */
215 while (! (high & FHIGHBIT)) {