Updated interpreters
[projects/chimara/chimara.git] / interpreters / glulxe / float.c
1 /* float.c: Glulxe code for floating-point operations
2     Designed by Andrew Plotkin <erkyrath@eblong.com>
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 #endif /* FLOAT_SUPPORT */