1 | /* Native implementation of soft float functions */
|
---|
2 | #include <math.h>
|
---|
3 |
|
---|
4 | #if (defined(_BSD) && !defined(__APPLE__)) || defined(HOST_SOLARIS)
|
---|
5 | #include <ieeefp.h>
|
---|
6 | #define fabsf(f) ((float)fabs(f))
|
---|
7 | #else
|
---|
8 | #include <fenv.h>
|
---|
9 | #endif
|
---|
10 |
|
---|
11 | /*
|
---|
12 | * Define some C99-7.12.3 classification macros and
|
---|
13 | * some C99-.12.4 for Solaris systems OS less than 10,
|
---|
14 | * or Solaris 10 systems running GCC 3.x or less.
|
---|
15 | * Solaris 10 with GCC4 does not need these macros as they
|
---|
16 | * are defined in <iso/math_c99.h> with a compiler directive
|
---|
17 | */
|
---|
18 | #if defined(HOST_SOLARIS) && (( HOST_SOLARIS <= 9 ) || ( ( HOST_SOLARIS >= 10 ) && ( __GNUC__ <= 4) ))
|
---|
19 | /*
|
---|
20 | * C99 7.12.3 classification macros
|
---|
21 | * and
|
---|
22 | * C99 7.12.14 comparison macros
|
---|
23 | *
|
---|
24 | * ... do not work on Solaris 10 using GNU CC 3.4.x.
|
---|
25 | * Try to workaround the missing / broken C99 math macros.
|
---|
26 | */
|
---|
27 |
|
---|
28 | #define isnormal(x) (fpclass(x) >= FP_NZERO)
|
---|
29 | #define isgreater(x, y) ((!unordered(x, y)) && ((x) > (y)))
|
---|
30 | #define isgreaterequal(x, y) ((!unordered(x, y)) && ((x) >= (y)))
|
---|
31 | #define isless(x, y) ((!unordered(x, y)) && ((x) < (y)))
|
---|
32 | #define islessequal(x, y) ((!unordered(x, y)) && ((x) <= (y)))
|
---|
33 | #define isunordered(x,y) unordered(x, y)
|
---|
34 | #define isinf(x) ((fpclass(x) == FP_NINF) || (fpclass(x) == FP_PINF))
|
---|
35 |
|
---|
36 | #endif
|
---|
37 |
|
---|
38 | typedef float float32;
|
---|
39 | typedef double float64;
|
---|
40 | #ifdef FLOATX80
|
---|
41 | typedef long double floatx80;
|
---|
42 | #endif
|
---|
43 |
|
---|
44 | typedef union {
|
---|
45 | float32 f;
|
---|
46 | uint32_t i;
|
---|
47 | } float32u;
|
---|
48 | typedef union {
|
---|
49 | float64 f;
|
---|
50 | uint64_t i;
|
---|
51 | } float64u;
|
---|
52 | #ifdef FLOATX80
|
---|
53 | typedef union {
|
---|
54 | floatx80 f;
|
---|
55 | struct {
|
---|
56 | uint64_t low;
|
---|
57 | uint16_t high;
|
---|
58 | } i;
|
---|
59 | } floatx80u;
|
---|
60 | #endif
|
---|
61 |
|
---|
62 | /*----------------------------------------------------------------------------
|
---|
63 | | Software IEC/IEEE floating-point rounding mode.
|
---|
64 | *----------------------------------------------------------------------------*/
|
---|
65 | #if (defined(_BSD) && !defined(__APPLE__)) || defined(HOST_SOLARIS)
|
---|
66 | enum {
|
---|
67 | float_round_nearest_even = FP_RN,
|
---|
68 | float_round_down = FP_RM,
|
---|
69 | float_round_up = FP_RP,
|
---|
70 | float_round_to_zero = FP_RZ
|
---|
71 | };
|
---|
72 | #elif defined(__arm__)
|
---|
73 | enum {
|
---|
74 | float_round_nearest_even = 0,
|
---|
75 | float_round_down = 1,
|
---|
76 | float_round_up = 2,
|
---|
77 | float_round_to_zero = 3
|
---|
78 | };
|
---|
79 | #else
|
---|
80 | enum {
|
---|
81 | float_round_nearest_even = FE_TONEAREST,
|
---|
82 | float_round_down = FE_DOWNWARD,
|
---|
83 | float_round_up = FE_UPWARD,
|
---|
84 | float_round_to_zero = FE_TOWARDZERO
|
---|
85 | };
|
---|
86 | #endif
|
---|
87 |
|
---|
88 | typedef struct float_status {
|
---|
89 | signed char float_rounding_mode;
|
---|
90 | #ifdef FLOATX80
|
---|
91 | signed char floatx80_rounding_precision;
|
---|
92 | #endif
|
---|
93 | } float_status;
|
---|
94 |
|
---|
95 | void set_float_rounding_mode(int val STATUS_PARAM);
|
---|
96 | #ifdef FLOATX80
|
---|
97 | void set_floatx80_rounding_precision(int val STATUS_PARAM);
|
---|
98 | #endif
|
---|
99 |
|
---|
100 | /*----------------------------------------------------------------------------
|
---|
101 | | Software IEC/IEEE integer-to-floating-point conversion routines.
|
---|
102 | *----------------------------------------------------------------------------*/
|
---|
103 | float32 int32_to_float32( int STATUS_PARAM);
|
---|
104 | float64 int32_to_float64( int STATUS_PARAM);
|
---|
105 | #ifdef FLOATX80
|
---|
106 | floatx80 int32_to_floatx80( int STATUS_PARAM);
|
---|
107 | #endif
|
---|
108 | #ifdef FLOAT128
|
---|
109 | float128 int32_to_float128( int STATUS_PARAM);
|
---|
110 | #endif
|
---|
111 | float32 int64_to_float32( int64_t STATUS_PARAM);
|
---|
112 | float64 int64_to_float64( int64_t STATUS_PARAM);
|
---|
113 | #ifdef FLOATX80
|
---|
114 | floatx80 int64_to_floatx80( int64_t STATUS_PARAM);
|
---|
115 | #endif
|
---|
116 | #ifdef FLOAT128
|
---|
117 | float128 int64_to_float128( int64_t STATUS_PARAM);
|
---|
118 | #endif
|
---|
119 |
|
---|
120 | /*----------------------------------------------------------------------------
|
---|
121 | | Software IEC/IEEE single-precision conversion routines.
|
---|
122 | *----------------------------------------------------------------------------*/
|
---|
123 | int float32_to_int32( float32 STATUS_PARAM);
|
---|
124 | int float32_to_int32_round_to_zero( float32 STATUS_PARAM);
|
---|
125 | int64_t float32_to_int64( float32 STATUS_PARAM);
|
---|
126 | int64_t float32_to_int64_round_to_zero( float32 STATUS_PARAM);
|
---|
127 | float64 float32_to_float64( float32 STATUS_PARAM);
|
---|
128 | #ifdef FLOATX80
|
---|
129 | floatx80 float32_to_floatx80( float32 STATUS_PARAM);
|
---|
130 | #endif
|
---|
131 | #ifdef FLOAT128
|
---|
132 | float128 float32_to_float128( float32 STATUS_PARAM);
|
---|
133 | #endif
|
---|
134 |
|
---|
135 | /*----------------------------------------------------------------------------
|
---|
136 | | Software IEC/IEEE single-precision operations.
|
---|
137 | *----------------------------------------------------------------------------*/
|
---|
138 | float32 float32_round_to_int( float32 STATUS_PARAM);
|
---|
139 | INLINE float32 float32_add( float32 a, float32 b STATUS_PARAM)
|
---|
140 | {
|
---|
141 | return a + b;
|
---|
142 | }
|
---|
143 | INLINE float32 float32_sub( float32 a, float32 b STATUS_PARAM)
|
---|
144 | {
|
---|
145 | return a - b;
|
---|
146 | }
|
---|
147 | INLINE float32 float32_mul( float32 a, float32 b STATUS_PARAM)
|
---|
148 | {
|
---|
149 | return a * b;
|
---|
150 | }
|
---|
151 | INLINE float32 float32_div( float32 a, float32 b STATUS_PARAM)
|
---|
152 | {
|
---|
153 | return a / b;
|
---|
154 | }
|
---|
155 | float32 float32_rem( float32, float32 STATUS_PARAM);
|
---|
156 | float32 float32_sqrt( float32 STATUS_PARAM);
|
---|
157 | INLINE int float32_eq( float32 a, float32 b STATUS_PARAM)
|
---|
158 | {
|
---|
159 | return a == b;
|
---|
160 | }
|
---|
161 | INLINE int float32_le( float32 a, float32 b STATUS_PARAM)
|
---|
162 | {
|
---|
163 | return a <= b;
|
---|
164 | }
|
---|
165 | INLINE int float32_lt( float32 a, float32 b STATUS_PARAM)
|
---|
166 | {
|
---|
167 | return a < b;
|
---|
168 | }
|
---|
169 | INLINE int float32_eq_signaling( float32 a, float32 b STATUS_PARAM)
|
---|
170 | {
|
---|
171 | return a <= b && a >= b;
|
---|
172 | }
|
---|
173 | INLINE int float32_le_quiet( float32 a, float32 b STATUS_PARAM)
|
---|
174 | {
|
---|
175 | return islessequal(a, b);
|
---|
176 | }
|
---|
177 | INLINE int float32_lt_quiet( float32 a, float32 b STATUS_PARAM)
|
---|
178 | {
|
---|
179 | return isless(a, b);
|
---|
180 | }
|
---|
181 | INLINE int float32_unordered( float32 a, float32 b STATUS_PARAM)
|
---|
182 | {
|
---|
183 | return isunordered(a, b);
|
---|
184 |
|
---|
185 | }
|
---|
186 | int float32_compare( float32, float32 STATUS_PARAM );
|
---|
187 | int float32_compare_quiet( float32, float32 STATUS_PARAM );
|
---|
188 | int float32_is_signaling_nan( float32 );
|
---|
189 |
|
---|
190 | INLINE float32 float32_abs(float32 a)
|
---|
191 | {
|
---|
192 | return fabsf(a);
|
---|
193 | }
|
---|
194 |
|
---|
195 | INLINE float32 float32_chs(float32 a)
|
---|
196 | {
|
---|
197 | return -a;
|
---|
198 | }
|
---|
199 |
|
---|
200 | /*----------------------------------------------------------------------------
|
---|
201 | | Software IEC/IEEE double-precision conversion routines.
|
---|
202 | *----------------------------------------------------------------------------*/
|
---|
203 | int float64_to_int32( float64 STATUS_PARAM );
|
---|
204 | int float64_to_int32_round_to_zero( float64 STATUS_PARAM );
|
---|
205 | int64_t float64_to_int64( float64 STATUS_PARAM );
|
---|
206 | int64_t float64_to_int64_round_to_zero( float64 STATUS_PARAM );
|
---|
207 | float32 float64_to_float32( float64 STATUS_PARAM );
|
---|
208 | #ifdef FLOATX80
|
---|
209 | floatx80 float64_to_floatx80( float64 STATUS_PARAM );
|
---|
210 | #endif
|
---|
211 | #ifdef FLOAT128
|
---|
212 | float128 float64_to_float128( float64 STATUS_PARAM );
|
---|
213 | #endif
|
---|
214 |
|
---|
215 | /*----------------------------------------------------------------------------
|
---|
216 | | Software IEC/IEEE double-precision operations.
|
---|
217 | *----------------------------------------------------------------------------*/
|
---|
218 | float64 float64_round_to_int( float64 STATUS_PARAM );
|
---|
219 | float64 float64_trunc_to_int( float64 STATUS_PARAM );
|
---|
220 | INLINE float64 float64_add( float64 a, float64 b STATUS_PARAM)
|
---|
221 | {
|
---|
222 | return a + b;
|
---|
223 | }
|
---|
224 | INLINE float64 float64_sub( float64 a, float64 b STATUS_PARAM)
|
---|
225 | {
|
---|
226 | return a - b;
|
---|
227 | }
|
---|
228 | INLINE float64 float64_mul( float64 a, float64 b STATUS_PARAM)
|
---|
229 | {
|
---|
230 | return a * b;
|
---|
231 | }
|
---|
232 | INLINE float64 float64_div( float64 a, float64 b STATUS_PARAM)
|
---|
233 | {
|
---|
234 | return a / b;
|
---|
235 | }
|
---|
236 | float64 float64_rem( float64, float64 STATUS_PARAM );
|
---|
237 | float64 float64_sqrt( float64 STATUS_PARAM );
|
---|
238 | INLINE int float64_eq( float64 a, float64 b STATUS_PARAM)
|
---|
239 | {
|
---|
240 | return a == b;
|
---|
241 | }
|
---|
242 | INLINE int float64_le( float64 a, float64 b STATUS_PARAM)
|
---|
243 | {
|
---|
244 | return a <= b;
|
---|
245 | }
|
---|
246 | INLINE int float64_lt( float64 a, float64 b STATUS_PARAM)
|
---|
247 | {
|
---|
248 | return a < b;
|
---|
249 | }
|
---|
250 | INLINE int float64_eq_signaling( float64 a, float64 b STATUS_PARAM)
|
---|
251 | {
|
---|
252 | return a <= b && a >= b;
|
---|
253 | }
|
---|
254 | INLINE int float64_le_quiet( float64 a, float64 b STATUS_PARAM)
|
---|
255 | {
|
---|
256 | return islessequal(a, b);
|
---|
257 | }
|
---|
258 | INLINE int float64_lt_quiet( float64 a, float64 b STATUS_PARAM)
|
---|
259 | {
|
---|
260 | return isless(a, b);
|
---|
261 |
|
---|
262 | }
|
---|
263 | INLINE int float64_unordered( float64 a, float64 b STATUS_PARAM)
|
---|
264 | {
|
---|
265 | return isunordered(a, b);
|
---|
266 |
|
---|
267 | }
|
---|
268 | int float64_compare( float64, float64 STATUS_PARAM );
|
---|
269 | int float64_compare_quiet( float64, float64 STATUS_PARAM );
|
---|
270 | int float64_is_signaling_nan( float64 );
|
---|
271 | int float64_is_nan( float64 );
|
---|
272 |
|
---|
273 | INLINE float64 float64_abs(float64 a)
|
---|
274 | {
|
---|
275 | return fabs(a);
|
---|
276 | }
|
---|
277 |
|
---|
278 | INLINE float64 float64_chs(float64 a)
|
---|
279 | {
|
---|
280 | return -a;
|
---|
281 | }
|
---|
282 |
|
---|
283 | #ifdef FLOATX80
|
---|
284 |
|
---|
285 | /*----------------------------------------------------------------------------
|
---|
286 | | Software IEC/IEEE extended double-precision conversion routines.
|
---|
287 | *----------------------------------------------------------------------------*/
|
---|
288 | int floatx80_to_int32( floatx80 STATUS_PARAM );
|
---|
289 | int floatx80_to_int32_round_to_zero( floatx80 STATUS_PARAM );
|
---|
290 | int64_t floatx80_to_int64( floatx80 STATUS_PARAM);
|
---|
291 | int64_t floatx80_to_int64_round_to_zero( floatx80 STATUS_PARAM);
|
---|
292 | float32 floatx80_to_float32( floatx80 STATUS_PARAM );
|
---|
293 | float64 floatx80_to_float64( floatx80 STATUS_PARAM );
|
---|
294 | #ifdef FLOAT128
|
---|
295 | float128 floatx80_to_float128( floatx80 STATUS_PARAM );
|
---|
296 | #endif
|
---|
297 |
|
---|
298 | /*----------------------------------------------------------------------------
|
---|
299 | | Software IEC/IEEE extended double-precision operations.
|
---|
300 | *----------------------------------------------------------------------------*/
|
---|
301 | floatx80 floatx80_round_to_int( floatx80 STATUS_PARAM );
|
---|
302 | INLINE floatx80 floatx80_add( floatx80 a, floatx80 b STATUS_PARAM)
|
---|
303 | {
|
---|
304 | return a + b;
|
---|
305 | }
|
---|
306 | INLINE floatx80 floatx80_sub( floatx80 a, floatx80 b STATUS_PARAM)
|
---|
307 | {
|
---|
308 | return a - b;
|
---|
309 | }
|
---|
310 | INLINE floatx80 floatx80_mul( floatx80 a, floatx80 b STATUS_PARAM)
|
---|
311 | {
|
---|
312 | return a * b;
|
---|
313 | }
|
---|
314 | INLINE floatx80 floatx80_div( floatx80 a, floatx80 b STATUS_PARAM)
|
---|
315 | {
|
---|
316 | return a / b;
|
---|
317 | }
|
---|
318 | floatx80 floatx80_rem( floatx80, floatx80 STATUS_PARAM );
|
---|
319 | floatx80 floatx80_sqrt( floatx80 STATUS_PARAM );
|
---|
320 | INLINE int floatx80_eq( floatx80 a, floatx80 b STATUS_PARAM)
|
---|
321 | {
|
---|
322 | return a == b;
|
---|
323 | }
|
---|
324 | INLINE int floatx80_le( floatx80 a, floatx80 b STATUS_PARAM)
|
---|
325 | {
|
---|
326 | return a <= b;
|
---|
327 | }
|
---|
328 | INLINE int floatx80_lt( floatx80 a, floatx80 b STATUS_PARAM)
|
---|
329 | {
|
---|
330 | return a < b;
|
---|
331 | }
|
---|
332 | INLINE int floatx80_eq_signaling( floatx80 a, floatx80 b STATUS_PARAM)
|
---|
333 | {
|
---|
334 | return a <= b && a >= b;
|
---|
335 | }
|
---|
336 | INLINE int floatx80_le_quiet( floatx80 a, floatx80 b STATUS_PARAM)
|
---|
337 | {
|
---|
338 | return islessequal(a, b);
|
---|
339 | }
|
---|
340 | INLINE int floatx80_lt_quiet( floatx80 a, floatx80 b STATUS_PARAM)
|
---|
341 | {
|
---|
342 | return isless(a, b);
|
---|
343 |
|
---|
344 | }
|
---|
345 | INLINE int floatx80_unordered( floatx80 a, floatx80 b STATUS_PARAM)
|
---|
346 | {
|
---|
347 | return isunordered(a, b);
|
---|
348 |
|
---|
349 | }
|
---|
350 | int floatx80_compare( floatx80, floatx80 STATUS_PARAM );
|
---|
351 | int floatx80_compare_quiet( floatx80, floatx80 STATUS_PARAM );
|
---|
352 | int floatx80_is_signaling_nan( floatx80 );
|
---|
353 |
|
---|
354 | INLINE floatx80 floatx80_abs(floatx80 a)
|
---|
355 | {
|
---|
356 | return fabsl(a);
|
---|
357 | }
|
---|
358 |
|
---|
359 | INLINE floatx80 floatx80_chs(floatx80 a)
|
---|
360 | {
|
---|
361 | return -a;
|
---|
362 | }
|
---|
363 | #endif
|
---|