Fold template-haskell.git into ghc.git (re #8545)
[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
18 #define IEEE_FLOATING_POINT 1
19
20 /*
21 * Encoding and decoding Doubles. Code based on the HBC code
22 * (lib/fltcode.c).
23 */
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 #if defined(WORDS_BIGENDIAN) || defined(FLOAT_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 /* Special version for words */
47 StgDouble
48 __word_encodeDouble (W_ j, I_ e)
49 {
50 StgDouble r;
51
52 r = (StgDouble)j;
53
54 /* Now raise to the exponent */
55 if ( r != 0.0 ) /* Lennart suggests this avoids a bug in MIPS's ldexp */
56 r = ldexp(r, e);
57
58 return r;
59 }
60
61 /* Special version for small Integers */
62 StgDouble
63 __int_encodeDouble (I_ j, I_ e)
64 {
65 StgDouble r;
66
67 r = (StgDouble)__abs(j);
68
69 /* Now raise to the exponent */
70 if ( r != 0.0 ) /* Lennart suggests this avoids a bug in MIPS's ldexp */
71 r = ldexp(r, e);
72
73 /* sign is encoded in the size */
74 if (j < 0)
75 r = -r;
76
77 return r;
78 }
79
80 /* Special version for small Integers */
81 StgFloat
82 __int_encodeFloat (I_ j, I_ e)
83 {
84 StgFloat r;
85
86 r = (StgFloat)__abs(j);
87
88 /* Now raise to the exponent */
89 if ( r != 0.0 ) /* Lennart suggests this avoids a bug in MIPS's ldexp */
90 r = ldexp(r, e);
91
92 /* sign is encoded in the size */
93 if (j < 0)
94 r = -r;
95
96 return r;
97 }
98
99 /* Special version for small positive Integers */
100 StgFloat
101 __word_encodeFloat (W_ j, I_ e)
102 {
103 StgFloat r;
104
105 r = (StgFloat)j;
106
107 /* Now raise to the exponent */
108 if ( r != 0.0 ) /* Lennart suggests this avoids a bug in MIPS's ldexp */
109 r = ldexp(r, e);
110
111 return r;
112 }
113
114 /* This only supports IEEE floating point */
115
116 void
117 __decodeDouble_2Int (I_ *man_sign, W_ *man_high, W_ *man_low, I_ *exp, StgDouble dbl)
118 {
119 /* Do some bit fiddling on IEEE */
120 unsigned int low, high; /* assuming 32 bit ints */
121 int sign, iexp;
122 union { double d; unsigned int i[2]; } u; /* assuming 32 bit ints, 64 bit double */
123
124 ASSERT(sizeof(unsigned int ) == 4 );
125 ASSERT(sizeof(dbl ) == 8 );
126 ASSERT(sizeof(dbl ) == SIZEOF_DOUBLE);
127
128 u.d = dbl; /* grab chunks of the double */
129 low = u.i[L];
130 high = u.i[H];
131
132 if (low == 0 && (high & ~DMSBIT) == 0) {
133 *man_low = 0;
134 *man_high = 0;
135 *exp = 0L;
136 } else {
137 iexp = ((high >> 20) & 0x7ff) + MY_DMINEXP;
138 sign = high;
139
140 high &= DHIGHBIT-1;
141 if (iexp != MY_DMINEXP) /* don't add hidden bit to denorms */
142 high |= DHIGHBIT;
143 else {
144 iexp++;
145 /* A denorm, normalize the mantissa */
146 while (! (high & DHIGHBIT)) {
147 high <<= 1;
148 if (low & DMSBIT)
149 high++;
150 low <<= 1;
151 iexp--;
152 }
153 }
154 *exp = (I_) iexp;
155 *man_low = low;
156 *man_high = high;
157 *man_sign = (sign < 0) ? -1 : 1;
158 }
159 }
160
161 /* Convenient union types for checking the layout of IEEE 754 types -
162 based on defs in GNU libc <ieee754.h>
163 */
164
165 void
166 __decodeFloat_Int (I_ *man, I_ *exp, StgFloat flt)
167 {
168 /* Do some bit fiddling on IEEE */
169 int high, sign; /* assuming 32 bit ints */
170 union { float f; int i; } u; /* assuming 32 bit float and int */
171
172 ASSERT(sizeof(int ) == 4 );
173 ASSERT(sizeof(flt ) == 4 );
174 ASSERT(sizeof(flt ) == SIZEOF_FLOAT );
175
176 u.f = flt; /* grab the float */
177 high = u.i;
178
179 if ((high & ~FMSBIT) == 0) {
180 *man = 0;
181 *exp = 0;
182 } else {
183 *exp = ((high >> 23) & 0xff) + MY_FMINEXP;
184 sign = high;
185
186 high &= FHIGHBIT-1;
187 if (*exp != MY_FMINEXP) /* don't add hidden bit to denorms */
188 high |= FHIGHBIT;
189 else {
190 (*exp)++;
191 /* A denorm, normalize the mantissa */
192 while (! (high & FHIGHBIT)) {
193 high <<= 1;
194 (*exp)--;
195 }
196 }
197 *man = high;
198 if (sign < 0)
199 *man = - *man;
200 }
201 }
202