xref: /openbmc/qemu/include/fpu/softfloat.h (revision 22e3284f)
1 /*
2  * QEMU float support
3  *
4  * The code in this source file is derived from release 2a of the SoftFloat
5  * IEC/IEEE Floating-point Arithmetic Package. Those parts of the code (and
6  * some later contributions) are provided under that license, as detailed below.
7  * It has subsequently been modified by contributors to the QEMU Project,
8  * so some portions are provided under:
9  *  the SoftFloat-2a license
10  *  the BSD license
11  *  GPL-v2-or-later
12  *
13  * Any future contributions to this file after December 1st 2014 will be
14  * taken to be licensed under the Softfloat-2a license unless specifically
15  * indicated otherwise.
16  */
17 
18 /*
19 ===============================================================================
20 This C header file is part of the SoftFloat IEC/IEEE Floating-point
21 Arithmetic Package, Release 2a.
22 
23 Written by John R. Hauser.  This work was made possible in part by the
24 International Computer Science Institute, located at Suite 600, 1947 Center
25 Street, Berkeley, California 94704.  Funding was partially provided by the
26 National Science Foundation under grant MIP-9311980.  The original version
27 of this code was written as part of a project to build a fixed-point vector
28 processor in collaboration with the University of California at Berkeley,
29 overseen by Profs. Nelson Morgan and John Wawrzynek.  More information
30 is available through the Web page `http://HTTP.CS.Berkeley.EDU/~jhauser/
31 arithmetic/SoftFloat.html'.
32 
33 THIS SOFTWARE IS DISTRIBUTED AS IS, FOR FREE.  Although reasonable effort
34 has been made to avoid it, THIS SOFTWARE MAY CONTAIN FAULTS THAT WILL AT
35 TIMES RESULT IN INCORRECT BEHAVIOR.  USE OF THIS SOFTWARE IS RESTRICTED TO
36 PERSONS AND ORGANIZATIONS WHO CAN AND WILL TAKE FULL RESPONSIBILITY FOR ANY
37 AND ALL LOSSES, COSTS, OR OTHER PROBLEMS ARISING FROM ITS USE.
38 
39 Derivative works are acceptable, even for commercial purposes, so long as
40 (1) they include prominent notice that the work is derivative, and (2) they
41 include prominent notice akin to these four paragraphs for those parts of
42 this code that are retained.
43 
44 ===============================================================================
45 */
46 
47 /* BSD licensing:
48  * Copyright (c) 2006, Fabrice Bellard
49  * All rights reserved.
50  *
51  * Redistribution and use in source and binary forms, with or without
52  * modification, are permitted provided that the following conditions are met:
53  *
54  * 1. Redistributions of source code must retain the above copyright notice,
55  * this list of conditions and the following disclaimer.
56  *
57  * 2. Redistributions in binary form must reproduce the above copyright notice,
58  * this list of conditions and the following disclaimer in the documentation
59  * and/or other materials provided with the distribution.
60  *
61  * 3. Neither the name of the copyright holder nor the names of its contributors
62  * may be used to endorse or promote products derived from this software without
63  * specific prior written permission.
64  *
65  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
66  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
67  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
68  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
69  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
70  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
71  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
72  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
73  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
74  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
75  * THE POSSIBILITY OF SUCH DAMAGE.
76  */
77 
78 /* Portions of this work are licensed under the terms of the GNU GPL,
79  * version 2 or later. See the COPYING file in the top-level directory.
80  */
81 
82 #ifndef SOFTFLOAT_H
83 #define SOFTFLOAT_H
84 
85 #define LIT64( a ) a##LL
86 
87 /*----------------------------------------------------------------------------
88 | Software IEC/IEEE floating-point ordering relations
89 *----------------------------------------------------------------------------*/
90 enum {
91     float_relation_less      = -1,
92     float_relation_equal     =  0,
93     float_relation_greater   =  1,
94     float_relation_unordered =  2
95 };
96 
97 #include "fpu/softfloat-types.h"
98 
99 static inline void set_float_detect_tininess(int val, float_status *status)
100 {
101     status->float_detect_tininess = val;
102 }
103 static inline void set_float_rounding_mode(int val, float_status *status)
104 {
105     status->float_rounding_mode = val;
106 }
107 static inline void set_float_exception_flags(int val, float_status *status)
108 {
109     status->float_exception_flags = val;
110 }
111 static inline void set_floatx80_rounding_precision(int val,
112                                                    float_status *status)
113 {
114     status->floatx80_rounding_precision = val;
115 }
116 static inline void set_flush_to_zero(flag val, float_status *status)
117 {
118     status->flush_to_zero = val;
119 }
120 static inline void set_flush_inputs_to_zero(flag val, float_status *status)
121 {
122     status->flush_inputs_to_zero = val;
123 }
124 static inline void set_default_nan_mode(flag val, float_status *status)
125 {
126     status->default_nan_mode = val;
127 }
128 static inline void set_snan_bit_is_one(flag val, float_status *status)
129 {
130     status->snan_bit_is_one = val;
131 }
132 static inline int get_float_detect_tininess(float_status *status)
133 {
134     return status->float_detect_tininess;
135 }
136 static inline int get_float_rounding_mode(float_status *status)
137 {
138     return status->float_rounding_mode;
139 }
140 static inline int get_float_exception_flags(float_status *status)
141 {
142     return status->float_exception_flags;
143 }
144 static inline int get_floatx80_rounding_precision(float_status *status)
145 {
146     return status->floatx80_rounding_precision;
147 }
148 static inline flag get_flush_to_zero(float_status *status)
149 {
150     return status->flush_to_zero;
151 }
152 static inline flag get_flush_inputs_to_zero(float_status *status)
153 {
154     return status->flush_inputs_to_zero;
155 }
156 static inline flag get_default_nan_mode(float_status *status)
157 {
158     return status->default_nan_mode;
159 }
160 
161 /*----------------------------------------------------------------------------
162 | Routine to raise any or all of the software IEC/IEEE floating-point
163 | exception flags.
164 *----------------------------------------------------------------------------*/
165 void float_raise(uint8_t flags, float_status *status);
166 
167 /*----------------------------------------------------------------------------
168 | If `a' is denormal and we are in flush-to-zero mode then set the
169 | input-denormal exception and return zero. Otherwise just return the value.
170 *----------------------------------------------------------------------------*/
171 float16 float16_squash_input_denormal(float16 a, float_status *status);
172 float32 float32_squash_input_denormal(float32 a, float_status *status);
173 float64 float64_squash_input_denormal(float64 a, float_status *status);
174 
175 /*----------------------------------------------------------------------------
176 | Options to indicate which negations to perform in float*_muladd()
177 | Using these differs from negating an input or output before calling
178 | the muladd function in that this means that a NaN doesn't have its
179 | sign bit inverted before it is propagated.
180 | We also support halving the result before rounding, as a special
181 | case to support the ARM fused-sqrt-step instruction FRSQRTS.
182 *----------------------------------------------------------------------------*/
183 enum {
184     float_muladd_negate_c = 1,
185     float_muladd_negate_product = 2,
186     float_muladd_negate_result = 4,
187     float_muladd_halve_result = 8,
188 };
189 
190 /*----------------------------------------------------------------------------
191 | Software IEC/IEEE integer-to-floating-point conversion routines.
192 *----------------------------------------------------------------------------*/
193 
194 float16 int16_to_float16_scalbn(int16_t a, int, float_status *status);
195 float16 int32_to_float16_scalbn(int32_t a, int, float_status *status);
196 float16 int64_to_float16_scalbn(int64_t a, int, float_status *status);
197 float16 uint16_to_float16_scalbn(uint16_t a, int, float_status *status);
198 float16 uint32_to_float16_scalbn(uint32_t a, int, float_status *status);
199 float16 uint64_to_float16_scalbn(uint64_t a, int, float_status *status);
200 
201 float16 int16_to_float16(int16_t a, float_status *status);
202 float16 int32_to_float16(int32_t a, float_status *status);
203 float16 int64_to_float16(int64_t a, float_status *status);
204 float16 uint16_to_float16(uint16_t a, float_status *status);
205 float16 uint32_to_float16(uint32_t a, float_status *status);
206 float16 uint64_to_float16(uint64_t a, float_status *status);
207 
208 float32 int16_to_float32_scalbn(int16_t, int, float_status *status);
209 float32 int32_to_float32_scalbn(int32_t, int, float_status *status);
210 float32 int64_to_float32_scalbn(int64_t, int, float_status *status);
211 float32 uint16_to_float32_scalbn(uint16_t, int, float_status *status);
212 float32 uint32_to_float32_scalbn(uint32_t, int, float_status *status);
213 float32 uint64_to_float32_scalbn(uint64_t, int, float_status *status);
214 
215 float32 int16_to_float32(int16_t, float_status *status);
216 float32 int32_to_float32(int32_t, float_status *status);
217 float32 int64_to_float32(int64_t, float_status *status);
218 float32 uint16_to_float32(uint16_t, float_status *status);
219 float32 uint32_to_float32(uint32_t, float_status *status);
220 float32 uint64_to_float32(uint64_t, float_status *status);
221 
222 float64 int16_to_float64_scalbn(int16_t, int, float_status *status);
223 float64 int32_to_float64_scalbn(int32_t, int, float_status *status);
224 float64 int64_to_float64_scalbn(int64_t, int, float_status *status);
225 float64 uint16_to_float64_scalbn(uint16_t, int, float_status *status);
226 float64 uint32_to_float64_scalbn(uint32_t, int, float_status *status);
227 float64 uint64_to_float64_scalbn(uint64_t, int, float_status *status);
228 
229 float64 int16_to_float64(int16_t, float_status *status);
230 float64 int32_to_float64(int32_t, float_status *status);
231 float64 int64_to_float64(int64_t, float_status *status);
232 float64 uint16_to_float64(uint16_t, float_status *status);
233 float64 uint32_to_float64(uint32_t, float_status *status);
234 float64 uint64_to_float64(uint64_t, float_status *status);
235 
236 floatx80 int32_to_floatx80(int32_t, float_status *status);
237 floatx80 int64_to_floatx80(int64_t, float_status *status);
238 
239 float128 int32_to_float128(int32_t, float_status *status);
240 float128 int64_to_float128(int64_t, float_status *status);
241 float128 uint64_to_float128(uint64_t, float_status *status);
242 
243 /*----------------------------------------------------------------------------
244 | Software half-precision conversion routines.
245 *----------------------------------------------------------------------------*/
246 
247 float16 float32_to_float16(float32, bool ieee, float_status *status);
248 float32 float16_to_float32(float16, bool ieee, float_status *status);
249 float16 float64_to_float16(float64 a, bool ieee, float_status *status);
250 float64 float16_to_float64(float16 a, bool ieee, float_status *status);
251 
252 int16_t float16_to_int16_scalbn(float16, int, int, float_status *status);
253 int32_t float16_to_int32_scalbn(float16, int, int, float_status *status);
254 int64_t float16_to_int64_scalbn(float16, int, int, float_status *status);
255 
256 int16_t float16_to_int16(float16, float_status *status);
257 int32_t float16_to_int32(float16, float_status *status);
258 int64_t float16_to_int64(float16, float_status *status);
259 
260 int16_t float16_to_int16_round_to_zero(float16, float_status *status);
261 int32_t float16_to_int32_round_to_zero(float16, float_status *status);
262 int64_t float16_to_int64_round_to_zero(float16, float_status *status);
263 
264 uint16_t float16_to_uint16_scalbn(float16 a, int, int, float_status *status);
265 uint32_t float16_to_uint32_scalbn(float16 a, int, int, float_status *status);
266 uint64_t float16_to_uint64_scalbn(float16 a, int, int, float_status *status);
267 
268 uint16_t float16_to_uint16(float16 a, float_status *status);
269 uint32_t float16_to_uint32(float16 a, float_status *status);
270 uint64_t float16_to_uint64(float16 a, float_status *status);
271 
272 uint16_t float16_to_uint16_round_to_zero(float16 a, float_status *status);
273 uint32_t float16_to_uint32_round_to_zero(float16 a, float_status *status);
274 uint64_t float16_to_uint64_round_to_zero(float16 a, float_status *status);
275 
276 /*----------------------------------------------------------------------------
277 | Software half-precision operations.
278 *----------------------------------------------------------------------------*/
279 
280 float16 float16_round_to_int(float16, float_status *status);
281 float16 float16_add(float16, float16, float_status *status);
282 float16 float16_sub(float16, float16, float_status *status);
283 float16 float16_mul(float16, float16, float_status *status);
284 float16 float16_muladd(float16, float16, float16, int, float_status *status);
285 float16 float16_div(float16, float16, float_status *status);
286 float16 float16_scalbn(float16, int, float_status *status);
287 float16 float16_min(float16, float16, float_status *status);
288 float16 float16_max(float16, float16, float_status *status);
289 float16 float16_minnum(float16, float16, float_status *status);
290 float16 float16_maxnum(float16, float16, float_status *status);
291 float16 float16_minnummag(float16, float16, float_status *status);
292 float16 float16_maxnummag(float16, float16, float_status *status);
293 float16 float16_sqrt(float16, float_status *status);
294 int float16_compare(float16, float16, float_status *status);
295 int float16_compare_quiet(float16, float16, float_status *status);
296 
297 int float16_is_quiet_nan(float16, float_status *status);
298 int float16_is_signaling_nan(float16, float_status *status);
299 float16 float16_silence_nan(float16, float_status *status);
300 
301 static inline int float16_is_any_nan(float16 a)
302 {
303     return ((float16_val(a) & ~0x8000) > 0x7c00);
304 }
305 
306 static inline int float16_is_neg(float16 a)
307 {
308     return float16_val(a) >> 15;
309 }
310 
311 static inline int float16_is_infinity(float16 a)
312 {
313     return (float16_val(a) & 0x7fff) == 0x7c00;
314 }
315 
316 static inline int float16_is_zero(float16 a)
317 {
318     return (float16_val(a) & 0x7fff) == 0;
319 }
320 
321 static inline int float16_is_zero_or_denormal(float16 a)
322 {
323     return (float16_val(a) & 0x7c00) == 0;
324 }
325 
326 static inline float16 float16_abs(float16 a)
327 {
328     /* Note that abs does *not* handle NaN specially, nor does
329      * it flush denormal inputs to zero.
330      */
331     return make_float16(float16_val(a) & 0x7fff);
332 }
333 
334 static inline float16 float16_chs(float16 a)
335 {
336     /* Note that chs does *not* handle NaN specially, nor does
337      * it flush denormal inputs to zero.
338      */
339     return make_float16(float16_val(a) ^ 0x8000);
340 }
341 
342 static inline float16 float16_set_sign(float16 a, int sign)
343 {
344     return make_float16((float16_val(a) & 0x7fff) | (sign << 15));
345 }
346 
347 #define float16_zero make_float16(0)
348 #define float16_half make_float16(0x3800)
349 #define float16_one make_float16(0x3c00)
350 #define float16_one_point_five make_float16(0x3e00)
351 #define float16_two make_float16(0x4000)
352 #define float16_three make_float16(0x4200)
353 #define float16_infinity make_float16(0x7c00)
354 
355 /*----------------------------------------------------------------------------
356 | The pattern for a default generated half-precision NaN.
357 *----------------------------------------------------------------------------*/
358 float16 float16_default_nan(float_status *status);
359 
360 /*----------------------------------------------------------------------------
361 | Software IEC/IEEE single-precision conversion routines.
362 *----------------------------------------------------------------------------*/
363 
364 int16_t float32_to_int16_scalbn(float32, int, int, float_status *status);
365 int32_t float32_to_int32_scalbn(float32, int, int, float_status *status);
366 int64_t float32_to_int64_scalbn(float32, int, int, float_status *status);
367 
368 int16_t float32_to_int16(float32, float_status *status);
369 int32_t float32_to_int32(float32, float_status *status);
370 int64_t float32_to_int64(float32, float_status *status);
371 
372 int16_t float32_to_int16_round_to_zero(float32, float_status *status);
373 int32_t float32_to_int32_round_to_zero(float32, float_status *status);
374 int64_t float32_to_int64_round_to_zero(float32, float_status *status);
375 
376 uint16_t float32_to_uint16_scalbn(float32, int, int, float_status *status);
377 uint32_t float32_to_uint32_scalbn(float32, int, int, float_status *status);
378 uint64_t float32_to_uint64_scalbn(float32, int, int, float_status *status);
379 
380 uint16_t float32_to_uint16(float32, float_status *status);
381 uint32_t float32_to_uint32(float32, float_status *status);
382 uint64_t float32_to_uint64(float32, float_status *status);
383 
384 uint16_t float32_to_uint16_round_to_zero(float32, float_status *status);
385 uint32_t float32_to_uint32_round_to_zero(float32, float_status *status);
386 uint64_t float32_to_uint64_round_to_zero(float32, float_status *status);
387 
388 float64 float32_to_float64(float32, float_status *status);
389 floatx80 float32_to_floatx80(float32, float_status *status);
390 float128 float32_to_float128(float32, float_status *status);
391 
392 /*----------------------------------------------------------------------------
393 | Software IEC/IEEE single-precision operations.
394 *----------------------------------------------------------------------------*/
395 float32 float32_round_to_int(float32, float_status *status);
396 float32 float32_add(float32, float32, float_status *status);
397 float32 float32_sub(float32, float32, float_status *status);
398 float32 float32_mul(float32, float32, float_status *status);
399 float32 float32_div(float32, float32, float_status *status);
400 float32 float32_rem(float32, float32, float_status *status);
401 float32 float32_muladd(float32, float32, float32, int, float_status *status);
402 float32 float32_sqrt(float32, float_status *status);
403 float32 float32_exp2(float32, float_status *status);
404 float32 float32_log2(float32, float_status *status);
405 int float32_eq(float32, float32, float_status *status);
406 int float32_le(float32, float32, float_status *status);
407 int float32_lt(float32, float32, float_status *status);
408 int float32_unordered(float32, float32, float_status *status);
409 int float32_eq_quiet(float32, float32, float_status *status);
410 int float32_le_quiet(float32, float32, float_status *status);
411 int float32_lt_quiet(float32, float32, float_status *status);
412 int float32_unordered_quiet(float32, float32, float_status *status);
413 int float32_compare(float32, float32, float_status *status);
414 int float32_compare_quiet(float32, float32, float_status *status);
415 float32 float32_min(float32, float32, float_status *status);
416 float32 float32_max(float32, float32, float_status *status);
417 float32 float32_minnum(float32, float32, float_status *status);
418 float32 float32_maxnum(float32, float32, float_status *status);
419 float32 float32_minnummag(float32, float32, float_status *status);
420 float32 float32_maxnummag(float32, float32, float_status *status);
421 int float32_is_quiet_nan(float32, float_status *status);
422 int float32_is_signaling_nan(float32, float_status *status);
423 float32 float32_silence_nan(float32, float_status *status);
424 float32 float32_scalbn(float32, int, float_status *status);
425 
426 static inline float32 float32_abs(float32 a)
427 {
428     /* Note that abs does *not* handle NaN specially, nor does
429      * it flush denormal inputs to zero.
430      */
431     return make_float32(float32_val(a) & 0x7fffffff);
432 }
433 
434 static inline float32 float32_chs(float32 a)
435 {
436     /* Note that chs does *not* handle NaN specially, nor does
437      * it flush denormal inputs to zero.
438      */
439     return make_float32(float32_val(a) ^ 0x80000000);
440 }
441 
442 static inline int float32_is_infinity(float32 a)
443 {
444     return (float32_val(a) & 0x7fffffff) == 0x7f800000;
445 }
446 
447 static inline int float32_is_neg(float32 a)
448 {
449     return float32_val(a) >> 31;
450 }
451 
452 static inline int float32_is_zero(float32 a)
453 {
454     return (float32_val(a) & 0x7fffffff) == 0;
455 }
456 
457 static inline int float32_is_any_nan(float32 a)
458 {
459     return ((float32_val(a) & ~(1 << 31)) > 0x7f800000UL);
460 }
461 
462 static inline int float32_is_zero_or_denormal(float32 a)
463 {
464     return (float32_val(a) & 0x7f800000) == 0;
465 }
466 
467 static inline bool float32_is_normal(float32 a)
468 {
469     return (((float32_val(a) >> 23) + 1) & 0xff) >= 2;
470 }
471 
472 static inline bool float32_is_denormal(float32 a)
473 {
474     return float32_is_zero_or_denormal(a) && !float32_is_zero(a);
475 }
476 
477 static inline bool float32_is_zero_or_normal(float32 a)
478 {
479     return float32_is_normal(a) || float32_is_zero(a);
480 }
481 
482 static inline float32 float32_set_sign(float32 a, int sign)
483 {
484     return make_float32((float32_val(a) & 0x7fffffff) | (sign << 31));
485 }
486 
487 #define float32_zero make_float32(0)
488 #define float32_half make_float32(0x3f000000)
489 #define float32_one make_float32(0x3f800000)
490 #define float32_one_point_five make_float32(0x3fc00000)
491 #define float32_two make_float32(0x40000000)
492 #define float32_three make_float32(0x40400000)
493 #define float32_infinity make_float32(0x7f800000)
494 
495 /*----------------------------------------------------------------------------
496 | Packs the sign `zSign', exponent `zExp', and significand `zSig' into a
497 | single-precision floating-point value, returning the result.  After being
498 | shifted into the proper positions, the three fields are simply added
499 | together to form the result.  This means that any integer portion of `zSig'
500 | will be added into the exponent.  Since a properly normalized significand
501 | will have an integer portion equal to 1, the `zExp' input should be 1 less
502 | than the desired result exponent whenever `zSig' is a complete, normalized
503 | significand.
504 *----------------------------------------------------------------------------*/
505 
506 static inline float32 packFloat32(flag zSign, int zExp, uint32_t zSig)
507 {
508     return make_float32(
509           (((uint32_t)zSign) << 31) + (((uint32_t)zExp) << 23) + zSig);
510 }
511 
512 /*----------------------------------------------------------------------------
513 | The pattern for a default generated single-precision NaN.
514 *----------------------------------------------------------------------------*/
515 float32 float32_default_nan(float_status *status);
516 
517 /*----------------------------------------------------------------------------
518 | Software IEC/IEEE double-precision conversion routines.
519 *----------------------------------------------------------------------------*/
520 
521 int16_t float64_to_int16_scalbn(float64, int, int, float_status *status);
522 int32_t float64_to_int32_scalbn(float64, int, int, float_status *status);
523 int64_t float64_to_int64_scalbn(float64, int, int, float_status *status);
524 
525 int16_t float64_to_int16(float64, float_status *status);
526 int32_t float64_to_int32(float64, float_status *status);
527 int64_t float64_to_int64(float64, float_status *status);
528 
529 int16_t float64_to_int16_round_to_zero(float64, float_status *status);
530 int32_t float64_to_int32_round_to_zero(float64, float_status *status);
531 int64_t float64_to_int64_round_to_zero(float64, float_status *status);
532 
533 uint16_t float64_to_uint16_scalbn(float64, int, int, float_status *status);
534 uint32_t float64_to_uint32_scalbn(float64, int, int, float_status *status);
535 uint64_t float64_to_uint64_scalbn(float64, int, int, float_status *status);
536 
537 uint16_t float64_to_uint16(float64, float_status *status);
538 uint32_t float64_to_uint32(float64, float_status *status);
539 uint64_t float64_to_uint64(float64, float_status *status);
540 
541 uint16_t float64_to_uint16_round_to_zero(float64, float_status *status);
542 uint32_t float64_to_uint32_round_to_zero(float64, float_status *status);
543 uint64_t float64_to_uint64_round_to_zero(float64, float_status *status);
544 
545 float32 float64_to_float32(float64, float_status *status);
546 floatx80 float64_to_floatx80(float64, float_status *status);
547 float128 float64_to_float128(float64, float_status *status);
548 
549 /*----------------------------------------------------------------------------
550 | Software IEC/IEEE double-precision operations.
551 *----------------------------------------------------------------------------*/
552 float64 float64_round_to_int(float64, float_status *status);
553 float64 float64_add(float64, float64, float_status *status);
554 float64 float64_sub(float64, float64, float_status *status);
555 float64 float64_mul(float64, float64, float_status *status);
556 float64 float64_div(float64, float64, float_status *status);
557 float64 float64_rem(float64, float64, float_status *status);
558 float64 float64_muladd(float64, float64, float64, int, float_status *status);
559 float64 float64_sqrt(float64, float_status *status);
560 float64 float64_log2(float64, float_status *status);
561 int float64_eq(float64, float64, float_status *status);
562 int float64_le(float64, float64, float_status *status);
563 int float64_lt(float64, float64, float_status *status);
564 int float64_unordered(float64, float64, float_status *status);
565 int float64_eq_quiet(float64, float64, float_status *status);
566 int float64_le_quiet(float64, float64, float_status *status);
567 int float64_lt_quiet(float64, float64, float_status *status);
568 int float64_unordered_quiet(float64, float64, float_status *status);
569 int float64_compare(float64, float64, float_status *status);
570 int float64_compare_quiet(float64, float64, float_status *status);
571 float64 float64_min(float64, float64, float_status *status);
572 float64 float64_max(float64, float64, float_status *status);
573 float64 float64_minnum(float64, float64, float_status *status);
574 float64 float64_maxnum(float64, float64, float_status *status);
575 float64 float64_minnummag(float64, float64, float_status *status);
576 float64 float64_maxnummag(float64, float64, float_status *status);
577 int float64_is_quiet_nan(float64 a, float_status *status);
578 int float64_is_signaling_nan(float64, float_status *status);
579 float64 float64_silence_nan(float64, float_status *status);
580 float64 float64_scalbn(float64, int, float_status *status);
581 
582 static inline float64 float64_abs(float64 a)
583 {
584     /* Note that abs does *not* handle NaN specially, nor does
585      * it flush denormal inputs to zero.
586      */
587     return make_float64(float64_val(a) & 0x7fffffffffffffffLL);
588 }
589 
590 static inline float64 float64_chs(float64 a)
591 {
592     /* Note that chs does *not* handle NaN specially, nor does
593      * it flush denormal inputs to zero.
594      */
595     return make_float64(float64_val(a) ^ 0x8000000000000000LL);
596 }
597 
598 static inline int float64_is_infinity(float64 a)
599 {
600     return (float64_val(a) & 0x7fffffffffffffffLL ) == 0x7ff0000000000000LL;
601 }
602 
603 static inline int float64_is_neg(float64 a)
604 {
605     return float64_val(a) >> 63;
606 }
607 
608 static inline int float64_is_zero(float64 a)
609 {
610     return (float64_val(a) & 0x7fffffffffffffffLL) == 0;
611 }
612 
613 static inline int float64_is_any_nan(float64 a)
614 {
615     return ((float64_val(a) & ~(1ULL << 63)) > 0x7ff0000000000000ULL);
616 }
617 
618 static inline int float64_is_zero_or_denormal(float64 a)
619 {
620     return (float64_val(a) & 0x7ff0000000000000LL) == 0;
621 }
622 
623 static inline bool float64_is_normal(float64 a)
624 {
625     return (((float64_val(a) >> 52) + 1) & 0x7ff) >= 2;
626 }
627 
628 static inline bool float64_is_denormal(float64 a)
629 {
630     return float64_is_zero_or_denormal(a) && !float64_is_zero(a);
631 }
632 
633 static inline bool float64_is_zero_or_normal(float64 a)
634 {
635     return float64_is_normal(a) || float64_is_zero(a);
636 }
637 
638 static inline float64 float64_set_sign(float64 a, int sign)
639 {
640     return make_float64((float64_val(a) & 0x7fffffffffffffffULL)
641                         | ((int64_t)sign << 63));
642 }
643 
644 #define float64_zero make_float64(0)
645 #define float64_half make_float64(0x3fe0000000000000LL)
646 #define float64_one make_float64(0x3ff0000000000000LL)
647 #define float64_one_point_five make_float64(0x3FF8000000000000ULL)
648 #define float64_two make_float64(0x4000000000000000ULL)
649 #define float64_three make_float64(0x4008000000000000ULL)
650 #define float64_ln2 make_float64(0x3fe62e42fefa39efLL)
651 #define float64_infinity make_float64(0x7ff0000000000000LL)
652 
653 /*----------------------------------------------------------------------------
654 | The pattern for a default generated double-precision NaN.
655 *----------------------------------------------------------------------------*/
656 float64 float64_default_nan(float_status *status);
657 
658 /*----------------------------------------------------------------------------
659 | Software IEC/IEEE extended double-precision conversion routines.
660 *----------------------------------------------------------------------------*/
661 int32_t floatx80_to_int32(floatx80, float_status *status);
662 int32_t floatx80_to_int32_round_to_zero(floatx80, float_status *status);
663 int64_t floatx80_to_int64(floatx80, float_status *status);
664 int64_t floatx80_to_int64_round_to_zero(floatx80, float_status *status);
665 float32 floatx80_to_float32(floatx80, float_status *status);
666 float64 floatx80_to_float64(floatx80, float_status *status);
667 float128 floatx80_to_float128(floatx80, float_status *status);
668 
669 /*----------------------------------------------------------------------------
670 | The pattern for an extended double-precision inf.
671 *----------------------------------------------------------------------------*/
672 extern const floatx80 floatx80_infinity;
673 
674 /*----------------------------------------------------------------------------
675 | Software IEC/IEEE extended double-precision operations.
676 *----------------------------------------------------------------------------*/
677 floatx80 floatx80_round(floatx80 a, float_status *status);
678 floatx80 floatx80_round_to_int(floatx80, float_status *status);
679 floatx80 floatx80_add(floatx80, floatx80, float_status *status);
680 floatx80 floatx80_sub(floatx80, floatx80, float_status *status);
681 floatx80 floatx80_mul(floatx80, floatx80, float_status *status);
682 floatx80 floatx80_div(floatx80, floatx80, float_status *status);
683 floatx80 floatx80_rem(floatx80, floatx80, float_status *status);
684 floatx80 floatx80_sqrt(floatx80, float_status *status);
685 int floatx80_eq(floatx80, floatx80, float_status *status);
686 int floatx80_le(floatx80, floatx80, float_status *status);
687 int floatx80_lt(floatx80, floatx80, float_status *status);
688 int floatx80_unordered(floatx80, floatx80, float_status *status);
689 int floatx80_eq_quiet(floatx80, floatx80, float_status *status);
690 int floatx80_le_quiet(floatx80, floatx80, float_status *status);
691 int floatx80_lt_quiet(floatx80, floatx80, float_status *status);
692 int floatx80_unordered_quiet(floatx80, floatx80, float_status *status);
693 int floatx80_compare(floatx80, floatx80, float_status *status);
694 int floatx80_compare_quiet(floatx80, floatx80, float_status *status);
695 int floatx80_is_quiet_nan(floatx80, float_status *status);
696 int floatx80_is_signaling_nan(floatx80, float_status *status);
697 floatx80 floatx80_silence_nan(floatx80, float_status *status);
698 floatx80 floatx80_scalbn(floatx80, int, float_status *status);
699 
700 static inline floatx80 floatx80_abs(floatx80 a)
701 {
702     a.high &= 0x7fff;
703     return a;
704 }
705 
706 static inline floatx80 floatx80_chs(floatx80 a)
707 {
708     a.high ^= 0x8000;
709     return a;
710 }
711 
712 static inline int floatx80_is_infinity(floatx80 a)
713 {
714 #if defined(TARGET_M68K)
715     return (a.high & 0x7fff) == floatx80_infinity.high && !(a.low << 1);
716 #else
717     return (a.high & 0x7fff) == floatx80_infinity.high &&
718                        a.low == floatx80_infinity.low;
719 #endif
720 }
721 
722 static inline int floatx80_is_neg(floatx80 a)
723 {
724     return a.high >> 15;
725 }
726 
727 static inline int floatx80_is_zero(floatx80 a)
728 {
729     return (a.high & 0x7fff) == 0 && a.low == 0;
730 }
731 
732 static inline int floatx80_is_zero_or_denormal(floatx80 a)
733 {
734     return (a.high & 0x7fff) == 0;
735 }
736 
737 static inline int floatx80_is_any_nan(floatx80 a)
738 {
739     return ((a.high & 0x7fff) == 0x7fff) && (a.low<<1);
740 }
741 
742 /*----------------------------------------------------------------------------
743 | Return whether the given value is an invalid floatx80 encoding.
744 | Invalid floatx80 encodings arise when the integer bit is not set, but
745 | the exponent is not zero. The only times the integer bit is permitted to
746 | be zero is in subnormal numbers and the value zero.
747 | This includes what the Intel software developer's manual calls pseudo-NaNs,
748 | pseudo-infinities and un-normal numbers. It does not include
749 | pseudo-denormals, which must still be correctly handled as inputs even
750 | if they are never generated as outputs.
751 *----------------------------------------------------------------------------*/
752 static inline bool floatx80_invalid_encoding(floatx80 a)
753 {
754     return (a.low & (1ULL << 63)) == 0 && (a.high & 0x7FFF) != 0;
755 }
756 
757 #define floatx80_zero make_floatx80(0x0000, 0x0000000000000000LL)
758 #define floatx80_one make_floatx80(0x3fff, 0x8000000000000000LL)
759 #define floatx80_ln2 make_floatx80(0x3ffe, 0xb17217f7d1cf79acLL)
760 #define floatx80_pi make_floatx80(0x4000, 0xc90fdaa22168c235LL)
761 #define floatx80_half make_floatx80(0x3ffe, 0x8000000000000000LL)
762 
763 /*----------------------------------------------------------------------------
764 | Returns the fraction bits of the extended double-precision floating-point
765 | value `a'.
766 *----------------------------------------------------------------------------*/
767 
768 static inline uint64_t extractFloatx80Frac(floatx80 a)
769 {
770     return a.low;
771 }
772 
773 /*----------------------------------------------------------------------------
774 | Returns the exponent bits of the extended double-precision floating-point
775 | value `a'.
776 *----------------------------------------------------------------------------*/
777 
778 static inline int32_t extractFloatx80Exp(floatx80 a)
779 {
780     return a.high & 0x7FFF;
781 }
782 
783 /*----------------------------------------------------------------------------
784 | Returns the sign bit of the extended double-precision floating-point value
785 | `a'.
786 *----------------------------------------------------------------------------*/
787 
788 static inline flag extractFloatx80Sign(floatx80 a)
789 {
790     return a.high >> 15;
791 }
792 
793 /*----------------------------------------------------------------------------
794 | Packs the sign `zSign', exponent `zExp', and significand `zSig' into an
795 | extended double-precision floating-point value, returning the result.
796 *----------------------------------------------------------------------------*/
797 
798 static inline floatx80 packFloatx80(flag zSign, int32_t zExp, uint64_t zSig)
799 {
800     floatx80 z;
801 
802     z.low = zSig;
803     z.high = (((uint16_t)zSign) << 15) + zExp;
804     return z;
805 }
806 
807 /*----------------------------------------------------------------------------
808 | Normalizes the subnormal extended double-precision floating-point value
809 | represented by the denormalized significand `aSig'.  The normalized exponent
810 | and significand are stored at the locations pointed to by `zExpPtr' and
811 | `zSigPtr', respectively.
812 *----------------------------------------------------------------------------*/
813 
814 void normalizeFloatx80Subnormal(uint64_t aSig, int32_t *zExpPtr,
815                                 uint64_t *zSigPtr);
816 
817 /*----------------------------------------------------------------------------
818 | Takes two extended double-precision floating-point values `a' and `b', one
819 | of which is a NaN, and returns the appropriate NaN result.  If either `a' or
820 | `b' is a signaling NaN, the invalid exception is raised.
821 *----------------------------------------------------------------------------*/
822 
823 floatx80 propagateFloatx80NaN(floatx80 a, floatx80 b, float_status *status);
824 
825 /*----------------------------------------------------------------------------
826 | Takes an abstract floating-point value having sign `zSign', exponent `zExp',
827 | and extended significand formed by the concatenation of `zSig0' and `zSig1',
828 | and returns the proper extended double-precision floating-point value
829 | corresponding to the abstract input.  Ordinarily, the abstract value is
830 | rounded and packed into the extended double-precision format, with the
831 | inexact exception raised if the abstract input cannot be represented
832 | exactly.  However, if the abstract value is too large, the overflow and
833 | inexact exceptions are raised and an infinity or maximal finite value is
834 | returned.  If the abstract value is too small, the input value is rounded to
835 | a subnormal number, and the underflow and inexact exceptions are raised if
836 | the abstract input cannot be represented exactly as a subnormal extended
837 | double-precision floating-point number.
838 |     If `roundingPrecision' is 32 or 64, the result is rounded to the same
839 | number of bits as single or double precision, respectively.  Otherwise, the
840 | result is rounded to the full precision of the extended double-precision
841 | format.
842 |     The input significand must be normalized or smaller.  If the input
843 | significand is not normalized, `zExp' must be 0; in that case, the result
844 | returned is a subnormal number, and it must not require rounding.  The
845 | handling of underflow and overflow follows the IEC/IEEE Standard for Binary
846 | Floating-Point Arithmetic.
847 *----------------------------------------------------------------------------*/
848 
849 floatx80 roundAndPackFloatx80(int8_t roundingPrecision, flag zSign,
850                               int32_t zExp, uint64_t zSig0, uint64_t zSig1,
851                               float_status *status);
852 
853 /*----------------------------------------------------------------------------
854 | Takes an abstract floating-point value having sign `zSign', exponent
855 | `zExp', and significand formed by the concatenation of `zSig0' and `zSig1',
856 | and returns the proper extended double-precision floating-point value
857 | corresponding to the abstract input.  This routine is just like
858 | `roundAndPackFloatx80' except that the input significand does not have to be
859 | normalized.
860 *----------------------------------------------------------------------------*/
861 
862 floatx80 normalizeRoundAndPackFloatx80(int8_t roundingPrecision,
863                                        flag zSign, int32_t zExp,
864                                        uint64_t zSig0, uint64_t zSig1,
865                                        float_status *status);
866 
867 /*----------------------------------------------------------------------------
868 | The pattern for a default generated extended double-precision NaN.
869 *----------------------------------------------------------------------------*/
870 floatx80 floatx80_default_nan(float_status *status);
871 
872 /*----------------------------------------------------------------------------
873 | Software IEC/IEEE quadruple-precision conversion routines.
874 *----------------------------------------------------------------------------*/
875 int32_t float128_to_int32(float128, float_status *status);
876 int32_t float128_to_int32_round_to_zero(float128, float_status *status);
877 int64_t float128_to_int64(float128, float_status *status);
878 int64_t float128_to_int64_round_to_zero(float128, float_status *status);
879 uint64_t float128_to_uint64(float128, float_status *status);
880 uint64_t float128_to_uint64_round_to_zero(float128, float_status *status);
881 uint32_t float128_to_uint32(float128, float_status *status);
882 uint32_t float128_to_uint32_round_to_zero(float128, float_status *status);
883 float32 float128_to_float32(float128, float_status *status);
884 float64 float128_to_float64(float128, float_status *status);
885 floatx80 float128_to_floatx80(float128, float_status *status);
886 
887 /*----------------------------------------------------------------------------
888 | Software IEC/IEEE quadruple-precision operations.
889 *----------------------------------------------------------------------------*/
890 float128 float128_round_to_int(float128, float_status *status);
891 float128 float128_add(float128, float128, float_status *status);
892 float128 float128_sub(float128, float128, float_status *status);
893 float128 float128_mul(float128, float128, float_status *status);
894 float128 float128_div(float128, float128, float_status *status);
895 float128 float128_rem(float128, float128, float_status *status);
896 float128 float128_sqrt(float128, float_status *status);
897 int float128_eq(float128, float128, float_status *status);
898 int float128_le(float128, float128, float_status *status);
899 int float128_lt(float128, float128, float_status *status);
900 int float128_unordered(float128, float128, float_status *status);
901 int float128_eq_quiet(float128, float128, float_status *status);
902 int float128_le_quiet(float128, float128, float_status *status);
903 int float128_lt_quiet(float128, float128, float_status *status);
904 int float128_unordered_quiet(float128, float128, float_status *status);
905 int float128_compare(float128, float128, float_status *status);
906 int float128_compare_quiet(float128, float128, float_status *status);
907 int float128_is_quiet_nan(float128, float_status *status);
908 int float128_is_signaling_nan(float128, float_status *status);
909 float128 float128_silence_nan(float128, float_status *status);
910 float128 float128_scalbn(float128, int, float_status *status);
911 
912 static inline float128 float128_abs(float128 a)
913 {
914     a.high &= 0x7fffffffffffffffLL;
915     return a;
916 }
917 
918 static inline float128 float128_chs(float128 a)
919 {
920     a.high ^= 0x8000000000000000LL;
921     return a;
922 }
923 
924 static inline int float128_is_infinity(float128 a)
925 {
926     return (a.high & 0x7fffffffffffffffLL) == 0x7fff000000000000LL && a.low == 0;
927 }
928 
929 static inline int float128_is_neg(float128 a)
930 {
931     return a.high >> 63;
932 }
933 
934 static inline int float128_is_zero(float128 a)
935 {
936     return (a.high & 0x7fffffffffffffffLL) == 0 && a.low == 0;
937 }
938 
939 static inline int float128_is_zero_or_denormal(float128 a)
940 {
941     return (a.high & 0x7fff000000000000LL) == 0;
942 }
943 
944 static inline bool float128_is_normal(float128 a)
945 {
946     return (((a.high >> 48) + 1) & 0x7fff) >= 2;
947 }
948 
949 static inline bool float128_is_denormal(float128 a)
950 {
951     return float128_is_zero_or_denormal(a) && !float128_is_zero(a);
952 }
953 
954 static inline int float128_is_any_nan(float128 a)
955 {
956     return ((a.high >> 48) & 0x7fff) == 0x7fff &&
957         ((a.low != 0) || ((a.high & 0xffffffffffffLL) != 0));
958 }
959 
960 #define float128_zero make_float128(0, 0)
961 
962 /*----------------------------------------------------------------------------
963 | The pattern for a default generated quadruple-precision NaN.
964 *----------------------------------------------------------------------------*/
965 float128 float128_default_nan(float_status *status);
966 
967 #endif /* SOFTFLOAT_H */
968