Debian Codegraph @

terps/glulxe/float.c

raw symbols SCIP
1 /* float.c: Glulxe code for floating-point operations
2 Designed by Andrew Plotkin <[email protected]>
3 http://eblong.com/zarf/glulx/index.html
4 */
5
6 #include "glk.h"
7 #include "glulxe.h"
8
9 #ifdef FLOAT_SUPPORT
10
11 #include <math.h>
12
13 /* This entire file is compiled out if the FLOAT_SUPPORT option is off.
14 (Because we probably can't define a gfloat32 in that case.) */
15
16 #ifndef FLOAT_NOT_NATIVE
17
18 int init_float()
19 {
20 /* Check and make sure the native float format is really
21 IEEE-754 single-precision. */
22
23 if (sizeof(gfloat32) != 4) {
24 fatal_error("gfloat32 is not 32 bits.");
25 return FALSE;
26 }
27 if (encode_float((gfloat32)(-1)) != 0xBF800000) {
28 fatal_error("The gfloat32 format of -1 did not match.");
29 return FALSE;
30 }
31 return TRUE;
32 }
33
34 /* Encode and decode floats by reinterpret-casting. */
35
36 glui32 encode_float(gfloat32 val)
37 {
38 glui32 res;
39 *(gfloat32 *)(&res) = val;
40 return res;
41 }
42
43 gfloat32 decode_float(glui32 val)
44 {
45 gfloat32 res;
46 *(glui32 *)(&res) = val;
47 return res;
48 }
49
50 #else /* FLOAT_NOT_NATIVE */
51
52 int init_float()
53 {
54 return TRUE;
55 }
56
57 /* Encode and decode floats by a lot of annoying bit manipulation.
58 The following functions are adapted from code in Python
59 (Objects/floatobject.c). */
60
61 glui32 encode_float(gfloat32 val)
62 {
63 gfloat32 absval;
64 glui32 sign;
65 int expo;
66 gfloat32 mant;
67 glui32 fbits;
68
69 if (signbit(val)) {
70 sign = 0x80000000;
71 absval = -val;
72 }
73 else {
74 sign = 0x0;
75 absval = val;
76 }
77
78 if (isinf(val)) {
79 return sign | 0x7f800000; /* infinity */
80 }
81
82 if (isnan(val)) {
83 return sign | 0x7fc00000;
84 }
85
86 mant = frexpf(absval, &expo);
87
88 /* Normalize mantissa to be in the range [1.0, 2.0) */
89 if (0.5 <= mant && mant < 1.0) {
90 mant *= 2.0;
91 expo--;
92 }
93 else if (mant == 0.0) {
94 expo = 0;
95 }
96 else {
97 return sign | 0x7f800000; /* infinity */
98 }
99
100 if (expo >= 128) {
101 return sign | 0x7f800000; /* infinity */
102 }
103 else if (expo < -126) {
104 /* Denormalized (very small) number */
105 mant = ldexpf(mant, 126 + expo);
106 expo = 0;
107 }
108 else if (!(expo == 0 && mant == 0.0)) {
109 expo += 127;
110 mant -= 1.0; /* Get rid of leading 1 */
111 }
112
113 mant *= 8388608.0; /* 2^23 */
114 fbits = (glui32)(mant + 0.5); /* round mant to nearest int */
115 if (fbits >> 23) {
116 /* The carry propagated out of a string of 23 1 bits. */
117 fbits = 0;
118 expo++;
119 if (expo >= 255) {
120 return sign | 0x7f800000; /* infinity */
121 }
122 }
123
124 return (sign) | ((glui32)(expo << 23)) | (fbits);
125 }
126
127 gfloat32 decode_float(glui32 val)
128 {
129 int sign;
130 int expo;
131 glui32 mant;
132 gfloat32 res;
133
134 /* First byte */
135 sign = ((val & 0x80000000) != 0);
136 expo = (val >> 23) & 0xFF;
137 mant = val & 0x7FFFFF;
138
139 if (expo == 255) {
140 if (mant == 0) {
141 /* Infinity */
142 return (sign ? (-INFINITY) : (INFINITY));
143 }
144 else {
145 /* Not a number */
146 return (sign ? (-NAN) : (NAN));
147 }
148 }
149
150 res = (gfloat32)mant / 8388608.0;
151
152 if (expo == 0) {
153 expo = -126;
154 }
155 else {
156 res += 1.0;
157 expo -= 127;
158 }
159 res = ldexpf(res, expo);
160
161 return (sign ? (-res) : (res));
162 }
163
164 #endif /* FLOAT_NOT_NATIVE */
165
166 #ifdef DOUBLE_SUPPORT /* Inside FLOAT_SUPPORT! */
167
168 /* We don't try to implement a reinterpret-cast version of these functions.
169 Just seems too risky, what with endianness and who knows what else. */
170
171 void encode_double(gfloat64 val, glui32 *reshi, glui32 *reslo)
172 {
173 gfloat64 absval;
174 glui32 sign;
175 int expo;
176 gfloat64 mant;
177 glui32 fhi, flo;
178
179 if (signbit(val)) {
180 sign = 0x80000000;
181 absval = -val;
182 }
183 else {
184 sign = 0x0;
185 absval = val;
186 }
187
188 if (isinf(val)) {
189 goto Infinity;
190 }
191
192 if (isnan(val)) {
193 goto NotANumber;
194 }
195
196 mant = frexp(absval, &expo);
197
198 /* Normalize mantissa to be in the range [1.0, 2.0) */
199 if (0.5 <= mant && mant < 1.0) {
200 mant *= 2.0;
201 expo--;
202 }
203 else if (mant == 0.0) {
204 expo = 0;
205 }
206 else {
207 goto Infinity;
208 }
209
210 if (expo >= 1024) {
211 goto Infinity;
212 }
213 else if (expo < -1022) {
214 /* Denormalized (very small) number */
215 mant = ldexp(mant, 1022 + expo);
216 expo = 0;
217 }
218 else if (!(expo == 0 && mant == 0.0)) {
219 expo += 1023;
220 mant -= 1.0; /* Get rid of leading 1 */
221 }
222
223 /* fhi receives the high 28 bits; flo the low 24 bits (total 52 bits) */
224 mant *= 268435456.0; /* 2^28 */
225 fhi = (glui32)mant; /* Truncate */
226 mant -= (double)fhi;
227 mant *= 16777216.0; /* 2^24 */
228 flo = (glui32)(mant+0.5); /* Round */
229
230 if (flo >> 24) {
231 /* The carry propagated out of a string of 24 1 bits. */
232 flo = 0;
233 fhi++;
234 if (fhi >> 28) {
235 /* And it also propagated out of the next 28 bits. */
236 fhi = 0;
237 expo++;
238 if (expo >= 255) {
239 goto Infinity;
240 }
241 }
242 }
243
244 *reshi = (sign) | ((glui32)(expo << 20)) | ((glui32)(fhi >> 8));
245 *reslo = (glui32)((fhi & 0xFF) << 24) | (glui32)(flo);
246 return;
247
248 Infinity:
249 *reshi = sign | 0x7FF00000;
250 *reslo = 0x00000000;
251 return;
252
253 NotANumber:
254 *reshi = sign | 0x7FF80000;
255 *reslo = 0x00000001;
256 return;
257 }
258
259 gfloat64 decode_double(glui32 valhi, glui32 vallo)
260 {
261 int sign;
262 int expo;
263 glui32 manthi, mantlo;
264 gfloat64 res;
265
266 /* First byte */
267 sign = ((valhi & 0x80000000) != 0);
268 expo = (valhi >> 20) & 0x7FF;
269 manthi = valhi & 0xFFFFF;
270 mantlo = vallo;
271
272 if (expo == 2047) {
273 if (manthi == 0 && mantlo == 0) {
274 /* Infinity */
275 return (sign ? (-INFINITY) : (INFINITY));
276 }
277 else {
278 /* Not a number */
279 return (sign ? (-NAN) : (NAN));
280 }
281 }
282
283 res = (gfloat64)mantlo / 4503599627370496.0 + (gfloat64)manthi / 1048576.0;
284
285 if (expo == 0) {
286 expo = -1022;
287 }
288 else {
289 res += 1.0;
290 expo -= 1023;
291 }
292 res = ldexp(res, expo);
293
294 return (sign ? (-res) : (res));
295 }
296
297 #endif /* DOUBLE_SUPPORT */
298
299 #endif /* FLOAT_SUPPORT */
300