ppc/pegasos2: Fix naming of device tree nodes
[qemu/armbru.git] / fpu / softfloat.c
blob0cc130ae9b27584a090a4f9b508048349743c2f8
1 /*
2 * QEMU float support
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
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.
19 ===============================================================================
20 This C source file is part of the SoftFloat IEC/IEEE Floating-point
21 Arithmetic Package, Release 2a.
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'.
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.
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.
44 ===============================================================================
47 /* BSD licensing:
48 * Copyright (c) 2006, Fabrice Bellard
49 * All rights reserved.
51 * Redistribution and use in source and binary forms, with or without
52 * modification, are permitted provided that the following conditions are met:
54 * 1. Redistributions of source code must retain the above copyright notice,
55 * this list of conditions and the following disclaimer.
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.
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.
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.
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.
82 /* softfloat (and in particular the code in softfloat-specialize.h) is
83 * target-dependent and needs the TARGET_* macros.
85 #include "qemu/osdep.h"
86 #include <math.h>
87 #include "qemu/bitops.h"
88 #include "fpu/softfloat.h"
90 /* We only need stdlib for abort() */
92 /*----------------------------------------------------------------------------
93 | Primitive arithmetic functions, including multi-word arithmetic, and
94 | division and square root approximations. (Can be specialized to target if
95 | desired.)
96 *----------------------------------------------------------------------------*/
97 #include "fpu/softfloat-macros.h"
100 * Hardfloat
102 * Fast emulation of guest FP instructions is challenging for two reasons.
103 * First, FP instruction semantics are similar but not identical, particularly
104 * when handling NaNs. Second, emulating at reasonable speed the guest FP
105 * exception flags is not trivial: reading the host's flags register with a
106 * feclearexcept & fetestexcept pair is slow [slightly slower than soft-fp],
107 * and trapping on every FP exception is not fast nor pleasant to work with.
109 * We address these challenges by leveraging the host FPU for a subset of the
110 * operations. To do this we expand on the idea presented in this paper:
112 * Guo, Yu-Chuan, et al. "Translating the ARM Neon and VFP instructions in a
113 * binary translator." Software: Practice and Experience 46.12 (2016):1591-1615.
115 * The idea is thus to leverage the host FPU to (1) compute FP operations
116 * and (2) identify whether FP exceptions occurred while avoiding
117 * expensive exception flag register accesses.
119 * An important optimization shown in the paper is that given that exception
120 * flags are rarely cleared by the guest, we can avoid recomputing some flags.
121 * This is particularly useful for the inexact flag, which is very frequently
122 * raised in floating-point workloads.
124 * We optimize the code further by deferring to soft-fp whenever FP exception
125 * detection might get hairy. Two examples: (1) when at least one operand is
126 * denormal/inf/NaN; (2) when operands are not guaranteed to lead to a 0 result
127 * and the result is < the minimum normal.
129 #define GEN_INPUT_FLUSH__NOCHECK(name, soft_t) \
130 static inline void name(soft_t *a, float_status *s) \
132 if (unlikely(soft_t ## _is_denormal(*a))) { \
133 *a = soft_t ## _set_sign(soft_t ## _zero, \
134 soft_t ## _is_neg(*a)); \
135 float_raise(float_flag_input_denormal, s); \
139 GEN_INPUT_FLUSH__NOCHECK(float32_input_flush__nocheck, float32)
140 GEN_INPUT_FLUSH__NOCHECK(float64_input_flush__nocheck, float64)
141 #undef GEN_INPUT_FLUSH__NOCHECK
143 #define GEN_INPUT_FLUSH1(name, soft_t) \
144 static inline void name(soft_t *a, float_status *s) \
146 if (likely(!s->flush_inputs_to_zero)) { \
147 return; \
149 soft_t ## _input_flush__nocheck(a, s); \
152 GEN_INPUT_FLUSH1(float32_input_flush1, float32)
153 GEN_INPUT_FLUSH1(float64_input_flush1, float64)
154 #undef GEN_INPUT_FLUSH1
156 #define GEN_INPUT_FLUSH2(name, soft_t) \
157 static inline void name(soft_t *a, soft_t *b, float_status *s) \
159 if (likely(!s->flush_inputs_to_zero)) { \
160 return; \
162 soft_t ## _input_flush__nocheck(a, s); \
163 soft_t ## _input_flush__nocheck(b, s); \
166 GEN_INPUT_FLUSH2(float32_input_flush2, float32)
167 GEN_INPUT_FLUSH2(float64_input_flush2, float64)
168 #undef GEN_INPUT_FLUSH2
170 #define GEN_INPUT_FLUSH3(name, soft_t) \
171 static inline void name(soft_t *a, soft_t *b, soft_t *c, float_status *s) \
173 if (likely(!s->flush_inputs_to_zero)) { \
174 return; \
176 soft_t ## _input_flush__nocheck(a, s); \
177 soft_t ## _input_flush__nocheck(b, s); \
178 soft_t ## _input_flush__nocheck(c, s); \
181 GEN_INPUT_FLUSH3(float32_input_flush3, float32)
182 GEN_INPUT_FLUSH3(float64_input_flush3, float64)
183 #undef GEN_INPUT_FLUSH3
186 * Choose whether to use fpclassify or float32/64_* primitives in the generated
187 * hardfloat functions. Each combination of number of inputs and float size
188 * gets its own value.
190 #if defined(__x86_64__)
191 # define QEMU_HARDFLOAT_1F32_USE_FP 0
192 # define QEMU_HARDFLOAT_1F64_USE_FP 1
193 # define QEMU_HARDFLOAT_2F32_USE_FP 0
194 # define QEMU_HARDFLOAT_2F64_USE_FP 1
195 # define QEMU_HARDFLOAT_3F32_USE_FP 0
196 # define QEMU_HARDFLOAT_3F64_USE_FP 1
197 #else
198 # define QEMU_HARDFLOAT_1F32_USE_FP 0
199 # define QEMU_HARDFLOAT_1F64_USE_FP 0
200 # define QEMU_HARDFLOAT_2F32_USE_FP 0
201 # define QEMU_HARDFLOAT_2F64_USE_FP 0
202 # define QEMU_HARDFLOAT_3F32_USE_FP 0
203 # define QEMU_HARDFLOAT_3F64_USE_FP 0
204 #endif
207 * QEMU_HARDFLOAT_USE_ISINF chooses whether to use isinf() over
208 * float{32,64}_is_infinity when !USE_FP.
209 * On x86_64/aarch64, using the former over the latter can yield a ~6% speedup.
210 * On power64 however, using isinf() reduces fp-bench performance by up to 50%.
212 #if defined(__x86_64__) || defined(__aarch64__)
213 # define QEMU_HARDFLOAT_USE_ISINF 1
214 #else
215 # define QEMU_HARDFLOAT_USE_ISINF 0
216 #endif
219 * Some targets clear the FP flags before most FP operations. This prevents
220 * the use of hardfloat, since hardfloat relies on the inexact flag being
221 * already set.
223 #if defined(TARGET_PPC) || defined(__FAST_MATH__)
224 # if defined(__FAST_MATH__)
225 # warning disabling hardfloat due to -ffast-math: hardfloat requires an exact \
226 IEEE implementation
227 # endif
228 # define QEMU_NO_HARDFLOAT 1
229 # define QEMU_SOFTFLOAT_ATTR QEMU_FLATTEN
230 #else
231 # define QEMU_NO_HARDFLOAT 0
232 # define QEMU_SOFTFLOAT_ATTR QEMU_FLATTEN __attribute__((noinline))
233 #endif
235 static inline bool can_use_fpu(const float_status *s)
237 if (QEMU_NO_HARDFLOAT) {
238 return false;
240 return likely(s->float_exception_flags & float_flag_inexact &&
241 s->float_rounding_mode == float_round_nearest_even);
245 * Hardfloat generation functions. Each operation can have two flavors:
246 * either using softfloat primitives (e.g. float32_is_zero_or_normal) for
247 * most condition checks, or native ones (e.g. fpclassify).
249 * The flavor is chosen by the callers. Instead of using macros, we rely on the
250 * compiler to propagate constants and inline everything into the callers.
252 * We only generate functions for operations with two inputs, since only
253 * these are common enough to justify consolidating them into common code.
256 typedef union {
257 float32 s;
258 float h;
259 } union_float32;
261 typedef union {
262 float64 s;
263 double h;
264 } union_float64;
266 typedef bool (*f32_check_fn)(union_float32 a, union_float32 b);
267 typedef bool (*f64_check_fn)(union_float64 a, union_float64 b);
269 typedef float32 (*soft_f32_op2_fn)(float32 a, float32 b, float_status *s);
270 typedef float64 (*soft_f64_op2_fn)(float64 a, float64 b, float_status *s);
271 typedef float (*hard_f32_op2_fn)(float a, float b);
272 typedef double (*hard_f64_op2_fn)(double a, double b);
274 /* 2-input is-zero-or-normal */
275 static inline bool f32_is_zon2(union_float32 a, union_float32 b)
277 if (QEMU_HARDFLOAT_2F32_USE_FP) {
279 * Not using a temp variable for consecutive fpclassify calls ends up
280 * generating faster code.
282 return (fpclassify(a.h) == FP_NORMAL || fpclassify(a.h) == FP_ZERO) &&
283 (fpclassify(b.h) == FP_NORMAL || fpclassify(b.h) == FP_ZERO);
285 return float32_is_zero_or_normal(a.s) &&
286 float32_is_zero_or_normal(b.s);
289 static inline bool f64_is_zon2(union_float64 a, union_float64 b)
291 if (QEMU_HARDFLOAT_2F64_USE_FP) {
292 return (fpclassify(a.h) == FP_NORMAL || fpclassify(a.h) == FP_ZERO) &&
293 (fpclassify(b.h) == FP_NORMAL || fpclassify(b.h) == FP_ZERO);
295 return float64_is_zero_or_normal(a.s) &&
296 float64_is_zero_or_normal(b.s);
299 /* 3-input is-zero-or-normal */
300 static inline
301 bool f32_is_zon3(union_float32 a, union_float32 b, union_float32 c)
303 if (QEMU_HARDFLOAT_3F32_USE_FP) {
304 return (fpclassify(a.h) == FP_NORMAL || fpclassify(a.h) == FP_ZERO) &&
305 (fpclassify(b.h) == FP_NORMAL || fpclassify(b.h) == FP_ZERO) &&
306 (fpclassify(c.h) == FP_NORMAL || fpclassify(c.h) == FP_ZERO);
308 return float32_is_zero_or_normal(a.s) &&
309 float32_is_zero_or_normal(b.s) &&
310 float32_is_zero_or_normal(c.s);
313 static inline
314 bool f64_is_zon3(union_float64 a, union_float64 b, union_float64 c)
316 if (QEMU_HARDFLOAT_3F64_USE_FP) {
317 return (fpclassify(a.h) == FP_NORMAL || fpclassify(a.h) == FP_ZERO) &&
318 (fpclassify(b.h) == FP_NORMAL || fpclassify(b.h) == FP_ZERO) &&
319 (fpclassify(c.h) == FP_NORMAL || fpclassify(c.h) == FP_ZERO);
321 return float64_is_zero_or_normal(a.s) &&
322 float64_is_zero_or_normal(b.s) &&
323 float64_is_zero_or_normal(c.s);
326 static inline bool f32_is_inf(union_float32 a)
328 if (QEMU_HARDFLOAT_USE_ISINF) {
329 return isinf(a.h);
331 return float32_is_infinity(a.s);
334 static inline bool f64_is_inf(union_float64 a)
336 if (QEMU_HARDFLOAT_USE_ISINF) {
337 return isinf(a.h);
339 return float64_is_infinity(a.s);
342 static inline float32
343 float32_gen2(float32 xa, float32 xb, float_status *s,
344 hard_f32_op2_fn hard, soft_f32_op2_fn soft,
345 f32_check_fn pre, f32_check_fn post)
347 union_float32 ua, ub, ur;
349 ua.s = xa;
350 ub.s = xb;
352 if (unlikely(!can_use_fpu(s))) {
353 goto soft;
356 float32_input_flush2(&ua.s, &ub.s, s);
357 if (unlikely(!pre(ua, ub))) {
358 goto soft;
361 ur.h = hard(ua.h, ub.h);
362 if (unlikely(f32_is_inf(ur))) {
363 float_raise(float_flag_overflow, s);
364 } else if (unlikely(fabsf(ur.h) <= FLT_MIN) && post(ua, ub)) {
365 goto soft;
367 return ur.s;
369 soft:
370 return soft(ua.s, ub.s, s);
373 static inline float64
374 float64_gen2(float64 xa, float64 xb, float_status *s,
375 hard_f64_op2_fn hard, soft_f64_op2_fn soft,
376 f64_check_fn pre, f64_check_fn post)
378 union_float64 ua, ub, ur;
380 ua.s = xa;
381 ub.s = xb;
383 if (unlikely(!can_use_fpu(s))) {
384 goto soft;
387 float64_input_flush2(&ua.s, &ub.s, s);
388 if (unlikely(!pre(ua, ub))) {
389 goto soft;
392 ur.h = hard(ua.h, ub.h);
393 if (unlikely(f64_is_inf(ur))) {
394 float_raise(float_flag_overflow, s);
395 } else if (unlikely(fabs(ur.h) <= DBL_MIN) && post(ua, ub)) {
396 goto soft;
398 return ur.s;
400 soft:
401 return soft(ua.s, ub.s, s);
405 * Classify a floating point number. Everything above float_class_qnan
406 * is a NaN so cls >= float_class_qnan is any NaN.
409 typedef enum __attribute__ ((__packed__)) {
410 float_class_unclassified,
411 float_class_zero,
412 float_class_normal,
413 float_class_inf,
414 float_class_qnan, /* all NaNs from here */
415 float_class_snan,
416 } FloatClass;
418 #define float_cmask(bit) (1u << (bit))
420 enum {
421 float_cmask_zero = float_cmask(float_class_zero),
422 float_cmask_normal = float_cmask(float_class_normal),
423 float_cmask_inf = float_cmask(float_class_inf),
424 float_cmask_qnan = float_cmask(float_class_qnan),
425 float_cmask_snan = float_cmask(float_class_snan),
427 float_cmask_infzero = float_cmask_zero | float_cmask_inf,
428 float_cmask_anynan = float_cmask_qnan | float_cmask_snan,
431 /* Flags for parts_minmax. */
432 enum {
433 /* Set for minimum; clear for maximum. */
434 minmax_ismin = 1,
435 /* Set for the IEEE 754-2008 minNum() and maxNum() operations. */
436 minmax_isnum = 2,
437 /* Set for the IEEE 754-2008 minNumMag() and minNumMag() operations. */
438 minmax_ismag = 4,
440 * Set for the IEEE 754-2019 minimumNumber() and maximumNumber()
441 * operations.
443 minmax_isnumber = 8,
446 /* Simple helpers for checking if, or what kind of, NaN we have */
447 static inline __attribute__((unused)) bool is_nan(FloatClass c)
449 return unlikely(c >= float_class_qnan);
452 static inline __attribute__((unused)) bool is_snan(FloatClass c)
454 return c == float_class_snan;
457 static inline __attribute__((unused)) bool is_qnan(FloatClass c)
459 return c == float_class_qnan;
463 * Structure holding all of the decomposed parts of a float.
464 * The exponent is unbiased and the fraction is normalized.
466 * The fraction words are stored in big-endian word ordering,
467 * so that truncation from a larger format to a smaller format
468 * can be done simply by ignoring subsequent elements.
471 typedef struct {
472 FloatClass cls;
473 bool sign;
474 int32_t exp;
475 union {
476 /* Routines that know the structure may reference the singular name. */
477 uint64_t frac;
479 * Routines expanded with multiple structures reference "hi" and "lo"
480 * depending on the operation. In FloatParts64, "hi" and "lo" are
481 * both the same word and aliased here.
483 uint64_t frac_hi;
484 uint64_t frac_lo;
486 } FloatParts64;
488 typedef struct {
489 FloatClass cls;
490 bool sign;
491 int32_t exp;
492 uint64_t frac_hi;
493 uint64_t frac_lo;
494 } FloatParts128;
496 typedef struct {
497 FloatClass cls;
498 bool sign;
499 int32_t exp;
500 uint64_t frac_hi;
501 uint64_t frac_hm; /* high-middle */
502 uint64_t frac_lm; /* low-middle */
503 uint64_t frac_lo;
504 } FloatParts256;
506 /* These apply to the most significant word of each FloatPartsN. */
507 #define DECOMPOSED_BINARY_POINT 63
508 #define DECOMPOSED_IMPLICIT_BIT (1ull << DECOMPOSED_BINARY_POINT)
510 /* Structure holding all of the relevant parameters for a format.
511 * exp_size: the size of the exponent field
512 * exp_bias: the offset applied to the exponent field
513 * exp_max: the maximum normalised exponent
514 * frac_size: the size of the fraction field
515 * frac_shift: shift to normalise the fraction with DECOMPOSED_BINARY_POINT
516 * The following are computed based the size of fraction
517 * round_mask: bits below lsb which must be rounded
518 * The following optional modifiers are available:
519 * arm_althp: handle ARM Alternative Half Precision
521 typedef struct {
522 int exp_size;
523 int exp_bias;
524 int exp_re_bias;
525 int exp_max;
526 int frac_size;
527 int frac_shift;
528 bool arm_althp;
529 uint64_t round_mask;
530 } FloatFmt;
532 /* Expand fields based on the size of exponent and fraction */
533 #define FLOAT_PARAMS_(E) \
534 .exp_size = E, \
535 .exp_bias = ((1 << E) - 1) >> 1, \
536 .exp_re_bias = (1 << (E - 1)) + (1 << (E - 2)), \
537 .exp_max = (1 << E) - 1
539 #define FLOAT_PARAMS(E, F) \
540 FLOAT_PARAMS_(E), \
541 .frac_size = F, \
542 .frac_shift = (-F - 1) & 63, \
543 .round_mask = (1ull << ((-F - 1) & 63)) - 1
545 static const FloatFmt float16_params = {
546 FLOAT_PARAMS(5, 10)
549 static const FloatFmt float16_params_ahp = {
550 FLOAT_PARAMS(5, 10),
551 .arm_althp = true
554 static const FloatFmt bfloat16_params = {
555 FLOAT_PARAMS(8, 7)
558 static const FloatFmt float32_params = {
559 FLOAT_PARAMS(8, 23)
562 static const FloatFmt float64_params = {
563 FLOAT_PARAMS(11, 52)
566 static const FloatFmt float128_params = {
567 FLOAT_PARAMS(15, 112)
570 #define FLOATX80_PARAMS(R) \
571 FLOAT_PARAMS_(15), \
572 .frac_size = R == 64 ? 63 : R, \
573 .frac_shift = 0, \
574 .round_mask = R == 64 ? -1 : (1ull << ((-R - 1) & 63)) - 1
576 static const FloatFmt floatx80_params[3] = {
577 [floatx80_precision_s] = { FLOATX80_PARAMS(23) },
578 [floatx80_precision_d] = { FLOATX80_PARAMS(52) },
579 [floatx80_precision_x] = { FLOATX80_PARAMS(64) },
582 /* Unpack a float to parts, but do not canonicalize. */
583 static void unpack_raw64(FloatParts64 *r, const FloatFmt *fmt, uint64_t raw)
585 const int f_size = fmt->frac_size;
586 const int e_size = fmt->exp_size;
588 *r = (FloatParts64) {
589 .cls = float_class_unclassified,
590 .sign = extract64(raw, f_size + e_size, 1),
591 .exp = extract64(raw, f_size, e_size),
592 .frac = extract64(raw, 0, f_size)
596 static void QEMU_FLATTEN float16_unpack_raw(FloatParts64 *p, float16 f)
598 unpack_raw64(p, &float16_params, f);
601 static void QEMU_FLATTEN bfloat16_unpack_raw(FloatParts64 *p, bfloat16 f)
603 unpack_raw64(p, &bfloat16_params, f);
606 static void QEMU_FLATTEN float32_unpack_raw(FloatParts64 *p, float32 f)
608 unpack_raw64(p, &float32_params, f);
611 static void QEMU_FLATTEN float64_unpack_raw(FloatParts64 *p, float64 f)
613 unpack_raw64(p, &float64_params, f);
616 static void QEMU_FLATTEN floatx80_unpack_raw(FloatParts128 *p, floatx80 f)
618 *p = (FloatParts128) {
619 .cls = float_class_unclassified,
620 .sign = extract32(f.high, 15, 1),
621 .exp = extract32(f.high, 0, 15),
622 .frac_hi = f.low
626 static void QEMU_FLATTEN float128_unpack_raw(FloatParts128 *p, float128 f)
628 const int f_size = float128_params.frac_size - 64;
629 const int e_size = float128_params.exp_size;
631 *p = (FloatParts128) {
632 .cls = float_class_unclassified,
633 .sign = extract64(f.high, f_size + e_size, 1),
634 .exp = extract64(f.high, f_size, e_size),
635 .frac_hi = extract64(f.high, 0, f_size),
636 .frac_lo = f.low,
640 /* Pack a float from parts, but do not canonicalize. */
641 static uint64_t pack_raw64(const FloatParts64 *p, const FloatFmt *fmt)
643 const int f_size = fmt->frac_size;
644 const int e_size = fmt->exp_size;
645 uint64_t ret;
647 ret = (uint64_t)p->sign << (f_size + e_size);
648 ret = deposit64(ret, f_size, e_size, p->exp);
649 ret = deposit64(ret, 0, f_size, p->frac);
650 return ret;
653 static float16 QEMU_FLATTEN float16_pack_raw(const FloatParts64 *p)
655 return make_float16(pack_raw64(p, &float16_params));
658 static bfloat16 QEMU_FLATTEN bfloat16_pack_raw(const FloatParts64 *p)
660 return pack_raw64(p, &bfloat16_params);
663 static float32 QEMU_FLATTEN float32_pack_raw(const FloatParts64 *p)
665 return make_float32(pack_raw64(p, &float32_params));
668 static float64 QEMU_FLATTEN float64_pack_raw(const FloatParts64 *p)
670 return make_float64(pack_raw64(p, &float64_params));
673 static float128 QEMU_FLATTEN float128_pack_raw(const FloatParts128 *p)
675 const int f_size = float128_params.frac_size - 64;
676 const int e_size = float128_params.exp_size;
677 uint64_t hi;
679 hi = (uint64_t)p->sign << (f_size + e_size);
680 hi = deposit64(hi, f_size, e_size, p->exp);
681 hi = deposit64(hi, 0, f_size, p->frac_hi);
682 return make_float128(hi, p->frac_lo);
685 /*----------------------------------------------------------------------------
686 | Functions and definitions to determine: (1) whether tininess for underflow
687 | is detected before or after rounding by default, (2) what (if anything)
688 | happens when exceptions are raised, (3) how signaling NaNs are distinguished
689 | from quiet NaNs, (4) the default generated quiet NaNs, and (5) how NaNs
690 | are propagated from function inputs to output. These details are target-
691 | specific.
692 *----------------------------------------------------------------------------*/
693 #include "softfloat-specialize.c.inc"
695 #define PARTS_GENERIC_64_128(NAME, P) \
696 _Generic((P), FloatParts64 *: parts64_##NAME, \
697 FloatParts128 *: parts128_##NAME)
699 #define PARTS_GENERIC_64_128_256(NAME, P) \
700 _Generic((P), FloatParts64 *: parts64_##NAME, \
701 FloatParts128 *: parts128_##NAME, \
702 FloatParts256 *: parts256_##NAME)
704 #define parts_default_nan(P, S) PARTS_GENERIC_64_128(default_nan, P)(P, S)
705 #define parts_silence_nan(P, S) PARTS_GENERIC_64_128(silence_nan, P)(P, S)
707 static void parts64_return_nan(FloatParts64 *a, float_status *s);
708 static void parts128_return_nan(FloatParts128 *a, float_status *s);
710 #define parts_return_nan(P, S) PARTS_GENERIC_64_128(return_nan, P)(P, S)
712 static FloatParts64 *parts64_pick_nan(FloatParts64 *a, FloatParts64 *b,
713 float_status *s);
714 static FloatParts128 *parts128_pick_nan(FloatParts128 *a, FloatParts128 *b,
715 float_status *s);
717 #define parts_pick_nan(A, B, S) PARTS_GENERIC_64_128(pick_nan, A)(A, B, S)
719 static FloatParts64 *parts64_pick_nan_muladd(FloatParts64 *a, FloatParts64 *b,
720 FloatParts64 *c, float_status *s,
721 int ab_mask, int abc_mask);
722 static FloatParts128 *parts128_pick_nan_muladd(FloatParts128 *a,
723 FloatParts128 *b,
724 FloatParts128 *c,
725 float_status *s,
726 int ab_mask, int abc_mask);
728 #define parts_pick_nan_muladd(A, B, C, S, ABM, ABCM) \
729 PARTS_GENERIC_64_128(pick_nan_muladd, A)(A, B, C, S, ABM, ABCM)
731 static void parts64_canonicalize(FloatParts64 *p, float_status *status,
732 const FloatFmt *fmt);
733 static void parts128_canonicalize(FloatParts128 *p, float_status *status,
734 const FloatFmt *fmt);
736 #define parts_canonicalize(A, S, F) \
737 PARTS_GENERIC_64_128(canonicalize, A)(A, S, F)
739 static void parts64_uncanon_normal(FloatParts64 *p, float_status *status,
740 const FloatFmt *fmt);
741 static void parts128_uncanon_normal(FloatParts128 *p, float_status *status,
742 const FloatFmt *fmt);
744 #define parts_uncanon_normal(A, S, F) \
745 PARTS_GENERIC_64_128(uncanon_normal, A)(A, S, F)
747 static void parts64_uncanon(FloatParts64 *p, float_status *status,
748 const FloatFmt *fmt);
749 static void parts128_uncanon(FloatParts128 *p, float_status *status,
750 const FloatFmt *fmt);
752 #define parts_uncanon(A, S, F) \
753 PARTS_GENERIC_64_128(uncanon, A)(A, S, F)
755 static void parts64_add_normal(FloatParts64 *a, FloatParts64 *b);
756 static void parts128_add_normal(FloatParts128 *a, FloatParts128 *b);
757 static void parts256_add_normal(FloatParts256 *a, FloatParts256 *b);
759 #define parts_add_normal(A, B) \
760 PARTS_GENERIC_64_128_256(add_normal, A)(A, B)
762 static bool parts64_sub_normal(FloatParts64 *a, FloatParts64 *b);
763 static bool parts128_sub_normal(FloatParts128 *a, FloatParts128 *b);
764 static bool parts256_sub_normal(FloatParts256 *a, FloatParts256 *b);
766 #define parts_sub_normal(A, B) \
767 PARTS_GENERIC_64_128_256(sub_normal, A)(A, B)
769 static FloatParts64 *parts64_addsub(FloatParts64 *a, FloatParts64 *b,
770 float_status *s, bool subtract);
771 static FloatParts128 *parts128_addsub(FloatParts128 *a, FloatParts128 *b,
772 float_status *s, bool subtract);
774 #define parts_addsub(A, B, S, Z) \
775 PARTS_GENERIC_64_128(addsub, A)(A, B, S, Z)
777 static FloatParts64 *parts64_mul(FloatParts64 *a, FloatParts64 *b,
778 float_status *s);
779 static FloatParts128 *parts128_mul(FloatParts128 *a, FloatParts128 *b,
780 float_status *s);
782 #define parts_mul(A, B, S) \
783 PARTS_GENERIC_64_128(mul, A)(A, B, S)
785 static FloatParts64 *parts64_muladd(FloatParts64 *a, FloatParts64 *b,
786 FloatParts64 *c, int flags,
787 float_status *s);
788 static FloatParts128 *parts128_muladd(FloatParts128 *a, FloatParts128 *b,
789 FloatParts128 *c, int flags,
790 float_status *s);
792 #define parts_muladd(A, B, C, Z, S) \
793 PARTS_GENERIC_64_128(muladd, A)(A, B, C, Z, S)
795 static FloatParts64 *parts64_div(FloatParts64 *a, FloatParts64 *b,
796 float_status *s);
797 static FloatParts128 *parts128_div(FloatParts128 *a, FloatParts128 *b,
798 float_status *s);
800 #define parts_div(A, B, S) \
801 PARTS_GENERIC_64_128(div, A)(A, B, S)
803 static FloatParts64 *parts64_modrem(FloatParts64 *a, FloatParts64 *b,
804 uint64_t *mod_quot, float_status *s);
805 static FloatParts128 *parts128_modrem(FloatParts128 *a, FloatParts128 *b,
806 uint64_t *mod_quot, float_status *s);
808 #define parts_modrem(A, B, Q, S) \
809 PARTS_GENERIC_64_128(modrem, A)(A, B, Q, S)
811 static void parts64_sqrt(FloatParts64 *a, float_status *s, const FloatFmt *f);
812 static void parts128_sqrt(FloatParts128 *a, float_status *s, const FloatFmt *f);
814 #define parts_sqrt(A, S, F) \
815 PARTS_GENERIC_64_128(sqrt, A)(A, S, F)
817 static bool parts64_round_to_int_normal(FloatParts64 *a, FloatRoundMode rm,
818 int scale, int frac_size);
819 static bool parts128_round_to_int_normal(FloatParts128 *a, FloatRoundMode r,
820 int scale, int frac_size);
822 #define parts_round_to_int_normal(A, R, C, F) \
823 PARTS_GENERIC_64_128(round_to_int_normal, A)(A, R, C, F)
825 static void parts64_round_to_int(FloatParts64 *a, FloatRoundMode rm,
826 int scale, float_status *s,
827 const FloatFmt *fmt);
828 static void parts128_round_to_int(FloatParts128 *a, FloatRoundMode r,
829 int scale, float_status *s,
830 const FloatFmt *fmt);
832 #define parts_round_to_int(A, R, C, S, F) \
833 PARTS_GENERIC_64_128(round_to_int, A)(A, R, C, S, F)
835 static int64_t parts64_float_to_sint(FloatParts64 *p, FloatRoundMode rmode,
836 int scale, int64_t min, int64_t max,
837 float_status *s);
838 static int64_t parts128_float_to_sint(FloatParts128 *p, FloatRoundMode rmode,
839 int scale, int64_t min, int64_t max,
840 float_status *s);
842 #define parts_float_to_sint(P, R, Z, MN, MX, S) \
843 PARTS_GENERIC_64_128(float_to_sint, P)(P, R, Z, MN, MX, S)
845 static uint64_t parts64_float_to_uint(FloatParts64 *p, FloatRoundMode rmode,
846 int scale, uint64_t max,
847 float_status *s);
848 static uint64_t parts128_float_to_uint(FloatParts128 *p, FloatRoundMode rmode,
849 int scale, uint64_t max,
850 float_status *s);
852 #define parts_float_to_uint(P, R, Z, M, S) \
853 PARTS_GENERIC_64_128(float_to_uint, P)(P, R, Z, M, S)
855 static int64_t parts64_float_to_sint_modulo(FloatParts64 *p,
856 FloatRoundMode rmode,
857 int bitsm1, float_status *s);
858 static int64_t parts128_float_to_sint_modulo(FloatParts128 *p,
859 FloatRoundMode rmode,
860 int bitsm1, float_status *s);
862 #define parts_float_to_sint_modulo(P, R, M, S) \
863 PARTS_GENERIC_64_128(float_to_sint_modulo, P)(P, R, M, S)
865 static void parts64_sint_to_float(FloatParts64 *p, int64_t a,
866 int scale, float_status *s);
867 static void parts128_sint_to_float(FloatParts128 *p, int64_t a,
868 int scale, float_status *s);
870 #define parts_float_to_sint(P, R, Z, MN, MX, S) \
871 PARTS_GENERIC_64_128(float_to_sint, P)(P, R, Z, MN, MX, S)
873 #define parts_sint_to_float(P, I, Z, S) \
874 PARTS_GENERIC_64_128(sint_to_float, P)(P, I, Z, S)
876 static void parts64_uint_to_float(FloatParts64 *p, uint64_t a,
877 int scale, float_status *s);
878 static void parts128_uint_to_float(FloatParts128 *p, uint64_t a,
879 int scale, float_status *s);
881 #define parts_uint_to_float(P, I, Z, S) \
882 PARTS_GENERIC_64_128(uint_to_float, P)(P, I, Z, S)
884 static FloatParts64 *parts64_minmax(FloatParts64 *a, FloatParts64 *b,
885 float_status *s, int flags);
886 static FloatParts128 *parts128_minmax(FloatParts128 *a, FloatParts128 *b,
887 float_status *s, int flags);
889 #define parts_minmax(A, B, S, F) \
890 PARTS_GENERIC_64_128(minmax, A)(A, B, S, F)
892 static FloatRelation parts64_compare(FloatParts64 *a, FloatParts64 *b,
893 float_status *s, bool q);
894 static FloatRelation parts128_compare(FloatParts128 *a, FloatParts128 *b,
895 float_status *s, bool q);
897 #define parts_compare(A, B, S, Q) \
898 PARTS_GENERIC_64_128(compare, A)(A, B, S, Q)
900 static void parts64_scalbn(FloatParts64 *a, int n, float_status *s);
901 static void parts128_scalbn(FloatParts128 *a, int n, float_status *s);
903 #define parts_scalbn(A, N, S) \
904 PARTS_GENERIC_64_128(scalbn, A)(A, N, S)
906 static void parts64_log2(FloatParts64 *a, float_status *s, const FloatFmt *f);
907 static void parts128_log2(FloatParts128 *a, float_status *s, const FloatFmt *f);
909 #define parts_log2(A, S, F) \
910 PARTS_GENERIC_64_128(log2, A)(A, S, F)
913 * Helper functions for softfloat-parts.c.inc, per-size operations.
916 #define FRAC_GENERIC_64_128(NAME, P) \
917 _Generic((P), FloatParts64 *: frac64_##NAME, \
918 FloatParts128 *: frac128_##NAME)
920 #define FRAC_GENERIC_64_128_256(NAME, P) \
921 _Generic((P), FloatParts64 *: frac64_##NAME, \
922 FloatParts128 *: frac128_##NAME, \
923 FloatParts256 *: frac256_##NAME)
925 static bool frac64_add(FloatParts64 *r, FloatParts64 *a, FloatParts64 *b)
927 return uadd64_overflow(a->frac, b->frac, &r->frac);
930 static bool frac128_add(FloatParts128 *r, FloatParts128 *a, FloatParts128 *b)
932 bool c = 0;
933 r->frac_lo = uadd64_carry(a->frac_lo, b->frac_lo, &c);
934 r->frac_hi = uadd64_carry(a->frac_hi, b->frac_hi, &c);
935 return c;
938 static bool frac256_add(FloatParts256 *r, FloatParts256 *a, FloatParts256 *b)
940 bool c = 0;
941 r->frac_lo = uadd64_carry(a->frac_lo, b->frac_lo, &c);
942 r->frac_lm = uadd64_carry(a->frac_lm, b->frac_lm, &c);
943 r->frac_hm = uadd64_carry(a->frac_hm, b->frac_hm, &c);
944 r->frac_hi = uadd64_carry(a->frac_hi, b->frac_hi, &c);
945 return c;
948 #define frac_add(R, A, B) FRAC_GENERIC_64_128_256(add, R)(R, A, B)
950 static bool frac64_addi(FloatParts64 *r, FloatParts64 *a, uint64_t c)
952 return uadd64_overflow(a->frac, c, &r->frac);
955 static bool frac128_addi(FloatParts128 *r, FloatParts128 *a, uint64_t c)
957 c = uadd64_overflow(a->frac_lo, c, &r->frac_lo);
958 return uadd64_overflow(a->frac_hi, c, &r->frac_hi);
961 #define frac_addi(R, A, C) FRAC_GENERIC_64_128(addi, R)(R, A, C)
963 static void frac64_allones(FloatParts64 *a)
965 a->frac = -1;
968 static void frac128_allones(FloatParts128 *a)
970 a->frac_hi = a->frac_lo = -1;
973 #define frac_allones(A) FRAC_GENERIC_64_128(allones, A)(A)
975 static FloatRelation frac64_cmp(FloatParts64 *a, FloatParts64 *b)
977 return (a->frac == b->frac ? float_relation_equal
978 : a->frac < b->frac ? float_relation_less
979 : float_relation_greater);
982 static FloatRelation frac128_cmp(FloatParts128 *a, FloatParts128 *b)
984 uint64_t ta = a->frac_hi, tb = b->frac_hi;
985 if (ta == tb) {
986 ta = a->frac_lo, tb = b->frac_lo;
987 if (ta == tb) {
988 return float_relation_equal;
991 return ta < tb ? float_relation_less : float_relation_greater;
994 #define frac_cmp(A, B) FRAC_GENERIC_64_128(cmp, A)(A, B)
996 static void frac64_clear(FloatParts64 *a)
998 a->frac = 0;
1001 static void frac128_clear(FloatParts128 *a)
1003 a->frac_hi = a->frac_lo = 0;
1006 #define frac_clear(A) FRAC_GENERIC_64_128(clear, A)(A)
1008 static bool frac64_div(FloatParts64 *a, FloatParts64 *b)
1010 uint64_t n1, n0, r, q;
1011 bool ret;
1014 * We want a 2*N / N-bit division to produce exactly an N-bit
1015 * result, so that we do not lose any precision and so that we
1016 * do not have to renormalize afterward. If A.frac < B.frac,
1017 * then division would produce an (N-1)-bit result; shift A left
1018 * by one to produce the an N-bit result, and return true to
1019 * decrement the exponent to match.
1021 * The udiv_qrnnd algorithm that we're using requires normalization,
1022 * i.e. the msb of the denominator must be set, which is already true.
1024 ret = a->frac < b->frac;
1025 if (ret) {
1026 n0 = a->frac;
1027 n1 = 0;
1028 } else {
1029 n0 = a->frac >> 1;
1030 n1 = a->frac << 63;
1032 q = udiv_qrnnd(&r, n0, n1, b->frac);
1034 /* Set lsb if there is a remainder, to set inexact. */
1035 a->frac = q | (r != 0);
1037 return ret;
1040 static bool frac128_div(FloatParts128 *a, FloatParts128 *b)
1042 uint64_t q0, q1, a0, a1, b0, b1;
1043 uint64_t r0, r1, r2, r3, t0, t1, t2, t3;
1044 bool ret = false;
1046 a0 = a->frac_hi, a1 = a->frac_lo;
1047 b0 = b->frac_hi, b1 = b->frac_lo;
1049 ret = lt128(a0, a1, b0, b1);
1050 if (!ret) {
1051 a1 = shr_double(a0, a1, 1);
1052 a0 = a0 >> 1;
1055 /* Use 128/64 -> 64 division as estimate for 192/128 -> 128 division. */
1056 q0 = estimateDiv128To64(a0, a1, b0);
1059 * Estimate is high because B1 was not included (unless B1 == 0).
1060 * Reduce quotient and increase remainder until remainder is non-negative.
1061 * This loop will execute 0 to 2 times.
1063 mul128By64To192(b0, b1, q0, &t0, &t1, &t2);
1064 sub192(a0, a1, 0, t0, t1, t2, &r0, &r1, &r2);
1065 while (r0 != 0) {
1066 q0--;
1067 add192(r0, r1, r2, 0, b0, b1, &r0, &r1, &r2);
1070 /* Repeat using the remainder, producing a second word of quotient. */
1071 q1 = estimateDiv128To64(r1, r2, b0);
1072 mul128By64To192(b0, b1, q1, &t1, &t2, &t3);
1073 sub192(r1, r2, 0, t1, t2, t3, &r1, &r2, &r3);
1074 while (r1 != 0) {
1075 q1--;
1076 add192(r1, r2, r3, 0, b0, b1, &r1, &r2, &r3);
1079 /* Any remainder indicates inexact; set sticky bit. */
1080 q1 |= (r2 | r3) != 0;
1082 a->frac_hi = q0;
1083 a->frac_lo = q1;
1084 return ret;
1087 #define frac_div(A, B) FRAC_GENERIC_64_128(div, A)(A, B)
1089 static bool frac64_eqz(FloatParts64 *a)
1091 return a->frac == 0;
1094 static bool frac128_eqz(FloatParts128 *a)
1096 return (a->frac_hi | a->frac_lo) == 0;
1099 #define frac_eqz(A) FRAC_GENERIC_64_128(eqz, A)(A)
1101 static void frac64_mulw(FloatParts128 *r, FloatParts64 *a, FloatParts64 *b)
1103 mulu64(&r->frac_lo, &r->frac_hi, a->frac, b->frac);
1106 static void frac128_mulw(FloatParts256 *r, FloatParts128 *a, FloatParts128 *b)
1108 mul128To256(a->frac_hi, a->frac_lo, b->frac_hi, b->frac_lo,
1109 &r->frac_hi, &r->frac_hm, &r->frac_lm, &r->frac_lo);
1112 #define frac_mulw(R, A, B) FRAC_GENERIC_64_128(mulw, A)(R, A, B)
1114 static void frac64_neg(FloatParts64 *a)
1116 a->frac = -a->frac;
1119 static void frac128_neg(FloatParts128 *a)
1121 bool c = 0;
1122 a->frac_lo = usub64_borrow(0, a->frac_lo, &c);
1123 a->frac_hi = usub64_borrow(0, a->frac_hi, &c);
1126 static void frac256_neg(FloatParts256 *a)
1128 bool c = 0;
1129 a->frac_lo = usub64_borrow(0, a->frac_lo, &c);
1130 a->frac_lm = usub64_borrow(0, a->frac_lm, &c);
1131 a->frac_hm = usub64_borrow(0, a->frac_hm, &c);
1132 a->frac_hi = usub64_borrow(0, a->frac_hi, &c);
1135 #define frac_neg(A) FRAC_GENERIC_64_128_256(neg, A)(A)
1137 static int frac64_normalize(FloatParts64 *a)
1139 if (a->frac) {
1140 int shift = clz64(a->frac);
1141 a->frac <<= shift;
1142 return shift;
1144 return 64;
1147 static int frac128_normalize(FloatParts128 *a)
1149 if (a->frac_hi) {
1150 int shl = clz64(a->frac_hi);
1151 a->frac_hi = shl_double(a->frac_hi, a->frac_lo, shl);
1152 a->frac_lo <<= shl;
1153 return shl;
1154 } else if (a->frac_lo) {
1155 int shl = clz64(a->frac_lo);
1156 a->frac_hi = a->frac_lo << shl;
1157 a->frac_lo = 0;
1158 return shl + 64;
1160 return 128;
1163 static int frac256_normalize(FloatParts256 *a)
1165 uint64_t a0 = a->frac_hi, a1 = a->frac_hm;
1166 uint64_t a2 = a->frac_lm, a3 = a->frac_lo;
1167 int ret, shl;
1169 if (likely(a0)) {
1170 shl = clz64(a0);
1171 if (shl == 0) {
1172 return 0;
1174 ret = shl;
1175 } else {
1176 if (a1) {
1177 ret = 64;
1178 a0 = a1, a1 = a2, a2 = a3, a3 = 0;
1179 } else if (a2) {
1180 ret = 128;
1181 a0 = a2, a1 = a3, a2 = 0, a3 = 0;
1182 } else if (a3) {
1183 ret = 192;
1184 a0 = a3, a1 = 0, a2 = 0, a3 = 0;
1185 } else {
1186 ret = 256;
1187 a0 = 0, a1 = 0, a2 = 0, a3 = 0;
1188 goto done;
1190 shl = clz64(a0);
1191 if (shl == 0) {
1192 goto done;
1194 ret += shl;
1197 a0 = shl_double(a0, a1, shl);
1198 a1 = shl_double(a1, a2, shl);
1199 a2 = shl_double(a2, a3, shl);
1200 a3 <<= shl;
1202 done:
1203 a->frac_hi = a0;
1204 a->frac_hm = a1;
1205 a->frac_lm = a2;
1206 a->frac_lo = a3;
1207 return ret;
1210 #define frac_normalize(A) FRAC_GENERIC_64_128_256(normalize, A)(A)
1212 static void frac64_modrem(FloatParts64 *a, FloatParts64 *b, uint64_t *mod_quot)
1214 uint64_t a0, a1, b0, t0, t1, q, quot;
1215 int exp_diff = a->exp - b->exp;
1216 int shift;
1218 a0 = a->frac;
1219 a1 = 0;
1221 if (exp_diff < -1) {
1222 if (mod_quot) {
1223 *mod_quot = 0;
1225 return;
1227 if (exp_diff == -1) {
1228 a0 >>= 1;
1229 exp_diff = 0;
1232 b0 = b->frac;
1233 quot = q = b0 <= a0;
1234 if (q) {
1235 a0 -= b0;
1238 exp_diff -= 64;
1239 while (exp_diff > 0) {
1240 q = estimateDiv128To64(a0, a1, b0);
1241 q = q > 2 ? q - 2 : 0;
1242 mul64To128(b0, q, &t0, &t1);
1243 sub128(a0, a1, t0, t1, &a0, &a1);
1244 shortShift128Left(a0, a1, 62, &a0, &a1);
1245 exp_diff -= 62;
1246 quot = (quot << 62) + q;
1249 exp_diff += 64;
1250 if (exp_diff > 0) {
1251 q = estimateDiv128To64(a0, a1, b0);
1252 q = q > 2 ? (q - 2) >> (64 - exp_diff) : 0;
1253 mul64To128(b0, q << (64 - exp_diff), &t0, &t1);
1254 sub128(a0, a1, t0, t1, &a0, &a1);
1255 shortShift128Left(0, b0, 64 - exp_diff, &t0, &t1);
1256 while (le128(t0, t1, a0, a1)) {
1257 ++q;
1258 sub128(a0, a1, t0, t1, &a0, &a1);
1260 quot = (exp_diff < 64 ? quot << exp_diff : 0) + q;
1261 } else {
1262 t0 = b0;
1263 t1 = 0;
1266 if (mod_quot) {
1267 *mod_quot = quot;
1268 } else {
1269 sub128(t0, t1, a0, a1, &t0, &t1);
1270 if (lt128(t0, t1, a0, a1) ||
1271 (eq128(t0, t1, a0, a1) && (q & 1))) {
1272 a0 = t0;
1273 a1 = t1;
1274 a->sign = !a->sign;
1278 if (likely(a0)) {
1279 shift = clz64(a0);
1280 shortShift128Left(a0, a1, shift, &a0, &a1);
1281 } else if (likely(a1)) {
1282 shift = clz64(a1);
1283 a0 = a1 << shift;
1284 a1 = 0;
1285 shift += 64;
1286 } else {
1287 a->cls = float_class_zero;
1288 return;
1291 a->exp = b->exp + exp_diff - shift;
1292 a->frac = a0 | (a1 != 0);
1295 static void frac128_modrem(FloatParts128 *a, FloatParts128 *b,
1296 uint64_t *mod_quot)
1298 uint64_t a0, a1, a2, b0, b1, t0, t1, t2, q, quot;
1299 int exp_diff = a->exp - b->exp;
1300 int shift;
1302 a0 = a->frac_hi;
1303 a1 = a->frac_lo;
1304 a2 = 0;
1306 if (exp_diff < -1) {
1307 if (mod_quot) {
1308 *mod_quot = 0;
1310 return;
1312 if (exp_diff == -1) {
1313 shift128Right(a0, a1, 1, &a0, &a1);
1314 exp_diff = 0;
1317 b0 = b->frac_hi;
1318 b1 = b->frac_lo;
1320 quot = q = le128(b0, b1, a0, a1);
1321 if (q) {
1322 sub128(a0, a1, b0, b1, &a0, &a1);
1325 exp_diff -= 64;
1326 while (exp_diff > 0) {
1327 q = estimateDiv128To64(a0, a1, b0);
1328 q = q > 4 ? q - 4 : 0;
1329 mul128By64To192(b0, b1, q, &t0, &t1, &t2);
1330 sub192(a0, a1, a2, t0, t1, t2, &a0, &a1, &a2);
1331 shortShift192Left(a0, a1, a2, 61, &a0, &a1, &a2);
1332 exp_diff -= 61;
1333 quot = (quot << 61) + q;
1336 exp_diff += 64;
1337 if (exp_diff > 0) {
1338 q = estimateDiv128To64(a0, a1, b0);
1339 q = q > 4 ? (q - 4) >> (64 - exp_diff) : 0;
1340 mul128By64To192(b0, b1, q << (64 - exp_diff), &t0, &t1, &t2);
1341 sub192(a0, a1, a2, t0, t1, t2, &a0, &a1, &a2);
1342 shortShift192Left(0, b0, b1, 64 - exp_diff, &t0, &t1, &t2);
1343 while (le192(t0, t1, t2, a0, a1, a2)) {
1344 ++q;
1345 sub192(a0, a1, a2, t0, t1, t2, &a0, &a1, &a2);
1347 quot = (exp_diff < 64 ? quot << exp_diff : 0) + q;
1348 } else {
1349 t0 = b0;
1350 t1 = b1;
1351 t2 = 0;
1354 if (mod_quot) {
1355 *mod_quot = quot;
1356 } else {
1357 sub192(t0, t1, t2, a0, a1, a2, &t0, &t1, &t2);
1358 if (lt192(t0, t1, t2, a0, a1, a2) ||
1359 (eq192(t0, t1, t2, a0, a1, a2) && (q & 1))) {
1360 a0 = t0;
1361 a1 = t1;
1362 a2 = t2;
1363 a->sign = !a->sign;
1367 if (likely(a0)) {
1368 shift = clz64(a0);
1369 shortShift192Left(a0, a1, a2, shift, &a0, &a1, &a2);
1370 } else if (likely(a1)) {
1371 shift = clz64(a1);
1372 shortShift128Left(a1, a2, shift, &a0, &a1);
1373 a2 = 0;
1374 shift += 64;
1375 } else if (likely(a2)) {
1376 shift = clz64(a2);
1377 a0 = a2 << shift;
1378 a1 = a2 = 0;
1379 shift += 128;
1380 } else {
1381 a->cls = float_class_zero;
1382 return;
1385 a->exp = b->exp + exp_diff - shift;
1386 a->frac_hi = a0;
1387 a->frac_lo = a1 | (a2 != 0);
1390 #define frac_modrem(A, B, Q) FRAC_GENERIC_64_128(modrem, A)(A, B, Q)
1392 static void frac64_shl(FloatParts64 *a, int c)
1394 a->frac <<= c;
1397 static void frac128_shl(FloatParts128 *a, int c)
1399 uint64_t a0 = a->frac_hi, a1 = a->frac_lo;
1401 if (c & 64) {
1402 a0 = a1, a1 = 0;
1405 c &= 63;
1406 if (c) {
1407 a0 = shl_double(a0, a1, c);
1408 a1 = a1 << c;
1411 a->frac_hi = a0;
1412 a->frac_lo = a1;
1415 #define frac_shl(A, C) FRAC_GENERIC_64_128(shl, A)(A, C)
1417 static void frac64_shr(FloatParts64 *a, int c)
1419 a->frac >>= c;
1422 static void frac128_shr(FloatParts128 *a, int c)
1424 uint64_t a0 = a->frac_hi, a1 = a->frac_lo;
1426 if (c & 64) {
1427 a1 = a0, a0 = 0;
1430 c &= 63;
1431 if (c) {
1432 a1 = shr_double(a0, a1, c);
1433 a0 = a0 >> c;
1436 a->frac_hi = a0;
1437 a->frac_lo = a1;
1440 #define frac_shr(A, C) FRAC_GENERIC_64_128(shr, A)(A, C)
1442 static void frac64_shrjam(FloatParts64 *a, int c)
1444 uint64_t a0 = a->frac;
1446 if (likely(c != 0)) {
1447 if (likely(c < 64)) {
1448 a0 = (a0 >> c) | (shr_double(a0, 0, c) != 0);
1449 } else {
1450 a0 = a0 != 0;
1452 a->frac = a0;
1456 static void frac128_shrjam(FloatParts128 *a, int c)
1458 uint64_t a0 = a->frac_hi, a1 = a->frac_lo;
1459 uint64_t sticky = 0;
1461 if (unlikely(c == 0)) {
1462 return;
1463 } else if (likely(c < 64)) {
1464 /* nothing */
1465 } else if (likely(c < 128)) {
1466 sticky = a1;
1467 a1 = a0;
1468 a0 = 0;
1469 c &= 63;
1470 if (c == 0) {
1471 goto done;
1473 } else {
1474 sticky = a0 | a1;
1475 a0 = a1 = 0;
1476 goto done;
1479 sticky |= shr_double(a1, 0, c);
1480 a1 = shr_double(a0, a1, c);
1481 a0 = a0 >> c;
1483 done:
1484 a->frac_lo = a1 | (sticky != 0);
1485 a->frac_hi = a0;
1488 static void frac256_shrjam(FloatParts256 *a, int c)
1490 uint64_t a0 = a->frac_hi, a1 = a->frac_hm;
1491 uint64_t a2 = a->frac_lm, a3 = a->frac_lo;
1492 uint64_t sticky = 0;
1494 if (unlikely(c == 0)) {
1495 return;
1496 } else if (likely(c < 64)) {
1497 /* nothing */
1498 } else if (likely(c < 256)) {
1499 if (unlikely(c & 128)) {
1500 sticky |= a2 | a3;
1501 a3 = a1, a2 = a0, a1 = 0, a0 = 0;
1503 if (unlikely(c & 64)) {
1504 sticky |= a3;
1505 a3 = a2, a2 = a1, a1 = a0, a0 = 0;
1507 c &= 63;
1508 if (c == 0) {
1509 goto done;
1511 } else {
1512 sticky = a0 | a1 | a2 | a3;
1513 a0 = a1 = a2 = a3 = 0;
1514 goto done;
1517 sticky |= shr_double(a3, 0, c);
1518 a3 = shr_double(a2, a3, c);
1519 a2 = shr_double(a1, a2, c);
1520 a1 = shr_double(a0, a1, c);
1521 a0 = a0 >> c;
1523 done:
1524 a->frac_lo = a3 | (sticky != 0);
1525 a->frac_lm = a2;
1526 a->frac_hm = a1;
1527 a->frac_hi = a0;
1530 #define frac_shrjam(A, C) FRAC_GENERIC_64_128_256(shrjam, A)(A, C)
1532 static bool frac64_sub(FloatParts64 *r, FloatParts64 *a, FloatParts64 *b)
1534 return usub64_overflow(a->frac, b->frac, &r->frac);
1537 static bool frac128_sub(FloatParts128 *r, FloatParts128 *a, FloatParts128 *b)
1539 bool c = 0;
1540 r->frac_lo = usub64_borrow(a->frac_lo, b->frac_lo, &c);
1541 r->frac_hi = usub64_borrow(a->frac_hi, b->frac_hi, &c);
1542 return c;
1545 static bool frac256_sub(FloatParts256 *r, FloatParts256 *a, FloatParts256 *b)
1547 bool c = 0;
1548 r->frac_lo = usub64_borrow(a->frac_lo, b->frac_lo, &c);
1549 r->frac_lm = usub64_borrow(a->frac_lm, b->frac_lm, &c);
1550 r->frac_hm = usub64_borrow(a->frac_hm, b->frac_hm, &c);
1551 r->frac_hi = usub64_borrow(a->frac_hi, b->frac_hi, &c);
1552 return c;
1555 #define frac_sub(R, A, B) FRAC_GENERIC_64_128_256(sub, R)(R, A, B)
1557 static void frac64_truncjam(FloatParts64 *r, FloatParts128 *a)
1559 r->frac = a->frac_hi | (a->frac_lo != 0);
1562 static void frac128_truncjam(FloatParts128 *r, FloatParts256 *a)
1564 r->frac_hi = a->frac_hi;
1565 r->frac_lo = a->frac_hm | ((a->frac_lm | a->frac_lo) != 0);
1568 #define frac_truncjam(R, A) FRAC_GENERIC_64_128(truncjam, R)(R, A)
1570 static void frac64_widen(FloatParts128 *r, FloatParts64 *a)
1572 r->frac_hi = a->frac;
1573 r->frac_lo = 0;
1576 static void frac128_widen(FloatParts256 *r, FloatParts128 *a)
1578 r->frac_hi = a->frac_hi;
1579 r->frac_hm = a->frac_lo;
1580 r->frac_lm = 0;
1581 r->frac_lo = 0;
1584 #define frac_widen(A, B) FRAC_GENERIC_64_128(widen, B)(A, B)
1587 * Reciprocal sqrt table. 1 bit of exponent, 6-bits of mantessa.
1588 * From https://git.musl-libc.org/cgit/musl/tree/src/math/sqrt_data.c
1589 * and thus MIT licenced.
1591 static const uint16_t rsqrt_tab[128] = {
1592 0xb451, 0xb2f0, 0xb196, 0xb044, 0xaef9, 0xadb6, 0xac79, 0xab43,
1593 0xaa14, 0xa8eb, 0xa7c8, 0xa6aa, 0xa592, 0xa480, 0xa373, 0xa26b,
1594 0xa168, 0xa06a, 0x9f70, 0x9e7b, 0x9d8a, 0x9c9d, 0x9bb5, 0x9ad1,
1595 0x99f0, 0x9913, 0x983a, 0x9765, 0x9693, 0x95c4, 0x94f8, 0x9430,
1596 0x936b, 0x92a9, 0x91ea, 0x912e, 0x9075, 0x8fbe, 0x8f0a, 0x8e59,
1597 0x8daa, 0x8cfe, 0x8c54, 0x8bac, 0x8b07, 0x8a64, 0x89c4, 0x8925,
1598 0x8889, 0x87ee, 0x8756, 0x86c0, 0x862b, 0x8599, 0x8508, 0x8479,
1599 0x83ec, 0x8361, 0x82d8, 0x8250, 0x81c9, 0x8145, 0x80c2, 0x8040,
1600 0xff02, 0xfd0e, 0xfb25, 0xf947, 0xf773, 0xf5aa, 0xf3ea, 0xf234,
1601 0xf087, 0xeee3, 0xed47, 0xebb3, 0xea27, 0xe8a3, 0xe727, 0xe5b2,
1602 0xe443, 0xe2dc, 0xe17a, 0xe020, 0xdecb, 0xdd7d, 0xdc34, 0xdaf1,
1603 0xd9b3, 0xd87b, 0xd748, 0xd61a, 0xd4f1, 0xd3cd, 0xd2ad, 0xd192,
1604 0xd07b, 0xcf69, 0xce5b, 0xcd51, 0xcc4a, 0xcb48, 0xca4a, 0xc94f,
1605 0xc858, 0xc764, 0xc674, 0xc587, 0xc49d, 0xc3b7, 0xc2d4, 0xc1f4,
1606 0xc116, 0xc03c, 0xbf65, 0xbe90, 0xbdbe, 0xbcef, 0xbc23, 0xbb59,
1607 0xba91, 0xb9cc, 0xb90a, 0xb84a, 0xb78c, 0xb6d0, 0xb617, 0xb560,
1610 #define partsN(NAME) glue(glue(glue(parts,N),_),NAME)
1611 #define FloatPartsN glue(FloatParts,N)
1612 #define FloatPartsW glue(FloatParts,W)
1614 #define N 64
1615 #define W 128
1617 #include "softfloat-parts-addsub.c.inc"
1618 #include "softfloat-parts.c.inc"
1620 #undef N
1621 #undef W
1622 #define N 128
1623 #define W 256
1625 #include "softfloat-parts-addsub.c.inc"
1626 #include "softfloat-parts.c.inc"
1628 #undef N
1629 #undef W
1630 #define N 256
1632 #include "softfloat-parts-addsub.c.inc"
1634 #undef N
1635 #undef W
1636 #undef partsN
1637 #undef FloatPartsN
1638 #undef FloatPartsW
1641 * Pack/unpack routines with a specific FloatFmt.
1644 static void float16a_unpack_canonical(FloatParts64 *p, float16 f,
1645 float_status *s, const FloatFmt *params)
1647 float16_unpack_raw(p, f);
1648 parts_canonicalize(p, s, params);
1651 static void float16_unpack_canonical(FloatParts64 *p, float16 f,
1652 float_status *s)
1654 float16a_unpack_canonical(p, f, s, &float16_params);
1657 static void bfloat16_unpack_canonical(FloatParts64 *p, bfloat16 f,
1658 float_status *s)
1660 bfloat16_unpack_raw(p, f);
1661 parts_canonicalize(p, s, &bfloat16_params);
1664 static float16 float16a_round_pack_canonical(FloatParts64 *p,
1665 float_status *s,
1666 const FloatFmt *params)
1668 parts_uncanon(p, s, params);
1669 return float16_pack_raw(p);
1672 static float16 float16_round_pack_canonical(FloatParts64 *p,
1673 float_status *s)
1675 return float16a_round_pack_canonical(p, s, &float16_params);
1678 static bfloat16 bfloat16_round_pack_canonical(FloatParts64 *p,
1679 float_status *s)
1681 parts_uncanon(p, s, &bfloat16_params);
1682 return bfloat16_pack_raw(p);
1685 static void float32_unpack_canonical(FloatParts64 *p, float32 f,
1686 float_status *s)
1688 float32_unpack_raw(p, f);
1689 parts_canonicalize(p, s, &float32_params);
1692 static float32 float32_round_pack_canonical(FloatParts64 *p,
1693 float_status *s)
1695 parts_uncanon(p, s, &float32_params);
1696 return float32_pack_raw(p);
1699 static void float64_unpack_canonical(FloatParts64 *p, float64 f,
1700 float_status *s)
1702 float64_unpack_raw(p, f);
1703 parts_canonicalize(p, s, &float64_params);
1706 static float64 float64_round_pack_canonical(FloatParts64 *p,
1707 float_status *s)
1709 parts_uncanon(p, s, &float64_params);
1710 return float64_pack_raw(p);
1713 static float64 float64r32_round_pack_canonical(FloatParts64 *p,
1714 float_status *s)
1716 parts_uncanon(p, s, &float32_params);
1719 * In parts_uncanon, we placed the fraction for float32 at the lsb.
1720 * We need to adjust the fraction higher so that the least N bits are
1721 * zero, and the fraction is adjacent to the float64 implicit bit.
1723 switch (p->cls) {
1724 case float_class_normal:
1725 if (unlikely(p->exp == 0)) {
1727 * The result is denormal for float32, but can be represented
1728 * in normalized form for float64. Adjust, per canonicalize.
1730 int shift = frac_normalize(p);
1731 p->exp = (float32_params.frac_shift -
1732 float32_params.exp_bias - shift + 1 +
1733 float64_params.exp_bias);
1734 frac_shr(p, float64_params.frac_shift);
1735 } else {
1736 frac_shl(p, float32_params.frac_shift - float64_params.frac_shift);
1737 p->exp += float64_params.exp_bias - float32_params.exp_bias;
1739 break;
1740 case float_class_snan:
1741 case float_class_qnan:
1742 frac_shl(p, float32_params.frac_shift - float64_params.frac_shift);
1743 p->exp = float64_params.exp_max;
1744 break;
1745 case float_class_inf:
1746 p->exp = float64_params.exp_max;
1747 break;
1748 case float_class_zero:
1749 break;
1750 default:
1751 g_assert_not_reached();
1754 return float64_pack_raw(p);
1757 static void float128_unpack_canonical(FloatParts128 *p, float128 f,
1758 float_status *s)
1760 float128_unpack_raw(p, f);
1761 parts_canonicalize(p, s, &float128_params);
1764 static float128 float128_round_pack_canonical(FloatParts128 *p,
1765 float_status *s)
1767 parts_uncanon(p, s, &float128_params);
1768 return float128_pack_raw(p);
1771 /* Returns false if the encoding is invalid. */
1772 static bool floatx80_unpack_canonical(FloatParts128 *p, floatx80 f,
1773 float_status *s)
1775 /* Ensure rounding precision is set before beginning. */
1776 switch (s->floatx80_rounding_precision) {
1777 case floatx80_precision_x:
1778 case floatx80_precision_d:
1779 case floatx80_precision_s:
1780 break;
1781 default:
1782 g_assert_not_reached();
1785 if (unlikely(floatx80_invalid_encoding(f))) {
1786 float_raise(float_flag_invalid, s);
1787 return false;
1790 floatx80_unpack_raw(p, f);
1792 if (likely(p->exp != floatx80_params[floatx80_precision_x].exp_max)) {
1793 parts_canonicalize(p, s, &floatx80_params[floatx80_precision_x]);
1794 } else {
1795 /* The explicit integer bit is ignored, after invalid checks. */
1796 p->frac_hi &= MAKE_64BIT_MASK(0, 63);
1797 p->cls = (p->frac_hi == 0 ? float_class_inf
1798 : parts_is_snan_frac(p->frac_hi, s)
1799 ? float_class_snan : float_class_qnan);
1801 return true;
1804 static floatx80 floatx80_round_pack_canonical(FloatParts128 *p,
1805 float_status *s)
1807 const FloatFmt *fmt = &floatx80_params[s->floatx80_rounding_precision];
1808 uint64_t frac;
1809 int exp;
1811 switch (p->cls) {
1812 case float_class_normal:
1813 if (s->floatx80_rounding_precision == floatx80_precision_x) {
1814 parts_uncanon_normal(p, s, fmt);
1815 frac = p->frac_hi;
1816 exp = p->exp;
1817 } else {
1818 FloatParts64 p64;
1820 p64.sign = p->sign;
1821 p64.exp = p->exp;
1822 frac_truncjam(&p64, p);
1823 parts_uncanon_normal(&p64, s, fmt);
1824 frac = p64.frac;
1825 exp = p64.exp;
1827 if (exp != fmt->exp_max) {
1828 break;
1830 /* rounded to inf -- fall through to set frac correctly */
1832 case float_class_inf:
1833 /* x86 and m68k differ in the setting of the integer bit. */
1834 frac = floatx80_infinity_low;
1835 exp = fmt->exp_max;
1836 break;
1838 case float_class_zero:
1839 frac = 0;
1840 exp = 0;
1841 break;
1843 case float_class_snan:
1844 case float_class_qnan:
1845 /* NaNs have the integer bit set. */
1846 frac = p->frac_hi | (1ull << 63);
1847 exp = fmt->exp_max;
1848 break;
1850 default:
1851 g_assert_not_reached();
1854 return packFloatx80(p->sign, exp, frac);
1858 * Addition and subtraction
1861 static float16 QEMU_FLATTEN
1862 float16_addsub(float16 a, float16 b, float_status *status, bool subtract)
1864 FloatParts64 pa, pb, *pr;
1866 float16_unpack_canonical(&pa, a, status);
1867 float16_unpack_canonical(&pb, b, status);
1868 pr = parts_addsub(&pa, &pb, status, subtract);
1870 return float16_round_pack_canonical(pr, status);
1873 float16 float16_add(float16 a, float16 b, float_status *status)
1875 return float16_addsub(a, b, status, false);
1878 float16 float16_sub(float16 a, float16 b, float_status *status)
1880 return float16_addsub(a, b, status, true);
1883 static float32 QEMU_SOFTFLOAT_ATTR
1884 soft_f32_addsub(float32 a, float32 b, float_status *status, bool subtract)
1886 FloatParts64 pa, pb, *pr;
1888 float32_unpack_canonical(&pa, a, status);
1889 float32_unpack_canonical(&pb, b, status);
1890 pr = parts_addsub(&pa, &pb, status, subtract);
1892 return float32_round_pack_canonical(pr, status);
1895 static float32 soft_f32_add(float32 a, float32 b, float_status *status)
1897 return soft_f32_addsub(a, b, status, false);
1900 static float32 soft_f32_sub(float32 a, float32 b, float_status *status)
1902 return soft_f32_addsub(a, b, status, true);
1905 static float64 QEMU_SOFTFLOAT_ATTR
1906 soft_f64_addsub(float64 a, float64 b, float_status *status, bool subtract)
1908 FloatParts64 pa, pb, *pr;
1910 float64_unpack_canonical(&pa, a, status);
1911 float64_unpack_canonical(&pb, b, status);
1912 pr = parts_addsub(&pa, &pb, status, subtract);
1914 return float64_round_pack_canonical(pr, status);
1917 static float64 soft_f64_add(float64 a, float64 b, float_status *status)
1919 return soft_f64_addsub(a, b, status, false);
1922 static float64 soft_f64_sub(float64 a, float64 b, float_status *status)
1924 return soft_f64_addsub(a, b, status, true);
1927 static float hard_f32_add(float a, float b)
1929 return a + b;
1932 static float hard_f32_sub(float a, float b)
1934 return a - b;
1937 static double hard_f64_add(double a, double b)
1939 return a + b;
1942 static double hard_f64_sub(double a, double b)
1944 return a - b;
1947 static bool f32_addsubmul_post(union_float32 a, union_float32 b)
1949 if (QEMU_HARDFLOAT_2F32_USE_FP) {
1950 return !(fpclassify(a.h) == FP_ZERO && fpclassify(b.h) == FP_ZERO);
1952 return !(float32_is_zero(a.s) && float32_is_zero(b.s));
1955 static bool f64_addsubmul_post(union_float64 a, union_float64 b)
1957 if (QEMU_HARDFLOAT_2F64_USE_FP) {
1958 return !(fpclassify(a.h) == FP_ZERO && fpclassify(b.h) == FP_ZERO);
1959 } else {
1960 return !(float64_is_zero(a.s) && float64_is_zero(b.s));
1964 static float32 float32_addsub(float32 a, float32 b, float_status *s,
1965 hard_f32_op2_fn hard, soft_f32_op2_fn soft)
1967 return float32_gen2(a, b, s, hard, soft,
1968 f32_is_zon2, f32_addsubmul_post);
1971 static float64 float64_addsub(float64 a, float64 b, float_status *s,
1972 hard_f64_op2_fn hard, soft_f64_op2_fn soft)
1974 return float64_gen2(a, b, s, hard, soft,
1975 f64_is_zon2, f64_addsubmul_post);
1978 float32 QEMU_FLATTEN
1979 float32_add(float32 a, float32 b, float_status *s)
1981 return float32_addsub(a, b, s, hard_f32_add, soft_f32_add);
1984 float32 QEMU_FLATTEN
1985 float32_sub(float32 a, float32 b, float_status *s)
1987 return float32_addsub(a, b, s, hard_f32_sub, soft_f32_sub);
1990 float64 QEMU_FLATTEN
1991 float64_add(float64 a, float64 b, float_status *s)
1993 return float64_addsub(a, b, s, hard_f64_add, soft_f64_add);
1996 float64 QEMU_FLATTEN
1997 float64_sub(float64 a, float64 b, float_status *s)
1999 return float64_addsub(a, b, s, hard_f64_sub, soft_f64_sub);
2002 static float64 float64r32_addsub(float64 a, float64 b, float_status *status,
2003 bool subtract)
2005 FloatParts64 pa, pb, *pr;
2007 float64_unpack_canonical(&pa, a, status);
2008 float64_unpack_canonical(&pb, b, status);
2009 pr = parts_addsub(&pa, &pb, status, subtract);
2011 return float64r32_round_pack_canonical(pr, status);
2014 float64 float64r32_add(float64 a, float64 b, float_status *status)
2016 return float64r32_addsub(a, b, status, false);
2019 float64 float64r32_sub(float64 a, float64 b, float_status *status)
2021 return float64r32_addsub(a, b, status, true);
2024 static bfloat16 QEMU_FLATTEN
2025 bfloat16_addsub(bfloat16 a, bfloat16 b, float_status *status, bool subtract)
2027 FloatParts64 pa, pb, *pr;
2029 bfloat16_unpack_canonical(&pa, a, status);
2030 bfloat16_unpack_canonical(&pb, b, status);
2031 pr = parts_addsub(&pa, &pb, status, subtract);
2033 return bfloat16_round_pack_canonical(pr, status);
2036 bfloat16 bfloat16_add(bfloat16 a, bfloat16 b, float_status *status)
2038 return bfloat16_addsub(a, b, status, false);
2041 bfloat16 bfloat16_sub(bfloat16 a, bfloat16 b, float_status *status)
2043 return bfloat16_addsub(a, b, status, true);
2046 static float128 QEMU_FLATTEN
2047 float128_addsub(float128 a, float128 b, float_status *status, bool subtract)
2049 FloatParts128 pa, pb, *pr;
2051 float128_unpack_canonical(&pa, a, status);
2052 float128_unpack_canonical(&pb, b, status);
2053 pr = parts_addsub(&pa, &pb, status, subtract);
2055 return float128_round_pack_canonical(pr, status);
2058 float128 float128_add(float128 a, float128 b, float_status *status)
2060 return float128_addsub(a, b, status, false);
2063 float128 float128_sub(float128 a, float128 b, float_status *status)
2065 return float128_addsub(a, b, status, true);
2068 static floatx80 QEMU_FLATTEN
2069 floatx80_addsub(floatx80 a, floatx80 b, float_status *status, bool subtract)
2071 FloatParts128 pa, pb, *pr;
2073 if (!floatx80_unpack_canonical(&pa, a, status) ||
2074 !floatx80_unpack_canonical(&pb, b, status)) {
2075 return floatx80_default_nan(status);
2078 pr = parts_addsub(&pa, &pb, status, subtract);
2079 return floatx80_round_pack_canonical(pr, status);
2082 floatx80 floatx80_add(floatx80 a, floatx80 b, float_status *status)
2084 return floatx80_addsub(a, b, status, false);
2087 floatx80 floatx80_sub(floatx80 a, floatx80 b, float_status *status)
2089 return floatx80_addsub(a, b, status, true);
2093 * Multiplication
2096 float16 QEMU_FLATTEN float16_mul(float16 a, float16 b, float_status *status)
2098 FloatParts64 pa, pb, *pr;
2100 float16_unpack_canonical(&pa, a, status);
2101 float16_unpack_canonical(&pb, b, status);
2102 pr = parts_mul(&pa, &pb, status);
2104 return float16_round_pack_canonical(pr, status);
2107 static float32 QEMU_SOFTFLOAT_ATTR
2108 soft_f32_mul(float32 a, float32 b, float_status *status)
2110 FloatParts64 pa, pb, *pr;
2112 float32_unpack_canonical(&pa, a, status);
2113 float32_unpack_canonical(&pb, b, status);
2114 pr = parts_mul(&pa, &pb, status);
2116 return float32_round_pack_canonical(pr, status);
2119 static float64 QEMU_SOFTFLOAT_ATTR
2120 soft_f64_mul(float64 a, float64 b, float_status *status)
2122 FloatParts64 pa, pb, *pr;
2124 float64_unpack_canonical(&pa, a, status);
2125 float64_unpack_canonical(&pb, b, status);
2126 pr = parts_mul(&pa, &pb, status);
2128 return float64_round_pack_canonical(pr, status);
2131 static float hard_f32_mul(float a, float b)
2133 return a * b;
2136 static double hard_f64_mul(double a, double b)
2138 return a * b;
2141 float32 QEMU_FLATTEN
2142 float32_mul(float32 a, float32 b, float_status *s)
2144 return float32_gen2(a, b, s, hard_f32_mul, soft_f32_mul,
2145 f32_is_zon2, f32_addsubmul_post);
2148 float64 QEMU_FLATTEN
2149 float64_mul(float64 a, float64 b, float_status *s)
2151 return float64_gen2(a, b, s, hard_f64_mul, soft_f64_mul,
2152 f64_is_zon2, f64_addsubmul_post);
2155 float64 float64r32_mul(float64 a, float64 b, float_status *status)
2157 FloatParts64 pa, pb, *pr;
2159 float64_unpack_canonical(&pa, a, status);
2160 float64_unpack_canonical(&pb, b, status);
2161 pr = parts_mul(&pa, &pb, status);
2163 return float64r32_round_pack_canonical(pr, status);
2166 bfloat16 QEMU_FLATTEN
2167 bfloat16_mul(bfloat16 a, bfloat16 b, float_status *status)
2169 FloatParts64 pa, pb, *pr;
2171 bfloat16_unpack_canonical(&pa, a, status);
2172 bfloat16_unpack_canonical(&pb, b, status);
2173 pr = parts_mul(&pa, &pb, status);
2175 return bfloat16_round_pack_canonical(pr, status);
2178 float128 QEMU_FLATTEN
2179 float128_mul(float128 a, float128 b, float_status *status)
2181 FloatParts128 pa, pb, *pr;
2183 float128_unpack_canonical(&pa, a, status);
2184 float128_unpack_canonical(&pb, b, status);
2185 pr = parts_mul(&pa, &pb, status);
2187 return float128_round_pack_canonical(pr, status);
2190 floatx80 QEMU_FLATTEN
2191 floatx80_mul(floatx80 a, floatx80 b, float_status *status)
2193 FloatParts128 pa, pb, *pr;
2195 if (!floatx80_unpack_canonical(&pa, a, status) ||
2196 !floatx80_unpack_canonical(&pb, b, status)) {
2197 return floatx80_default_nan(status);
2200 pr = parts_mul(&pa, &pb, status);
2201 return floatx80_round_pack_canonical(pr, status);
2205 * Fused multiply-add
2208 float16 QEMU_FLATTEN float16_muladd(float16 a, float16 b, float16 c,
2209 int flags, float_status *status)
2211 FloatParts64 pa, pb, pc, *pr;
2213 float16_unpack_canonical(&pa, a, status);
2214 float16_unpack_canonical(&pb, b, status);
2215 float16_unpack_canonical(&pc, c, status);
2216 pr = parts_muladd(&pa, &pb, &pc, flags, status);
2218 return float16_round_pack_canonical(pr, status);
2221 static float32 QEMU_SOFTFLOAT_ATTR
2222 soft_f32_muladd(float32 a, float32 b, float32 c, int flags,
2223 float_status *status)
2225 FloatParts64 pa, pb, pc, *pr;
2227 float32_unpack_canonical(&pa, a, status);
2228 float32_unpack_canonical(&pb, b, status);
2229 float32_unpack_canonical(&pc, c, status);
2230 pr = parts_muladd(&pa, &pb, &pc, flags, status);
2232 return float32_round_pack_canonical(pr, status);
2235 static float64 QEMU_SOFTFLOAT_ATTR
2236 soft_f64_muladd(float64 a, float64 b, float64 c, int flags,
2237 float_status *status)
2239 FloatParts64 pa, pb, pc, *pr;
2241 float64_unpack_canonical(&pa, a, status);
2242 float64_unpack_canonical(&pb, b, status);
2243 float64_unpack_canonical(&pc, c, status);
2244 pr = parts_muladd(&pa, &pb, &pc, flags, status);
2246 return float64_round_pack_canonical(pr, status);
2249 static bool force_soft_fma;
2251 float32 QEMU_FLATTEN
2252 float32_muladd(float32 xa, float32 xb, float32 xc, int flags, float_status *s)
2254 union_float32 ua, ub, uc, ur;
2256 ua.s = xa;
2257 ub.s = xb;
2258 uc.s = xc;
2260 if (unlikely(!can_use_fpu(s))) {
2261 goto soft;
2263 if (unlikely(flags & float_muladd_halve_result)) {
2264 goto soft;
2267 float32_input_flush3(&ua.s, &ub.s, &uc.s, s);
2268 if (unlikely(!f32_is_zon3(ua, ub, uc))) {
2269 goto soft;
2272 if (unlikely(force_soft_fma)) {
2273 goto soft;
2277 * When (a || b) == 0, there's no need to check for under/over flow,
2278 * since we know the addend is (normal || 0) and the product is 0.
2280 if (float32_is_zero(ua.s) || float32_is_zero(ub.s)) {
2281 union_float32 up;
2282 bool prod_sign;
2284 prod_sign = float32_is_neg(ua.s) ^ float32_is_neg(ub.s);
2285 prod_sign ^= !!(flags & float_muladd_negate_product);
2286 up.s = float32_set_sign(float32_zero, prod_sign);
2288 if (flags & float_muladd_negate_c) {
2289 uc.h = -uc.h;
2291 ur.h = up.h + uc.h;
2292 } else {
2293 union_float32 ua_orig = ua;
2294 union_float32 uc_orig = uc;
2296 if (flags & float_muladd_negate_product) {
2297 ua.h = -ua.h;
2299 if (flags & float_muladd_negate_c) {
2300 uc.h = -uc.h;
2303 ur.h = fmaf(ua.h, ub.h, uc.h);
2305 if (unlikely(f32_is_inf(ur))) {
2306 float_raise(float_flag_overflow, s);
2307 } else if (unlikely(fabsf(ur.h) <= FLT_MIN)) {
2308 ua = ua_orig;
2309 uc = uc_orig;
2310 goto soft;
2313 if (flags & float_muladd_negate_result) {
2314 return float32_chs(ur.s);
2316 return ur.s;
2318 soft:
2319 return soft_f32_muladd(ua.s, ub.s, uc.s, flags, s);
2322 float64 QEMU_FLATTEN
2323 float64_muladd(float64 xa, float64 xb, float64 xc, int flags, float_status *s)
2325 union_float64 ua, ub, uc, ur;
2327 ua.s = xa;
2328 ub.s = xb;
2329 uc.s = xc;
2331 if (unlikely(!can_use_fpu(s))) {
2332 goto soft;
2334 if (unlikely(flags & float_muladd_halve_result)) {
2335 goto soft;
2338 float64_input_flush3(&ua.s, &ub.s, &uc.s, s);
2339 if (unlikely(!f64_is_zon3(ua, ub, uc))) {
2340 goto soft;
2343 if (unlikely(force_soft_fma)) {
2344 goto soft;
2348 * When (a || b) == 0, there's no need to check for under/over flow,
2349 * since we know the addend is (normal || 0) and the product is 0.
2351 if (float64_is_zero(ua.s) || float64_is_zero(ub.s)) {
2352 union_float64 up;
2353 bool prod_sign;
2355 prod_sign = float64_is_neg(ua.s) ^ float64_is_neg(ub.s);
2356 prod_sign ^= !!(flags & float_muladd_negate_product);
2357 up.s = float64_set_sign(float64_zero, prod_sign);
2359 if (flags & float_muladd_negate_c) {
2360 uc.h = -uc.h;
2362 ur.h = up.h + uc.h;
2363 } else {
2364 union_float64 ua_orig = ua;
2365 union_float64 uc_orig = uc;
2367 if (flags & float_muladd_negate_product) {
2368 ua.h = -ua.h;
2370 if (flags & float_muladd_negate_c) {
2371 uc.h = -uc.h;
2374 ur.h = fma(ua.h, ub.h, uc.h);
2376 if (unlikely(f64_is_inf(ur))) {
2377 float_raise(float_flag_overflow, s);
2378 } else if (unlikely(fabs(ur.h) <= FLT_MIN)) {
2379 ua = ua_orig;
2380 uc = uc_orig;
2381 goto soft;
2384 if (flags & float_muladd_negate_result) {
2385 return float64_chs(ur.s);
2387 return ur.s;
2389 soft:
2390 return soft_f64_muladd(ua.s, ub.s, uc.s, flags, s);
2393 float64 float64r32_muladd(float64 a, float64 b, float64 c,
2394 int flags, float_status *status)
2396 FloatParts64 pa, pb, pc, *pr;
2398 float64_unpack_canonical(&pa, a, status);
2399 float64_unpack_canonical(&pb, b, status);
2400 float64_unpack_canonical(&pc, c, status);
2401 pr = parts_muladd(&pa, &pb, &pc, flags, status);
2403 return float64r32_round_pack_canonical(pr, status);
2406 bfloat16 QEMU_FLATTEN bfloat16_muladd(bfloat16 a, bfloat16 b, bfloat16 c,
2407 int flags, float_status *status)
2409 FloatParts64 pa, pb, pc, *pr;
2411 bfloat16_unpack_canonical(&pa, a, status);
2412 bfloat16_unpack_canonical(&pb, b, status);
2413 bfloat16_unpack_canonical(&pc, c, status);
2414 pr = parts_muladd(&pa, &pb, &pc, flags, status);
2416 return bfloat16_round_pack_canonical(pr, status);
2419 float128 QEMU_FLATTEN float128_muladd(float128 a, float128 b, float128 c,
2420 int flags, float_status *status)
2422 FloatParts128 pa, pb, pc, *pr;
2424 float128_unpack_canonical(&pa, a, status);
2425 float128_unpack_canonical(&pb, b, status);
2426 float128_unpack_canonical(&pc, c, status);
2427 pr = parts_muladd(&pa, &pb, &pc, flags, status);
2429 return float128_round_pack_canonical(pr, status);
2433 * Division
2436 float16 float16_div(float16 a, float16 b, float_status *status)
2438 FloatParts64 pa, pb, *pr;
2440 float16_unpack_canonical(&pa, a, status);
2441 float16_unpack_canonical(&pb, b, status);
2442 pr = parts_div(&pa, &pb, status);
2444 return float16_round_pack_canonical(pr, status);
2447 static float32 QEMU_SOFTFLOAT_ATTR
2448 soft_f32_div(float32 a, float32 b, float_status *status)
2450 FloatParts64 pa, pb, *pr;
2452 float32_unpack_canonical(&pa, a, status);
2453 float32_unpack_canonical(&pb, b, status);
2454 pr = parts_div(&pa, &pb, status);
2456 return float32_round_pack_canonical(pr, status);
2459 static float64 QEMU_SOFTFLOAT_ATTR
2460 soft_f64_div(float64 a, float64 b, float_status *status)
2462 FloatParts64 pa, pb, *pr;
2464 float64_unpack_canonical(&pa, a, status);
2465 float64_unpack_canonical(&pb, b, status);
2466 pr = parts_div(&pa, &pb, status);
2468 return float64_round_pack_canonical(pr, status);
2471 static float hard_f32_div(float a, float b)
2473 return a / b;
2476 static double hard_f64_div(double a, double b)
2478 return a / b;
2481 static bool f32_div_pre(union_float32 a, union_float32 b)
2483 if (QEMU_HARDFLOAT_2F32_USE_FP) {
2484 return (fpclassify(a.h) == FP_NORMAL || fpclassify(a.h) == FP_ZERO) &&
2485 fpclassify(b.h) == FP_NORMAL;
2487 return float32_is_zero_or_normal(a.s) && float32_is_normal(b.s);
2490 static bool f64_div_pre(union_float64 a, union_float64 b)
2492 if (QEMU_HARDFLOAT_2F64_USE_FP) {
2493 return (fpclassify(a.h) == FP_NORMAL || fpclassify(a.h) == FP_ZERO) &&
2494 fpclassify(b.h) == FP_NORMAL;
2496 return float64_is_zero_or_normal(a.s) && float64_is_normal(b.s);
2499 static bool f32_div_post(union_float32 a, union_float32 b)
2501 if (QEMU_HARDFLOAT_2F32_USE_FP) {
2502 return fpclassify(a.h) != FP_ZERO;
2504 return !float32_is_zero(a.s);
2507 static bool f64_div_post(union_float64 a, union_float64 b)
2509 if (QEMU_HARDFLOAT_2F64_USE_FP) {
2510 return fpclassify(a.h) != FP_ZERO;
2512 return !float64_is_zero(a.s);
2515 float32 QEMU_FLATTEN
2516 float32_div(float32 a, float32 b, float_status *s)
2518 return float32_gen2(a, b, s, hard_f32_div, soft_f32_div,
2519 f32_div_pre, f32_div_post);
2522 float64 QEMU_FLATTEN
2523 float64_div(float64 a, float64 b, float_status *s)
2525 return float64_gen2(a, b, s, hard_f64_div, soft_f64_div,
2526 f64_div_pre, f64_div_post);
2529 float64 float64r32_div(float64 a, float64 b, float_status *status)
2531 FloatParts64 pa, pb, *pr;
2533 float64_unpack_canonical(&pa, a, status);
2534 float64_unpack_canonical(&pb, b, status);
2535 pr = parts_div(&pa, &pb, status);
2537 return float64r32_round_pack_canonical(pr, status);
2540 bfloat16 QEMU_FLATTEN
2541 bfloat16_div(bfloat16 a, bfloat16 b, float_status *status)
2543 FloatParts64 pa, pb, *pr;
2545 bfloat16_unpack_canonical(&pa, a, status);
2546 bfloat16_unpack_canonical(&pb, b, status);
2547 pr = parts_div(&pa, &pb, status);
2549 return bfloat16_round_pack_canonical(pr, status);
2552 float128 QEMU_FLATTEN
2553 float128_div(float128 a, float128 b, float_status *status)
2555 FloatParts128 pa, pb, *pr;
2557 float128_unpack_canonical(&pa, a, status);
2558 float128_unpack_canonical(&pb, b, status);
2559 pr = parts_div(&pa, &pb, status);
2561 return float128_round_pack_canonical(pr, status);
2564 floatx80 floatx80_div(floatx80 a, floatx80 b, float_status *status)
2566 FloatParts128 pa, pb, *pr;
2568 if (!floatx80_unpack_canonical(&pa, a, status) ||
2569 !floatx80_unpack_canonical(&pb, b, status)) {
2570 return floatx80_default_nan(status);
2573 pr = parts_div(&pa, &pb, status);
2574 return floatx80_round_pack_canonical(pr, status);
2578 * Remainder
2581 float32 float32_rem(float32 a, float32 b, float_status *status)
2583 FloatParts64 pa, pb, *pr;
2585 float32_unpack_canonical(&pa, a, status);
2586 float32_unpack_canonical(&pb, b, status);
2587 pr = parts_modrem(&pa, &pb, NULL, status);
2589 return float32_round_pack_canonical(pr, status);
2592 float64 float64_rem(float64 a, float64 b, float_status *status)
2594 FloatParts64 pa, pb, *pr;
2596 float64_unpack_canonical(&pa, a, status);
2597 float64_unpack_canonical(&pb, b, status);
2598 pr = parts_modrem(&pa, &pb, NULL, status);
2600 return float64_round_pack_canonical(pr, status);
2603 float128 float128_rem(float128 a, float128 b, float_status *status)
2605 FloatParts128 pa, pb, *pr;
2607 float128_unpack_canonical(&pa, a, status);
2608 float128_unpack_canonical(&pb, b, status);
2609 pr = parts_modrem(&pa, &pb, NULL, status);
2611 return float128_round_pack_canonical(pr, status);
2615 * Returns the remainder of the extended double-precision floating-point value
2616 * `a' with respect to the corresponding value `b'.
2617 * If 'mod' is false, the operation is performed according to the IEC/IEEE
2618 * Standard for Binary Floating-Point Arithmetic. If 'mod' is true, return
2619 * the remainder based on truncating the quotient toward zero instead and
2620 * *quotient is set to the low 64 bits of the absolute value of the integer
2621 * quotient.
2623 floatx80 floatx80_modrem(floatx80 a, floatx80 b, bool mod,
2624 uint64_t *quotient, float_status *status)
2626 FloatParts128 pa, pb, *pr;
2628 *quotient = 0;
2629 if (!floatx80_unpack_canonical(&pa, a, status) ||
2630 !floatx80_unpack_canonical(&pb, b, status)) {
2631 return floatx80_default_nan(status);
2633 pr = parts_modrem(&pa, &pb, mod ? quotient : NULL, status);
2635 return floatx80_round_pack_canonical(pr, status);
2638 floatx80 floatx80_rem(floatx80 a, floatx80 b, float_status *status)
2640 uint64_t quotient;
2641 return floatx80_modrem(a, b, false, &quotient, status);
2644 floatx80 floatx80_mod(floatx80 a, floatx80 b, float_status *status)
2646 uint64_t quotient;
2647 return floatx80_modrem(a, b, true, &quotient, status);
2651 * Float to Float conversions
2653 * Returns the result of converting one float format to another. The
2654 * conversion is performed according to the IEC/IEEE Standard for
2655 * Binary Floating-Point Arithmetic.
2657 * Usually this only needs to take care of raising invalid exceptions
2658 * and handling the conversion on NaNs.
2661 static void parts_float_to_ahp(FloatParts64 *a, float_status *s)
2663 switch (a->cls) {
2664 case float_class_snan:
2665 float_raise(float_flag_invalid_snan, s);
2666 /* fall through */
2667 case float_class_qnan:
2669 * There is no NaN in the destination format. Raise Invalid
2670 * and return a zero with the sign of the input NaN.
2672 float_raise(float_flag_invalid, s);
2673 a->cls = float_class_zero;
2674 break;
2676 case float_class_inf:
2678 * There is no Inf in the destination format. Raise Invalid
2679 * and return the maximum normal with the correct sign.
2681 float_raise(float_flag_invalid, s);
2682 a->cls = float_class_normal;
2683 a->exp = float16_params_ahp.exp_max;
2684 a->frac = MAKE_64BIT_MASK(float16_params_ahp.frac_shift,
2685 float16_params_ahp.frac_size + 1);
2686 break;
2688 case float_class_normal:
2689 case float_class_zero:
2690 break;
2692 default:
2693 g_assert_not_reached();
2697 static void parts64_float_to_float(FloatParts64 *a, float_status *s)
2699 if (is_nan(a->cls)) {
2700 parts_return_nan(a, s);
2704 static void parts128_float_to_float(FloatParts128 *a, float_status *s)
2706 if (is_nan(a->cls)) {
2707 parts_return_nan(a, s);
2711 #define parts_float_to_float(P, S) \
2712 PARTS_GENERIC_64_128(float_to_float, P)(P, S)
2714 static void parts_float_to_float_narrow(FloatParts64 *a, FloatParts128 *b,
2715 float_status *s)
2717 a->cls = b->cls;
2718 a->sign = b->sign;
2719 a->exp = b->exp;
2721 if (a->cls == float_class_normal) {
2722 frac_truncjam(a, b);
2723 } else if (is_nan(a->cls)) {
2724 /* Discard the low bits of the NaN. */
2725 a->frac = b->frac_hi;
2726 parts_return_nan(a, s);
2730 static void parts_float_to_float_widen(FloatParts128 *a, FloatParts64 *b,
2731 float_status *s)
2733 a->cls = b->cls;
2734 a->sign = b->sign;
2735 a->exp = b->exp;
2736 frac_widen(a, b);
2738 if (is_nan(a->cls)) {
2739 parts_return_nan(a, s);
2743 float32 float16_to_float32(float16 a, bool ieee, float_status *s)
2745 const FloatFmt *fmt16 = ieee ? &float16_params : &float16_params_ahp;
2746 FloatParts64 p;
2748 float16a_unpack_canonical(&p, a, s, fmt16);
2749 parts_float_to_float(&p, s);
2750 return float32_round_pack_canonical(&p, s);
2753 float64 float16_to_float64(float16 a, bool ieee, float_status *s)
2755 const FloatFmt *fmt16 = ieee ? &float16_params : &float16_params_ahp;
2756 FloatParts64 p;
2758 float16a_unpack_canonical(&p, a, s, fmt16);
2759 parts_float_to_float(&p, s);
2760 return float64_round_pack_canonical(&p, s);
2763 float16 float32_to_float16(float32 a, bool ieee, float_status *s)
2765 FloatParts64 p;
2766 const FloatFmt *fmt;
2768 float32_unpack_canonical(&p, a, s);
2769 if (ieee) {
2770 parts_float_to_float(&p, s);
2771 fmt = &float16_params;
2772 } else {
2773 parts_float_to_ahp(&p, s);
2774 fmt = &float16_params_ahp;
2776 return float16a_round_pack_canonical(&p, s, fmt);
2779 static float64 QEMU_SOFTFLOAT_ATTR
2780 soft_float32_to_float64(float32 a, float_status *s)
2782 FloatParts64 p;
2784 float32_unpack_canonical(&p, a, s);
2785 parts_float_to_float(&p, s);
2786 return float64_round_pack_canonical(&p, s);
2789 float64 float32_to_float64(float32 a, float_status *s)
2791 if (likely(float32_is_normal(a))) {
2792 /* Widening conversion can never produce inexact results. */
2793 union_float32 uf;
2794 union_float64 ud;
2795 uf.s = a;
2796 ud.h = uf.h;
2797 return ud.s;
2798 } else if (float32_is_zero(a)) {
2799 return float64_set_sign(float64_zero, float32_is_neg(a));
2800 } else {
2801 return soft_float32_to_float64(a, s);
2805 float16 float64_to_float16(float64 a, bool ieee, float_status *s)
2807 FloatParts64 p;
2808 const FloatFmt *fmt;
2810 float64_unpack_canonical(&p, a, s);
2811 if (ieee) {
2812 parts_float_to_float(&p, s);
2813 fmt = &float16_params;
2814 } else {
2815 parts_float_to_ahp(&p, s);
2816 fmt = &float16_params_ahp;
2818 return float16a_round_pack_canonical(&p, s, fmt);
2821 float32 float64_to_float32(float64 a, float_status *s)
2823 FloatParts64 p;
2825 float64_unpack_canonical(&p, a, s);
2826 parts_float_to_float(&p, s);
2827 return float32_round_pack_canonical(&p, s);
2830 float32 bfloat16_to_float32(bfloat16 a, float_status *s)
2832 FloatParts64 p;
2834 bfloat16_unpack_canonical(&p, a, s);
2835 parts_float_to_float(&p, s);
2836 return float32_round_pack_canonical(&p, s);
2839 float64 bfloat16_to_float64(bfloat16 a, float_status *s)
2841 FloatParts64 p;
2843 bfloat16_unpack_canonical(&p, a, s);
2844 parts_float_to_float(&p, s);
2845 return float64_round_pack_canonical(&p, s);
2848 bfloat16 float32_to_bfloat16(float32 a, float_status *s)
2850 FloatParts64 p;
2852 float32_unpack_canonical(&p, a, s);
2853 parts_float_to_float(&p, s);
2854 return bfloat16_round_pack_canonical(&p, s);
2857 bfloat16 float64_to_bfloat16(float64 a, float_status *s)
2859 FloatParts64 p;
2861 float64_unpack_canonical(&p, a, s);
2862 parts_float_to_float(&p, s);
2863 return bfloat16_round_pack_canonical(&p, s);
2866 float32 float128_to_float32(float128 a, float_status *s)
2868 FloatParts64 p64;
2869 FloatParts128 p128;
2871 float128_unpack_canonical(&p128, a, s);
2872 parts_float_to_float_narrow(&p64, &p128, s);
2873 return float32_round_pack_canonical(&p64, s);
2876 float64 float128_to_float64(float128 a, float_status *s)
2878 FloatParts64 p64;
2879 FloatParts128 p128;
2881 float128_unpack_canonical(&p128, a, s);
2882 parts_float_to_float_narrow(&p64, &p128, s);
2883 return float64_round_pack_canonical(&p64, s);
2886 float128 float32_to_float128(float32 a, float_status *s)
2888 FloatParts64 p64;
2889 FloatParts128 p128;
2891 float32_unpack_canonical(&p64, a, s);
2892 parts_float_to_float_widen(&p128, &p64, s);
2893 return float128_round_pack_canonical(&p128, s);
2896 float128 float64_to_float128(float64 a, float_status *s)
2898 FloatParts64 p64;
2899 FloatParts128 p128;
2901 float64_unpack_canonical(&p64, a, s);
2902 parts_float_to_float_widen(&p128, &p64, s);
2903 return float128_round_pack_canonical(&p128, s);
2906 float32 floatx80_to_float32(floatx80 a, float_status *s)
2908 FloatParts64 p64;
2909 FloatParts128 p128;
2911 if (floatx80_unpack_canonical(&p128, a, s)) {
2912 parts_float_to_float_narrow(&p64, &p128, s);
2913 } else {
2914 parts_default_nan(&p64, s);
2916 return float32_round_pack_canonical(&p64, s);
2919 float64 floatx80_to_float64(floatx80 a, float_status *s)
2921 FloatParts64 p64;
2922 FloatParts128 p128;
2924 if (floatx80_unpack_canonical(&p128, a, s)) {
2925 parts_float_to_float_narrow(&p64, &p128, s);
2926 } else {
2927 parts_default_nan(&p64, s);
2929 return float64_round_pack_canonical(&p64, s);
2932 float128 floatx80_to_float128(floatx80 a, float_status *s)
2934 FloatParts128 p;
2936 if (floatx80_unpack_canonical(&p, a, s)) {
2937 parts_float_to_float(&p, s);
2938 } else {
2939 parts_default_nan(&p, s);
2941 return float128_round_pack_canonical(&p, s);
2944 floatx80 float32_to_floatx80(float32 a, float_status *s)
2946 FloatParts64 p64;
2947 FloatParts128 p128;
2949 float32_unpack_canonical(&p64, a, s);
2950 parts_float_to_float_widen(&p128, &p64, s);
2951 return floatx80_round_pack_canonical(&p128, s);
2954 floatx80 float64_to_floatx80(float64 a, float_status *s)
2956 FloatParts64 p64;
2957 FloatParts128 p128;
2959 float64_unpack_canonical(&p64, a, s);
2960 parts_float_to_float_widen(&p128, &p64, s);
2961 return floatx80_round_pack_canonical(&p128, s);
2964 floatx80 float128_to_floatx80(float128 a, float_status *s)
2966 FloatParts128 p;
2968 float128_unpack_canonical(&p, a, s);
2969 parts_float_to_float(&p, s);
2970 return floatx80_round_pack_canonical(&p, s);
2974 * Round to integral value
2977 float16 float16_round_to_int(float16 a, float_status *s)
2979 FloatParts64 p;
2981 float16_unpack_canonical(&p, a, s);
2982 parts_round_to_int(&p, s->float_rounding_mode, 0, s, &float16_params);
2983 return float16_round_pack_canonical(&p, s);
2986 float32 float32_round_to_int(float32 a, float_status *s)
2988 FloatParts64 p;
2990 float32_unpack_canonical(&p, a, s);
2991 parts_round_to_int(&p, s->float_rounding_mode, 0, s, &float32_params);
2992 return float32_round_pack_canonical(&p, s);
2995 float64 float64_round_to_int(float64 a, float_status *s)
2997 FloatParts64 p;
2999 float64_unpack_canonical(&p, a, s);
3000 parts_round_to_int(&p, s->float_rounding_mode, 0, s, &float64_params);
3001 return float64_round_pack_canonical(&p, s);
3004 bfloat16 bfloat16_round_to_int(bfloat16 a, float_status *s)
3006 FloatParts64 p;
3008 bfloat16_unpack_canonical(&p, a, s);
3009 parts_round_to_int(&p, s->float_rounding_mode, 0, s, &bfloat16_params);
3010 return bfloat16_round_pack_canonical(&p, s);
3013 float128 float128_round_to_int(float128 a, float_status *s)
3015 FloatParts128 p;
3017 float128_unpack_canonical(&p, a, s);
3018 parts_round_to_int(&p, s->float_rounding_mode, 0, s, &float128_params);
3019 return float128_round_pack_canonical(&p, s);
3022 floatx80 floatx80_round_to_int(floatx80 a, float_status *status)
3024 FloatParts128 p;
3026 if (!floatx80_unpack_canonical(&p, a, status)) {
3027 return floatx80_default_nan(status);
3030 parts_round_to_int(&p, status->float_rounding_mode, 0, status,
3031 &floatx80_params[status->floatx80_rounding_precision]);
3032 return floatx80_round_pack_canonical(&p, status);
3036 * Floating-point to signed integer conversions
3039 int8_t float16_to_int8_scalbn(float16 a, FloatRoundMode rmode, int scale,
3040 float_status *s)
3042 FloatParts64 p;
3044 float16_unpack_canonical(&p, a, s);
3045 return parts_float_to_sint(&p, rmode, scale, INT8_MIN, INT8_MAX, s);
3048 int16_t float16_to_int16_scalbn(float16 a, FloatRoundMode rmode, int scale,
3049 float_status *s)
3051 FloatParts64 p;
3053 float16_unpack_canonical(&p, a, s);
3054 return parts_float_to_sint(&p, rmode, scale, INT16_MIN, INT16_MAX, s);
3057 int32_t float16_to_int32_scalbn(float16 a, FloatRoundMode rmode, int scale,
3058 float_status *s)
3060 FloatParts64 p;
3062 float16_unpack_canonical(&p, a, s);
3063 return parts_float_to_sint(&p, rmode, scale, INT32_MIN, INT32_MAX, s);
3066 int64_t float16_to_int64_scalbn(float16 a, FloatRoundMode rmode, int scale,
3067 float_status *s)
3069 FloatParts64 p;
3071 float16_unpack_canonical(&p, a, s);
3072 return parts_float_to_sint(&p, rmode, scale, INT64_MIN, INT64_MAX, s);
3075 int16_t float32_to_int16_scalbn(float32 a, FloatRoundMode rmode, int scale,
3076 float_status *s)
3078 FloatParts64 p;
3080 float32_unpack_canonical(&p, a, s);
3081 return parts_float_to_sint(&p, rmode, scale, INT16_MIN, INT16_MAX, s);
3084 int32_t float32_to_int32_scalbn(float32 a, FloatRoundMode rmode, int scale,
3085 float_status *s)
3087 FloatParts64 p;
3089 float32_unpack_canonical(&p, a, s);
3090 return parts_float_to_sint(&p, rmode, scale, INT32_MIN, INT32_MAX, s);
3093 int64_t float32_to_int64_scalbn(float32 a, FloatRoundMode rmode, int scale,
3094 float_status *s)
3096 FloatParts64 p;
3098 float32_unpack_canonical(&p, a, s);
3099 return parts_float_to_sint(&p, rmode, scale, INT64_MIN, INT64_MAX, s);
3102 int16_t float64_to_int16_scalbn(float64 a, FloatRoundMode rmode, int scale,
3103 float_status *s)
3105 FloatParts64 p;
3107 float64_unpack_canonical(&p, a, s);
3108 return parts_float_to_sint(&p, rmode, scale, INT16_MIN, INT16_MAX, s);
3111 int32_t float64_to_int32_scalbn(float64 a, FloatRoundMode rmode, int scale,
3112 float_status *s)
3114 FloatParts64 p;
3116 float64_unpack_canonical(&p, a, s);
3117 return parts_float_to_sint(&p, rmode, scale, INT32_MIN, INT32_MAX, s);
3120 int64_t float64_to_int64_scalbn(float64 a, FloatRoundMode rmode, int scale,
3121 float_status *s)
3123 FloatParts64 p;
3125 float64_unpack_canonical(&p, a, s);
3126 return parts_float_to_sint(&p, rmode, scale, INT64_MIN, INT64_MAX, s);
3129 int16_t bfloat16_to_int16_scalbn(bfloat16 a, FloatRoundMode rmode, int scale,
3130 float_status *s)
3132 FloatParts64 p;
3134 bfloat16_unpack_canonical(&p, a, s);
3135 return parts_float_to_sint(&p, rmode, scale, INT16_MIN, INT16_MAX, s);
3138 int32_t bfloat16_to_int32_scalbn(bfloat16 a, FloatRoundMode rmode, int scale,
3139 float_status *s)
3141 FloatParts64 p;
3143 bfloat16_unpack_canonical(&p, a, s);
3144 return parts_float_to_sint(&p, rmode, scale, INT32_MIN, INT32_MAX, s);
3147 int64_t bfloat16_to_int64_scalbn(bfloat16 a, FloatRoundMode rmode, int scale,
3148 float_status *s)
3150 FloatParts64 p;
3152 bfloat16_unpack_canonical(&p, a, s);
3153 return parts_float_to_sint(&p, rmode, scale, INT64_MIN, INT64_MAX, s);
3156 static int32_t float128_to_int32_scalbn(float128 a, FloatRoundMode rmode,
3157 int scale, float_status *s)
3159 FloatParts128 p;
3161 float128_unpack_canonical(&p, a, s);
3162 return parts_float_to_sint(&p, rmode, scale, INT32_MIN, INT32_MAX, s);
3165 static int64_t float128_to_int64_scalbn(float128 a, FloatRoundMode rmode,
3166 int scale, float_status *s)
3168 FloatParts128 p;
3170 float128_unpack_canonical(&p, a, s);
3171 return parts_float_to_sint(&p, rmode, scale, INT64_MIN, INT64_MAX, s);
3174 static Int128 float128_to_int128_scalbn(float128 a, FloatRoundMode rmode,
3175 int scale, float_status *s)
3177 int flags = 0;
3178 Int128 r;
3179 FloatParts128 p;
3181 float128_unpack_canonical(&p, a, s);
3183 switch (p.cls) {
3184 case float_class_snan:
3185 flags |= float_flag_invalid_snan;
3186 /* fall through */
3187 case float_class_qnan:
3188 flags |= float_flag_invalid;
3189 r = UINT128_MAX;
3190 break;
3192 case float_class_inf:
3193 flags = float_flag_invalid | float_flag_invalid_cvti;
3194 r = p.sign ? INT128_MIN : INT128_MAX;
3195 break;
3197 case float_class_zero:
3198 return int128_zero();
3200 case float_class_normal:
3201 if (parts_round_to_int_normal(&p, rmode, scale, 128 - 2)) {
3202 flags = float_flag_inexact;
3205 if (p.exp < 127) {
3206 int shift = 127 - p.exp;
3207 r = int128_urshift(int128_make128(p.frac_lo, p.frac_hi), shift);
3208 if (p.sign) {
3209 r = int128_neg(r);
3211 } else if (p.exp == 127 && p.sign && p.frac_lo == 0 &&
3212 p.frac_hi == DECOMPOSED_IMPLICIT_BIT) {
3213 r = INT128_MIN;
3214 } else {
3215 flags = float_flag_invalid | float_flag_invalid_cvti;
3216 r = p.sign ? INT128_MIN : INT128_MAX;
3218 break;
3220 default:
3221 g_assert_not_reached();
3224 float_raise(flags, s);
3225 return r;
3228 static int32_t floatx80_to_int32_scalbn(floatx80 a, FloatRoundMode rmode,
3229 int scale, float_status *s)
3231 FloatParts128 p;
3233 if (!floatx80_unpack_canonical(&p, a, s)) {
3234 parts_default_nan(&p, s);
3236 return parts_float_to_sint(&p, rmode, scale, INT32_MIN, INT32_MAX, s);
3239 static int64_t floatx80_to_int64_scalbn(floatx80 a, FloatRoundMode rmode,
3240 int scale, float_status *s)
3242 FloatParts128 p;
3244 if (!floatx80_unpack_canonical(&p, a, s)) {
3245 parts_default_nan(&p, s);
3247 return parts_float_to_sint(&p, rmode, scale, INT64_MIN, INT64_MAX, s);
3250 int8_t float16_to_int8(float16 a, float_status *s)
3252 return float16_to_int8_scalbn(a, s->float_rounding_mode, 0, s);
3255 int16_t float16_to_int16(float16 a, float_status *s)
3257 return float16_to_int16_scalbn(a, s->float_rounding_mode, 0, s);
3260 int32_t float16_to_int32(float16 a, float_status *s)
3262 return float16_to_int32_scalbn(a, s->float_rounding_mode, 0, s);
3265 int64_t float16_to_int64(float16 a, float_status *s)
3267 return float16_to_int64_scalbn(a, s->float_rounding_mode, 0, s);
3270 int16_t float32_to_int16(float32 a, float_status *s)
3272 return float32_to_int16_scalbn(a, s->float_rounding_mode, 0, s);
3275 int32_t float32_to_int32(float32 a, float_status *s)
3277 return float32_to_int32_scalbn(a, s->float_rounding_mode, 0, s);
3280 int64_t float32_to_int64(float32 a, float_status *s)
3282 return float32_to_int64_scalbn(a, s->float_rounding_mode, 0, s);
3285 int16_t float64_to_int16(float64 a, float_status *s)
3287 return float64_to_int16_scalbn(a, s->float_rounding_mode, 0, s);
3290 int32_t float64_to_int32(float64 a, float_status *s)
3292 return float64_to_int32_scalbn(a, s->float_rounding_mode, 0, s);
3295 int64_t float64_to_int64(float64 a, float_status *s)
3297 return float64_to_int64_scalbn(a, s->float_rounding_mode, 0, s);
3300 int32_t float128_to_int32(float128 a, float_status *s)
3302 return float128_to_int32_scalbn(a, s->float_rounding_mode, 0, s);
3305 int64_t float128_to_int64(float128 a, float_status *s)
3307 return float128_to_int64_scalbn(a, s->float_rounding_mode, 0, s);
3310 Int128 float128_to_int128(float128 a, float_status *s)
3312 return float128_to_int128_scalbn(a, s->float_rounding_mode, 0, s);
3315 int32_t floatx80_to_int32(floatx80 a, float_status *s)
3317 return floatx80_to_int32_scalbn(a, s->float_rounding_mode, 0, s);
3320 int64_t floatx80_to_int64(floatx80 a, float_status *s)
3322 return floatx80_to_int64_scalbn(a, s->float_rounding_mode, 0, s);
3325 int16_t float16_to_int16_round_to_zero(float16 a, float_status *s)
3327 return float16_to_int16_scalbn(a, float_round_to_zero, 0, s);
3330 int32_t float16_to_int32_round_to_zero(float16 a, float_status *s)
3332 return float16_to_int32_scalbn(a, float_round_to_zero, 0, s);
3335 int64_t float16_to_int64_round_to_zero(float16 a, float_status *s)
3337 return float16_to_int64_scalbn(a, float_round_to_zero, 0, s);
3340 int16_t float32_to_int16_round_to_zero(float32 a, float_status *s)
3342 return float32_to_int16_scalbn(a, float_round_to_zero, 0, s);
3345 int32_t float32_to_int32_round_to_zero(float32 a, float_status *s)
3347 return float32_to_int32_scalbn(a, float_round_to_zero, 0, s);
3350 int64_t float32_to_int64_round_to_zero(float32 a, float_status *s)
3352 return float32_to_int64_scalbn(a, float_round_to_zero, 0, s);
3355 int16_t float64_to_int16_round_to_zero(float64 a, float_status *s)
3357 return float64_to_int16_scalbn(a, float_round_to_zero, 0, s);
3360 int32_t float64_to_int32_round_to_zero(float64 a, float_status *s)
3362 return float64_to_int32_scalbn(a, float_round_to_zero, 0, s);
3365 int64_t float64_to_int64_round_to_zero(float64 a, float_status *s)
3367 return float64_to_int64_scalbn(a, float_round_to_zero, 0, s);
3370 int32_t float128_to_int32_round_to_zero(float128 a, float_status *s)
3372 return float128_to_int32_scalbn(a, float_round_to_zero, 0, s);
3375 int64_t float128_to_int64_round_to_zero(float128 a, float_status *s)
3377 return float128_to_int64_scalbn(a, float_round_to_zero, 0, s);
3380 Int128 float128_to_int128_round_to_zero(float128 a, float_status *s)
3382 return float128_to_int128_scalbn(a, float_round_to_zero, 0, s);
3385 int32_t floatx80_to_int32_round_to_zero(floatx80 a, float_status *s)
3387 return floatx80_to_int32_scalbn(a, float_round_to_zero, 0, s);
3390 int64_t floatx80_to_int64_round_to_zero(floatx80 a, float_status *s)
3392 return floatx80_to_int64_scalbn(a, float_round_to_zero, 0, s);
3395 int16_t bfloat16_to_int16(bfloat16 a, float_status *s)
3397 return bfloat16_to_int16_scalbn(a, s->float_rounding_mode, 0, s);
3400 int32_t bfloat16_to_int32(bfloat16 a, float_status *s)
3402 return bfloat16_to_int32_scalbn(a, s->float_rounding_mode, 0, s);
3405 int64_t bfloat16_to_int64(bfloat16 a, float_status *s)
3407 return bfloat16_to_int64_scalbn(a, s->float_rounding_mode, 0, s);
3410 int16_t bfloat16_to_int16_round_to_zero(bfloat16 a, float_status *s)
3412 return bfloat16_to_int16_scalbn(a, float_round_to_zero, 0, s);
3415 int32_t bfloat16_to_int32_round_to_zero(bfloat16 a, float_status *s)
3417 return bfloat16_to_int32_scalbn(a, float_round_to_zero, 0, s);
3420 int64_t bfloat16_to_int64_round_to_zero(bfloat16 a, float_status *s)
3422 return bfloat16_to_int64_scalbn(a, float_round_to_zero, 0, s);
3425 int32_t float64_to_int32_modulo(float64 a, FloatRoundMode rmode,
3426 float_status *s)
3428 FloatParts64 p;
3430 float64_unpack_canonical(&p, a, s);
3431 return parts_float_to_sint_modulo(&p, rmode, 31, s);
3434 int64_t float64_to_int64_modulo(float64 a, FloatRoundMode rmode,
3435 float_status *s)
3437 FloatParts64 p;
3439 float64_unpack_canonical(&p, a, s);
3440 return parts_float_to_sint_modulo(&p, rmode, 63, s);
3444 * Floating-point to unsigned integer conversions
3447 uint8_t float16_to_uint8_scalbn(float16 a, FloatRoundMode rmode, int scale,
3448 float_status *s)
3450 FloatParts64 p;
3452 float16_unpack_canonical(&p, a, s);
3453 return parts_float_to_uint(&p, rmode, scale, UINT8_MAX, s);
3456 uint16_t float16_to_uint16_scalbn(float16 a, FloatRoundMode rmode, int scale,
3457 float_status *s)
3459 FloatParts64 p;
3461 float16_unpack_canonical(&p, a, s);
3462 return parts_float_to_uint(&p, rmode, scale, UINT16_MAX, s);
3465 uint32_t float16_to_uint32_scalbn(float16 a, FloatRoundMode rmode, int scale,
3466 float_status *s)
3468 FloatParts64 p;
3470 float16_unpack_canonical(&p, a, s);
3471 return parts_float_to_uint(&p, rmode, scale, UINT32_MAX, s);
3474 uint64_t float16_to_uint64_scalbn(float16 a, FloatRoundMode rmode, int scale,
3475 float_status *s)
3477 FloatParts64 p;
3479 float16_unpack_canonical(&p, a, s);
3480 return parts_float_to_uint(&p, rmode, scale, UINT64_MAX, s);
3483 uint16_t float32_to_uint16_scalbn(float32 a, FloatRoundMode rmode, int scale,
3484 float_status *s)
3486 FloatParts64 p;
3488 float32_unpack_canonical(&p, a, s);
3489 return parts_float_to_uint(&p, rmode, scale, UINT16_MAX, s);
3492 uint32_t float32_to_uint32_scalbn(float32 a, FloatRoundMode rmode, int scale,
3493 float_status *s)
3495 FloatParts64 p;
3497 float32_unpack_canonical(&p, a, s);
3498 return parts_float_to_uint(&p, rmode, scale, UINT32_MAX, s);
3501 uint64_t float32_to_uint64_scalbn(float32 a, FloatRoundMode rmode, int scale,
3502 float_status *s)
3504 FloatParts64 p;
3506 float32_unpack_canonical(&p, a, s);
3507 return parts_float_to_uint(&p, rmode, scale, UINT64_MAX, s);
3510 uint16_t float64_to_uint16_scalbn(float64 a, FloatRoundMode rmode, int scale,
3511 float_status *s)
3513 FloatParts64 p;
3515 float64_unpack_canonical(&p, a, s);
3516 return parts_float_to_uint(&p, rmode, scale, UINT16_MAX, s);
3519 uint32_t float64_to_uint32_scalbn(float64 a, FloatRoundMode rmode, int scale,
3520 float_status *s)
3522 FloatParts64 p;
3524 float64_unpack_canonical(&p, a, s);
3525 return parts_float_to_uint(&p, rmode, scale, UINT32_MAX, s);
3528 uint64_t float64_to_uint64_scalbn(float64 a, FloatRoundMode rmode, int scale,
3529 float_status *s)
3531 FloatParts64 p;
3533 float64_unpack_canonical(&p, a, s);
3534 return parts_float_to_uint(&p, rmode, scale, UINT64_MAX, s);
3537 uint16_t bfloat16_to_uint16_scalbn(bfloat16 a, FloatRoundMode rmode,
3538 int scale, float_status *s)
3540 FloatParts64 p;
3542 bfloat16_unpack_canonical(&p, a, s);
3543 return parts_float_to_uint(&p, rmode, scale, UINT16_MAX, s);
3546 uint32_t bfloat16_to_uint32_scalbn(bfloat16 a, FloatRoundMode rmode,
3547 int scale, float_status *s)
3549 FloatParts64 p;
3551 bfloat16_unpack_canonical(&p, a, s);
3552 return parts_float_to_uint(&p, rmode, scale, UINT32_MAX, s);
3555 uint64_t bfloat16_to_uint64_scalbn(bfloat16 a, FloatRoundMode rmode,
3556 int scale, float_status *s)
3558 FloatParts64 p;
3560 bfloat16_unpack_canonical(&p, a, s);
3561 return parts_float_to_uint(&p, rmode, scale, UINT64_MAX, s);
3564 static uint32_t float128_to_uint32_scalbn(float128 a, FloatRoundMode rmode,
3565 int scale, float_status *s)
3567 FloatParts128 p;
3569 float128_unpack_canonical(&p, a, s);
3570 return parts_float_to_uint(&p, rmode, scale, UINT32_MAX, s);
3573 static uint64_t float128_to_uint64_scalbn(float128 a, FloatRoundMode rmode,
3574 int scale, float_status *s)
3576 FloatParts128 p;
3578 float128_unpack_canonical(&p, a, s);
3579 return parts_float_to_uint(&p, rmode, scale, UINT64_MAX, s);
3582 static Int128 float128_to_uint128_scalbn(float128 a, FloatRoundMode rmode,
3583 int scale, float_status *s)
3585 int flags = 0;
3586 Int128 r;
3587 FloatParts128 p;
3589 float128_unpack_canonical(&p, a, s);
3591 switch (p.cls) {
3592 case float_class_snan:
3593 flags |= float_flag_invalid_snan;
3594 /* fall through */
3595 case float_class_qnan:
3596 flags |= float_flag_invalid;
3597 r = UINT128_MAX;
3598 break;
3600 case float_class_inf:
3601 flags = float_flag_invalid | float_flag_invalid_cvti;
3602 r = p.sign ? int128_zero() : UINT128_MAX;
3603 break;
3605 case float_class_zero:
3606 return int128_zero();
3608 case float_class_normal:
3609 if (parts_round_to_int_normal(&p, rmode, scale, 128 - 2)) {
3610 flags = float_flag_inexact;
3611 if (p.cls == float_class_zero) {
3612 r = int128_zero();
3613 break;
3617 if (p.sign) {
3618 flags = float_flag_invalid | float_flag_invalid_cvti;
3619 r = int128_zero();
3620 } else if (p.exp <= 127) {
3621 int shift = 127 - p.exp;
3622 r = int128_urshift(int128_make128(p.frac_lo, p.frac_hi), shift);
3623 } else {
3624 flags = float_flag_invalid | float_flag_invalid_cvti;
3625 r = UINT128_MAX;
3627 break;
3629 default:
3630 g_assert_not_reached();
3633 float_raise(flags, s);
3634 return r;
3637 uint8_t float16_to_uint8(float16 a, float_status *s)
3639 return float16_to_uint8_scalbn(a, s->float_rounding_mode, 0, s);
3642 uint16_t float16_to_uint16(float16 a, float_status *s)
3644 return float16_to_uint16_scalbn(a, s->float_rounding_mode, 0, s);
3647 uint32_t float16_to_uint32(float16 a, float_status *s)
3649 return float16_to_uint32_scalbn(a, s->float_rounding_mode, 0, s);
3652 uint64_t float16_to_uint64(float16 a, float_status *s)
3654 return float16_to_uint64_scalbn(a, s->float_rounding_mode, 0, s);
3657 uint16_t float32_to_uint16(float32 a, float_status *s)
3659 return float32_to_uint16_scalbn(a, s->float_rounding_mode, 0, s);
3662 uint32_t float32_to_uint32(float32 a, float_status *s)
3664 return float32_to_uint32_scalbn(a, s->float_rounding_mode, 0, s);
3667 uint64_t float32_to_uint64(float32 a, float_status *s)
3669 return float32_to_uint64_scalbn(a, s->float_rounding_mode, 0, s);
3672 uint16_t float64_to_uint16(float64 a, float_status *s)
3674 return float64_to_uint16_scalbn(a, s->float_rounding_mode, 0, s);
3677 uint32_t float64_to_uint32(float64 a, float_status *s)
3679 return float64_to_uint32_scalbn(a, s->float_rounding_mode, 0, s);
3682 uint64_t float64_to_uint64(float64 a, float_status *s)
3684 return float64_to_uint64_scalbn(a, s->float_rounding_mode, 0, s);
3687 uint32_t float128_to_uint32(float128 a, float_status *s)
3689 return float128_to_uint32_scalbn(a, s->float_rounding_mode, 0, s);
3692 uint64_t float128_to_uint64(float128 a, float_status *s)
3694 return float128_to_uint64_scalbn(a, s->float_rounding_mode, 0, s);
3697 Int128 float128_to_uint128(float128 a, float_status *s)
3699 return float128_to_uint128_scalbn(a, s->float_rounding_mode, 0, s);
3702 uint16_t float16_to_uint16_round_to_zero(float16 a, float_status *s)
3704 return float16_to_uint16_scalbn(a, float_round_to_zero, 0, s);
3707 uint32_t float16_to_uint32_round_to_zero(float16 a, float_status *s)
3709 return float16_to_uint32_scalbn(a, float_round_to_zero, 0, s);
3712 uint64_t float16_to_uint64_round_to_zero(float16 a, float_status *s)
3714 return float16_to_uint64_scalbn(a, float_round_to_zero, 0, s);
3717 uint16_t float32_to_uint16_round_to_zero(float32 a, float_status *s)
3719 return float32_to_uint16_scalbn(a, float_round_to_zero, 0, s);
3722 uint32_t float32_to_uint32_round_to_zero(float32 a, float_status *s)
3724 return float32_to_uint32_scalbn(a, float_round_to_zero, 0, s);
3727 uint64_t float32_to_uint64_round_to_zero(float32 a, float_status *s)
3729 return float32_to_uint64_scalbn(a, float_round_to_zero, 0, s);
3732 uint16_t float64_to_uint16_round_to_zero(float64 a, float_status *s)
3734 return float64_to_uint16_scalbn(a, float_round_to_zero, 0, s);
3737 uint32_t float64_to_uint32_round_to_zero(float64 a, float_status *s)
3739 return float64_to_uint32_scalbn(a, float_round_to_zero, 0, s);
3742 uint64_t float64_to_uint64_round_to_zero(float64 a, float_status *s)
3744 return float64_to_uint64_scalbn(a, float_round_to_zero, 0, s);
3747 uint32_t float128_to_uint32_round_to_zero(float128 a, float_status *s)
3749 return float128_to_uint32_scalbn(a, float_round_to_zero, 0, s);
3752 uint64_t float128_to_uint64_round_to_zero(float128 a, float_status *s)
3754 return float128_to_uint64_scalbn(a, float_round_to_zero, 0, s);
3757 Int128 float128_to_uint128_round_to_zero(float128 a, float_status *s)
3759 return float128_to_uint128_scalbn(a, float_round_to_zero, 0, s);
3762 uint16_t bfloat16_to_uint16(bfloat16 a, float_status *s)
3764 return bfloat16_to_uint16_scalbn(a, s->float_rounding_mode, 0, s);
3767 uint32_t bfloat16_to_uint32(bfloat16 a, float_status *s)
3769 return bfloat16_to_uint32_scalbn(a, s->float_rounding_mode, 0, s);
3772 uint64_t bfloat16_to_uint64(bfloat16 a, float_status *s)
3774 return bfloat16_to_uint64_scalbn(a, s->float_rounding_mode, 0, s);
3777 uint16_t bfloat16_to_uint16_round_to_zero(bfloat16 a, float_status *s)
3779 return bfloat16_to_uint16_scalbn(a, float_round_to_zero, 0, s);
3782 uint32_t bfloat16_to_uint32_round_to_zero(bfloat16 a, float_status *s)
3784 return bfloat16_to_uint32_scalbn(a, float_round_to_zero, 0, s);
3787 uint64_t bfloat16_to_uint64_round_to_zero(bfloat16 a, float_status *s)
3789 return bfloat16_to_uint64_scalbn(a, float_round_to_zero, 0, s);
3793 * Signed integer to floating-point conversions
3796 float16 int64_to_float16_scalbn(int64_t a, int scale, float_status *status)
3798 FloatParts64 p;
3800 parts_sint_to_float(&p, a, scale, status);
3801 return float16_round_pack_canonical(&p, status);
3804 float16 int32_to_float16_scalbn(int32_t a, int scale, float_status *status)
3806 return int64_to_float16_scalbn(a, scale, status);
3809 float16 int16_to_float16_scalbn(int16_t a, int scale, float_status *status)
3811 return int64_to_float16_scalbn(a, scale, status);
3814 float16 int64_to_float16(int64_t a, float_status *status)
3816 return int64_to_float16_scalbn(a, 0, status);
3819 float16 int32_to_float16(int32_t a, float_status *status)
3821 return int64_to_float16_scalbn(a, 0, status);
3824 float16 int16_to_float16(int16_t a, float_status *status)
3826 return int64_to_float16_scalbn(a, 0, status);
3829 float16 int8_to_float16(int8_t a, float_status *status)
3831 return int64_to_float16_scalbn(a, 0, status);
3834 float32 int64_to_float32_scalbn(int64_t a, int scale, float_status *status)
3836 FloatParts64 p;
3838 /* Without scaling, there are no overflow concerns. */
3839 if (likely(scale == 0) && can_use_fpu(status)) {
3840 union_float32 ur;
3841 ur.h = a;
3842 return ur.s;
3845 parts64_sint_to_float(&p, a, scale, status);
3846 return float32_round_pack_canonical(&p, status);
3849 float32 int32_to_float32_scalbn(int32_t a, int scale, float_status *status)
3851 return int64_to_float32_scalbn(a, scale, status);
3854 float32 int16_to_float32_scalbn(int16_t a, int scale, float_status *status)
3856 return int64_to_float32_scalbn(a, scale, status);
3859 float32 int64_to_float32(int64_t a, float_status *status)
3861 return int64_to_float32_scalbn(a, 0, status);
3864 float32 int32_to_float32(int32_t a, float_status *status)
3866 return int64_to_float32_scalbn(a, 0, status);
3869 float32 int16_to_float32(int16_t a, float_status *status)
3871 return int64_to_float32_scalbn(a, 0, status);
3874 float64 int64_to_float64_scalbn(int64_t a, int scale, float_status *status)
3876 FloatParts64 p;
3878 /* Without scaling, there are no overflow concerns. */
3879 if (likely(scale == 0) && can_use_fpu(status)) {
3880 union_float64 ur;
3881 ur.h = a;
3882 return ur.s;
3885 parts_sint_to_float(&p, a, scale, status);
3886 return float64_round_pack_canonical(&p, status);
3889 float64 int32_to_float64_scalbn(int32_t a, int scale, float_status *status)
3891 return int64_to_float64_scalbn(a, scale, status);
3894 float64 int16_to_float64_scalbn(int16_t a, int scale, float_status *status)
3896 return int64_to_float64_scalbn(a, scale, status);
3899 float64 int64_to_float64(int64_t a, float_status *status)
3901 return int64_to_float64_scalbn(a, 0, status);
3904 float64 int32_to_float64(int32_t a, float_status *status)
3906 return int64_to_float64_scalbn(a, 0, status);
3909 float64 int16_to_float64(int16_t a, float_status *status)
3911 return int64_to_float64_scalbn(a, 0, status);
3914 bfloat16 int64_to_bfloat16_scalbn(int64_t a, int scale, float_status *status)
3916 FloatParts64 p;
3918 parts_sint_to_float(&p, a, scale, status);
3919 return bfloat16_round_pack_canonical(&p, status);
3922 bfloat16 int32_to_bfloat16_scalbn(int32_t a, int scale, float_status *status)
3924 return int64_to_bfloat16_scalbn(a, scale, status);
3927 bfloat16 int16_to_bfloat16_scalbn(int16_t a, int scale, float_status *status)
3929 return int64_to_bfloat16_scalbn(a, scale, status);
3932 bfloat16 int64_to_bfloat16(int64_t a, float_status *status)
3934 return int64_to_bfloat16_scalbn(a, 0, status);
3937 bfloat16 int32_to_bfloat16(int32_t a, float_status *status)
3939 return int64_to_bfloat16_scalbn(a, 0, status);
3942 bfloat16 int16_to_bfloat16(int16_t a, float_status *status)
3944 return int64_to_bfloat16_scalbn(a, 0, status);
3947 float128 int128_to_float128(Int128 a, float_status *status)
3949 FloatParts128 p = { };
3950 int shift;
3952 if (int128_nz(a)) {
3953 p.cls = float_class_normal;
3954 if (!int128_nonneg(a)) {
3955 p.sign = true;
3956 a = int128_neg(a);
3959 shift = clz64(int128_gethi(a));
3960 if (shift == 64) {
3961 shift += clz64(int128_getlo(a));
3964 p.exp = 127 - shift;
3965 a = int128_lshift(a, shift);
3967 p.frac_hi = int128_gethi(a);
3968 p.frac_lo = int128_getlo(a);
3969 } else {
3970 p.cls = float_class_zero;
3973 return float128_round_pack_canonical(&p, status);
3976 float128 int64_to_float128(int64_t a, float_status *status)
3978 FloatParts128 p;
3980 parts_sint_to_float(&p, a, 0, status);
3981 return float128_round_pack_canonical(&p, status);
3984 float128 int32_to_float128(int32_t a, float_status *status)
3986 return int64_to_float128(a, status);
3989 floatx80 int64_to_floatx80(int64_t a, float_status *status)
3991 FloatParts128 p;
3993 parts_sint_to_float(&p, a, 0, status);
3994 return floatx80_round_pack_canonical(&p, status);
3997 floatx80 int32_to_floatx80(int32_t a, float_status *status)
3999 return int64_to_floatx80(a, status);
4003 * Unsigned Integer to floating-point conversions
4006 float16 uint64_to_float16_scalbn(uint64_t a, int scale, float_status *status)
4008 FloatParts64 p;
4010 parts_uint_to_float(&p, a, scale, status);
4011 return float16_round_pack_canonical(&p, status);
4014 float16 uint32_to_float16_scalbn(uint32_t a, int scale, float_status *status)
4016 return uint64_to_float16_scalbn(a, scale, status);
4019 float16 uint16_to_float16_scalbn(uint16_t a, int scale, float_status *status)
4021 return uint64_to_float16_scalbn(a, scale, status);
4024 float16 uint64_to_float16(uint64_t a, float_status *status)
4026 return uint64_to_float16_scalbn(a, 0, status);
4029 float16 uint32_to_float16(uint32_t a, float_status *status)
4031 return uint64_to_float16_scalbn(a, 0, status);
4034 float16 uint16_to_float16(uint16_t a, float_status *status)
4036 return uint64_to_float16_scalbn(a, 0, status);
4039 float16 uint8_to_float16(uint8_t a, float_status *status)
4041 return uint64_to_float16_scalbn(a, 0, status);
4044 float32 uint64_to_float32_scalbn(uint64_t a, int scale, float_status *status)
4046 FloatParts64 p;
4048 /* Without scaling, there are no overflow concerns. */
4049 if (likely(scale == 0) && can_use_fpu(status)) {
4050 union_float32 ur;
4051 ur.h = a;
4052 return ur.s;
4055 parts_uint_to_float(&p, a, scale, status);
4056 return float32_round_pack_canonical(&p, status);
4059 float32 uint32_to_float32_scalbn(uint32_t a, int scale, float_status *status)
4061 return uint64_to_float32_scalbn(a, scale, status);
4064 float32 uint16_to_float32_scalbn(uint16_t a, int scale, float_status *status)
4066 return uint64_to_float32_scalbn(a, scale, status);
4069 float32 uint64_to_float32(uint64_t a, float_status *status)
4071 return uint64_to_float32_scalbn(a, 0, status);
4074 float32 uint32_to_float32(uint32_t a, float_status *status)
4076 return uint64_to_float32_scalbn(a, 0, status);
4079 float32 uint16_to_float32(uint16_t a, float_status *status)
4081 return uint64_to_float32_scalbn(a, 0, status);
4084 float64 uint64_to_float64_scalbn(uint64_t a, int scale, float_status *status)
4086 FloatParts64 p;
4088 /* Without scaling, there are no overflow concerns. */
4089 if (likely(scale == 0) && can_use_fpu(status)) {
4090 union_float64 ur;
4091 ur.h = a;
4092 return ur.s;
4095 parts_uint_to_float(&p, a, scale, status);
4096 return float64_round_pack_canonical(&p, status);
4099 float64 uint32_to_float64_scalbn(uint32_t a, int scale, float_status *status)
4101 return uint64_to_float64_scalbn(a, scale, status);
4104 float64 uint16_to_float64_scalbn(uint16_t a, int scale, float_status *status)
4106 return uint64_to_float64_scalbn(a, scale, status);
4109 float64 uint64_to_float64(uint64_t a, float_status *status)
4111 return uint64_to_float64_scalbn(a, 0, status);
4114 float64 uint32_to_float64(uint32_t a, float_status *status)
4116 return uint64_to_float64_scalbn(a, 0, status);
4119 float64 uint16_to_float64(uint16_t a, float_status *status)
4121 return uint64_to_float64_scalbn(a, 0, status);
4124 bfloat16 uint64_to_bfloat16_scalbn(uint64_t a, int scale, float_status *status)
4126 FloatParts64 p;
4128 parts_uint_to_float(&p, a, scale, status);
4129 return bfloat16_round_pack_canonical(&p, status);
4132 bfloat16 uint32_to_bfloat16_scalbn(uint32_t a, int scale, float_status *status)
4134 return uint64_to_bfloat16_scalbn(a, scale, status);
4137 bfloat16 uint16_to_bfloat16_scalbn(uint16_t a, int scale, float_status *status)
4139 return uint64_to_bfloat16_scalbn(a, scale, status);
4142 bfloat16 uint64_to_bfloat16(uint64_t a, float_status *status)
4144 return uint64_to_bfloat16_scalbn(a, 0, status);
4147 bfloat16 uint32_to_bfloat16(uint32_t a, float_status *status)
4149 return uint64_to_bfloat16_scalbn(a, 0, status);
4152 bfloat16 uint16_to_bfloat16(uint16_t a, float_status *status)
4154 return uint64_to_bfloat16_scalbn(a, 0, status);
4157 float128 uint64_to_float128(uint64_t a, float_status *status)
4159 FloatParts128 p;
4161 parts_uint_to_float(&p, a, 0, status);
4162 return float128_round_pack_canonical(&p, status);
4165 float128 uint128_to_float128(Int128 a, float_status *status)
4167 FloatParts128 p = { };
4168 int shift;
4170 if (int128_nz(a)) {
4171 p.cls = float_class_normal;
4173 shift = clz64(int128_gethi(a));
4174 if (shift == 64) {
4175 shift += clz64(int128_getlo(a));
4178 p.exp = 127 - shift;
4179 a = int128_lshift(a, shift);
4181 p.frac_hi = int128_gethi(a);
4182 p.frac_lo = int128_getlo(a);
4183 } else {
4184 p.cls = float_class_zero;
4187 return float128_round_pack_canonical(&p, status);
4191 * Minimum and maximum
4194 static float16 float16_minmax(float16 a, float16 b, float_status *s, int flags)
4196 FloatParts64 pa, pb, *pr;
4198 float16_unpack_canonical(&pa, a, s);
4199 float16_unpack_canonical(&pb, b, s);
4200 pr = parts_minmax(&pa, &pb, s, flags);
4202 return float16_round_pack_canonical(pr, s);
4205 static bfloat16 bfloat16_minmax(bfloat16 a, bfloat16 b,
4206 float_status *s, int flags)
4208 FloatParts64 pa, pb, *pr;
4210 bfloat16_unpack_canonical(&pa, a, s);
4211 bfloat16_unpack_canonical(&pb, b, s);
4212 pr = parts_minmax(&pa, &pb, s, flags);
4214 return bfloat16_round_pack_canonical(pr, s);
4217 static float32 float32_minmax(float32 a, float32 b, float_status *s, int flags)
4219 FloatParts64 pa, pb, *pr;
4221 float32_unpack_canonical(&pa, a, s);
4222 float32_unpack_canonical(&pb, b, s);
4223 pr = parts_minmax(&pa, &pb, s, flags);
4225 return float32_round_pack_canonical(pr, s);
4228 static float64 float64_minmax(float64 a, float64 b, float_status *s, int flags)
4230 FloatParts64 pa, pb, *pr;
4232 float64_unpack_canonical(&pa, a, s);
4233 float64_unpack_canonical(&pb, b, s);
4234 pr = parts_minmax(&pa, &pb, s, flags);
4236 return float64_round_pack_canonical(pr, s);
4239 static float128 float128_minmax(float128 a, float128 b,
4240 float_status *s, int flags)
4242 FloatParts128 pa, pb, *pr;
4244 float128_unpack_canonical(&pa, a, s);
4245 float128_unpack_canonical(&pb, b, s);
4246 pr = parts_minmax(&pa, &pb, s, flags);
4248 return float128_round_pack_canonical(pr, s);
4251 #define MINMAX_1(type, name, flags) \
4252 type type##_##name(type a, type b, float_status *s) \
4253 { return type##_minmax(a, b, s, flags); }
4255 #define MINMAX_2(type) \
4256 MINMAX_1(type, max, 0) \
4257 MINMAX_1(type, maxnum, minmax_isnum) \
4258 MINMAX_1(type, maxnummag, minmax_isnum | minmax_ismag) \
4259 MINMAX_1(type, maximum_number, minmax_isnumber) \
4260 MINMAX_1(type, min, minmax_ismin) \
4261 MINMAX_1(type, minnum, minmax_ismin | minmax_isnum) \
4262 MINMAX_1(type, minnummag, minmax_ismin | minmax_isnum | minmax_ismag) \
4263 MINMAX_1(type, minimum_number, minmax_ismin | minmax_isnumber) \
4265 MINMAX_2(float16)
4266 MINMAX_2(bfloat16)
4267 MINMAX_2(float32)
4268 MINMAX_2(float64)
4269 MINMAX_2(float128)
4271 #undef MINMAX_1
4272 #undef MINMAX_2
4275 * Floating point compare
4278 static FloatRelation QEMU_FLATTEN
4279 float16_do_compare(float16 a, float16 b, float_status *s, bool is_quiet)
4281 FloatParts64 pa, pb;
4283 float16_unpack_canonical(&pa, a, s);
4284 float16_unpack_canonical(&pb, b, s);
4285 return parts_compare(&pa, &pb, s, is_quiet);
4288 FloatRelation float16_compare(float16 a, float16 b, float_status *s)
4290 return float16_do_compare(a, b, s, false);
4293 FloatRelation float16_compare_quiet(float16 a, float16 b, float_status *s)
4295 return float16_do_compare(a, b, s, true);
4298 static FloatRelation QEMU_SOFTFLOAT_ATTR
4299 float32_do_compare(float32 a, float32 b, float_status *s, bool is_quiet)
4301 FloatParts64 pa, pb;
4303 float32_unpack_canonical(&pa, a, s);
4304 float32_unpack_canonical(&pb, b, s);
4305 return parts_compare(&pa, &pb, s, is_quiet);
4308 static FloatRelation QEMU_FLATTEN
4309 float32_hs_compare(float32 xa, float32 xb, float_status *s, bool is_quiet)
4311 union_float32 ua, ub;
4313 ua.s = xa;
4314 ub.s = xb;
4316 if (QEMU_NO_HARDFLOAT) {
4317 goto soft;
4320 float32_input_flush2(&ua.s, &ub.s, s);
4321 if (isgreaterequal(ua.h, ub.h)) {
4322 if (isgreater(ua.h, ub.h)) {
4323 return float_relation_greater;
4325 return float_relation_equal;
4327 if (likely(isless(ua.h, ub.h))) {
4328 return float_relation_less;
4331 * The only condition remaining is unordered.
4332 * Fall through to set flags.
4334 soft:
4335 return float32_do_compare(ua.s, ub.s, s, is_quiet);
4338 FloatRelation float32_compare(float32 a, float32 b, float_status *s)
4340 return float32_hs_compare(a, b, s, false);
4343 FloatRelation float32_compare_quiet(float32 a, float32 b, float_status *s)
4345 return float32_hs_compare(a, b, s, true);
4348 static FloatRelation QEMU_SOFTFLOAT_ATTR
4349 float64_do_compare(float64 a, float64 b, float_status *s, bool is_quiet)
4351 FloatParts64 pa, pb;
4353 float64_unpack_canonical(&pa, a, s);
4354 float64_unpack_canonical(&pb, b, s);
4355 return parts_compare(&pa, &pb, s, is_quiet);
4358 static FloatRelation QEMU_FLATTEN
4359 float64_hs_compare(float64 xa, float64 xb, float_status *s, bool is_quiet)
4361 union_float64 ua, ub;
4363 ua.s = xa;
4364 ub.s = xb;
4366 if (QEMU_NO_HARDFLOAT) {
4367 goto soft;
4370 float64_input_flush2(&ua.s, &ub.s, s);
4371 if (isgreaterequal(ua.h, ub.h)) {
4372 if (isgreater(ua.h, ub.h)) {
4373 return float_relation_greater;
4375 return float_relation_equal;
4377 if (likely(isless(ua.h, ub.h))) {
4378 return float_relation_less;
4381 * The only condition remaining is unordered.
4382 * Fall through to set flags.
4384 soft:
4385 return float64_do_compare(ua.s, ub.s, s, is_quiet);
4388 FloatRelation float64_compare(float64 a, float64 b, float_status *s)
4390 return float64_hs_compare(a, b, s, false);
4393 FloatRelation float64_compare_quiet(float64 a, float64 b, float_status *s)
4395 return float64_hs_compare(a, b, s, true);
4398 static FloatRelation QEMU_FLATTEN
4399 bfloat16_do_compare(bfloat16 a, bfloat16 b, float_status *s, bool is_quiet)
4401 FloatParts64 pa, pb;
4403 bfloat16_unpack_canonical(&pa, a, s);
4404 bfloat16_unpack_canonical(&pb, b, s);
4405 return parts_compare(&pa, &pb, s, is_quiet);
4408 FloatRelation bfloat16_compare(bfloat16 a, bfloat16 b, float_status *s)
4410 return bfloat16_do_compare(a, b, s, false);
4413 FloatRelation bfloat16_compare_quiet(bfloat16 a, bfloat16 b, float_status *s)
4415 return bfloat16_do_compare(a, b, s, true);
4418 static FloatRelation QEMU_FLATTEN
4419 float128_do_compare(float128 a, float128 b, float_status *s, bool is_quiet)
4421 FloatParts128 pa, pb;
4423 float128_unpack_canonical(&pa, a, s);
4424 float128_unpack_canonical(&pb, b, s);
4425 return parts_compare(&pa, &pb, s, is_quiet);
4428 FloatRelation float128_compare(float128 a, float128 b, float_status *s)
4430 return float128_do_compare(a, b, s, false);
4433 FloatRelation float128_compare_quiet(float128 a, float128 b, float_status *s)
4435 return float128_do_compare(a, b, s, true);
4438 static FloatRelation QEMU_FLATTEN
4439 floatx80_do_compare(floatx80 a, floatx80 b, float_status *s, bool is_quiet)
4441 FloatParts128 pa, pb;
4443 if (!floatx80_unpack_canonical(&pa, a, s) ||
4444 !floatx80_unpack_canonical(&pb, b, s)) {
4445 return float_relation_unordered;
4447 return parts_compare(&pa, &pb, s, is_quiet);
4450 FloatRelation floatx80_compare(floatx80 a, floatx80 b, float_status *s)
4452 return floatx80_do_compare(a, b, s, false);
4455 FloatRelation floatx80_compare_quiet(floatx80 a, floatx80 b, float_status *s)
4457 return floatx80_do_compare(a, b, s, true);
4461 * Scale by 2**N
4464 float16 float16_scalbn(float16 a, int n, float_status *status)
4466 FloatParts64 p;
4468 float16_unpack_canonical(&p, a, status);
4469 parts_scalbn(&p, n, status);
4470 return float16_round_pack_canonical(&p, status);
4473 float32 float32_scalbn(float32 a, int n, float_status *status)
4475 FloatParts64 p;
4477 float32_unpack_canonical(&p, a, status);
4478 parts_scalbn(&p, n, status);
4479 return float32_round_pack_canonical(&p, status);
4482 float64 float64_scalbn(float64 a, int n, float_status *status)
4484 FloatParts64 p;
4486 float64_unpack_canonical(&p, a, status);
4487 parts_scalbn(&p, n, status);
4488 return float64_round_pack_canonical(&p, status);
4491 bfloat16 bfloat16_scalbn(bfloat16 a, int n, float_status *status)
4493 FloatParts64 p;
4495 bfloat16_unpack_canonical(&p, a, status);
4496 parts_scalbn(&p, n, status);
4497 return bfloat16_round_pack_canonical(&p, status);
4500 float128 float128_scalbn(float128 a, int n, float_status *status)
4502 FloatParts128 p;
4504 float128_unpack_canonical(&p, a, status);
4505 parts_scalbn(&p, n, status);
4506 return float128_round_pack_canonical(&p, status);
4509 floatx80 floatx80_scalbn(floatx80 a, int n, float_status *status)
4511 FloatParts128 p;
4513 if (!floatx80_unpack_canonical(&p, a, status)) {
4514 return floatx80_default_nan(status);
4516 parts_scalbn(&p, n, status);
4517 return floatx80_round_pack_canonical(&p, status);
4521 * Square Root
4524 float16 QEMU_FLATTEN float16_sqrt(float16 a, float_status *status)
4526 FloatParts64 p;
4528 float16_unpack_canonical(&p, a, status);
4529 parts_sqrt(&p, status, &float16_params);
4530 return float16_round_pack_canonical(&p, status);
4533 static float32 QEMU_SOFTFLOAT_ATTR
4534 soft_f32_sqrt(float32 a, float_status *status)
4536 FloatParts64 p;
4538 float32_unpack_canonical(&p, a, status);
4539 parts_sqrt(&p, status, &float32_params);
4540 return float32_round_pack_canonical(&p, status);
4543 static float64 QEMU_SOFTFLOAT_ATTR
4544 soft_f64_sqrt(float64 a, float_status *status)
4546 FloatParts64 p;
4548 float64_unpack_canonical(&p, a, status);
4549 parts_sqrt(&p, status, &float64_params);
4550 return float64_round_pack_canonical(&p, status);
4553 float32 QEMU_FLATTEN float32_sqrt(float32 xa, float_status *s)
4555 union_float32 ua, ur;
4557 ua.s = xa;
4558 if (unlikely(!can_use_fpu(s))) {
4559 goto soft;
4562 float32_input_flush1(&ua.s, s);
4563 if (QEMU_HARDFLOAT_1F32_USE_FP) {
4564 if (unlikely(!(fpclassify(ua.h) == FP_NORMAL ||
4565 fpclassify(ua.h) == FP_ZERO) ||
4566 signbit(ua.h))) {
4567 goto soft;
4569 } else if (unlikely(!float32_is_zero_or_normal(ua.s) ||
4570 float32_is_neg(ua.s))) {
4571 goto soft;
4573 ur.h = sqrtf(ua.h);
4574 return ur.s;
4576 soft:
4577 return soft_f32_sqrt(ua.s, s);
4580 float64 QEMU_FLATTEN float64_sqrt(float64 xa, float_status *s)
4582 union_float64 ua, ur;
4584 ua.s = xa;
4585 if (unlikely(!can_use_fpu(s))) {
4586 goto soft;
4589 float64_input_flush1(&ua.s, s);
4590 if (QEMU_HARDFLOAT_1F64_USE_FP) {
4591 if (unlikely(!(fpclassify(ua.h) == FP_NORMAL ||
4592 fpclassify(ua.h) == FP_ZERO) ||
4593 signbit(ua.h))) {
4594 goto soft;
4596 } else if (unlikely(!float64_is_zero_or_normal(ua.s) ||
4597 float64_is_neg(ua.s))) {
4598 goto soft;
4600 ur.h = sqrt(ua.h);
4601 return ur.s;
4603 soft:
4604 return soft_f64_sqrt(ua.s, s);
4607 float64 float64r32_sqrt(float64 a, float_status *status)
4609 FloatParts64 p;
4611 float64_unpack_canonical(&p, a, status);
4612 parts_sqrt(&p, status, &float64_params);
4613 return float64r32_round_pack_canonical(&p, status);
4616 bfloat16 QEMU_FLATTEN bfloat16_sqrt(bfloat16 a, float_status *status)
4618 FloatParts64 p;
4620 bfloat16_unpack_canonical(&p, a, status);
4621 parts_sqrt(&p, status, &bfloat16_params);
4622 return bfloat16_round_pack_canonical(&p, status);
4625 float128 QEMU_FLATTEN float128_sqrt(float128 a, float_status *status)
4627 FloatParts128 p;
4629 float128_unpack_canonical(&p, a, status);
4630 parts_sqrt(&p, status, &float128_params);
4631 return float128_round_pack_canonical(&p, status);
4634 floatx80 floatx80_sqrt(floatx80 a, float_status *s)
4636 FloatParts128 p;
4638 if (!floatx80_unpack_canonical(&p, a, s)) {
4639 return floatx80_default_nan(s);
4641 parts_sqrt(&p, s, &floatx80_params[s->floatx80_rounding_precision]);
4642 return floatx80_round_pack_canonical(&p, s);
4646 * log2
4648 float32 float32_log2(float32 a, float_status *status)
4650 FloatParts64 p;
4652 float32_unpack_canonical(&p, a, status);
4653 parts_log2(&p, status, &float32_params);
4654 return float32_round_pack_canonical(&p, status);
4657 float64 float64_log2(float64 a, float_status *status)
4659 FloatParts64 p;
4661 float64_unpack_canonical(&p, a, status);
4662 parts_log2(&p, status, &float64_params);
4663 return float64_round_pack_canonical(&p, status);
4666 /*----------------------------------------------------------------------------
4667 | The pattern for a default generated NaN.
4668 *----------------------------------------------------------------------------*/
4670 float16 float16_default_nan(float_status *status)
4672 FloatParts64 p;
4674 parts_default_nan(&p, status);
4675 p.frac >>= float16_params.frac_shift;
4676 return float16_pack_raw(&p);
4679 float32 float32_default_nan(float_status *status)
4681 FloatParts64 p;
4683 parts_default_nan(&p, status);
4684 p.frac >>= float32_params.frac_shift;
4685 return float32_pack_raw(&p);
4688 float64 float64_default_nan(float_status *status)
4690 FloatParts64 p;
4692 parts_default_nan(&p, status);
4693 p.frac >>= float64_params.frac_shift;
4694 return float64_pack_raw(&p);
4697 float128 float128_default_nan(float_status *status)
4699 FloatParts128 p;
4701 parts_default_nan(&p, status);
4702 frac_shr(&p, float128_params.frac_shift);
4703 return float128_pack_raw(&p);
4706 bfloat16 bfloat16_default_nan(float_status *status)
4708 FloatParts64 p;
4710 parts_default_nan(&p, status);
4711 p.frac >>= bfloat16_params.frac_shift;
4712 return bfloat16_pack_raw(&p);
4715 /*----------------------------------------------------------------------------
4716 | Returns a quiet NaN from a signalling NaN for the floating point value `a'.
4717 *----------------------------------------------------------------------------*/
4719 float16 float16_silence_nan(float16 a, float_status *status)
4721 FloatParts64 p;
4723 float16_unpack_raw(&p, a);
4724 p.frac <<= float16_params.frac_shift;
4725 parts_silence_nan(&p, status);
4726 p.frac >>= float16_params.frac_shift;
4727 return float16_pack_raw(&p);
4730 float32 float32_silence_nan(float32 a, float_status *status)
4732 FloatParts64 p;
4734 float32_unpack_raw(&p, a);
4735 p.frac <<= float32_params.frac_shift;
4736 parts_silence_nan(&p, status);
4737 p.frac >>= float32_params.frac_shift;
4738 return float32_pack_raw(&p);
4741 float64 float64_silence_nan(float64 a, float_status *status)
4743 FloatParts64 p;
4745 float64_unpack_raw(&p, a);
4746 p.frac <<= float64_params.frac_shift;
4747 parts_silence_nan(&p, status);
4748 p.frac >>= float64_params.frac_shift;
4749 return float64_pack_raw(&p);
4752 bfloat16 bfloat16_silence_nan(bfloat16 a, float_status *status)
4754 FloatParts64 p;
4756 bfloat16_unpack_raw(&p, a);
4757 p.frac <<= bfloat16_params.frac_shift;
4758 parts_silence_nan(&p, status);
4759 p.frac >>= bfloat16_params.frac_shift;
4760 return bfloat16_pack_raw(&p);
4763 float128 float128_silence_nan(float128 a, float_status *status)
4765 FloatParts128 p;
4767 float128_unpack_raw(&p, a);
4768 frac_shl(&p, float128_params.frac_shift);
4769 parts_silence_nan(&p, status);
4770 frac_shr(&p, float128_params.frac_shift);
4771 return float128_pack_raw(&p);
4774 /*----------------------------------------------------------------------------
4775 | If `a' is denormal and we are in flush-to-zero mode then set the
4776 | input-denormal exception and return zero. Otherwise just return the value.
4777 *----------------------------------------------------------------------------*/
4779 static bool parts_squash_denormal(FloatParts64 p, float_status *status)
4781 if (p.exp == 0 && p.frac != 0) {
4782 float_raise(float_flag_input_denormal, status);
4783 return true;
4786 return false;
4789 float16 float16_squash_input_denormal(float16 a, float_status *status)
4791 if (status->flush_inputs_to_zero) {
4792 FloatParts64 p;
4794 float16_unpack_raw(&p, a);
4795 if (parts_squash_denormal(p, status)) {
4796 return float16_set_sign(float16_zero, p.sign);
4799 return a;
4802 float32 float32_squash_input_denormal(float32 a, float_status *status)
4804 if (status->flush_inputs_to_zero) {
4805 FloatParts64 p;
4807 float32_unpack_raw(&p, a);
4808 if (parts_squash_denormal(p, status)) {
4809 return float32_set_sign(float32_zero, p.sign);
4812 return a;
4815 float64 float64_squash_input_denormal(float64 a, float_status *status)
4817 if (status->flush_inputs_to_zero) {
4818 FloatParts64 p;
4820 float64_unpack_raw(&p, a);
4821 if (parts_squash_denormal(p, status)) {
4822 return float64_set_sign(float64_zero, p.sign);
4825 return a;
4828 bfloat16 bfloat16_squash_input_denormal(bfloat16 a, float_status *status)
4830 if (status->flush_inputs_to_zero) {
4831 FloatParts64 p;
4833 bfloat16_unpack_raw(&p, a);
4834 if (parts_squash_denormal(p, status)) {
4835 return bfloat16_set_sign(bfloat16_zero, p.sign);
4838 return a;
4841 /*----------------------------------------------------------------------------
4842 | Normalizes the subnormal extended double-precision floating-point value
4843 | represented by the denormalized significand `aSig'. The normalized exponent
4844 | and significand are stored at the locations pointed to by `zExpPtr' and
4845 | `zSigPtr', respectively.
4846 *----------------------------------------------------------------------------*/
4848 void normalizeFloatx80Subnormal(uint64_t aSig, int32_t *zExpPtr,
4849 uint64_t *zSigPtr)
4851 int8_t shiftCount;
4853 shiftCount = clz64(aSig);
4854 *zSigPtr = aSig<<shiftCount;
4855 *zExpPtr = 1 - shiftCount;
4858 /*----------------------------------------------------------------------------
4859 | Takes an abstract floating-point value having sign `zSign', exponent `zExp',
4860 | and extended significand formed by the concatenation of `zSig0' and `zSig1',
4861 | and returns the proper extended double-precision floating-point value
4862 | corresponding to the abstract input. Ordinarily, the abstract value is
4863 | rounded and packed into the extended double-precision format, with the
4864 | inexact exception raised if the abstract input cannot be represented
4865 | exactly. However, if the abstract value is too large, the overflow and
4866 | inexact exceptions are raised and an infinity or maximal finite value is
4867 | returned. If the abstract value is too small, the input value is rounded to
4868 | a subnormal number, and the underflow and inexact exceptions are raised if
4869 | the abstract input cannot be represented exactly as a subnormal extended
4870 | double-precision floating-point number.
4871 | If `roundingPrecision' is floatx80_precision_s or floatx80_precision_d,
4872 | the result is rounded to the same number of bits as single or double
4873 | precision, respectively. Otherwise, the result is rounded to the full
4874 | precision of the extended double-precision format.
4875 | The input significand must be normalized or smaller. If the input
4876 | significand is not normalized, `zExp' must be 0; in that case, the result
4877 | returned is a subnormal number, and it must not require rounding. The
4878 | handling of underflow and overflow follows the IEC/IEEE Standard for Binary
4879 | Floating-Point Arithmetic.
4880 *----------------------------------------------------------------------------*/
4882 floatx80 roundAndPackFloatx80(FloatX80RoundPrec roundingPrecision, bool zSign,
4883 int32_t zExp, uint64_t zSig0, uint64_t zSig1,
4884 float_status *status)
4886 FloatRoundMode roundingMode;
4887 bool roundNearestEven, increment, isTiny;
4888 int64_t roundIncrement, roundMask, roundBits;
4890 roundingMode = status->float_rounding_mode;
4891 roundNearestEven = ( roundingMode == float_round_nearest_even );
4892 switch (roundingPrecision) {
4893 case floatx80_precision_x:
4894 goto precision80;
4895 case floatx80_precision_d:
4896 roundIncrement = UINT64_C(0x0000000000000400);
4897 roundMask = UINT64_C(0x00000000000007FF);
4898 break;
4899 case floatx80_precision_s:
4900 roundIncrement = UINT64_C(0x0000008000000000);
4901 roundMask = UINT64_C(0x000000FFFFFFFFFF);
4902 break;
4903 default:
4904 g_assert_not_reached();
4906 zSig0 |= ( zSig1 != 0 );
4907 switch (roundingMode) {
4908 case float_round_nearest_even:
4909 case float_round_ties_away:
4910 break;
4911 case float_round_to_zero:
4912 roundIncrement = 0;
4913 break;
4914 case float_round_up:
4915 roundIncrement = zSign ? 0 : roundMask;
4916 break;
4917 case float_round_down:
4918 roundIncrement = zSign ? roundMask : 0;
4919 break;
4920 default:
4921 abort();
4923 roundBits = zSig0 & roundMask;
4924 if ( 0x7FFD <= (uint32_t) ( zExp - 1 ) ) {
4925 if ( ( 0x7FFE < zExp )
4926 || ( ( zExp == 0x7FFE ) && ( zSig0 + roundIncrement < zSig0 ) )
4928 goto overflow;
4930 if ( zExp <= 0 ) {
4931 if (status->flush_to_zero) {
4932 float_raise(float_flag_output_denormal, status);
4933 return packFloatx80(zSign, 0, 0);
4935 isTiny = status->tininess_before_rounding
4936 || (zExp < 0 )
4937 || (zSig0 <= zSig0 + roundIncrement);
4938 shift64RightJamming( zSig0, 1 - zExp, &zSig0 );
4939 zExp = 0;
4940 roundBits = zSig0 & roundMask;
4941 if (isTiny && roundBits) {
4942 float_raise(float_flag_underflow, status);
4944 if (roundBits) {
4945 float_raise(float_flag_inexact, status);
4947 zSig0 += roundIncrement;
4948 if ( (int64_t) zSig0 < 0 ) zExp = 1;
4949 roundIncrement = roundMask + 1;
4950 if ( roundNearestEven && ( roundBits<<1 == roundIncrement ) ) {
4951 roundMask |= roundIncrement;
4953 zSig0 &= ~ roundMask;
4954 return packFloatx80( zSign, zExp, zSig0 );
4957 if (roundBits) {
4958 float_raise(float_flag_inexact, status);
4960 zSig0 += roundIncrement;
4961 if ( zSig0 < roundIncrement ) {
4962 ++zExp;
4963 zSig0 = UINT64_C(0x8000000000000000);
4965 roundIncrement = roundMask + 1;
4966 if ( roundNearestEven && ( roundBits<<1 == roundIncrement ) ) {
4967 roundMask |= roundIncrement;
4969 zSig0 &= ~ roundMask;
4970 if ( zSig0 == 0 ) zExp = 0;
4971 return packFloatx80( zSign, zExp, zSig0 );
4972 precision80:
4973 switch (roundingMode) {
4974 case float_round_nearest_even:
4975 case float_round_ties_away:
4976 increment = ((int64_t)zSig1 < 0);
4977 break;
4978 case float_round_to_zero:
4979 increment = 0;
4980 break;
4981 case float_round_up:
4982 increment = !zSign && zSig1;
4983 break;
4984 case float_round_down:
4985 increment = zSign && zSig1;
4986 break;
4987 default:
4988 abort();
4990 if ( 0x7FFD <= (uint32_t) ( zExp - 1 ) ) {
4991 if ( ( 0x7FFE < zExp )
4992 || ( ( zExp == 0x7FFE )
4993 && ( zSig0 == UINT64_C(0xFFFFFFFFFFFFFFFF) )
4994 && increment
4997 roundMask = 0;
4998 overflow:
4999 float_raise(float_flag_overflow | float_flag_inexact, status);
5000 if ( ( roundingMode == float_round_to_zero )
5001 || ( zSign && ( roundingMode == float_round_up ) )
5002 || ( ! zSign && ( roundingMode == float_round_down ) )
5004 return packFloatx80( zSign, 0x7FFE, ~ roundMask );
5006 return packFloatx80(zSign,
5007 floatx80_infinity_high,
5008 floatx80_infinity_low);
5010 if ( zExp <= 0 ) {
5011 isTiny = status->tininess_before_rounding
5012 || (zExp < 0)
5013 || !increment
5014 || (zSig0 < UINT64_C(0xFFFFFFFFFFFFFFFF));
5015 shift64ExtraRightJamming( zSig0, zSig1, 1 - zExp, &zSig0, &zSig1 );
5016 zExp = 0;
5017 if (isTiny && zSig1) {
5018 float_raise(float_flag_underflow, status);
5020 if (zSig1) {
5021 float_raise(float_flag_inexact, status);
5023 switch (roundingMode) {
5024 case float_round_nearest_even:
5025 case float_round_ties_away:
5026 increment = ((int64_t)zSig1 < 0);
5027 break;
5028 case float_round_to_zero:
5029 increment = 0;
5030 break;
5031 case float_round_up:
5032 increment = !zSign && zSig1;
5033 break;
5034 case float_round_down:
5035 increment = zSign && zSig1;
5036 break;
5037 default:
5038 abort();
5040 if ( increment ) {
5041 ++zSig0;
5042 if (!(zSig1 << 1) && roundNearestEven) {
5043 zSig0 &= ~1;
5045 if ( (int64_t) zSig0 < 0 ) zExp = 1;
5047 return packFloatx80( zSign, zExp, zSig0 );
5050 if (zSig1) {
5051 float_raise(float_flag_inexact, status);
5053 if ( increment ) {
5054 ++zSig0;
5055 if ( zSig0 == 0 ) {
5056 ++zExp;
5057 zSig0 = UINT64_C(0x8000000000000000);
5059 else {
5060 if (!(zSig1 << 1) && roundNearestEven) {
5061 zSig0 &= ~1;
5065 else {
5066 if ( zSig0 == 0 ) zExp = 0;
5068 return packFloatx80( zSign, zExp, zSig0 );
5072 /*----------------------------------------------------------------------------
5073 | Takes an abstract floating-point value having sign `zSign', exponent
5074 | `zExp', and significand formed by the concatenation of `zSig0' and `zSig1',
5075 | and returns the proper extended double-precision floating-point value
5076 | corresponding to the abstract input. This routine is just like
5077 | `roundAndPackFloatx80' except that the input significand does not have to be
5078 | normalized.
5079 *----------------------------------------------------------------------------*/
5081 floatx80 normalizeRoundAndPackFloatx80(FloatX80RoundPrec roundingPrecision,
5082 bool zSign, int32_t zExp,
5083 uint64_t zSig0, uint64_t zSig1,
5084 float_status *status)
5086 int8_t shiftCount;
5088 if ( zSig0 == 0 ) {
5089 zSig0 = zSig1;
5090 zSig1 = 0;
5091 zExp -= 64;
5093 shiftCount = clz64(zSig0);
5094 shortShift128Left( zSig0, zSig1, shiftCount, &zSig0, &zSig1 );
5095 zExp -= shiftCount;
5096 return roundAndPackFloatx80(roundingPrecision, zSign, zExp,
5097 zSig0, zSig1, status);
5101 /*----------------------------------------------------------------------------
5102 | Returns the binary exponential of the single-precision floating-point value
5103 | `a'. The operation is performed according to the IEC/IEEE Standard for
5104 | Binary Floating-Point Arithmetic.
5106 | Uses the following identities:
5108 | 1. -------------------------------------------------------------------------
5109 | x x*ln(2)
5110 | 2 = e
5112 | 2. -------------------------------------------------------------------------
5113 | 2 3 4 5 n
5114 | x x x x x x x
5115 | e = 1 + --- + --- + --- + --- + --- + ... + --- + ...
5116 | 1! 2! 3! 4! 5! n!
5117 *----------------------------------------------------------------------------*/
5119 static const float64 float32_exp2_coefficients[15] =
5121 const_float64( 0x3ff0000000000000ll ), /* 1 */
5122 const_float64( 0x3fe0000000000000ll ), /* 2 */
5123 const_float64( 0x3fc5555555555555ll ), /* 3 */
5124 const_float64( 0x3fa5555555555555ll ), /* 4 */
5125 const_float64( 0x3f81111111111111ll ), /* 5 */
5126 const_float64( 0x3f56c16c16c16c17ll ), /* 6 */
5127 const_float64( 0x3f2a01a01a01a01all ), /* 7 */
5128 const_float64( 0x3efa01a01a01a01all ), /* 8 */
5129 const_float64( 0x3ec71de3a556c734ll ), /* 9 */
5130 const_float64( 0x3e927e4fb7789f5cll ), /* 10 */
5131 const_float64( 0x3e5ae64567f544e4ll ), /* 11 */
5132 const_float64( 0x3e21eed8eff8d898ll ), /* 12 */
5133 const_float64( 0x3de6124613a86d09ll ), /* 13 */
5134 const_float64( 0x3da93974a8c07c9dll ), /* 14 */
5135 const_float64( 0x3d6ae7f3e733b81fll ), /* 15 */
5138 float32 float32_exp2(float32 a, float_status *status)
5140 FloatParts64 xp, xnp, tp, rp;
5141 int i;
5143 float32_unpack_canonical(&xp, a, status);
5144 if (unlikely(xp.cls != float_class_normal)) {
5145 switch (xp.cls) {
5146 case float_class_snan:
5147 case float_class_qnan:
5148 parts_return_nan(&xp, status);
5149 return float32_round_pack_canonical(&xp, status);
5150 case float_class_inf:
5151 return xp.sign ? float32_zero : a;
5152 case float_class_zero:
5153 return float32_one;
5154 default:
5155 break;
5157 g_assert_not_reached();
5160 float_raise(float_flag_inexact, status);
5162 float64_unpack_canonical(&tp, float64_ln2, status);
5163 xp = *parts_mul(&xp, &tp, status);
5164 xnp = xp;
5166 float64_unpack_canonical(&rp, float64_one, status);
5167 for (i = 0 ; i < 15 ; i++) {
5168 float64_unpack_canonical(&tp, float32_exp2_coefficients[i], status);
5169 rp = *parts_muladd(&tp, &xnp, &rp, 0, status);
5170 xnp = *parts_mul(&xnp, &xp, status);
5173 return float32_round_pack_canonical(&rp, status);
5176 /*----------------------------------------------------------------------------
5177 | Rounds the extended double-precision floating-point value `a'
5178 | to the precision provided by floatx80_rounding_precision and returns the
5179 | result as an extended double-precision floating-point value.
5180 | The operation is performed according to the IEC/IEEE Standard for Binary
5181 | Floating-Point Arithmetic.
5182 *----------------------------------------------------------------------------*/
5184 floatx80 floatx80_round(floatx80 a, float_status *status)
5186 FloatParts128 p;
5188 if (!floatx80_unpack_canonical(&p, a, status)) {
5189 return floatx80_default_nan(status);
5191 return floatx80_round_pack_canonical(&p, status);
5194 static void __attribute__((constructor)) softfloat_init(void)
5196 union_float64 ua, ub, uc, ur;
5198 if (QEMU_NO_HARDFLOAT) {
5199 return;
5202 * Test that the host's FMA is not obviously broken. For example,
5203 * glibc < 2.23 can perform an incorrect FMA on certain hosts; see
5204 * https://sourceware.org/bugzilla/show_bug.cgi?id=13304
5206 ua.s = 0x0020000000000001ULL;
5207 ub.s = 0x3ca0000000000000ULL;
5208 uc.s = 0x0020000000000000ULL;
5209 ur.h = fma(ua.h, ub.h, uc.h);
5210 if (ur.s != 0x0020000000000001ULL) {
5211 force_soft_fma = true;