Fix for Trac #15611: Scope errors lie about what modules are imported.
[ghc.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 "StgPrimFloat.h"
14
15 #include <math.h>
16 #include <float.h>
17 #include <limits.h>
18
19 #define IEEE_FLOATING_POINT 1
20
21 #if FLT_RADIX != 2
22 # error FLT_RADIX != 2 not supported
23 #endif
24
25 /*
26 * Encoding and decoding Doubles. Code based on the HBC code
27 * (lib/fltcode.c).
28 */
29
30 #if IEEE_FLOATING_POINT
31 #define MY_DMINEXP ((DBL_MIN_EXP) - (DBL_MANT_DIG) - 1)
32 /* DMINEXP is defined in values.h on Linux (for example) */
33 #define DHIGHBIT 0x00100000
34 #define DMSBIT 0x80000000
35
36 #define MY_FMINEXP ((FLT_MIN_EXP) - (FLT_MANT_DIG) - 1)
37 #define FHIGHBIT 0x00800000
38 #define FMSBIT 0x80000000
39 #endif
40
41 #if defined(WORDS_BIGENDIAN) || defined(FLOAT_WORDS_BIGENDIAN)
42 #define L 1
43 #define H 0
44 #else
45 #define L 0
46 #define H 1
47 #endif
48
49 #define __abs(a) (( (a) >= 0 ) ? (a) : (-(a)))
50
51 /** Trac #15271: Some large ratios are converted into double incorrectly.
52 * This occurs when StgInt has 64 bits and C int has 32 bits, where wrapping
53 * occurs and an incorrect signed value is passed into ldexp */
54 STATIC_INLINE int
55 truncExponent(I_ e)
56 {
57 #if INT_MAX < STG_INT_MAX
58 if (RTS_UNLIKELY(e > INT_MAX))
59 e = INT_MAX;
60 #endif
61 #if INT_MIN > STG_INT_MIN
62 if (RTS_UNLIKELY(e < INT_MIN))
63 e = INT_MIN;
64 #endif
65 return e;
66 }
67
68 /* Special version for words */
69 StgDouble
70 __word_encodeDouble (W_ j, I_ e)
71 {
72 StgDouble r;
73
74 r = (StgDouble)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, truncExponent(e));
79
80 return r;
81 }
82
83 /* Special version for small Integers */
84 StgDouble
85 __int_encodeDouble (I_ j, I_ e)
86 {
87 StgDouble r;
88
89 r = (StgDouble)__abs(j);
90
91 /* Now raise to the exponent */
92 if ( r != 0.0 ) /* Lennart suggests this avoids a bug in MIPS's ldexp */
93 r = ldexp(r, truncExponent(e));
94
95 /* sign is encoded in the size */
96 if (j < 0)
97 r = -r;
98
99 return r;
100 }
101
102 /* Special version for small Integers */
103 StgFloat
104 __int_encodeFloat (I_ j, I_ e)
105 {
106 StgFloat r;
107
108 r = (StgFloat)__abs(j);
109
110 /* Now raise to the exponent */
111 if ( r != 0.0 ) /* Lennart suggests this avoids a bug in MIPS's ldexp */
112 r = ldexp(r, truncExponent(e));
113
114 /* sign is encoded in the size */
115 if (j < 0)
116 r = -r;
117
118 return r;
119 }
120
121 /* Special version for small positive Integers */
122 StgFloat
123 __word_encodeFloat (W_ j, I_ e)
124 {
125 StgFloat r;
126
127 r = (StgFloat)j;
128
129 /* Now raise to the exponent */
130 if ( r != 0.0 ) /* Lennart suggests this avoids a bug in MIPS's ldexp */
131 r = ldexp(r, truncExponent(e));
132
133 return r;
134 }
135
136 /* This only supports IEEE floating point */
137
138 void
139 __decodeDouble_2Int (I_ *man_sign, W_ *man_high, W_ *man_low, I_ *exp, StgDouble dbl)
140 {
141 /* Do some bit fiddling on IEEE */
142 unsigned int low, high; /* assuming 32 bit ints */
143 int sign, iexp;
144 union { double d; unsigned int i[2]; } u; /* assuming 32 bit ints, 64 bit double */
145
146 ASSERT(sizeof(unsigned int ) == 4 );
147 ASSERT(sizeof(dbl ) == 8 );
148 ASSERT(sizeof(dbl ) == SIZEOF_DOUBLE);
149
150 u.d = dbl; /* grab chunks of the double */
151 low = u.i[L];
152 high = u.i[H];
153
154 if (low == 0 && (high & ~DMSBIT) == 0) {
155 *man_low = 0;
156 *man_high = 0;
157 *exp = 0L;
158 } else {
159 iexp = ((high >> 20) & 0x7ff) + MY_DMINEXP;
160 sign = high;
161
162 high &= DHIGHBIT-1;
163 if (iexp != MY_DMINEXP) /* don't add hidden bit to denorms */
164 high |= DHIGHBIT;
165 else {
166 iexp++;
167 /* A denorm, normalize the mantissa */
168 while (! (high & DHIGHBIT)) {
169 high <<= 1;
170 if (low & DMSBIT)
171 high++;
172 low <<= 1;
173 iexp--;
174 }
175 }
176 *exp = (I_) iexp;
177 *man_low = low;
178 *man_high = high;
179 *man_sign = (sign < 0) ? -1 : 1;
180 }
181 }
182
183 /* This is expected to replace uses of __decodeDouble_2Int() in the long run */
184 StgInt
185 __decodeDouble_Int64 (StgInt64 *const mantissa, const StgDouble dbl)
186 {
187 #if 0
188 // We can't use this yet as-is, see ticket #9810
189 if (dbl) {
190 int exp = 0;
191 *mantissa = (StgInt64)scalbn(frexp(dbl, &exp), DBL_MANT_DIG);
192 return exp-DBL_MANT_DIG;
193 } else {
194 *mantissa = 0;
195 return 0;
196 }
197 #else
198 I_ man_sign = 0;
199 W_ man_high = 0, man_low = 0;
200 I_ exp = 0;
201
202 __decodeDouble_2Int (&man_sign, &man_high, &man_low, &exp, dbl);
203 ASSIGN_Int64((W_*)mantissa, ((((StgInt64)man_high << 32)
204 | (StgInt64)man_low)
205 * (StgInt64)man_sign));
206 return exp;
207 #endif
208 }
209
210 /* Convenient union types for checking the layout of IEEE 754 types -
211 based on defs in GNU libc <ieee754.h>
212 */
213
214 void
215 __decodeFloat_Int (I_ *man, I_ *exp, StgFloat flt)
216 {
217 /* Do some bit fiddling on IEEE */
218 int high, sign; /* assuming 32 bit ints */
219 union { float f; int i; } u; /* assuming 32 bit float and int */
220
221 ASSERT(sizeof(int ) == 4 );
222 ASSERT(sizeof(flt ) == 4 );
223 ASSERT(sizeof(flt ) == SIZEOF_FLOAT );
224
225 u.f = flt; /* grab the float */
226 high = u.i;
227
228 if ((high & ~FMSBIT) == 0) {
229 *man = 0;
230 *exp = 0;
231 } else {
232 *exp = ((high >> 23) & 0xff) + MY_FMINEXP;
233 sign = high;
234
235 high &= FHIGHBIT-1;
236 if (*exp != MY_FMINEXP) /* don't add hidden bit to denorms */
237 high |= FHIGHBIT;
238 else {
239 (*exp)++;
240 /* A denorm, normalize the mantissa */
241 while (! (high & FHIGHBIT)) {
242 high <<= 1;
243 (*exp)--;
244 }
245 }
246 *man = high;
247 if (sign < 0)
248 *man = - *man;
249 }
250 }
251