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
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 ===============================================================================
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"
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
96 *----------------------------------------------------------------------------*/
97 #include "fpu/softfloat-macros.h"
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)) { \
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)) { \
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)) { \
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
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
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
215 # define QEMU_HARDFLOAT_USE_ISINF 0
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
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 \
228 # define QEMU_NO_HARDFLOAT 1
229 # define QEMU_SOFTFLOAT_ATTR QEMU_FLATTEN
231 # define QEMU_NO_HARDFLOAT 0
232 # define QEMU_SOFTFLOAT_ATTR QEMU_FLATTEN __attribute__((noinline))
235 static inline bool can_use_fpu(const float_status
*s
)
237 if (QEMU_NO_HARDFLOAT
) {
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.
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 */
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
);
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
) {
331 return float32_is_infinity(a
.s
);
334 static inline bool f64_is_inf(union_float64 a
)
336 if (QEMU_HARDFLOAT_USE_ISINF
) {
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
;
352 if (unlikely(!can_use_fpu(s
))) {
356 float32_input_flush2(&ua
.s
, &ub
.s
, s
);
357 if (unlikely(!pre(ua
, ub
))) {
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
)) {
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
;
383 if (unlikely(!can_use_fpu(s
))) {
387 float64_input_flush2(&ua
.s
, &ub
.s
, s
);
388 if (unlikely(!pre(ua
, ub
))) {
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
)) {
401 return soft(ua
.s
, ub
.s
, s
);
404 /*----------------------------------------------------------------------------
405 | Returns the fraction bits of the single-precision floating-point value `a'.
406 *----------------------------------------------------------------------------*/
408 static inline uint32_t extractFloat32Frac(float32 a
)
410 return float32_val(a
) & 0x007FFFFF;
413 /*----------------------------------------------------------------------------
414 | Returns the exponent bits of the single-precision floating-point value `a'.
415 *----------------------------------------------------------------------------*/
417 static inline int extractFloat32Exp(float32 a
)
419 return (float32_val(a
) >> 23) & 0xFF;
422 /*----------------------------------------------------------------------------
423 | Returns the sign bit of the single-precision floating-point value `a'.
424 *----------------------------------------------------------------------------*/
426 static inline bool extractFloat32Sign(float32 a
)
428 return float32_val(a
) >> 31;
431 /*----------------------------------------------------------------------------
432 | Returns the fraction bits of the double-precision floating-point value `a'.
433 *----------------------------------------------------------------------------*/
435 static inline uint64_t extractFloat64Frac(float64 a
)
437 return float64_val(a
) & UINT64_C(0x000FFFFFFFFFFFFF);
440 /*----------------------------------------------------------------------------
441 | Returns the exponent bits of the double-precision floating-point value `a'.
442 *----------------------------------------------------------------------------*/
444 static inline int extractFloat64Exp(float64 a
)
446 return (float64_val(a
) >> 52) & 0x7FF;
449 /*----------------------------------------------------------------------------
450 | Returns the sign bit of the double-precision floating-point value `a'.
451 *----------------------------------------------------------------------------*/
453 static inline bool extractFloat64Sign(float64 a
)
455 return float64_val(a
) >> 63;
459 * Classify a floating point number. Everything above float_class_qnan
460 * is a NaN so cls >= float_class_qnan is any NaN.
463 typedef enum __attribute__ ((__packed__
)) {
464 float_class_unclassified
,
468 float_class_qnan
, /* all NaNs from here */
472 #define float_cmask(bit) (1u << (bit))
475 float_cmask_zero
= float_cmask(float_class_zero
),
476 float_cmask_normal
= float_cmask(float_class_normal
),
477 float_cmask_inf
= float_cmask(float_class_inf
),
478 float_cmask_qnan
= float_cmask(float_class_qnan
),
479 float_cmask_snan
= float_cmask(float_class_snan
),
481 float_cmask_infzero
= float_cmask_zero
| float_cmask_inf
,
482 float_cmask_anynan
= float_cmask_qnan
| float_cmask_snan
,
486 /* Simple helpers for checking if, or what kind of, NaN we have */
487 static inline __attribute__((unused
)) bool is_nan(FloatClass c
)
489 return unlikely(c
>= float_class_qnan
);
492 static inline __attribute__((unused
)) bool is_snan(FloatClass c
)
494 return c
== float_class_snan
;
497 static inline __attribute__((unused
)) bool is_qnan(FloatClass c
)
499 return c
== float_class_qnan
;
503 * Structure holding all of the decomposed parts of a float.
504 * The exponent is unbiased and the fraction is normalized.
506 * The fraction words are stored in big-endian word ordering,
507 * so that truncation from a larger format to a smaller format
508 * can be done simply by ignoring subsequent elements.
516 /* Routines that know the structure may reference the singular name. */
519 * Routines expanded with multiple structures reference "hi" and "lo"
520 * depending on the operation. In FloatParts64, "hi" and "lo" are
521 * both the same word and aliased here.
541 uint64_t frac_hm
; /* high-middle */
542 uint64_t frac_lm
; /* low-middle */
546 /* These apply to the most significant word of each FloatPartsN. */
547 #define DECOMPOSED_BINARY_POINT 63
548 #define DECOMPOSED_IMPLICIT_BIT (1ull << DECOMPOSED_BINARY_POINT)
550 /* Structure holding all of the relevant parameters for a format.
551 * exp_size: the size of the exponent field
552 * exp_bias: the offset applied to the exponent field
553 * exp_max: the maximum normalised exponent
554 * frac_size: the size of the fraction field
555 * frac_shift: shift to normalise the fraction with DECOMPOSED_BINARY_POINT
556 * The following are computed based the size of fraction
557 * frac_lsb: least significant bit of fraction
558 * frac_lsbm1: the bit below the least significant bit (for rounding)
559 * round_mask/roundeven_mask: masks used for rounding
560 * The following optional modifiers are available:
561 * arm_althp: handle ARM Alternative Half Precision
572 uint64_t roundeven_mask
;
576 /* Expand fields based on the size of exponent and fraction */
577 #define FLOAT_PARAMS(E, F) \
579 .exp_bias = ((1 << E) - 1) >> 1, \
580 .exp_max = (1 << E) - 1, \
582 .frac_shift = (-F - 1) & 63, \
583 .frac_lsb = 1ull << ((-F - 1) & 63), \
584 .frac_lsbm1 = 1ull << ((-F - 2) & 63), \
585 .round_mask = (1ull << ((-F - 1) & 63)) - 1, \
586 .roundeven_mask = (2ull << ((-F - 1) & 63)) - 1
588 static const FloatFmt float16_params
= {
592 static const FloatFmt float16_params_ahp
= {
597 static const FloatFmt bfloat16_params
= {
601 static const FloatFmt float32_params
= {
605 static const FloatFmt float64_params
= {
609 static const FloatFmt float128_params
= {
610 FLOAT_PARAMS(15, 112)
613 /* Unpack a float to parts, but do not canonicalize. */
614 static void unpack_raw64(FloatParts64
*r
, const FloatFmt
*fmt
, uint64_t raw
)
616 const int f_size
= fmt
->frac_size
;
617 const int e_size
= fmt
->exp_size
;
619 *r
= (FloatParts64
) {
620 .cls
= float_class_unclassified
,
621 .sign
= extract64(raw
, f_size
+ e_size
, 1),
622 .exp
= extract64(raw
, f_size
, e_size
),
623 .frac
= extract64(raw
, 0, f_size
)
627 static inline void float16_unpack_raw(FloatParts64
*p
, float16 f
)
629 unpack_raw64(p
, &float16_params
, f
);
632 static inline void bfloat16_unpack_raw(FloatParts64
*p
, bfloat16 f
)
634 unpack_raw64(p
, &bfloat16_params
, f
);
637 static inline void float32_unpack_raw(FloatParts64
*p
, float32 f
)
639 unpack_raw64(p
, &float32_params
, f
);
642 static inline void float64_unpack_raw(FloatParts64
*p
, float64 f
)
644 unpack_raw64(p
, &float64_params
, f
);
647 static void float128_unpack_raw(FloatParts128
*p
, float128 f
)
649 const int f_size
= float128_params
.frac_size
- 64;
650 const int e_size
= float128_params
.exp_size
;
652 *p
= (FloatParts128
) {
653 .cls
= float_class_unclassified
,
654 .sign
= extract64(f
.high
, f_size
+ e_size
, 1),
655 .exp
= extract64(f
.high
, f_size
, e_size
),
656 .frac_hi
= extract64(f
.high
, 0, f_size
),
661 /* Pack a float from parts, but do not canonicalize. */
662 static uint64_t pack_raw64(const FloatParts64
*p
, const FloatFmt
*fmt
)
664 const int f_size
= fmt
->frac_size
;
665 const int e_size
= fmt
->exp_size
;
668 ret
= (uint64_t)p
->sign
<< (f_size
+ e_size
);
669 ret
= deposit64(ret
, f_size
, e_size
, p
->exp
);
670 ret
= deposit64(ret
, 0, f_size
, p
->frac
);
674 static inline float16
float16_pack_raw(const FloatParts64
*p
)
676 return make_float16(pack_raw64(p
, &float16_params
));
679 static inline bfloat16
bfloat16_pack_raw(const FloatParts64
*p
)
681 return pack_raw64(p
, &bfloat16_params
);
684 static inline float32
float32_pack_raw(const FloatParts64
*p
)
686 return make_float32(pack_raw64(p
, &float32_params
));
689 static inline float64
float64_pack_raw(const FloatParts64
*p
)
691 return make_float64(pack_raw64(p
, &float64_params
));
694 static float128
float128_pack_raw(const FloatParts128
*p
)
696 const int f_size
= float128_params
.frac_size
- 64;
697 const int e_size
= float128_params
.exp_size
;
700 hi
= (uint64_t)p
->sign
<< (f_size
+ e_size
);
701 hi
= deposit64(hi
, f_size
, e_size
, p
->exp
);
702 hi
= deposit64(hi
, 0, f_size
, p
->frac_hi
);
703 return make_float128(hi
, p
->frac_lo
);
706 /*----------------------------------------------------------------------------
707 | Functions and definitions to determine: (1) whether tininess for underflow
708 | is detected before or after rounding by default, (2) what (if anything)
709 | happens when exceptions are raised, (3) how signaling NaNs are distinguished
710 | from quiet NaNs, (4) the default generated quiet NaNs, and (5) how NaNs
711 | are propagated from function inputs to output. These details are target-
713 *----------------------------------------------------------------------------*/
714 #include "softfloat-specialize.c.inc"
716 #define PARTS_GENERIC_64_128(NAME, P) \
717 QEMU_GENERIC(P, (FloatParts128 *, parts128_##NAME), parts64_##NAME)
719 #define PARTS_GENERIC_64_128_256(NAME, P) \
720 QEMU_GENERIC(P, (FloatParts256 *, parts256_##NAME), \
721 (FloatParts128 *, parts128_##NAME), parts64_##NAME)
723 #define parts_default_nan(P, S) PARTS_GENERIC_64_128(default_nan, P)(P, S)
724 #define parts_silence_nan(P, S) PARTS_GENERIC_64_128(silence_nan, P)(P, S)
726 static void parts64_return_nan(FloatParts64
*a
, float_status
*s
);
727 static void parts128_return_nan(FloatParts128
*a
, float_status
*s
);
729 #define parts_return_nan(P, S) PARTS_GENERIC_64_128(return_nan, P)(P, S)
731 static FloatParts64
*parts64_pick_nan(FloatParts64
*a
, FloatParts64
*b
,
733 static FloatParts128
*parts128_pick_nan(FloatParts128
*a
, FloatParts128
*b
,
736 #define parts_pick_nan(A, B, S) PARTS_GENERIC_64_128(pick_nan, A)(A, B, S)
738 static FloatParts64
*parts64_pick_nan_muladd(FloatParts64
*a
, FloatParts64
*b
,
739 FloatParts64
*c
, float_status
*s
,
740 int ab_mask
, int abc_mask
);
741 static FloatParts128
*parts128_pick_nan_muladd(FloatParts128
*a
,
745 int ab_mask
, int abc_mask
);
747 #define parts_pick_nan_muladd(A, B, C, S, ABM, ABCM) \
748 PARTS_GENERIC_64_128(pick_nan_muladd, A)(A, B, C, S, ABM, ABCM)
750 static void parts64_canonicalize(FloatParts64
*p
, float_status
*status
,
751 const FloatFmt
*fmt
);
752 static void parts128_canonicalize(FloatParts128
*p
, float_status
*status
,
753 const FloatFmt
*fmt
);
755 #define parts_canonicalize(A, S, F) \
756 PARTS_GENERIC_64_128(canonicalize, A)(A, S, F)
758 static void parts64_uncanon(FloatParts64
*p
, float_status
*status
,
759 const FloatFmt
*fmt
);
760 static void parts128_uncanon(FloatParts128
*p
, float_status
*status
,
761 const FloatFmt
*fmt
);
763 #define parts_uncanon(A, S, F) \
764 PARTS_GENERIC_64_128(uncanon, A)(A, S, F)
766 static void parts64_add_normal(FloatParts64
*a
, FloatParts64
*b
);
767 static void parts128_add_normal(FloatParts128
*a
, FloatParts128
*b
);
768 static void parts256_add_normal(FloatParts256
*a
, FloatParts256
*b
);
770 #define parts_add_normal(A, B) \
771 PARTS_GENERIC_64_128_256(add_normal, A)(A, B)
773 static bool parts64_sub_normal(FloatParts64
*a
, FloatParts64
*b
);
774 static bool parts128_sub_normal(FloatParts128
*a
, FloatParts128
*b
);
775 static bool parts256_sub_normal(FloatParts256
*a
, FloatParts256
*b
);
777 #define parts_sub_normal(A, B) \
778 PARTS_GENERIC_64_128_256(sub_normal, A)(A, B)
780 static FloatParts64
*parts64_addsub(FloatParts64
*a
, FloatParts64
*b
,
781 float_status
*s
, bool subtract
);
782 static FloatParts128
*parts128_addsub(FloatParts128
*a
, FloatParts128
*b
,
783 float_status
*s
, bool subtract
);
785 #define parts_addsub(A, B, S, Z) \
786 PARTS_GENERIC_64_128(addsub, A)(A, B, S, Z)
788 static FloatParts64
*parts64_mul(FloatParts64
*a
, FloatParts64
*b
,
790 static FloatParts128
*parts128_mul(FloatParts128
*a
, FloatParts128
*b
,
793 #define parts_mul(A, B, S) \
794 PARTS_GENERIC_64_128(mul, A)(A, B, S)
796 static FloatParts64
*parts64_muladd(FloatParts64
*a
, FloatParts64
*b
,
797 FloatParts64
*c
, int flags
,
799 static FloatParts128
*parts128_muladd(FloatParts128
*a
, FloatParts128
*b
,
800 FloatParts128
*c
, int flags
,
803 #define parts_muladd(A, B, C, Z, S) \
804 PARTS_GENERIC_64_128(muladd, A)(A, B, C, Z, S)
806 static FloatParts64
*parts64_div(FloatParts64
*a
, FloatParts64
*b
,
808 static FloatParts128
*parts128_div(FloatParts128
*a
, FloatParts128
*b
,
811 #define parts_div(A, B, S) \
812 PARTS_GENERIC_64_128(div, A)(A, B, S)
814 static bool parts64_round_to_int_normal(FloatParts64
*a
, FloatRoundMode rm
,
815 int scale
, int frac_size
);
816 static bool parts128_round_to_int_normal(FloatParts128
*a
, FloatRoundMode r
,
817 int scale
, int frac_size
);
819 #define parts_round_to_int_normal(A, R, C, F) \
820 PARTS_GENERIC_64_128(round_to_int_normal, A)(A, R, C, F)
822 static void parts64_round_to_int(FloatParts64
*a
, FloatRoundMode rm
,
823 int scale
, float_status
*s
,
824 const FloatFmt
*fmt
);
825 static void parts128_round_to_int(FloatParts128
*a
, FloatRoundMode r
,
826 int scale
, float_status
*s
,
827 const FloatFmt
*fmt
);
829 #define parts_round_to_int(A, R, C, S, F) \
830 PARTS_GENERIC_64_128(round_to_int, A)(A, R, C, S, F)
832 static int64_t parts64_float_to_sint(FloatParts64
*p
, FloatRoundMode rmode
,
833 int scale
, int64_t min
, int64_t max
,
835 static int64_t parts128_float_to_sint(FloatParts128
*p
, FloatRoundMode rmode
,
836 int scale
, int64_t min
, int64_t max
,
839 #define parts_float_to_sint(P, R, Z, MN, MX, S) \
840 PARTS_GENERIC_64_128(float_to_sint, P)(P, R, Z, MN, MX, S)
843 * Helper functions for softfloat-parts.c.inc, per-size operations.
846 #define FRAC_GENERIC_64_128(NAME, P) \
847 QEMU_GENERIC(P, (FloatParts128 *, frac128_##NAME), frac64_##NAME)
849 #define FRAC_GENERIC_64_128_256(NAME, P) \
850 QEMU_GENERIC(P, (FloatParts256 *, frac256_##NAME), \
851 (FloatParts128 *, frac128_##NAME), frac64_##NAME)
853 static bool frac64_add(FloatParts64
*r
, FloatParts64
*a
, FloatParts64
*b
)
855 return uadd64_overflow(a
->frac
, b
->frac
, &r
->frac
);
858 static bool frac128_add(FloatParts128
*r
, FloatParts128
*a
, FloatParts128
*b
)
861 r
->frac_lo
= uadd64_carry(a
->frac_lo
, b
->frac_lo
, &c
);
862 r
->frac_hi
= uadd64_carry(a
->frac_hi
, b
->frac_hi
, &c
);
866 static bool frac256_add(FloatParts256
*r
, FloatParts256
*a
, FloatParts256
*b
)
869 r
->frac_lo
= uadd64_carry(a
->frac_lo
, b
->frac_lo
, &c
);
870 r
->frac_lm
= uadd64_carry(a
->frac_lm
, b
->frac_lm
, &c
);
871 r
->frac_hm
= uadd64_carry(a
->frac_hm
, b
->frac_hm
, &c
);
872 r
->frac_hi
= uadd64_carry(a
->frac_hi
, b
->frac_hi
, &c
);
876 #define frac_add(R, A, B) FRAC_GENERIC_64_128_256(add, R)(R, A, B)
878 static bool frac64_addi(FloatParts64
*r
, FloatParts64
*a
, uint64_t c
)
880 return uadd64_overflow(a
->frac
, c
, &r
->frac
);
883 static bool frac128_addi(FloatParts128
*r
, FloatParts128
*a
, uint64_t c
)
885 c
= uadd64_overflow(a
->frac_lo
, c
, &r
->frac_lo
);
886 return uadd64_overflow(a
->frac_hi
, c
, &r
->frac_hi
);
889 #define frac_addi(R, A, C) FRAC_GENERIC_64_128(addi, R)(R, A, C)
891 static void frac64_allones(FloatParts64
*a
)
896 static void frac128_allones(FloatParts128
*a
)
898 a
->frac_hi
= a
->frac_lo
= -1;
901 #define frac_allones(A) FRAC_GENERIC_64_128(allones, A)(A)
903 static int frac64_cmp(FloatParts64
*a
, FloatParts64
*b
)
905 return a
->frac
== b
->frac
? 0 : a
->frac
< b
->frac
? -1 : 1;
908 static int frac128_cmp(FloatParts128
*a
, FloatParts128
*b
)
910 uint64_t ta
= a
->frac_hi
, tb
= b
->frac_hi
;
912 ta
= a
->frac_lo
, tb
= b
->frac_lo
;
917 return ta
< tb
? -1 : 1;
920 #define frac_cmp(A, B) FRAC_GENERIC_64_128(cmp, A)(A, B)
922 static void frac64_clear(FloatParts64
*a
)
927 static void frac128_clear(FloatParts128
*a
)
929 a
->frac_hi
= a
->frac_lo
= 0;
932 #define frac_clear(A) FRAC_GENERIC_64_128(clear, A)(A)
934 static bool frac64_div(FloatParts64
*a
, FloatParts64
*b
)
936 uint64_t n1
, n0
, r
, q
;
940 * We want a 2*N / N-bit division to produce exactly an N-bit
941 * result, so that we do not lose any precision and so that we
942 * do not have to renormalize afterward. If A.frac < B.frac,
943 * then division would produce an (N-1)-bit result; shift A left
944 * by one to produce the an N-bit result, and return true to
945 * decrement the exponent to match.
947 * The udiv_qrnnd algorithm that we're using requires normalization,
948 * i.e. the msb of the denominator must be set, which is already true.
950 ret
= a
->frac
< b
->frac
;
958 q
= udiv_qrnnd(&r
, n0
, n1
, b
->frac
);
960 /* Set lsb if there is a remainder, to set inexact. */
961 a
->frac
= q
| (r
!= 0);
966 static bool frac128_div(FloatParts128
*a
, FloatParts128
*b
)
968 uint64_t q0
, q1
, a0
, a1
, b0
, b1
;
969 uint64_t r0
, r1
, r2
, r3
, t0
, t1
, t2
, t3
;
972 a0
= a
->frac_hi
, a1
= a
->frac_lo
;
973 b0
= b
->frac_hi
, b1
= b
->frac_lo
;
975 ret
= lt128(a0
, a1
, b0
, b1
);
977 a1
= shr_double(a0
, a1
, 1);
981 /* Use 128/64 -> 64 division as estimate for 192/128 -> 128 division. */
982 q0
= estimateDiv128To64(a0
, a1
, b0
);
985 * Estimate is high because B1 was not included (unless B1 == 0).
986 * Reduce quotient and increase remainder until remainder is non-negative.
987 * This loop will execute 0 to 2 times.
989 mul128By64To192(b0
, b1
, q0
, &t0
, &t1
, &t2
);
990 sub192(a0
, a1
, 0, t0
, t1
, t2
, &r0
, &r1
, &r2
);
993 add192(r0
, r1
, r2
, 0, b0
, b1
, &r0
, &r1
, &r2
);
996 /* Repeat using the remainder, producing a second word of quotient. */
997 q1
= estimateDiv128To64(r1
, r2
, b0
);
998 mul128By64To192(b0
, b1
, q1
, &t1
, &t2
, &t3
);
999 sub192(r1
, r2
, 0, t1
, t2
, t3
, &r1
, &r2
, &r3
);
1002 add192(r1
, r2
, r3
, 0, b0
, b1
, &r1
, &r2
, &r3
);
1005 /* Any remainder indicates inexact; set sticky bit. */
1006 q1
|= (r2
| r3
) != 0;
1013 #define frac_div(A, B) FRAC_GENERIC_64_128(div, A)(A, B)
1015 static bool frac64_eqz(FloatParts64
*a
)
1017 return a
->frac
== 0;
1020 static bool frac128_eqz(FloatParts128
*a
)
1022 return (a
->frac_hi
| a
->frac_lo
) == 0;
1025 #define frac_eqz(A) FRAC_GENERIC_64_128(eqz, A)(A)
1027 static void frac64_mulw(FloatParts128
*r
, FloatParts64
*a
, FloatParts64
*b
)
1029 mulu64(&r
->frac_lo
, &r
->frac_hi
, a
->frac
, b
->frac
);
1032 static void frac128_mulw(FloatParts256
*r
, FloatParts128
*a
, FloatParts128
*b
)
1034 mul128To256(a
->frac_hi
, a
->frac_lo
, b
->frac_hi
, b
->frac_lo
,
1035 &r
->frac_hi
, &r
->frac_hm
, &r
->frac_lm
, &r
->frac_lo
);
1038 #define frac_mulw(R, A, B) FRAC_GENERIC_64_128(mulw, A)(R, A, B)
1040 static void frac64_neg(FloatParts64
*a
)
1045 static void frac128_neg(FloatParts128
*a
)
1048 a
->frac_lo
= usub64_borrow(0, a
->frac_lo
, &c
);
1049 a
->frac_hi
= usub64_borrow(0, a
->frac_hi
, &c
);
1052 static void frac256_neg(FloatParts256
*a
)
1055 a
->frac_lo
= usub64_borrow(0, a
->frac_lo
, &c
);
1056 a
->frac_lm
= usub64_borrow(0, a
->frac_lm
, &c
);
1057 a
->frac_hm
= usub64_borrow(0, a
->frac_hm
, &c
);
1058 a
->frac_hi
= usub64_borrow(0, a
->frac_hi
, &c
);
1061 #define frac_neg(A) FRAC_GENERIC_64_128_256(neg, A)(A)
1063 static int frac64_normalize(FloatParts64
*a
)
1066 int shift
= clz64(a
->frac
);
1073 static int frac128_normalize(FloatParts128
*a
)
1076 int shl
= clz64(a
->frac_hi
);
1077 a
->frac_hi
= shl_double(a
->frac_hi
, a
->frac_lo
, shl
);
1080 } else if (a
->frac_lo
) {
1081 int shl
= clz64(a
->frac_lo
);
1082 a
->frac_hi
= a
->frac_lo
<< shl
;
1089 static int frac256_normalize(FloatParts256
*a
)
1091 uint64_t a0
= a
->frac_hi
, a1
= a
->frac_hm
;
1092 uint64_t a2
= a
->frac_lm
, a3
= a
->frac_lo
;
1104 a0
= a1
, a1
= a2
, a2
= a3
, a3
= 0;
1107 a0
= a2
, a1
= a3
, a2
= 0, a3
= 0;
1110 a0
= a3
, a1
= 0, a2
= 0, a3
= 0;
1113 a0
= 0, a1
= 0, a2
= 0, a3
= 0;
1123 a0
= shl_double(a0
, a1
, shl
);
1124 a1
= shl_double(a1
, a2
, shl
);
1125 a2
= shl_double(a2
, a3
, shl
);
1136 #define frac_normalize(A) FRAC_GENERIC_64_128_256(normalize, A)(A)
1138 static void frac64_shl(FloatParts64
*a
, int c
)
1143 static void frac128_shl(FloatParts128
*a
, int c
)
1145 uint64_t a0
= a
->frac_hi
, a1
= a
->frac_lo
;
1153 a0
= shl_double(a0
, a1
, c
);
1161 #define frac_shl(A, C) FRAC_GENERIC_64_128(shl, A)(A, C)
1163 static void frac64_shr(FloatParts64
*a
, int c
)
1168 static void frac128_shr(FloatParts128
*a
, int c
)
1170 uint64_t a0
= a
->frac_hi
, a1
= a
->frac_lo
;
1178 a1
= shr_double(a0
, a1
, c
);
1186 #define frac_shr(A, C) FRAC_GENERIC_64_128(shr, A)(A, C)
1188 static void frac64_shrjam(FloatParts64
*a
, int c
)
1190 uint64_t a0
= a
->frac
;
1192 if (likely(c
!= 0)) {
1193 if (likely(c
< 64)) {
1194 a0
= (a0
>> c
) | (shr_double(a0
, 0, c
) != 0);
1202 static void frac128_shrjam(FloatParts128
*a
, int c
)
1204 uint64_t a0
= a
->frac_hi
, a1
= a
->frac_lo
;
1205 uint64_t sticky
= 0;
1207 if (unlikely(c
== 0)) {
1209 } else if (likely(c
< 64)) {
1211 } else if (likely(c
< 128)) {
1225 sticky
|= shr_double(a1
, 0, c
);
1226 a1
= shr_double(a0
, a1
, c
);
1230 a
->frac_lo
= a1
| (sticky
!= 0);
1234 static void frac256_shrjam(FloatParts256
*a
, int c
)
1236 uint64_t a0
= a
->frac_hi
, a1
= a
->frac_hm
;
1237 uint64_t a2
= a
->frac_lm
, a3
= a
->frac_lo
;
1238 uint64_t sticky
= 0;
1240 if (unlikely(c
== 0)) {
1242 } else if (likely(c
< 64)) {
1244 } else if (likely(c
< 256)) {
1245 if (unlikely(c
& 128)) {
1247 a3
= a1
, a2
= a0
, a1
= 0, a0
= 0;
1249 if (unlikely(c
& 64)) {
1251 a3
= a2
, a2
= a1
, a1
= a0
, a0
= 0;
1258 sticky
= a0
| a1
| a2
| a3
;
1259 a0
= a1
= a2
= a3
= 0;
1263 sticky
|= shr_double(a3
, 0, c
);
1264 a3
= shr_double(a2
, a3
, c
);
1265 a2
= shr_double(a1
, a2
, c
);
1266 a1
= shr_double(a0
, a1
, c
);
1270 a
->frac_lo
= a3
| (sticky
!= 0);
1276 #define frac_shrjam(A, C) FRAC_GENERIC_64_128_256(shrjam, A)(A, C)
1278 static bool frac64_sub(FloatParts64
*r
, FloatParts64
*a
, FloatParts64
*b
)
1280 return usub64_overflow(a
->frac
, b
->frac
, &r
->frac
);
1283 static bool frac128_sub(FloatParts128
*r
, FloatParts128
*a
, FloatParts128
*b
)
1286 r
->frac_lo
= usub64_borrow(a
->frac_lo
, b
->frac_lo
, &c
);
1287 r
->frac_hi
= usub64_borrow(a
->frac_hi
, b
->frac_hi
, &c
);
1291 static bool frac256_sub(FloatParts256
*r
, FloatParts256
*a
, FloatParts256
*b
)
1294 r
->frac_lo
= usub64_borrow(a
->frac_lo
, b
->frac_lo
, &c
);
1295 r
->frac_lm
= usub64_borrow(a
->frac_lm
, b
->frac_lm
, &c
);
1296 r
->frac_hm
= usub64_borrow(a
->frac_hm
, b
->frac_hm
, &c
);
1297 r
->frac_hi
= usub64_borrow(a
->frac_hi
, b
->frac_hi
, &c
);
1301 #define frac_sub(R, A, B) FRAC_GENERIC_64_128_256(sub, R)(R, A, B)
1303 static void frac64_truncjam(FloatParts64
*r
, FloatParts128
*a
)
1305 r
->frac
= a
->frac_hi
| (a
->frac_lo
!= 0);
1308 static void frac128_truncjam(FloatParts128
*r
, FloatParts256
*a
)
1310 r
->frac_hi
= a
->frac_hi
;
1311 r
->frac_lo
= a
->frac_hm
| ((a
->frac_lm
| a
->frac_lo
) != 0);
1314 #define frac_truncjam(R, A) FRAC_GENERIC_64_128(truncjam, R)(R, A)
1316 static void frac64_widen(FloatParts128
*r
, FloatParts64
*a
)
1318 r
->frac_hi
= a
->frac
;
1322 static void frac128_widen(FloatParts256
*r
, FloatParts128
*a
)
1324 r
->frac_hi
= a
->frac_hi
;
1325 r
->frac_hm
= a
->frac_lo
;
1330 #define frac_widen(A, B) FRAC_GENERIC_64_128(widen, B)(A, B)
1332 #define partsN(NAME) glue(glue(glue(parts,N),_),NAME)
1333 #define FloatPartsN glue(FloatParts,N)
1334 #define FloatPartsW glue(FloatParts,W)
1339 #include "softfloat-parts-addsub.c.inc"
1340 #include "softfloat-parts.c.inc"
1347 #include "softfloat-parts-addsub.c.inc"
1348 #include "softfloat-parts.c.inc"
1354 #include "softfloat-parts-addsub.c.inc"
1363 * Pack/unpack routines with a specific FloatFmt.
1366 static void float16a_unpack_canonical(FloatParts64
*p
, float16 f
,
1367 float_status
*s
, const FloatFmt
*params
)
1369 float16_unpack_raw(p
, f
);
1370 parts_canonicalize(p
, s
, params
);
1373 static void float16_unpack_canonical(FloatParts64
*p
, float16 f
,
1376 float16a_unpack_canonical(p
, f
, s
, &float16_params
);
1379 static void bfloat16_unpack_canonical(FloatParts64
*p
, bfloat16 f
,
1382 bfloat16_unpack_raw(p
, f
);
1383 parts_canonicalize(p
, s
, &bfloat16_params
);
1386 static float16
float16a_round_pack_canonical(FloatParts64
*p
,
1388 const FloatFmt
*params
)
1390 parts_uncanon(p
, s
, params
);
1391 return float16_pack_raw(p
);
1394 static float16
float16_round_pack_canonical(FloatParts64
*p
,
1397 return float16a_round_pack_canonical(p
, s
, &float16_params
);
1400 static bfloat16
bfloat16_round_pack_canonical(FloatParts64
*p
,
1403 parts_uncanon(p
, s
, &bfloat16_params
);
1404 return bfloat16_pack_raw(p
);
1407 static void float32_unpack_canonical(FloatParts64
*p
, float32 f
,
1410 float32_unpack_raw(p
, f
);
1411 parts_canonicalize(p
, s
, &float32_params
);
1414 static float32
float32_round_pack_canonical(FloatParts64
*p
,
1417 parts_uncanon(p
, s
, &float32_params
);
1418 return float32_pack_raw(p
);
1421 static void float64_unpack_canonical(FloatParts64
*p
, float64 f
,
1424 float64_unpack_raw(p
, f
);
1425 parts_canonicalize(p
, s
, &float64_params
);
1428 static float64
float64_round_pack_canonical(FloatParts64
*p
,
1431 parts_uncanon(p
, s
, &float64_params
);
1432 return float64_pack_raw(p
);
1435 static void float128_unpack_canonical(FloatParts128
*p
, float128 f
,
1438 float128_unpack_raw(p
, f
);
1439 parts_canonicalize(p
, s
, &float128_params
);
1442 static float128
float128_round_pack_canonical(FloatParts128
*p
,
1445 parts_uncanon(p
, s
, &float128_params
);
1446 return float128_pack_raw(p
);
1450 * Addition and subtraction
1453 static float16 QEMU_FLATTEN
1454 float16_addsub(float16 a
, float16 b
, float_status
*status
, bool subtract
)
1456 FloatParts64 pa
, pb
, *pr
;
1458 float16_unpack_canonical(&pa
, a
, status
);
1459 float16_unpack_canonical(&pb
, b
, status
);
1460 pr
= parts_addsub(&pa
, &pb
, status
, subtract
);
1462 return float16_round_pack_canonical(pr
, status
);
1465 float16
float16_add(float16 a
, float16 b
, float_status
*status
)
1467 return float16_addsub(a
, b
, status
, false);
1470 float16
float16_sub(float16 a
, float16 b
, float_status
*status
)
1472 return float16_addsub(a
, b
, status
, true);
1475 static float32 QEMU_SOFTFLOAT_ATTR
1476 soft_f32_addsub(float32 a
, float32 b
, float_status
*status
, bool subtract
)
1478 FloatParts64 pa
, pb
, *pr
;
1480 float32_unpack_canonical(&pa
, a
, status
);
1481 float32_unpack_canonical(&pb
, b
, status
);
1482 pr
= parts_addsub(&pa
, &pb
, status
, subtract
);
1484 return float32_round_pack_canonical(pr
, status
);
1487 static float32
soft_f32_add(float32 a
, float32 b
, float_status
*status
)
1489 return soft_f32_addsub(a
, b
, status
, false);
1492 static float32
soft_f32_sub(float32 a
, float32 b
, float_status
*status
)
1494 return soft_f32_addsub(a
, b
, status
, true);
1497 static float64 QEMU_SOFTFLOAT_ATTR
1498 soft_f64_addsub(float64 a
, float64 b
, float_status
*status
, bool subtract
)
1500 FloatParts64 pa
, pb
, *pr
;
1502 float64_unpack_canonical(&pa
, a
, status
);
1503 float64_unpack_canonical(&pb
, b
, status
);
1504 pr
= parts_addsub(&pa
, &pb
, status
, subtract
);
1506 return float64_round_pack_canonical(pr
, status
);
1509 static float64
soft_f64_add(float64 a
, float64 b
, float_status
*status
)
1511 return soft_f64_addsub(a
, b
, status
, false);
1514 static float64
soft_f64_sub(float64 a
, float64 b
, float_status
*status
)
1516 return soft_f64_addsub(a
, b
, status
, true);
1519 static float hard_f32_add(float a
, float b
)
1524 static float hard_f32_sub(float a
, float b
)
1529 static double hard_f64_add(double a
, double b
)
1534 static double hard_f64_sub(double a
, double b
)
1539 static bool f32_addsubmul_post(union_float32 a
, union_float32 b
)
1541 if (QEMU_HARDFLOAT_2F32_USE_FP
) {
1542 return !(fpclassify(a
.h
) == FP_ZERO
&& fpclassify(b
.h
) == FP_ZERO
);
1544 return !(float32_is_zero(a
.s
) && float32_is_zero(b
.s
));
1547 static bool f64_addsubmul_post(union_float64 a
, union_float64 b
)
1549 if (QEMU_HARDFLOAT_2F64_USE_FP
) {
1550 return !(fpclassify(a
.h
) == FP_ZERO
&& fpclassify(b
.h
) == FP_ZERO
);
1552 return !(float64_is_zero(a
.s
) && float64_is_zero(b
.s
));
1556 static float32
float32_addsub(float32 a
, float32 b
, float_status
*s
,
1557 hard_f32_op2_fn hard
, soft_f32_op2_fn soft
)
1559 return float32_gen2(a
, b
, s
, hard
, soft
,
1560 f32_is_zon2
, f32_addsubmul_post
);
1563 static float64
float64_addsub(float64 a
, float64 b
, float_status
*s
,
1564 hard_f64_op2_fn hard
, soft_f64_op2_fn soft
)
1566 return float64_gen2(a
, b
, s
, hard
, soft
,
1567 f64_is_zon2
, f64_addsubmul_post
);
1570 float32 QEMU_FLATTEN
1571 float32_add(float32 a
, float32 b
, float_status
*s
)
1573 return float32_addsub(a
, b
, s
, hard_f32_add
, soft_f32_add
);
1576 float32 QEMU_FLATTEN
1577 float32_sub(float32 a
, float32 b
, float_status
*s
)
1579 return float32_addsub(a
, b
, s
, hard_f32_sub
, soft_f32_sub
);
1582 float64 QEMU_FLATTEN
1583 float64_add(float64 a
, float64 b
, float_status
*s
)
1585 return float64_addsub(a
, b
, s
, hard_f64_add
, soft_f64_add
);
1588 float64 QEMU_FLATTEN
1589 float64_sub(float64 a
, float64 b
, float_status
*s
)
1591 return float64_addsub(a
, b
, s
, hard_f64_sub
, soft_f64_sub
);
1594 static bfloat16 QEMU_FLATTEN
1595 bfloat16_addsub(bfloat16 a
, bfloat16 b
, float_status
*status
, bool subtract
)
1597 FloatParts64 pa
, pb
, *pr
;
1599 bfloat16_unpack_canonical(&pa
, a
, status
);
1600 bfloat16_unpack_canonical(&pb
, b
, status
);
1601 pr
= parts_addsub(&pa
, &pb
, status
, subtract
);
1603 return bfloat16_round_pack_canonical(pr
, status
);
1606 bfloat16
bfloat16_add(bfloat16 a
, bfloat16 b
, float_status
*status
)
1608 return bfloat16_addsub(a
, b
, status
, false);
1611 bfloat16
bfloat16_sub(bfloat16 a
, bfloat16 b
, float_status
*status
)
1613 return bfloat16_addsub(a
, b
, status
, true);
1616 static float128 QEMU_FLATTEN
1617 float128_addsub(float128 a
, float128 b
, float_status
*status
, bool subtract
)
1619 FloatParts128 pa
, pb
, *pr
;
1621 float128_unpack_canonical(&pa
, a
, status
);
1622 float128_unpack_canonical(&pb
, b
, status
);
1623 pr
= parts_addsub(&pa
, &pb
, status
, subtract
);
1625 return float128_round_pack_canonical(pr
, status
);
1628 float128
float128_add(float128 a
, float128 b
, float_status
*status
)
1630 return float128_addsub(a
, b
, status
, false);
1633 float128
float128_sub(float128 a
, float128 b
, float_status
*status
)
1635 return float128_addsub(a
, b
, status
, true);
1642 float16 QEMU_FLATTEN
float16_mul(float16 a
, float16 b
, float_status
*status
)
1644 FloatParts64 pa
, pb
, *pr
;
1646 float16_unpack_canonical(&pa
, a
, status
);
1647 float16_unpack_canonical(&pb
, b
, status
);
1648 pr
= parts_mul(&pa
, &pb
, status
);
1650 return float16_round_pack_canonical(pr
, status
);
1653 static float32 QEMU_SOFTFLOAT_ATTR
1654 soft_f32_mul(float32 a
, float32 b
, float_status
*status
)
1656 FloatParts64 pa
, pb
, *pr
;
1658 float32_unpack_canonical(&pa
, a
, status
);
1659 float32_unpack_canonical(&pb
, b
, status
);
1660 pr
= parts_mul(&pa
, &pb
, status
);
1662 return float32_round_pack_canonical(pr
, status
);
1665 static float64 QEMU_SOFTFLOAT_ATTR
1666 soft_f64_mul(float64 a
, float64 b
, float_status
*status
)
1668 FloatParts64 pa
, pb
, *pr
;
1670 float64_unpack_canonical(&pa
, a
, status
);
1671 float64_unpack_canonical(&pb
, b
, status
);
1672 pr
= parts_mul(&pa
, &pb
, status
);
1674 return float64_round_pack_canonical(pr
, status
);
1677 static float hard_f32_mul(float a
, float b
)
1682 static double hard_f64_mul(double a
, double b
)
1687 float32 QEMU_FLATTEN
1688 float32_mul(float32 a
, float32 b
, float_status
*s
)
1690 return float32_gen2(a
, b
, s
, hard_f32_mul
, soft_f32_mul
,
1691 f32_is_zon2
, f32_addsubmul_post
);
1694 float64 QEMU_FLATTEN
1695 float64_mul(float64 a
, float64 b
, float_status
*s
)
1697 return float64_gen2(a
, b
, s
, hard_f64_mul
, soft_f64_mul
,
1698 f64_is_zon2
, f64_addsubmul_post
);
1701 bfloat16 QEMU_FLATTEN
1702 bfloat16_mul(bfloat16 a
, bfloat16 b
, float_status
*status
)
1704 FloatParts64 pa
, pb
, *pr
;
1706 bfloat16_unpack_canonical(&pa
, a
, status
);
1707 bfloat16_unpack_canonical(&pb
, b
, status
);
1708 pr
= parts_mul(&pa
, &pb
, status
);
1710 return bfloat16_round_pack_canonical(pr
, status
);
1713 float128 QEMU_FLATTEN
1714 float128_mul(float128 a
, float128 b
, float_status
*status
)
1716 FloatParts128 pa
, pb
, *pr
;
1718 float128_unpack_canonical(&pa
, a
, status
);
1719 float128_unpack_canonical(&pb
, b
, status
);
1720 pr
= parts_mul(&pa
, &pb
, status
);
1722 return float128_round_pack_canonical(pr
, status
);
1726 * Fused multiply-add
1729 float16 QEMU_FLATTEN
float16_muladd(float16 a
, float16 b
, float16 c
,
1730 int flags
, float_status
*status
)
1732 FloatParts64 pa
, pb
, pc
, *pr
;
1734 float16_unpack_canonical(&pa
, a
, status
);
1735 float16_unpack_canonical(&pb
, b
, status
);
1736 float16_unpack_canonical(&pc
, c
, status
);
1737 pr
= parts_muladd(&pa
, &pb
, &pc
, flags
, status
);
1739 return float16_round_pack_canonical(pr
, status
);
1742 static float32 QEMU_SOFTFLOAT_ATTR
1743 soft_f32_muladd(float32 a
, float32 b
, float32 c
, int flags
,
1744 float_status
*status
)
1746 FloatParts64 pa
, pb
, pc
, *pr
;
1748 float32_unpack_canonical(&pa
, a
, status
);
1749 float32_unpack_canonical(&pb
, b
, status
);
1750 float32_unpack_canonical(&pc
, c
, status
);
1751 pr
= parts_muladd(&pa
, &pb
, &pc
, flags
, status
);
1753 return float32_round_pack_canonical(pr
, status
);
1756 static float64 QEMU_SOFTFLOAT_ATTR
1757 soft_f64_muladd(float64 a
, float64 b
, float64 c
, int flags
,
1758 float_status
*status
)
1760 FloatParts64 pa
, pb
, pc
, *pr
;
1762 float64_unpack_canonical(&pa
, a
, status
);
1763 float64_unpack_canonical(&pb
, b
, status
);
1764 float64_unpack_canonical(&pc
, c
, status
);
1765 pr
= parts_muladd(&pa
, &pb
, &pc
, flags
, status
);
1767 return float64_round_pack_canonical(pr
, status
);
1770 static bool force_soft_fma
;
1772 float32 QEMU_FLATTEN
1773 float32_muladd(float32 xa
, float32 xb
, float32 xc
, int flags
, float_status
*s
)
1775 union_float32 ua
, ub
, uc
, ur
;
1781 if (unlikely(!can_use_fpu(s
))) {
1784 if (unlikely(flags
& float_muladd_halve_result
)) {
1788 float32_input_flush3(&ua
.s
, &ub
.s
, &uc
.s
, s
);
1789 if (unlikely(!f32_is_zon3(ua
, ub
, uc
))) {
1793 if (unlikely(force_soft_fma
)) {
1798 * When (a || b) == 0, there's no need to check for under/over flow,
1799 * since we know the addend is (normal || 0) and the product is 0.
1801 if (float32_is_zero(ua
.s
) || float32_is_zero(ub
.s
)) {
1805 prod_sign
= float32_is_neg(ua
.s
) ^ float32_is_neg(ub
.s
);
1806 prod_sign
^= !!(flags
& float_muladd_negate_product
);
1807 up
.s
= float32_set_sign(float32_zero
, prod_sign
);
1809 if (flags
& float_muladd_negate_c
) {
1814 union_float32 ua_orig
= ua
;
1815 union_float32 uc_orig
= uc
;
1817 if (flags
& float_muladd_negate_product
) {
1820 if (flags
& float_muladd_negate_c
) {
1824 ur
.h
= fmaf(ua
.h
, ub
.h
, uc
.h
);
1826 if (unlikely(f32_is_inf(ur
))) {
1827 float_raise(float_flag_overflow
, s
);
1828 } else if (unlikely(fabsf(ur
.h
) <= FLT_MIN
)) {
1834 if (flags
& float_muladd_negate_result
) {
1835 return float32_chs(ur
.s
);
1840 return soft_f32_muladd(ua
.s
, ub
.s
, uc
.s
, flags
, s
);
1843 float64 QEMU_FLATTEN
1844 float64_muladd(float64 xa
, float64 xb
, float64 xc
, int flags
, float_status
*s
)
1846 union_float64 ua
, ub
, uc
, ur
;
1852 if (unlikely(!can_use_fpu(s
))) {
1855 if (unlikely(flags
& float_muladd_halve_result
)) {
1859 float64_input_flush3(&ua
.s
, &ub
.s
, &uc
.s
, s
);
1860 if (unlikely(!f64_is_zon3(ua
, ub
, uc
))) {
1864 if (unlikely(force_soft_fma
)) {
1869 * When (a || b) == 0, there's no need to check for under/over flow,
1870 * since we know the addend is (normal || 0) and the product is 0.
1872 if (float64_is_zero(ua
.s
) || float64_is_zero(ub
.s
)) {
1876 prod_sign
= float64_is_neg(ua
.s
) ^ float64_is_neg(ub
.s
);
1877 prod_sign
^= !!(flags
& float_muladd_negate_product
);
1878 up
.s
= float64_set_sign(float64_zero
, prod_sign
);
1880 if (flags
& float_muladd_negate_c
) {
1885 union_float64 ua_orig
= ua
;
1886 union_float64 uc_orig
= uc
;
1888 if (flags
& float_muladd_negate_product
) {
1891 if (flags
& float_muladd_negate_c
) {
1895 ur
.h
= fma(ua
.h
, ub
.h
, uc
.h
);
1897 if (unlikely(f64_is_inf(ur
))) {
1898 float_raise(float_flag_overflow
, s
);
1899 } else if (unlikely(fabs(ur
.h
) <= FLT_MIN
)) {
1905 if (flags
& float_muladd_negate_result
) {
1906 return float64_chs(ur
.s
);
1911 return soft_f64_muladd(ua
.s
, ub
.s
, uc
.s
, flags
, s
);
1914 bfloat16 QEMU_FLATTEN
bfloat16_muladd(bfloat16 a
, bfloat16 b
, bfloat16 c
,
1915 int flags
, float_status
*status
)
1917 FloatParts64 pa
, pb
, pc
, *pr
;
1919 bfloat16_unpack_canonical(&pa
, a
, status
);
1920 bfloat16_unpack_canonical(&pb
, b
, status
);
1921 bfloat16_unpack_canonical(&pc
, c
, status
);
1922 pr
= parts_muladd(&pa
, &pb
, &pc
, flags
, status
);
1924 return bfloat16_round_pack_canonical(pr
, status
);
1927 float128 QEMU_FLATTEN
float128_muladd(float128 a
, float128 b
, float128 c
,
1928 int flags
, float_status
*status
)
1930 FloatParts128 pa
, pb
, pc
, *pr
;
1932 float128_unpack_canonical(&pa
, a
, status
);
1933 float128_unpack_canonical(&pb
, b
, status
);
1934 float128_unpack_canonical(&pc
, c
, status
);
1935 pr
= parts_muladd(&pa
, &pb
, &pc
, flags
, status
);
1937 return float128_round_pack_canonical(pr
, status
);
1944 float16
float16_div(float16 a
, float16 b
, float_status
*status
)
1946 FloatParts64 pa
, pb
, *pr
;
1948 float16_unpack_canonical(&pa
, a
, status
);
1949 float16_unpack_canonical(&pb
, b
, status
);
1950 pr
= parts_div(&pa
, &pb
, status
);
1952 return float16_round_pack_canonical(pr
, status
);
1955 static float32 QEMU_SOFTFLOAT_ATTR
1956 soft_f32_div(float32 a
, float32 b
, float_status
*status
)
1958 FloatParts64 pa
, pb
, *pr
;
1960 float32_unpack_canonical(&pa
, a
, status
);
1961 float32_unpack_canonical(&pb
, b
, status
);
1962 pr
= parts_div(&pa
, &pb
, status
);
1964 return float32_round_pack_canonical(pr
, status
);
1967 static float64 QEMU_SOFTFLOAT_ATTR
1968 soft_f64_div(float64 a
, float64 b
, float_status
*status
)
1970 FloatParts64 pa
, pb
, *pr
;
1972 float64_unpack_canonical(&pa
, a
, status
);
1973 float64_unpack_canonical(&pb
, b
, status
);
1974 pr
= parts_div(&pa
, &pb
, status
);
1976 return float64_round_pack_canonical(pr
, status
);
1979 static float hard_f32_div(float a
, float b
)
1984 static double hard_f64_div(double a
, double b
)
1989 static bool f32_div_pre(union_float32 a
, union_float32 b
)
1991 if (QEMU_HARDFLOAT_2F32_USE_FP
) {
1992 return (fpclassify(a
.h
) == FP_NORMAL
|| fpclassify(a
.h
) == FP_ZERO
) &&
1993 fpclassify(b
.h
) == FP_NORMAL
;
1995 return float32_is_zero_or_normal(a
.s
) && float32_is_normal(b
.s
);
1998 static bool f64_div_pre(union_float64 a
, union_float64 b
)
2000 if (QEMU_HARDFLOAT_2F64_USE_FP
) {
2001 return (fpclassify(a
.h
) == FP_NORMAL
|| fpclassify(a
.h
) == FP_ZERO
) &&
2002 fpclassify(b
.h
) == FP_NORMAL
;
2004 return float64_is_zero_or_normal(a
.s
) && float64_is_normal(b
.s
);
2007 static bool f32_div_post(union_float32 a
, union_float32 b
)
2009 if (QEMU_HARDFLOAT_2F32_USE_FP
) {
2010 return fpclassify(a
.h
) != FP_ZERO
;
2012 return !float32_is_zero(a
.s
);
2015 static bool f64_div_post(union_float64 a
, union_float64 b
)
2017 if (QEMU_HARDFLOAT_2F64_USE_FP
) {
2018 return fpclassify(a
.h
) != FP_ZERO
;
2020 return !float64_is_zero(a
.s
);
2023 float32 QEMU_FLATTEN
2024 float32_div(float32 a
, float32 b
, float_status
*s
)
2026 return float32_gen2(a
, b
, s
, hard_f32_div
, soft_f32_div
,
2027 f32_div_pre
, f32_div_post
);
2030 float64 QEMU_FLATTEN
2031 float64_div(float64 a
, float64 b
, float_status
*s
)
2033 return float64_gen2(a
, b
, s
, hard_f64_div
, soft_f64_div
,
2034 f64_div_pre
, f64_div_post
);
2037 bfloat16 QEMU_FLATTEN
2038 bfloat16_div(bfloat16 a
, bfloat16 b
, float_status
*status
)
2040 FloatParts64 pa
, pb
, *pr
;
2042 bfloat16_unpack_canonical(&pa
, a
, status
);
2043 bfloat16_unpack_canonical(&pb
, b
, status
);
2044 pr
= parts_div(&pa
, &pb
, status
);
2046 return bfloat16_round_pack_canonical(pr
, status
);
2049 float128 QEMU_FLATTEN
2050 float128_div(float128 a
, float128 b
, float_status
*status
)
2052 FloatParts128 pa
, pb
, *pr
;
2054 float128_unpack_canonical(&pa
, a
, status
);
2055 float128_unpack_canonical(&pb
, b
, status
);
2056 pr
= parts_div(&pa
, &pb
, status
);
2058 return float128_round_pack_canonical(pr
, status
);
2062 * Float to Float conversions
2064 * Returns the result of converting one float format to another. The
2065 * conversion is performed according to the IEC/IEEE Standard for
2066 * Binary Floating-Point Arithmetic.
2068 * Usually this only needs to take care of raising invalid exceptions
2069 * and handling the conversion on NaNs.
2072 static void parts_float_to_ahp(FloatParts64
*a
, float_status
*s
)
2075 case float_class_qnan
:
2076 case float_class_snan
:
2078 * There is no NaN in the destination format. Raise Invalid
2079 * and return a zero with the sign of the input NaN.
2081 float_raise(float_flag_invalid
, s
);
2082 a
->cls
= float_class_zero
;
2085 case float_class_inf
:
2087 * There is no Inf in the destination format. Raise Invalid
2088 * and return the maximum normal with the correct sign.
2090 float_raise(float_flag_invalid
, s
);
2091 a
->cls
= float_class_normal
;
2092 a
->exp
= float16_params_ahp
.exp_max
;
2093 a
->frac
= MAKE_64BIT_MASK(float16_params_ahp
.frac_shift
,
2094 float16_params_ahp
.frac_size
+ 1);
2097 case float_class_normal
:
2098 case float_class_zero
:
2102 g_assert_not_reached();
2106 static void parts64_float_to_float(FloatParts64
*a
, float_status
*s
)
2108 if (is_nan(a
->cls
)) {
2109 parts_return_nan(a
, s
);
2113 static void parts128_float_to_float(FloatParts128
*a
, float_status
*s
)
2115 if (is_nan(a
->cls
)) {
2116 parts_return_nan(a
, s
);
2120 #define parts_float_to_float(P, S) \
2121 PARTS_GENERIC_64_128(float_to_float, P)(P, S)
2123 static void parts_float_to_float_narrow(FloatParts64
*a
, FloatParts128
*b
,
2130 if (a
->cls
== float_class_normal
) {
2131 frac_truncjam(a
, b
);
2132 } else if (is_nan(a
->cls
)) {
2133 /* Discard the low bits of the NaN. */
2134 a
->frac
= b
->frac_hi
;
2135 parts_return_nan(a
, s
);
2139 static void parts_float_to_float_widen(FloatParts128
*a
, FloatParts64
*b
,
2147 if (is_nan(a
->cls
)) {
2148 parts_return_nan(a
, s
);
2152 float32
float16_to_float32(float16 a
, bool ieee
, float_status
*s
)
2154 const FloatFmt
*fmt16
= ieee
? &float16_params
: &float16_params_ahp
;
2157 float16a_unpack_canonical(&p
, a
, s
, fmt16
);
2158 parts_float_to_float(&p
, s
);
2159 return float32_round_pack_canonical(&p
, s
);
2162 float64
float16_to_float64(float16 a
, bool ieee
, float_status
*s
)
2164 const FloatFmt
*fmt16
= ieee
? &float16_params
: &float16_params_ahp
;
2167 float16a_unpack_canonical(&p
, a
, s
, fmt16
);
2168 parts_float_to_float(&p
, s
);
2169 return float64_round_pack_canonical(&p
, s
);
2172 float16
float32_to_float16(float32 a
, bool ieee
, float_status
*s
)
2175 const FloatFmt
*fmt
;
2177 float32_unpack_canonical(&p
, a
, s
);
2179 parts_float_to_float(&p
, s
);
2180 fmt
= &float16_params
;
2182 parts_float_to_ahp(&p
, s
);
2183 fmt
= &float16_params_ahp
;
2185 return float16a_round_pack_canonical(&p
, s
, fmt
);
2188 static float64 QEMU_SOFTFLOAT_ATTR
2189 soft_float32_to_float64(float32 a
, float_status
*s
)
2193 float32_unpack_canonical(&p
, a
, s
);
2194 parts_float_to_float(&p
, s
);
2195 return float64_round_pack_canonical(&p
, s
);
2198 float64
float32_to_float64(float32 a
, float_status
*s
)
2200 if (likely(float32_is_normal(a
))) {
2201 /* Widening conversion can never produce inexact results. */
2207 } else if (float32_is_zero(a
)) {
2208 return float64_set_sign(float64_zero
, float32_is_neg(a
));
2210 return soft_float32_to_float64(a
, s
);
2214 float16
float64_to_float16(float64 a
, bool ieee
, float_status
*s
)
2217 const FloatFmt
*fmt
;
2219 float64_unpack_canonical(&p
, a
, s
);
2221 parts_float_to_float(&p
, s
);
2222 fmt
= &float16_params
;
2224 parts_float_to_ahp(&p
, s
);
2225 fmt
= &float16_params_ahp
;
2227 return float16a_round_pack_canonical(&p
, s
, fmt
);
2230 float32
float64_to_float32(float64 a
, float_status
*s
)
2234 float64_unpack_canonical(&p
, a
, s
);
2235 parts_float_to_float(&p
, s
);
2236 return float32_round_pack_canonical(&p
, s
);
2239 float32
bfloat16_to_float32(bfloat16 a
, float_status
*s
)
2243 bfloat16_unpack_canonical(&p
, a
, s
);
2244 parts_float_to_float(&p
, s
);
2245 return float32_round_pack_canonical(&p
, s
);
2248 float64
bfloat16_to_float64(bfloat16 a
, float_status
*s
)
2252 bfloat16_unpack_canonical(&p
, a
, s
);
2253 parts_float_to_float(&p
, s
);
2254 return float64_round_pack_canonical(&p
, s
);
2257 bfloat16
float32_to_bfloat16(float32 a
, float_status
*s
)
2261 float32_unpack_canonical(&p
, a
, s
);
2262 parts_float_to_float(&p
, s
);
2263 return bfloat16_round_pack_canonical(&p
, s
);
2266 bfloat16
float64_to_bfloat16(float64 a
, float_status
*s
)
2270 float64_unpack_canonical(&p
, a
, s
);
2271 parts_float_to_float(&p
, s
);
2272 return bfloat16_round_pack_canonical(&p
, s
);
2275 float32
float128_to_float32(float128 a
, float_status
*s
)
2280 float128_unpack_canonical(&p128
, a
, s
);
2281 parts_float_to_float_narrow(&p64
, &p128
, s
);
2282 return float32_round_pack_canonical(&p64
, s
);
2285 float64
float128_to_float64(float128 a
, float_status
*s
)
2290 float128_unpack_canonical(&p128
, a
, s
);
2291 parts_float_to_float_narrow(&p64
, &p128
, s
);
2292 return float64_round_pack_canonical(&p64
, s
);
2295 float128
float32_to_float128(float32 a
, float_status
*s
)
2300 float32_unpack_canonical(&p64
, a
, s
);
2301 parts_float_to_float_widen(&p128
, &p64
, s
);
2302 return float128_round_pack_canonical(&p128
, s
);
2305 float128
float64_to_float128(float64 a
, float_status
*s
)
2310 float64_unpack_canonical(&p64
, a
, s
);
2311 parts_float_to_float_widen(&p128
, &p64
, s
);
2312 return float128_round_pack_canonical(&p128
, s
);
2316 * Round to integral value
2319 float16
float16_round_to_int(float16 a
, float_status
*s
)
2323 float16_unpack_canonical(&p
, a
, s
);
2324 parts_round_to_int(&p
, s
->float_rounding_mode
, 0, s
, &float16_params
);
2325 return float16_round_pack_canonical(&p
, s
);
2328 float32
float32_round_to_int(float32 a
, float_status
*s
)
2332 float32_unpack_canonical(&p
, a
, s
);
2333 parts_round_to_int(&p
, s
->float_rounding_mode
, 0, s
, &float32_params
);
2334 return float32_round_pack_canonical(&p
, s
);
2337 float64
float64_round_to_int(float64 a
, float_status
*s
)
2341 float64_unpack_canonical(&p
, a
, s
);
2342 parts_round_to_int(&p
, s
->float_rounding_mode
, 0, s
, &float64_params
);
2343 return float64_round_pack_canonical(&p
, s
);
2346 bfloat16
bfloat16_round_to_int(bfloat16 a
, float_status
*s
)
2350 bfloat16_unpack_canonical(&p
, a
, s
);
2351 parts_round_to_int(&p
, s
->float_rounding_mode
, 0, s
, &bfloat16_params
);
2352 return bfloat16_round_pack_canonical(&p
, s
);
2355 float128
float128_round_to_int(float128 a
, float_status
*s
)
2359 float128_unpack_canonical(&p
, a
, s
);
2360 parts_round_to_int(&p
, s
->float_rounding_mode
, 0, s
, &float128_params
);
2361 return float128_round_pack_canonical(&p
, s
);
2365 * Floating-point to signed integer conversions
2368 int8_t float16_to_int8_scalbn(float16 a
, FloatRoundMode rmode
, int scale
,
2373 float16_unpack_canonical(&p
, a
, s
);
2374 return parts_float_to_sint(&p
, rmode
, scale
, INT8_MIN
, INT8_MAX
, s
);
2377 int16_t float16_to_int16_scalbn(float16 a
, FloatRoundMode rmode
, int scale
,
2382 float16_unpack_canonical(&p
, a
, s
);
2383 return parts_float_to_sint(&p
, rmode
, scale
, INT16_MIN
, INT16_MAX
, s
);
2386 int32_t float16_to_int32_scalbn(float16 a
, FloatRoundMode rmode
, int scale
,
2391 float16_unpack_canonical(&p
, a
, s
);
2392 return parts_float_to_sint(&p
, rmode
, scale
, INT32_MIN
, INT32_MAX
, s
);
2395 int64_t float16_to_int64_scalbn(float16 a
, FloatRoundMode rmode
, int scale
,
2400 float16_unpack_canonical(&p
, a
, s
);
2401 return parts_float_to_sint(&p
, rmode
, scale
, INT64_MIN
, INT64_MAX
, s
);
2404 int16_t float32_to_int16_scalbn(float32 a
, FloatRoundMode rmode
, int scale
,
2409 float32_unpack_canonical(&p
, a
, s
);
2410 return parts_float_to_sint(&p
, rmode
, scale
, INT16_MIN
, INT16_MAX
, s
);
2413 int32_t float32_to_int32_scalbn(float32 a
, FloatRoundMode rmode
, int scale
,
2418 float32_unpack_canonical(&p
, a
, s
);
2419 return parts_float_to_sint(&p
, rmode
, scale
, INT32_MIN
, INT32_MAX
, s
);
2422 int64_t float32_to_int64_scalbn(float32 a
, FloatRoundMode rmode
, int scale
,
2427 float32_unpack_canonical(&p
, a
, s
);
2428 return parts_float_to_sint(&p
, rmode
, scale
, INT64_MIN
, INT64_MAX
, s
);
2431 int16_t float64_to_int16_scalbn(float64 a
, FloatRoundMode rmode
, int scale
,
2436 float64_unpack_canonical(&p
, a
, s
);
2437 return parts_float_to_sint(&p
, rmode
, scale
, INT16_MIN
, INT16_MAX
, s
);
2440 int32_t float64_to_int32_scalbn(float64 a
, FloatRoundMode rmode
, int scale
,
2445 float64_unpack_canonical(&p
, a
, s
);
2446 return parts_float_to_sint(&p
, rmode
, scale
, INT32_MIN
, INT32_MAX
, s
);
2449 int64_t float64_to_int64_scalbn(float64 a
, FloatRoundMode rmode
, int scale
,
2454 float64_unpack_canonical(&p
, a
, s
);
2455 return parts_float_to_sint(&p
, rmode
, scale
, INT64_MIN
, INT64_MAX
, s
);
2458 int16_t bfloat16_to_int16_scalbn(bfloat16 a
, FloatRoundMode rmode
, int scale
,
2463 bfloat16_unpack_canonical(&p
, a
, s
);
2464 return parts_float_to_sint(&p
, rmode
, scale
, INT16_MIN
, INT16_MAX
, s
);
2467 int32_t bfloat16_to_int32_scalbn(bfloat16 a
, FloatRoundMode rmode
, int scale
,
2472 bfloat16_unpack_canonical(&p
, a
, s
);
2473 return parts_float_to_sint(&p
, rmode
, scale
, INT32_MIN
, INT32_MAX
, s
);
2476 int64_t bfloat16_to_int64_scalbn(bfloat16 a
, FloatRoundMode rmode
, int scale
,
2481 bfloat16_unpack_canonical(&p
, a
, s
);
2482 return parts_float_to_sint(&p
, rmode
, scale
, INT64_MIN
, INT64_MAX
, s
);
2485 static int32_t float128_to_int32_scalbn(float128 a
, FloatRoundMode rmode
,
2486 int scale
, float_status
*s
)
2490 float128_unpack_canonical(&p
, a
, s
);
2491 return parts_float_to_sint(&p
, rmode
, scale
, INT32_MIN
, INT32_MAX
, s
);
2494 static int64_t float128_to_int64_scalbn(float128 a
, FloatRoundMode rmode
,
2495 int scale
, float_status
*s
)
2499 float128_unpack_canonical(&p
, a
, s
);
2500 return parts_float_to_sint(&p
, rmode
, scale
, INT64_MIN
, INT64_MAX
, s
);
2503 int8_t float16_to_int8(float16 a
, float_status
*s
)
2505 return float16_to_int8_scalbn(a
, s
->float_rounding_mode
, 0, s
);
2508 int16_t float16_to_int16(float16 a
, float_status
*s
)
2510 return float16_to_int16_scalbn(a
, s
->float_rounding_mode
, 0, s
);
2513 int32_t float16_to_int32(float16 a
, float_status
*s
)
2515 return float16_to_int32_scalbn(a
, s
->float_rounding_mode
, 0, s
);
2518 int64_t float16_to_int64(float16 a
, float_status
*s
)
2520 return float16_to_int64_scalbn(a
, s
->float_rounding_mode
, 0, s
);
2523 int16_t float32_to_int16(float32 a
, float_status
*s
)
2525 return float32_to_int16_scalbn(a
, s
->float_rounding_mode
, 0, s
);
2528 int32_t float32_to_int32(float32 a
, float_status
*s
)
2530 return float32_to_int32_scalbn(a
, s
->float_rounding_mode
, 0, s
);
2533 int64_t float32_to_int64(float32 a
, float_status
*s
)
2535 return float32_to_int64_scalbn(a
, s
->float_rounding_mode
, 0, s
);
2538 int16_t float64_to_int16(float64 a
, float_status
*s
)
2540 return float64_to_int16_scalbn(a
, s
->float_rounding_mode
, 0, s
);
2543 int32_t float64_to_int32(float64 a
, float_status
*s
)
2545 return float64_to_int32_scalbn(a
, s
->float_rounding_mode
, 0, s
);
2548 int64_t float64_to_int64(float64 a
, float_status
*s
)
2550 return float64_to_int64_scalbn(a
, s
->float_rounding_mode
, 0, s
);
2553 int32_t float128_to_int32(float128 a
, float_status
*s
)
2555 return float128_to_int32_scalbn(a
, s
->float_rounding_mode
, 0, s
);
2558 int64_t float128_to_int64(float128 a
, float_status
*s
)
2560 return float128_to_int64_scalbn(a
, s
->float_rounding_mode
, 0, s
);
2563 int16_t float16_to_int16_round_to_zero(float16 a
, float_status
*s
)
2565 return float16_to_int16_scalbn(a
, float_round_to_zero
, 0, s
);
2568 int32_t float16_to_int32_round_to_zero(float16 a
, float_status
*s
)
2570 return float16_to_int32_scalbn(a
, float_round_to_zero
, 0, s
);
2573 int64_t float16_to_int64_round_to_zero(float16 a
, float_status
*s
)
2575 return float16_to_int64_scalbn(a
, float_round_to_zero
, 0, s
);
2578 int16_t float32_to_int16_round_to_zero(float32 a
, float_status
*s
)
2580 return float32_to_int16_scalbn(a
, float_round_to_zero
, 0, s
);
2583 int32_t float32_to_int32_round_to_zero(float32 a
, float_status
*s
)
2585 return float32_to_int32_scalbn(a
, float_round_to_zero
, 0, s
);
2588 int64_t float32_to_int64_round_to_zero(float32 a
, float_status
*s
)
2590 return float32_to_int64_scalbn(a
, float_round_to_zero
, 0, s
);
2593 int16_t float64_to_int16_round_to_zero(float64 a
, float_status
*s
)
2595 return float64_to_int16_scalbn(a
, float_round_to_zero
, 0, s
);
2598 int32_t float64_to_int32_round_to_zero(float64 a
, float_status
*s
)
2600 return float64_to_int32_scalbn(a
, float_round_to_zero
, 0, s
);
2603 int64_t float64_to_int64_round_to_zero(float64 a
, float_status
*s
)
2605 return float64_to_int64_scalbn(a
, float_round_to_zero
, 0, s
);
2608 int32_t float128_to_int32_round_to_zero(float128 a
, float_status
*s
)
2610 return float128_to_int32_scalbn(a
, float_round_to_zero
, 0, s
);
2613 int64_t float128_to_int64_round_to_zero(float128 a
, float_status
*s
)
2615 return float128_to_int64_scalbn(a
, float_round_to_zero
, 0, s
);
2618 int16_t bfloat16_to_int16(bfloat16 a
, float_status
*s
)
2620 return bfloat16_to_int16_scalbn(a
, s
->float_rounding_mode
, 0, s
);
2623 int32_t bfloat16_to_int32(bfloat16 a
, float_status
*s
)
2625 return bfloat16_to_int32_scalbn(a
, s
->float_rounding_mode
, 0, s
);
2628 int64_t bfloat16_to_int64(bfloat16 a
, float_status
*s
)
2630 return bfloat16_to_int64_scalbn(a
, s
->float_rounding_mode
, 0, s
);
2633 int16_t bfloat16_to_int16_round_to_zero(bfloat16 a
, float_status
*s
)
2635 return bfloat16_to_int16_scalbn(a
, float_round_to_zero
, 0, s
);
2638 int32_t bfloat16_to_int32_round_to_zero(bfloat16 a
, float_status
*s
)
2640 return bfloat16_to_int32_scalbn(a
, float_round_to_zero
, 0, s
);
2643 int64_t bfloat16_to_int64_round_to_zero(bfloat16 a
, float_status
*s
)
2645 return bfloat16_to_int64_scalbn(a
, float_round_to_zero
, 0, s
);
2649 * Returns the result of converting the floating-point value `a' to
2650 * the unsigned integer format. The conversion is performed according
2651 * to the IEC/IEEE Standard for Binary Floating-Point
2652 * Arithmetic---which means in particular that the conversion is
2653 * rounded according to the current rounding mode. If `a' is a NaN,
2654 * the largest unsigned integer is returned. Otherwise, if the
2655 * conversion overflows, the largest unsigned integer is returned. If
2656 * the 'a' is negative, the result is rounded and zero is returned;
2657 * values that do not round to zero will raise the inexact exception
2661 static uint64_t round_to_uint_and_pack(FloatParts64 p
, FloatRoundMode rmode
,
2662 int scale
, uint64_t max
,
2669 case float_class_snan
:
2670 case float_class_qnan
:
2671 flags
= float_flag_invalid
;
2675 case float_class_inf
:
2676 flags
= float_flag_invalid
;
2677 r
= p
.sign
? 0 : max
;
2680 case float_class_zero
:
2683 case float_class_normal
:
2684 /* TODO: 62 = N - 2, frac_size for rounding */
2685 if (parts_round_to_int_normal(&p
, rmode
, scale
, 62)) {
2686 flags
= float_flag_inexact
;
2687 if (p
.cls
== float_class_zero
) {
2694 flags
= float_flag_invalid
;
2696 } else if (p
.exp
> DECOMPOSED_BINARY_POINT
) {
2697 flags
= float_flag_invalid
;
2700 r
= p
.frac
>> (DECOMPOSED_BINARY_POINT
- p
.exp
);
2702 flags
= float_flag_invalid
;
2709 g_assert_not_reached();
2712 float_raise(flags
, s
);
2716 uint8_t float16_to_uint8_scalbn(float16 a
, FloatRoundMode rmode
, int scale
,
2721 float16_unpack_canonical(&p
, a
, s
);
2722 return round_to_uint_and_pack(p
, rmode
, scale
, UINT8_MAX
, s
);
2725 uint16_t float16_to_uint16_scalbn(float16 a
, FloatRoundMode rmode
, int scale
,
2730 float16_unpack_canonical(&p
, a
, s
);
2731 return round_to_uint_and_pack(p
, rmode
, scale
, UINT16_MAX
, s
);
2734 uint32_t float16_to_uint32_scalbn(float16 a
, FloatRoundMode rmode
, int scale
,
2739 float16_unpack_canonical(&p
, a
, s
);
2740 return round_to_uint_and_pack(p
, rmode
, scale
, UINT32_MAX
, s
);
2743 uint64_t float16_to_uint64_scalbn(float16 a
, FloatRoundMode rmode
, int scale
,
2748 float16_unpack_canonical(&p
, a
, s
);
2749 return round_to_uint_and_pack(p
, rmode
, scale
, UINT64_MAX
, s
);
2752 uint16_t float32_to_uint16_scalbn(float32 a
, FloatRoundMode rmode
, int scale
,
2757 float32_unpack_canonical(&p
, a
, s
);
2758 return round_to_uint_and_pack(p
, rmode
, scale
, UINT16_MAX
, s
);
2761 uint32_t float32_to_uint32_scalbn(float32 a
, FloatRoundMode rmode
, int scale
,
2766 float32_unpack_canonical(&p
, a
, s
);
2767 return round_to_uint_and_pack(p
, rmode
, scale
, UINT32_MAX
, s
);
2770 uint64_t float32_to_uint64_scalbn(float32 a
, FloatRoundMode rmode
, int scale
,
2775 float32_unpack_canonical(&p
, a
, s
);
2776 return round_to_uint_and_pack(p
, rmode
, scale
, UINT64_MAX
, s
);
2779 uint16_t float64_to_uint16_scalbn(float64 a
, FloatRoundMode rmode
, int scale
,
2784 float64_unpack_canonical(&p
, a
, s
);
2785 return round_to_uint_and_pack(p
, rmode
, scale
, UINT16_MAX
, s
);
2788 uint32_t float64_to_uint32_scalbn(float64 a
, FloatRoundMode rmode
, int scale
,
2793 float64_unpack_canonical(&p
, a
, s
);
2794 return round_to_uint_and_pack(p
, rmode
, scale
, UINT32_MAX
, s
);
2797 uint64_t float64_to_uint64_scalbn(float64 a
, FloatRoundMode rmode
, int scale
,
2802 float64_unpack_canonical(&p
, a
, s
);
2803 return round_to_uint_and_pack(p
, rmode
, scale
, UINT64_MAX
, s
);
2806 uint8_t float16_to_uint8(float16 a
, float_status
*s
)
2808 return float16_to_uint8_scalbn(a
, s
->float_rounding_mode
, 0, s
);
2811 uint16_t float16_to_uint16(float16 a
, float_status
*s
)
2813 return float16_to_uint16_scalbn(a
, s
->float_rounding_mode
, 0, s
);
2816 uint32_t float16_to_uint32(float16 a
, float_status
*s
)
2818 return float16_to_uint32_scalbn(a
, s
->float_rounding_mode
, 0, s
);
2821 uint64_t float16_to_uint64(float16 a
, float_status
*s
)
2823 return float16_to_uint64_scalbn(a
, s
->float_rounding_mode
, 0, s
);
2826 uint16_t float32_to_uint16(float32 a
, float_status
*s
)
2828 return float32_to_uint16_scalbn(a
, s
->float_rounding_mode
, 0, s
);
2831 uint32_t float32_to_uint32(float32 a
, float_status
*s
)
2833 return float32_to_uint32_scalbn(a
, s
->float_rounding_mode
, 0, s
);
2836 uint64_t float32_to_uint64(float32 a
, float_status
*s
)
2838 return float32_to_uint64_scalbn(a
, s
->float_rounding_mode
, 0, s
);
2841 uint16_t float64_to_uint16(float64 a
, float_status
*s
)
2843 return float64_to_uint16_scalbn(a
, s
->float_rounding_mode
, 0, s
);
2846 uint32_t float64_to_uint32(float64 a
, float_status
*s
)
2848 return float64_to_uint32_scalbn(a
, s
->float_rounding_mode
, 0, s
);
2851 uint64_t float64_to_uint64(float64 a
, float_status
*s
)
2853 return float64_to_uint64_scalbn(a
, s
->float_rounding_mode
, 0, s
);
2856 uint16_t float16_to_uint16_round_to_zero(float16 a
, float_status
*s
)
2858 return float16_to_uint16_scalbn(a
, float_round_to_zero
, 0, s
);
2861 uint32_t float16_to_uint32_round_to_zero(float16 a
, float_status
*s
)
2863 return float16_to_uint32_scalbn(a
, float_round_to_zero
, 0, s
);
2866 uint64_t float16_to_uint64_round_to_zero(float16 a
, float_status
*s
)
2868 return float16_to_uint64_scalbn(a
, float_round_to_zero
, 0, s
);
2871 uint16_t float32_to_uint16_round_to_zero(float32 a
, float_status
*s
)
2873 return float32_to_uint16_scalbn(a
, float_round_to_zero
, 0, s
);
2876 uint32_t float32_to_uint32_round_to_zero(float32 a
, float_status
*s
)
2878 return float32_to_uint32_scalbn(a
, float_round_to_zero
, 0, s
);
2881 uint64_t float32_to_uint64_round_to_zero(float32 a
, float_status
*s
)
2883 return float32_to_uint64_scalbn(a
, float_round_to_zero
, 0, s
);
2886 uint16_t float64_to_uint16_round_to_zero(float64 a
, float_status
*s
)
2888 return float64_to_uint16_scalbn(a
, float_round_to_zero
, 0, s
);
2891 uint32_t float64_to_uint32_round_to_zero(float64 a
, float_status
*s
)
2893 return float64_to_uint32_scalbn(a
, float_round_to_zero
, 0, s
);
2896 uint64_t float64_to_uint64_round_to_zero(float64 a
, float_status
*s
)
2898 return float64_to_uint64_scalbn(a
, float_round_to_zero
, 0, s
);
2902 * Returns the result of converting the bfloat16 value `a' to
2903 * the unsigned integer format.
2906 uint16_t bfloat16_to_uint16_scalbn(bfloat16 a
, FloatRoundMode rmode
,
2907 int scale
, float_status
*s
)
2911 bfloat16_unpack_canonical(&p
, a
, s
);
2912 return round_to_uint_and_pack(p
, rmode
, scale
, UINT16_MAX
, s
);
2915 uint32_t bfloat16_to_uint32_scalbn(bfloat16 a
, FloatRoundMode rmode
,
2916 int scale
, float_status
*s
)
2920 bfloat16_unpack_canonical(&p
, a
, s
);
2921 return round_to_uint_and_pack(p
, rmode
, scale
, UINT32_MAX
, s
);
2924 uint64_t bfloat16_to_uint64_scalbn(bfloat16 a
, FloatRoundMode rmode
,
2925 int scale
, float_status
*s
)
2929 bfloat16_unpack_canonical(&p
, a
, s
);
2930 return round_to_uint_and_pack(p
, rmode
, scale
, UINT64_MAX
, s
);
2933 uint16_t bfloat16_to_uint16(bfloat16 a
, float_status
*s
)
2935 return bfloat16_to_uint16_scalbn(a
, s
->float_rounding_mode
, 0, s
);
2938 uint32_t bfloat16_to_uint32(bfloat16 a
, float_status
*s
)
2940 return bfloat16_to_uint32_scalbn(a
, s
->float_rounding_mode
, 0, s
);
2943 uint64_t bfloat16_to_uint64(bfloat16 a
, float_status
*s
)
2945 return bfloat16_to_uint64_scalbn(a
, s
->float_rounding_mode
, 0, s
);
2948 uint16_t bfloat16_to_uint16_round_to_zero(bfloat16 a
, float_status
*s
)
2950 return bfloat16_to_uint16_scalbn(a
, float_round_to_zero
, 0, s
);
2953 uint32_t bfloat16_to_uint32_round_to_zero(bfloat16 a
, float_status
*s
)
2955 return bfloat16_to_uint32_scalbn(a
, float_round_to_zero
, 0, s
);
2958 uint64_t bfloat16_to_uint64_round_to_zero(bfloat16 a
, float_status
*s
)
2960 return bfloat16_to_uint64_scalbn(a
, float_round_to_zero
, 0, s
);
2964 * Integer to float conversions
2966 * Returns the result of converting the two's complement integer `a'
2967 * to the floating-point format. The conversion is performed according
2968 * to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
2971 static FloatParts64
int_to_float(int64_t a
, int scale
, float_status
*status
)
2973 FloatParts64 r
= { .sign
= false };
2976 r
.cls
= float_class_zero
;
2981 r
.cls
= float_class_normal
;
2987 scale
= MIN(MAX(scale
, -0x10000), 0x10000);
2989 r
.exp
= DECOMPOSED_BINARY_POINT
- shift
+ scale
;
2990 r
.frac
= f
<< shift
;
2996 float16
int64_to_float16_scalbn(int64_t a
, int scale
, float_status
*status
)
2998 FloatParts64 pa
= int_to_float(a
, scale
, status
);
2999 return float16_round_pack_canonical(&pa
, status
);
3002 float16
int32_to_float16_scalbn(int32_t a
, int scale
, float_status
*status
)
3004 return int64_to_float16_scalbn(a
, scale
, status
);
3007 float16
int16_to_float16_scalbn(int16_t a
, int scale
, float_status
*status
)
3009 return int64_to_float16_scalbn(a
, scale
, status
);
3012 float16
int64_to_float16(int64_t a
, float_status
*status
)
3014 return int64_to_float16_scalbn(a
, 0, status
);
3017 float16
int32_to_float16(int32_t a
, float_status
*status
)
3019 return int64_to_float16_scalbn(a
, 0, status
);
3022 float16
int16_to_float16(int16_t a
, float_status
*status
)
3024 return int64_to_float16_scalbn(a
, 0, status
);
3027 float16
int8_to_float16(int8_t a
, float_status
*status
)
3029 return int64_to_float16_scalbn(a
, 0, status
);
3032 float32
int64_to_float32_scalbn(int64_t a
, int scale
, float_status
*status
)
3034 FloatParts64 pa
= int_to_float(a
, scale
, status
);
3035 return float32_round_pack_canonical(&pa
, status
);
3038 float32
int32_to_float32_scalbn(int32_t a
, int scale
, float_status
*status
)
3040 return int64_to_float32_scalbn(a
, scale
, status
);
3043 float32
int16_to_float32_scalbn(int16_t a
, int scale
, float_status
*status
)
3045 return int64_to_float32_scalbn(a
, scale
, status
);
3048 float32
int64_to_float32(int64_t a
, float_status
*status
)
3050 return int64_to_float32_scalbn(a
, 0, status
);
3053 float32
int32_to_float32(int32_t a
, float_status
*status
)
3055 return int64_to_float32_scalbn(a
, 0, status
);
3058 float32
int16_to_float32(int16_t a
, float_status
*status
)
3060 return int64_to_float32_scalbn(a
, 0, status
);
3063 float64
int64_to_float64_scalbn(int64_t a
, int scale
, float_status
*status
)
3065 FloatParts64 pa
= int_to_float(a
, scale
, status
);
3066 return float64_round_pack_canonical(&pa
, status
);
3069 float64
int32_to_float64_scalbn(int32_t a
, int scale
, float_status
*status
)
3071 return int64_to_float64_scalbn(a
, scale
, status
);
3074 float64
int16_to_float64_scalbn(int16_t a
, int scale
, float_status
*status
)
3076 return int64_to_float64_scalbn(a
, scale
, status
);
3079 float64
int64_to_float64(int64_t a
, float_status
*status
)
3081 return int64_to_float64_scalbn(a
, 0, status
);
3084 float64
int32_to_float64(int32_t a
, float_status
*status
)
3086 return int64_to_float64_scalbn(a
, 0, status
);
3089 float64
int16_to_float64(int16_t a
, float_status
*status
)
3091 return int64_to_float64_scalbn(a
, 0, status
);
3095 * Returns the result of converting the two's complement integer `a'
3096 * to the bfloat16 format.
3099 bfloat16
int64_to_bfloat16_scalbn(int64_t a
, int scale
, float_status
*status
)
3101 FloatParts64 pa
= int_to_float(a
, scale
, status
);
3102 return bfloat16_round_pack_canonical(&pa
, status
);
3105 bfloat16
int32_to_bfloat16_scalbn(int32_t a
, int scale
, float_status
*status
)
3107 return int64_to_bfloat16_scalbn(a
, scale
, status
);
3110 bfloat16
int16_to_bfloat16_scalbn(int16_t a
, int scale
, float_status
*status
)
3112 return int64_to_bfloat16_scalbn(a
, scale
, status
);
3115 bfloat16
int64_to_bfloat16(int64_t a
, float_status
*status
)
3117 return int64_to_bfloat16_scalbn(a
, 0, status
);
3120 bfloat16
int32_to_bfloat16(int32_t a
, float_status
*status
)
3122 return int64_to_bfloat16_scalbn(a
, 0, status
);
3125 bfloat16
int16_to_bfloat16(int16_t a
, float_status
*status
)
3127 return int64_to_bfloat16_scalbn(a
, 0, status
);
3131 * Unsigned Integer to float conversions
3133 * Returns the result of converting the unsigned integer `a' to the
3134 * floating-point format. The conversion is performed according to the
3135 * IEC/IEEE Standard for Binary Floating-Point Arithmetic.
3138 static FloatParts64
uint_to_float(uint64_t a
, int scale
, float_status
*status
)
3140 FloatParts64 r
= { .sign
= false };
3144 r
.cls
= float_class_zero
;
3146 scale
= MIN(MAX(scale
, -0x10000), 0x10000);
3148 r
.cls
= float_class_normal
;
3149 r
.exp
= DECOMPOSED_BINARY_POINT
- shift
+ scale
;
3150 r
.frac
= a
<< shift
;
3156 float16
uint64_to_float16_scalbn(uint64_t a
, int scale
, float_status
*status
)
3158 FloatParts64 pa
= uint_to_float(a
, scale
, status
);
3159 return float16_round_pack_canonical(&pa
, status
);
3162 float16
uint32_to_float16_scalbn(uint32_t a
, int scale
, float_status
*status
)
3164 return uint64_to_float16_scalbn(a
, scale
, status
);
3167 float16
uint16_to_float16_scalbn(uint16_t a
, int scale
, float_status
*status
)
3169 return uint64_to_float16_scalbn(a
, scale
, status
);
3172 float16
uint64_to_float16(uint64_t a
, float_status
*status
)
3174 return uint64_to_float16_scalbn(a
, 0, status
);
3177 float16
uint32_to_float16(uint32_t a
, float_status
*status
)
3179 return uint64_to_float16_scalbn(a
, 0, status
);
3182 float16
uint16_to_float16(uint16_t a
, float_status
*status
)
3184 return uint64_to_float16_scalbn(a
, 0, status
);
3187 float16
uint8_to_float16(uint8_t a
, float_status
*status
)
3189 return uint64_to_float16_scalbn(a
, 0, status
);
3192 float32
uint64_to_float32_scalbn(uint64_t a
, int scale
, float_status
*status
)
3194 FloatParts64 pa
= uint_to_float(a
, scale
, status
);
3195 return float32_round_pack_canonical(&pa
, status
);
3198 float32
uint32_to_float32_scalbn(uint32_t a
, int scale
, float_status
*status
)
3200 return uint64_to_float32_scalbn(a
, scale
, status
);
3203 float32
uint16_to_float32_scalbn(uint16_t a
, int scale
, float_status
*status
)
3205 return uint64_to_float32_scalbn(a
, scale
, status
);
3208 float32
uint64_to_float32(uint64_t a
, float_status
*status
)
3210 return uint64_to_float32_scalbn(a
, 0, status
);
3213 float32
uint32_to_float32(uint32_t a
, float_status
*status
)
3215 return uint64_to_float32_scalbn(a
, 0, status
);
3218 float32
uint16_to_float32(uint16_t a
, float_status
*status
)
3220 return uint64_to_float32_scalbn(a
, 0, status
);
3223 float64
uint64_to_float64_scalbn(uint64_t a
, int scale
, float_status
*status
)
3225 FloatParts64 pa
= uint_to_float(a
, scale
, status
);
3226 return float64_round_pack_canonical(&pa
, status
);
3229 float64
uint32_to_float64_scalbn(uint32_t a
, int scale
, float_status
*status
)
3231 return uint64_to_float64_scalbn(a
, scale
, status
);
3234 float64
uint16_to_float64_scalbn(uint16_t a
, int scale
, float_status
*status
)
3236 return uint64_to_float64_scalbn(a
, scale
, status
);
3239 float64
uint64_to_float64(uint64_t a
, float_status
*status
)
3241 return uint64_to_float64_scalbn(a
, 0, status
);
3244 float64
uint32_to_float64(uint32_t a
, float_status
*status
)
3246 return uint64_to_float64_scalbn(a
, 0, status
);
3249 float64
uint16_to_float64(uint16_t a
, float_status
*status
)
3251 return uint64_to_float64_scalbn(a
, 0, status
);
3255 * Returns the result of converting the unsigned integer `a' to the
3259 bfloat16
uint64_to_bfloat16_scalbn(uint64_t a
, int scale
, float_status
*status
)
3261 FloatParts64 pa
= uint_to_float(a
, scale
, status
);
3262 return bfloat16_round_pack_canonical(&pa
, status
);
3265 bfloat16
uint32_to_bfloat16_scalbn(uint32_t a
, int scale
, float_status
*status
)
3267 return uint64_to_bfloat16_scalbn(a
, scale
, status
);
3270 bfloat16
uint16_to_bfloat16_scalbn(uint16_t a
, int scale
, float_status
*status
)
3272 return uint64_to_bfloat16_scalbn(a
, scale
, status
);
3275 bfloat16
uint64_to_bfloat16(uint64_t a
, float_status
*status
)
3277 return uint64_to_bfloat16_scalbn(a
, 0, status
);
3280 bfloat16
uint32_to_bfloat16(uint32_t a
, float_status
*status
)
3282 return uint64_to_bfloat16_scalbn(a
, 0, status
);
3285 bfloat16
uint16_to_bfloat16(uint16_t a
, float_status
*status
)
3287 return uint64_to_bfloat16_scalbn(a
, 0, status
);
3291 /* min() and max() functions. These can't be implemented as
3292 * 'compare and pick one input' because that would mishandle
3293 * NaNs and +0 vs -0.
3295 * minnum() and maxnum() functions. These are similar to the min()
3296 * and max() functions but if one of the arguments is a QNaN and
3297 * the other is numerical then the numerical argument is returned.
3298 * SNaNs will get quietened before being returned.
3299 * minnum() and maxnum correspond to the IEEE 754-2008 minNum()
3300 * and maxNum() operations. min() and max() are the typical min/max
3301 * semantics provided by many CPUs which predate that specification.
3303 * minnummag() and maxnummag() functions correspond to minNumMag()
3304 * and minNumMag() from the IEEE-754 2008.
3306 static FloatParts64
minmax_floats(FloatParts64 a
, FloatParts64 b
, bool ismin
,
3307 bool ieee
, bool ismag
, float_status
*s
)
3309 if (unlikely(is_nan(a
.cls
) || is_nan(b
.cls
))) {
3311 /* Takes two floating-point values `a' and `b', one of
3312 * which is a NaN, and returns the appropriate NaN
3313 * result. If either `a' or `b' is a signaling NaN,
3314 * the invalid exception is raised.
3316 if (is_snan(a
.cls
) || is_snan(b
.cls
)) {
3317 return *parts_pick_nan(&a
, &b
, s
);
3318 } else if (is_nan(a
.cls
) && !is_nan(b
.cls
)) {
3320 } else if (is_nan(b
.cls
) && !is_nan(a
.cls
)) {
3324 return *parts_pick_nan(&a
, &b
, s
);
3329 case float_class_normal
:
3332 case float_class_inf
:
3335 case float_class_zero
:
3339 g_assert_not_reached();
3343 case float_class_normal
:
3346 case float_class_inf
:
3349 case float_class_zero
:
3353 g_assert_not_reached();
3357 if (ismag
&& (a_exp
!= b_exp
|| a
.frac
!= b
.frac
)) {
3358 bool a_less
= a_exp
< b_exp
;
3359 if (a_exp
== b_exp
) {
3360 a_less
= a
.frac
< b
.frac
;
3362 return a_less
^ ismin
? b
: a
;
3365 if (a
.sign
== b
.sign
) {
3366 bool a_less
= a_exp
< b_exp
;
3367 if (a_exp
== b_exp
) {
3368 a_less
= a
.frac
< b
.frac
;
3370 return a
.sign
^ a_less
^ ismin
? b
: a
;
3372 return a
.sign
^ ismin
? b
: a
;
3377 #define MINMAX(sz, name, ismin, isiee, ismag) \
3378 float ## sz float ## sz ## _ ## name(float ## sz a, float ## sz b, \
3381 FloatParts64 pa, pb, pr; \
3382 float ## sz ## _unpack_canonical(&pa, a, s); \
3383 float ## sz ## _unpack_canonical(&pb, b, s); \
3384 pr = minmax_floats(pa, pb, ismin, isiee, ismag, s); \
3385 return float ## sz ## _round_pack_canonical(&pr, s); \
3388 MINMAX(16, min
, true, false, false)
3389 MINMAX(16, minnum
, true, true, false)
3390 MINMAX(16, minnummag
, true, true, true)
3391 MINMAX(16, max
, false, false, false)
3392 MINMAX(16, maxnum
, false, true, false)
3393 MINMAX(16, maxnummag
, false, true, true)
3395 MINMAX(32, min
, true, false, false)
3396 MINMAX(32, minnum
, true, true, false)
3397 MINMAX(32, minnummag
, true, true, true)
3398 MINMAX(32, max
, false, false, false)
3399 MINMAX(32, maxnum
, false, true, false)
3400 MINMAX(32, maxnummag
, false, true, true)
3402 MINMAX(64, min
, true, false, false)
3403 MINMAX(64, minnum
, true, true, false)
3404 MINMAX(64, minnummag
, true, true, true)
3405 MINMAX(64, max
, false, false, false)
3406 MINMAX(64, maxnum
, false, true, false)
3407 MINMAX(64, maxnummag
, false, true, true)
3411 #define BF16_MINMAX(name, ismin, isiee, ismag) \
3412 bfloat16 bfloat16_ ## name(bfloat16 a, bfloat16 b, float_status *s) \
3414 FloatParts64 pa, pb, pr; \
3415 bfloat16_unpack_canonical(&pa, a, s); \
3416 bfloat16_unpack_canonical(&pb, b, s); \
3417 pr = minmax_floats(pa, pb, ismin, isiee, ismag, s); \
3418 return bfloat16_round_pack_canonical(&pr, s); \
3421 BF16_MINMAX(min
, true, false, false)
3422 BF16_MINMAX(minnum
, true, true, false)
3423 BF16_MINMAX(minnummag
, true, true, true)
3424 BF16_MINMAX(max
, false, false, false)
3425 BF16_MINMAX(maxnum
, false, true, false)
3426 BF16_MINMAX(maxnummag
, false, true, true)
3430 /* Floating point compare */
3431 static FloatRelation
compare_floats(FloatParts64 a
, FloatParts64 b
, bool is_quiet
,
3434 if (is_nan(a
.cls
) || is_nan(b
.cls
)) {
3436 a
.cls
== float_class_snan
||
3437 b
.cls
== float_class_snan
) {
3438 float_raise(float_flag_invalid
, s
);
3440 return float_relation_unordered
;
3443 if (a
.cls
== float_class_zero
) {
3444 if (b
.cls
== float_class_zero
) {
3445 return float_relation_equal
;
3447 return b
.sign
? float_relation_greater
: float_relation_less
;
3448 } else if (b
.cls
== float_class_zero
) {
3449 return a
.sign
? float_relation_less
: float_relation_greater
;
3452 /* The only really important thing about infinity is its sign. If
3453 * both are infinities the sign marks the smallest of the two.
3455 if (a
.cls
== float_class_inf
) {
3456 if ((b
.cls
== float_class_inf
) && (a
.sign
== b
.sign
)) {
3457 return float_relation_equal
;
3459 return a
.sign
? float_relation_less
: float_relation_greater
;
3460 } else if (b
.cls
== float_class_inf
) {
3461 return b
.sign
? float_relation_greater
: float_relation_less
;
3464 if (a
.sign
!= b
.sign
) {
3465 return a
.sign
? float_relation_less
: float_relation_greater
;
3468 if (a
.exp
== b
.exp
) {
3469 if (a
.frac
== b
.frac
) {
3470 return float_relation_equal
;
3473 return a
.frac
> b
.frac
?
3474 float_relation_less
: float_relation_greater
;
3476 return a
.frac
> b
.frac
?
3477 float_relation_greater
: float_relation_less
;
3481 return a
.exp
> b
.exp
? float_relation_less
: float_relation_greater
;
3483 return a
.exp
> b
.exp
? float_relation_greater
: float_relation_less
;
3488 #define COMPARE(name, attr, sz) \
3490 name(float ## sz a, float ## sz b, bool is_quiet, float_status *s) \
3492 FloatParts64 pa, pb; \
3493 float ## sz ## _unpack_canonical(&pa, a, s); \
3494 float ## sz ## _unpack_canonical(&pb, b, s); \
3495 return compare_floats(pa, pb, is_quiet, s); \
3498 COMPARE(soft_f16_compare
, QEMU_FLATTEN
, 16)
3499 COMPARE(soft_f32_compare
, QEMU_SOFTFLOAT_ATTR
, 32)
3500 COMPARE(soft_f64_compare
, QEMU_SOFTFLOAT_ATTR
, 64)
3504 FloatRelation
float16_compare(float16 a
, float16 b
, float_status
*s
)
3506 return soft_f16_compare(a
, b
, false, s
);
3509 FloatRelation
float16_compare_quiet(float16 a
, float16 b
, float_status
*s
)
3511 return soft_f16_compare(a
, b
, true, s
);
3514 static FloatRelation QEMU_FLATTEN
3515 f32_compare(float32 xa
, float32 xb
, bool is_quiet
, float_status
*s
)
3517 union_float32 ua
, ub
;
3522 if (QEMU_NO_HARDFLOAT
) {
3526 float32_input_flush2(&ua
.s
, &ub
.s
, s
);
3527 if (isgreaterequal(ua
.h
, ub
.h
)) {
3528 if (isgreater(ua
.h
, ub
.h
)) {
3529 return float_relation_greater
;
3531 return float_relation_equal
;
3533 if (likely(isless(ua
.h
, ub
.h
))) {
3534 return float_relation_less
;
3536 /* The only condition remaining is unordered.
3537 * Fall through to set flags.
3540 return soft_f32_compare(ua
.s
, ub
.s
, is_quiet
, s
);
3543 FloatRelation
float32_compare(float32 a
, float32 b
, float_status
*s
)
3545 return f32_compare(a
, b
, false, s
);
3548 FloatRelation
float32_compare_quiet(float32 a
, float32 b
, float_status
*s
)
3550 return f32_compare(a
, b
, true, s
);
3553 static FloatRelation QEMU_FLATTEN
3554 f64_compare(float64 xa
, float64 xb
, bool is_quiet
, float_status
*s
)
3556 union_float64 ua
, ub
;
3561 if (QEMU_NO_HARDFLOAT
) {
3565 float64_input_flush2(&ua
.s
, &ub
.s
, s
);
3566 if (isgreaterequal(ua
.h
, ub
.h
)) {
3567 if (isgreater(ua
.h
, ub
.h
)) {
3568 return float_relation_greater
;
3570 return float_relation_equal
;
3572 if (likely(isless(ua
.h
, ub
.h
))) {
3573 return float_relation_less
;
3575 /* The only condition remaining is unordered.
3576 * Fall through to set flags.
3579 return soft_f64_compare(ua
.s
, ub
.s
, is_quiet
, s
);
3582 FloatRelation
float64_compare(float64 a
, float64 b
, float_status
*s
)
3584 return f64_compare(a
, b
, false, s
);
3587 FloatRelation
float64_compare_quiet(float64 a
, float64 b
, float_status
*s
)
3589 return f64_compare(a
, b
, true, s
);
3592 static FloatRelation QEMU_FLATTEN
3593 soft_bf16_compare(bfloat16 a
, bfloat16 b
, bool is_quiet
, float_status
*s
)
3595 FloatParts64 pa
, pb
;
3597 bfloat16_unpack_canonical(&pa
, a
, s
);
3598 bfloat16_unpack_canonical(&pb
, b
, s
);
3599 return compare_floats(pa
, pb
, is_quiet
, s
);
3602 FloatRelation
bfloat16_compare(bfloat16 a
, bfloat16 b
, float_status
*s
)
3604 return soft_bf16_compare(a
, b
, false, s
);
3607 FloatRelation
bfloat16_compare_quiet(bfloat16 a
, bfloat16 b
, float_status
*s
)
3609 return soft_bf16_compare(a
, b
, true, s
);
3612 /* Multiply A by 2 raised to the power N. */
3613 static FloatParts64
scalbn_decomposed(FloatParts64 a
, int n
, float_status
*s
)
3615 if (unlikely(is_nan(a
.cls
))) {
3616 parts_return_nan(&a
, s
);
3618 if (a
.cls
== float_class_normal
) {
3619 /* The largest float type (even though not supported by FloatParts64)
3620 * is float128, which has a 15 bit exponent. Bounding N to 16 bits
3621 * still allows rounding to infinity, without allowing overflow
3622 * within the int32_t that backs FloatParts64.exp.
3624 n
= MIN(MAX(n
, -0x10000), 0x10000);
3630 float16
float16_scalbn(float16 a
, int n
, float_status
*status
)
3632 FloatParts64 pa
, pr
;
3634 float16_unpack_canonical(&pa
, a
, status
);
3635 pr
= scalbn_decomposed(pa
, n
, status
);
3636 return float16_round_pack_canonical(&pr
, status
);
3639 float32
float32_scalbn(float32 a
, int n
, float_status
*status
)
3641 FloatParts64 pa
, pr
;
3643 float32_unpack_canonical(&pa
, a
, status
);
3644 pr
= scalbn_decomposed(pa
, n
, status
);
3645 return float32_round_pack_canonical(&pr
, status
);
3648 float64
float64_scalbn(float64 a
, int n
, float_status
*status
)
3650 FloatParts64 pa
, pr
;
3652 float64_unpack_canonical(&pa
, a
, status
);
3653 pr
= scalbn_decomposed(pa
, n
, status
);
3654 return float64_round_pack_canonical(&pr
, status
);
3657 bfloat16
bfloat16_scalbn(bfloat16 a
, int n
, float_status
*status
)
3659 FloatParts64 pa
, pr
;
3661 bfloat16_unpack_canonical(&pa
, a
, status
);
3662 pr
= scalbn_decomposed(pa
, n
, status
);
3663 return bfloat16_round_pack_canonical(&pr
, status
);
3669 * The old softfloat code did an approximation step before zeroing in
3670 * on the final result. However for simpleness we just compute the
3671 * square root by iterating down from the implicit bit to enough extra
3672 * bits to ensure we get a correctly rounded result.
3674 * This does mean however the calculation is slower than before,
3675 * especially for 64 bit floats.
3678 static FloatParts64
sqrt_float(FloatParts64 a
, float_status
*s
, const FloatFmt
*p
)
3680 uint64_t a_frac
, r_frac
, s_frac
;
3683 if (is_nan(a
.cls
)) {
3684 parts_return_nan(&a
, s
);
3687 if (a
.cls
== float_class_zero
) {
3688 return a
; /* sqrt(+-0) = +-0 */
3691 float_raise(float_flag_invalid
, s
);
3692 parts_default_nan(&a
, s
);
3695 if (a
.cls
== float_class_inf
) {
3696 return a
; /* sqrt(+inf) = +inf */
3699 assert(a
.cls
== float_class_normal
);
3701 /* We need two overflow bits at the top. Adding room for that is a
3702 * right shift. If the exponent is odd, we can discard the low bit
3703 * by multiplying the fraction by 2; that's a left shift. Combine
3704 * those and we shift right by 1 if the exponent is odd, otherwise 2.
3706 a_frac
= a
.frac
>> (2 - (a
.exp
& 1));
3709 /* Bit-by-bit computation of sqrt. */
3713 /* Iterate from implicit bit down to the 3 extra bits to compute a
3714 * properly rounded result. Remember we've inserted two more bits
3715 * at the top, so these positions are two less.
3717 bit
= DECOMPOSED_BINARY_POINT
- 2;
3718 last_bit
= MAX(p
->frac_shift
- 4, 0);
3720 uint64_t q
= 1ULL << bit
;
3721 uint64_t t_frac
= s_frac
+ q
;
3722 if (t_frac
<= a_frac
) {
3723 s_frac
= t_frac
+ q
;
3728 } while (--bit
>= last_bit
);
3730 /* Undo the right shift done above. If there is any remaining
3731 * fraction, the result is inexact. Set the sticky bit.
3733 a
.frac
= (r_frac
<< 2) + (a_frac
!= 0);
3738 float16 QEMU_FLATTEN
float16_sqrt(float16 a
, float_status
*status
)
3740 FloatParts64 pa
, pr
;
3742 float16_unpack_canonical(&pa
, a
, status
);
3743 pr
= sqrt_float(pa
, status
, &float16_params
);
3744 return float16_round_pack_canonical(&pr
, status
);
3747 static float32 QEMU_SOFTFLOAT_ATTR
3748 soft_f32_sqrt(float32 a
, float_status
*status
)
3750 FloatParts64 pa
, pr
;
3752 float32_unpack_canonical(&pa
, a
, status
);
3753 pr
= sqrt_float(pa
, status
, &float32_params
);
3754 return float32_round_pack_canonical(&pr
, status
);
3757 static float64 QEMU_SOFTFLOAT_ATTR
3758 soft_f64_sqrt(float64 a
, float_status
*status
)
3760 FloatParts64 pa
, pr
;
3762 float64_unpack_canonical(&pa
, a
, status
);
3763 pr
= sqrt_float(pa
, status
, &float64_params
);
3764 return float64_round_pack_canonical(&pr
, status
);
3767 float32 QEMU_FLATTEN
float32_sqrt(float32 xa
, float_status
*s
)
3769 union_float32 ua
, ur
;
3772 if (unlikely(!can_use_fpu(s
))) {
3776 float32_input_flush1(&ua
.s
, s
);
3777 if (QEMU_HARDFLOAT_1F32_USE_FP
) {
3778 if (unlikely(!(fpclassify(ua
.h
) == FP_NORMAL
||
3779 fpclassify(ua
.h
) == FP_ZERO
) ||
3783 } else if (unlikely(!float32_is_zero_or_normal(ua
.s
) ||
3784 float32_is_neg(ua
.s
))) {
3791 return soft_f32_sqrt(ua
.s
, s
);
3794 float64 QEMU_FLATTEN
float64_sqrt(float64 xa
, float_status
*s
)
3796 union_float64 ua
, ur
;
3799 if (unlikely(!can_use_fpu(s
))) {
3803 float64_input_flush1(&ua
.s
, s
);
3804 if (QEMU_HARDFLOAT_1F64_USE_FP
) {
3805 if (unlikely(!(fpclassify(ua
.h
) == FP_NORMAL
||
3806 fpclassify(ua
.h
) == FP_ZERO
) ||
3810 } else if (unlikely(!float64_is_zero_or_normal(ua
.s
) ||
3811 float64_is_neg(ua
.s
))) {
3818 return soft_f64_sqrt(ua
.s
, s
);
3821 bfloat16 QEMU_FLATTEN
bfloat16_sqrt(bfloat16 a
, float_status
*status
)
3823 FloatParts64 pa
, pr
;
3825 bfloat16_unpack_canonical(&pa
, a
, status
);
3826 pr
= sqrt_float(pa
, status
, &bfloat16_params
);
3827 return bfloat16_round_pack_canonical(&pr
, status
);
3830 /*----------------------------------------------------------------------------
3831 | The pattern for a default generated NaN.
3832 *----------------------------------------------------------------------------*/
3834 float16
float16_default_nan(float_status
*status
)
3838 parts_default_nan(&p
, status
);
3839 p
.frac
>>= float16_params
.frac_shift
;
3840 return float16_pack_raw(&p
);
3843 float32
float32_default_nan(float_status
*status
)
3847 parts_default_nan(&p
, status
);
3848 p
.frac
>>= float32_params
.frac_shift
;
3849 return float32_pack_raw(&p
);
3852 float64
float64_default_nan(float_status
*status
)
3856 parts_default_nan(&p
, status
);
3857 p
.frac
>>= float64_params
.frac_shift
;
3858 return float64_pack_raw(&p
);
3861 float128
float128_default_nan(float_status
*status
)
3865 parts_default_nan(&p
, status
);
3866 frac_shr(&p
, float128_params
.frac_shift
);
3867 return float128_pack_raw(&p
);
3870 bfloat16
bfloat16_default_nan(float_status
*status
)
3874 parts_default_nan(&p
, status
);
3875 p
.frac
>>= bfloat16_params
.frac_shift
;
3876 return bfloat16_pack_raw(&p
);
3879 /*----------------------------------------------------------------------------
3880 | Returns a quiet NaN from a signalling NaN for the floating point value `a'.
3881 *----------------------------------------------------------------------------*/
3883 float16
float16_silence_nan(float16 a
, float_status
*status
)
3887 float16_unpack_raw(&p
, a
);
3888 p
.frac
<<= float16_params
.frac_shift
;
3889 parts_silence_nan(&p
, status
);
3890 p
.frac
>>= float16_params
.frac_shift
;
3891 return float16_pack_raw(&p
);
3894 float32
float32_silence_nan(float32 a
, float_status
*status
)
3898 float32_unpack_raw(&p
, a
);
3899 p
.frac
<<= float32_params
.frac_shift
;
3900 parts_silence_nan(&p
, status
);
3901 p
.frac
>>= float32_params
.frac_shift
;
3902 return float32_pack_raw(&p
);
3905 float64
float64_silence_nan(float64 a
, float_status
*status
)
3909 float64_unpack_raw(&p
, a
);
3910 p
.frac
<<= float64_params
.frac_shift
;
3911 parts_silence_nan(&p
, status
);
3912 p
.frac
>>= float64_params
.frac_shift
;
3913 return float64_pack_raw(&p
);
3916 bfloat16
bfloat16_silence_nan(bfloat16 a
, float_status
*status
)
3920 bfloat16_unpack_raw(&p
, a
);
3921 p
.frac
<<= bfloat16_params
.frac_shift
;
3922 parts_silence_nan(&p
, status
);
3923 p
.frac
>>= bfloat16_params
.frac_shift
;
3924 return bfloat16_pack_raw(&p
);
3927 float128
float128_silence_nan(float128 a
, float_status
*status
)
3931 float128_unpack_raw(&p
, a
);
3932 frac_shl(&p
, float128_params
.frac_shift
);
3933 parts_silence_nan(&p
, status
);
3934 frac_shr(&p
, float128_params
.frac_shift
);
3935 return float128_pack_raw(&p
);
3938 /*----------------------------------------------------------------------------
3939 | If `a' is denormal and we are in flush-to-zero mode then set the
3940 | input-denormal exception and return zero. Otherwise just return the value.
3941 *----------------------------------------------------------------------------*/
3943 static bool parts_squash_denormal(FloatParts64 p
, float_status
*status
)
3945 if (p
.exp
== 0 && p
.frac
!= 0) {
3946 float_raise(float_flag_input_denormal
, status
);
3953 float16
float16_squash_input_denormal(float16 a
, float_status
*status
)
3955 if (status
->flush_inputs_to_zero
) {
3958 float16_unpack_raw(&p
, a
);
3959 if (parts_squash_denormal(p
, status
)) {
3960 return float16_set_sign(float16_zero
, p
.sign
);
3966 float32
float32_squash_input_denormal(float32 a
, float_status
*status
)
3968 if (status
->flush_inputs_to_zero
) {
3971 float32_unpack_raw(&p
, a
);
3972 if (parts_squash_denormal(p
, status
)) {
3973 return float32_set_sign(float32_zero
, p
.sign
);
3979 float64
float64_squash_input_denormal(float64 a
, float_status
*status
)
3981 if (status
->flush_inputs_to_zero
) {
3984 float64_unpack_raw(&p
, a
);
3985 if (parts_squash_denormal(p
, status
)) {
3986 return float64_set_sign(float64_zero
, p
.sign
);
3992 bfloat16
bfloat16_squash_input_denormal(bfloat16 a
, float_status
*status
)
3994 if (status
->flush_inputs_to_zero
) {
3997 bfloat16_unpack_raw(&p
, a
);
3998 if (parts_squash_denormal(p
, status
)) {
3999 return bfloat16_set_sign(bfloat16_zero
, p
.sign
);
4005 /*----------------------------------------------------------------------------
4006 | Takes a 64-bit fixed-point value `absZ' with binary point between bits 6
4007 | and 7, and returns the properly rounded 32-bit integer corresponding to the
4008 | input. If `zSign' is 1, the input is negated before being converted to an
4009 | integer. Bit 63 of `absZ' must be zero. Ordinarily, the fixed-point input
4010 | is simply rounded to an integer, with the inexact exception raised if the
4011 | input cannot be represented exactly as an integer. However, if the fixed-
4012 | point input is too large, the invalid exception is raised and the largest
4013 | positive or negative integer is returned.
4014 *----------------------------------------------------------------------------*/
4016 static int32_t roundAndPackInt32(bool zSign
, uint64_t absZ
,
4017 float_status
*status
)
4019 int8_t roundingMode
;
4020 bool roundNearestEven
;
4021 int8_t roundIncrement
, roundBits
;
4024 roundingMode
= status
->float_rounding_mode
;
4025 roundNearestEven
= ( roundingMode
== float_round_nearest_even
);
4026 switch (roundingMode
) {
4027 case float_round_nearest_even
:
4028 case float_round_ties_away
:
4029 roundIncrement
= 0x40;
4031 case float_round_to_zero
:
4034 case float_round_up
:
4035 roundIncrement
= zSign
? 0 : 0x7f;
4037 case float_round_down
:
4038 roundIncrement
= zSign
? 0x7f : 0;
4040 case float_round_to_odd
:
4041 roundIncrement
= absZ
& 0x80 ? 0 : 0x7f;
4046 roundBits
= absZ
& 0x7F;
4047 absZ
= ( absZ
+ roundIncrement
)>>7;
4048 if (!(roundBits
^ 0x40) && roundNearestEven
) {
4052 if ( zSign
) z
= - z
;
4053 if ( ( absZ
>>32 ) || ( z
&& ( ( z
< 0 ) ^ zSign
) ) ) {
4054 float_raise(float_flag_invalid
, status
);
4055 return zSign
? INT32_MIN
: INT32_MAX
;
4058 float_raise(float_flag_inexact
, status
);
4064 /*----------------------------------------------------------------------------
4065 | Takes the 128-bit fixed-point value formed by concatenating `absZ0' and
4066 | `absZ1', with binary point between bits 63 and 64 (between the input words),
4067 | and returns the properly rounded 64-bit integer corresponding to the input.
4068 | If `zSign' is 1, the input is negated before being converted to an integer.
4069 | Ordinarily, the fixed-point input is simply rounded to an integer, with
4070 | the inexact exception raised if the input cannot be represented exactly as
4071 | an integer. However, if the fixed-point input is too large, the invalid
4072 | exception is raised and the largest positive or negative integer is
4074 *----------------------------------------------------------------------------*/
4076 static int64_t roundAndPackInt64(bool zSign
, uint64_t absZ0
, uint64_t absZ1
,
4077 float_status
*status
)
4079 int8_t roundingMode
;
4080 bool roundNearestEven
, increment
;
4083 roundingMode
= status
->float_rounding_mode
;
4084 roundNearestEven
= ( roundingMode
== float_round_nearest_even
);
4085 switch (roundingMode
) {
4086 case float_round_nearest_even
:
4087 case float_round_ties_away
:
4088 increment
= ((int64_t) absZ1
< 0);
4090 case float_round_to_zero
:
4093 case float_round_up
:
4094 increment
= !zSign
&& absZ1
;
4096 case float_round_down
:
4097 increment
= zSign
&& absZ1
;
4099 case float_round_to_odd
:
4100 increment
= !(absZ0
& 1) && absZ1
;
4107 if ( absZ0
== 0 ) goto overflow
;
4108 if (!(absZ1
<< 1) && roundNearestEven
) {
4113 if ( zSign
) z
= - z
;
4114 if ( z
&& ( ( z
< 0 ) ^ zSign
) ) {
4116 float_raise(float_flag_invalid
, status
);
4117 return zSign
? INT64_MIN
: INT64_MAX
;
4120 float_raise(float_flag_inexact
, status
);
4126 /*----------------------------------------------------------------------------
4127 | Takes the 128-bit fixed-point value formed by concatenating `absZ0' and
4128 | `absZ1', with binary point between bits 63 and 64 (between the input words),
4129 | and returns the properly rounded 64-bit unsigned integer corresponding to the
4130 | input. Ordinarily, the fixed-point input is simply rounded to an integer,
4131 | with the inexact exception raised if the input cannot be represented exactly
4132 | as an integer. However, if the fixed-point input is too large, the invalid
4133 | exception is raised and the largest unsigned integer is returned.
4134 *----------------------------------------------------------------------------*/
4136 static int64_t roundAndPackUint64(bool zSign
, uint64_t absZ0
,
4137 uint64_t absZ1
, float_status
*status
)
4139 int8_t roundingMode
;
4140 bool roundNearestEven
, increment
;
4142 roundingMode
= status
->float_rounding_mode
;
4143 roundNearestEven
= (roundingMode
== float_round_nearest_even
);
4144 switch (roundingMode
) {
4145 case float_round_nearest_even
:
4146 case float_round_ties_away
:
4147 increment
= ((int64_t)absZ1
< 0);
4149 case float_round_to_zero
:
4152 case float_round_up
:
4153 increment
= !zSign
&& absZ1
;
4155 case float_round_down
:
4156 increment
= zSign
&& absZ1
;
4158 case float_round_to_odd
:
4159 increment
= !(absZ0
& 1) && absZ1
;
4167 float_raise(float_flag_invalid
, status
);
4170 if (!(absZ1
<< 1) && roundNearestEven
) {
4175 if (zSign
&& absZ0
) {
4176 float_raise(float_flag_invalid
, status
);
4181 float_raise(float_flag_inexact
, status
);
4186 /*----------------------------------------------------------------------------
4187 | Normalizes the subnormal single-precision floating-point value represented
4188 | by the denormalized significand `aSig'. The normalized exponent and
4189 | significand are stored at the locations pointed to by `zExpPtr' and
4190 | `zSigPtr', respectively.
4191 *----------------------------------------------------------------------------*/
4194 normalizeFloat32Subnormal(uint32_t aSig
, int *zExpPtr
, uint32_t *zSigPtr
)
4198 shiftCount
= clz32(aSig
) - 8;
4199 *zSigPtr
= aSig
<<shiftCount
;
4200 *zExpPtr
= 1 - shiftCount
;
4204 /*----------------------------------------------------------------------------
4205 | Takes an abstract floating-point value having sign `zSign', exponent `zExp',
4206 | and significand `zSig', and returns the proper single-precision floating-
4207 | point value corresponding to the abstract input. Ordinarily, the abstract
4208 | value is simply rounded and packed into the single-precision format, with
4209 | the inexact exception raised if the abstract input cannot be represented
4210 | exactly. However, if the abstract value is too large, the overflow and
4211 | inexact exceptions are raised and an infinity or maximal finite value is
4212 | returned. If the abstract value is too small, the input value is rounded to
4213 | a subnormal number, and the underflow and inexact exceptions are raised if
4214 | the abstract input cannot be represented exactly as a subnormal single-
4215 | precision floating-point number.
4216 | The input significand `zSig' has its binary point between bits 30
4217 | and 29, which is 7 bits to the left of the usual location. This shifted
4218 | significand must be normalized or smaller. If `zSig' is not normalized,
4219 | `zExp' must be 0; in that case, the result returned is a subnormal number,
4220 | and it must not require rounding. In the usual case that `zSig' is
4221 | normalized, `zExp' must be 1 less than the ``true'' floating-point exponent.
4222 | The handling of underflow and overflow follows the IEC/IEEE Standard for
4223 | Binary Floating-Point Arithmetic.
4224 *----------------------------------------------------------------------------*/
4226 static float32
roundAndPackFloat32(bool zSign
, int zExp
, uint32_t zSig
,
4227 float_status
*status
)
4229 int8_t roundingMode
;
4230 bool roundNearestEven
;
4231 int8_t roundIncrement
, roundBits
;
4234 roundingMode
= status
->float_rounding_mode
;
4235 roundNearestEven
= ( roundingMode
== float_round_nearest_even
);
4236 switch (roundingMode
) {
4237 case float_round_nearest_even
:
4238 case float_round_ties_away
:
4239 roundIncrement
= 0x40;
4241 case float_round_to_zero
:
4244 case float_round_up
:
4245 roundIncrement
= zSign
? 0 : 0x7f;
4247 case float_round_down
:
4248 roundIncrement
= zSign
? 0x7f : 0;
4250 case float_round_to_odd
:
4251 roundIncrement
= zSig
& 0x80 ? 0 : 0x7f;
4257 roundBits
= zSig
& 0x7F;
4258 if ( 0xFD <= (uint16_t) zExp
) {
4259 if ( ( 0xFD < zExp
)
4260 || ( ( zExp
== 0xFD )
4261 && ( (int32_t) ( zSig
+ roundIncrement
) < 0 ) )
4263 bool overflow_to_inf
= roundingMode
!= float_round_to_odd
&&
4264 roundIncrement
!= 0;
4265 float_raise(float_flag_overflow
| float_flag_inexact
, status
);
4266 return packFloat32(zSign
, 0xFF, -!overflow_to_inf
);
4269 if (status
->flush_to_zero
) {
4270 float_raise(float_flag_output_denormal
, status
);
4271 return packFloat32(zSign
, 0, 0);
4273 isTiny
= status
->tininess_before_rounding
4275 || (zSig
+ roundIncrement
< 0x80000000);
4276 shift32RightJamming( zSig
, - zExp
, &zSig
);
4278 roundBits
= zSig
& 0x7F;
4279 if (isTiny
&& roundBits
) {
4280 float_raise(float_flag_underflow
, status
);
4282 if (roundingMode
== float_round_to_odd
) {
4284 * For round-to-odd case, the roundIncrement depends on
4285 * zSig which just changed.
4287 roundIncrement
= zSig
& 0x80 ? 0 : 0x7f;
4292 float_raise(float_flag_inexact
, status
);
4294 zSig
= ( zSig
+ roundIncrement
)>>7;
4295 if (!(roundBits
^ 0x40) && roundNearestEven
) {
4298 if ( zSig
== 0 ) zExp
= 0;
4299 return packFloat32( zSign
, zExp
, zSig
);
4303 /*----------------------------------------------------------------------------
4304 | Takes an abstract floating-point value having sign `zSign', exponent `zExp',
4305 | and significand `zSig', and returns the proper single-precision floating-
4306 | point value corresponding to the abstract input. This routine is just like
4307 | `roundAndPackFloat32' except that `zSig' does not have to be normalized.
4308 | Bit 31 of `zSig' must be zero, and `zExp' must be 1 less than the ``true''
4309 | floating-point exponent.
4310 *----------------------------------------------------------------------------*/
4313 normalizeRoundAndPackFloat32(bool zSign
, int zExp
, uint32_t zSig
,
4314 float_status
*status
)
4318 shiftCount
= clz32(zSig
) - 1;
4319 return roundAndPackFloat32(zSign
, zExp
- shiftCount
, zSig
<<shiftCount
,
4324 /*----------------------------------------------------------------------------
4325 | Normalizes the subnormal double-precision floating-point value represented
4326 | by the denormalized significand `aSig'. The normalized exponent and
4327 | significand are stored at the locations pointed to by `zExpPtr' and
4328 | `zSigPtr', respectively.
4329 *----------------------------------------------------------------------------*/
4332 normalizeFloat64Subnormal(uint64_t aSig
, int *zExpPtr
, uint64_t *zSigPtr
)
4336 shiftCount
= clz64(aSig
) - 11;
4337 *zSigPtr
= aSig
<<shiftCount
;
4338 *zExpPtr
= 1 - shiftCount
;
4342 /*----------------------------------------------------------------------------
4343 | Packs the sign `zSign', exponent `zExp', and significand `zSig' into a
4344 | double-precision floating-point value, returning the result. After being
4345 | shifted into the proper positions, the three fields are simply added
4346 | together to form the result. This means that any integer portion of `zSig'
4347 | will be added into the exponent. Since a properly normalized significand
4348 | will have an integer portion equal to 1, the `zExp' input should be 1 less
4349 | than the desired result exponent whenever `zSig' is a complete, normalized
4351 *----------------------------------------------------------------------------*/
4353 static inline float64
packFloat64(bool zSign
, int zExp
, uint64_t zSig
)
4356 return make_float64(
4357 ( ( (uint64_t) zSign
)<<63 ) + ( ( (uint64_t) zExp
)<<52 ) + zSig
);
4361 /*----------------------------------------------------------------------------
4362 | Takes an abstract floating-point value having sign `zSign', exponent `zExp',
4363 | and significand `zSig', and returns the proper double-precision floating-
4364 | point value corresponding to the abstract input. Ordinarily, the abstract
4365 | value is simply rounded and packed into the double-precision format, with
4366 | the inexact exception raised if the abstract input cannot be represented
4367 | exactly. However, if the abstract value is too large, the overflow and
4368 | inexact exceptions are raised and an infinity or maximal finite value is
4369 | returned. If the abstract value is too small, the input value is rounded to
4370 | a subnormal number, and the underflow and inexact exceptions are raised if
4371 | the abstract input cannot be represented exactly as a subnormal double-
4372 | precision floating-point number.
4373 | The input significand `zSig' has its binary point between bits 62
4374 | and 61, which is 10 bits to the left of the usual location. This shifted
4375 | significand must be normalized or smaller. If `zSig' is not normalized,
4376 | `zExp' must be 0; in that case, the result returned is a subnormal number,
4377 | and it must not require rounding. In the usual case that `zSig' is
4378 | normalized, `zExp' must be 1 less than the ``true'' floating-point exponent.
4379 | The handling of underflow and overflow follows the IEC/IEEE Standard for
4380 | Binary Floating-Point Arithmetic.
4381 *----------------------------------------------------------------------------*/
4383 static float64
roundAndPackFloat64(bool zSign
, int zExp
, uint64_t zSig
,
4384 float_status
*status
)
4386 int8_t roundingMode
;
4387 bool roundNearestEven
;
4388 int roundIncrement
, roundBits
;
4391 roundingMode
= status
->float_rounding_mode
;
4392 roundNearestEven
= ( roundingMode
== float_round_nearest_even
);
4393 switch (roundingMode
) {
4394 case float_round_nearest_even
:
4395 case float_round_ties_away
:
4396 roundIncrement
= 0x200;
4398 case float_round_to_zero
:
4401 case float_round_up
:
4402 roundIncrement
= zSign
? 0 : 0x3ff;
4404 case float_round_down
:
4405 roundIncrement
= zSign
? 0x3ff : 0;
4407 case float_round_to_odd
:
4408 roundIncrement
= (zSig
& 0x400) ? 0 : 0x3ff;
4413 roundBits
= zSig
& 0x3FF;
4414 if ( 0x7FD <= (uint16_t) zExp
) {
4415 if ( ( 0x7FD < zExp
)
4416 || ( ( zExp
== 0x7FD )
4417 && ( (int64_t) ( zSig
+ roundIncrement
) < 0 ) )
4419 bool overflow_to_inf
= roundingMode
!= float_round_to_odd
&&
4420 roundIncrement
!= 0;
4421 float_raise(float_flag_overflow
| float_flag_inexact
, status
);
4422 return packFloat64(zSign
, 0x7FF, -(!overflow_to_inf
));
4425 if (status
->flush_to_zero
) {
4426 float_raise(float_flag_output_denormal
, status
);
4427 return packFloat64(zSign
, 0, 0);
4429 isTiny
= status
->tininess_before_rounding
4431 || (zSig
+ roundIncrement
< UINT64_C(0x8000000000000000));
4432 shift64RightJamming( zSig
, - zExp
, &zSig
);
4434 roundBits
= zSig
& 0x3FF;
4435 if (isTiny
&& roundBits
) {
4436 float_raise(float_flag_underflow
, status
);
4438 if (roundingMode
== float_round_to_odd
) {
4440 * For round-to-odd case, the roundIncrement depends on
4441 * zSig which just changed.
4443 roundIncrement
= (zSig
& 0x400) ? 0 : 0x3ff;
4448 float_raise(float_flag_inexact
, status
);
4450 zSig
= ( zSig
+ roundIncrement
)>>10;
4451 if (!(roundBits
^ 0x200) && roundNearestEven
) {
4454 if ( zSig
== 0 ) zExp
= 0;
4455 return packFloat64( zSign
, zExp
, zSig
);
4459 /*----------------------------------------------------------------------------
4460 | Takes an abstract floating-point value having sign `zSign', exponent `zExp',
4461 | and significand `zSig', and returns the proper double-precision floating-
4462 | point value corresponding to the abstract input. This routine is just like
4463 | `roundAndPackFloat64' except that `zSig' does not have to be normalized.
4464 | Bit 63 of `zSig' must be zero, and `zExp' must be 1 less than the ``true''
4465 | floating-point exponent.
4466 *----------------------------------------------------------------------------*/
4469 normalizeRoundAndPackFloat64(bool zSign
, int zExp
, uint64_t zSig
,
4470 float_status
*status
)
4474 shiftCount
= clz64(zSig
) - 1;
4475 return roundAndPackFloat64(zSign
, zExp
- shiftCount
, zSig
<<shiftCount
,
4480 /*----------------------------------------------------------------------------
4481 | Normalizes the subnormal extended double-precision floating-point value
4482 | represented by the denormalized significand `aSig'. The normalized exponent
4483 | and significand are stored at the locations pointed to by `zExpPtr' and
4484 | `zSigPtr', respectively.
4485 *----------------------------------------------------------------------------*/
4487 void normalizeFloatx80Subnormal(uint64_t aSig
, int32_t *zExpPtr
,
4492 shiftCount
= clz64(aSig
);
4493 *zSigPtr
= aSig
<<shiftCount
;
4494 *zExpPtr
= 1 - shiftCount
;
4497 /*----------------------------------------------------------------------------
4498 | Takes an abstract floating-point value having sign `zSign', exponent `zExp',
4499 | and extended significand formed by the concatenation of `zSig0' and `zSig1',
4500 | and returns the proper extended double-precision floating-point value
4501 | corresponding to the abstract input. Ordinarily, the abstract value is
4502 | rounded and packed into the extended double-precision format, with the
4503 | inexact exception raised if the abstract input cannot be represented
4504 | exactly. However, if the abstract value is too large, the overflow and
4505 | inexact exceptions are raised and an infinity or maximal finite value is
4506 | returned. If the abstract value is too small, the input value is rounded to
4507 | a subnormal number, and the underflow and inexact exceptions are raised if
4508 | the abstract input cannot be represented exactly as a subnormal extended
4509 | double-precision floating-point number.
4510 | If `roundingPrecision' is 32 or 64, the result is rounded to the same
4511 | number of bits as single or double precision, respectively. Otherwise, the
4512 | result is rounded to the full precision of the extended double-precision
4514 | The input significand must be normalized or smaller. If the input
4515 | significand is not normalized, `zExp' must be 0; in that case, the result
4516 | returned is a subnormal number, and it must not require rounding. The
4517 | handling of underflow and overflow follows the IEC/IEEE Standard for Binary
4518 | Floating-Point Arithmetic.
4519 *----------------------------------------------------------------------------*/
4521 floatx80
roundAndPackFloatx80(int8_t roundingPrecision
, bool zSign
,
4522 int32_t zExp
, uint64_t zSig0
, uint64_t zSig1
,
4523 float_status
*status
)
4525 int8_t roundingMode
;
4526 bool roundNearestEven
, increment
, isTiny
;
4527 int64_t roundIncrement
, roundMask
, roundBits
;
4529 roundingMode
= status
->float_rounding_mode
;
4530 roundNearestEven
= ( roundingMode
== float_round_nearest_even
);
4531 if ( roundingPrecision
== 80 ) goto precision80
;
4532 if ( roundingPrecision
== 64 ) {
4533 roundIncrement
= UINT64_C(0x0000000000000400);
4534 roundMask
= UINT64_C(0x00000000000007FF);
4536 else if ( roundingPrecision
== 32 ) {
4537 roundIncrement
= UINT64_C(0x0000008000000000);
4538 roundMask
= UINT64_C(0x000000FFFFFFFFFF);
4543 zSig0
|= ( zSig1
!= 0 );
4544 switch (roundingMode
) {
4545 case float_round_nearest_even
:
4546 case float_round_ties_away
:
4548 case float_round_to_zero
:
4551 case float_round_up
:
4552 roundIncrement
= zSign
? 0 : roundMask
;
4554 case float_round_down
:
4555 roundIncrement
= zSign
? roundMask
: 0;
4560 roundBits
= zSig0
& roundMask
;
4561 if ( 0x7FFD <= (uint32_t) ( zExp
- 1 ) ) {
4562 if ( ( 0x7FFE < zExp
)
4563 || ( ( zExp
== 0x7FFE ) && ( zSig0
+ roundIncrement
< zSig0
) )
4568 if (status
->flush_to_zero
) {
4569 float_raise(float_flag_output_denormal
, status
);
4570 return packFloatx80(zSign
, 0, 0);
4572 isTiny
= status
->tininess_before_rounding
4574 || (zSig0
<= zSig0
+ roundIncrement
);
4575 shift64RightJamming( zSig0
, 1 - zExp
, &zSig0
);
4577 roundBits
= zSig0
& roundMask
;
4578 if (isTiny
&& roundBits
) {
4579 float_raise(float_flag_underflow
, status
);
4582 float_raise(float_flag_inexact
, status
);
4584 zSig0
+= roundIncrement
;
4585 if ( (int64_t) zSig0
< 0 ) zExp
= 1;
4586 roundIncrement
= roundMask
+ 1;
4587 if ( roundNearestEven
&& ( roundBits
<<1 == roundIncrement
) ) {
4588 roundMask
|= roundIncrement
;
4590 zSig0
&= ~ roundMask
;
4591 return packFloatx80( zSign
, zExp
, zSig0
);
4595 float_raise(float_flag_inexact
, status
);
4597 zSig0
+= roundIncrement
;
4598 if ( zSig0
< roundIncrement
) {
4600 zSig0
= UINT64_C(0x8000000000000000);
4602 roundIncrement
= roundMask
+ 1;
4603 if ( roundNearestEven
&& ( roundBits
<<1 == roundIncrement
) ) {
4604 roundMask
|= roundIncrement
;
4606 zSig0
&= ~ roundMask
;
4607 if ( zSig0
== 0 ) zExp
= 0;
4608 return packFloatx80( zSign
, zExp
, zSig0
);
4610 switch (roundingMode
) {
4611 case float_round_nearest_even
:
4612 case float_round_ties_away
:
4613 increment
= ((int64_t)zSig1
< 0);
4615 case float_round_to_zero
:
4618 case float_round_up
:
4619 increment
= !zSign
&& zSig1
;
4621 case float_round_down
:
4622 increment
= zSign
&& zSig1
;
4627 if ( 0x7FFD <= (uint32_t) ( zExp
- 1 ) ) {
4628 if ( ( 0x7FFE < zExp
)
4629 || ( ( zExp
== 0x7FFE )
4630 && ( zSig0
== UINT64_C(0xFFFFFFFFFFFFFFFF) )
4636 float_raise(float_flag_overflow
| float_flag_inexact
, status
);
4637 if ( ( roundingMode
== float_round_to_zero
)
4638 || ( zSign
&& ( roundingMode
== float_round_up
) )
4639 || ( ! zSign
&& ( roundingMode
== float_round_down
) )
4641 return packFloatx80( zSign
, 0x7FFE, ~ roundMask
);
4643 return packFloatx80(zSign
,
4644 floatx80_infinity_high
,
4645 floatx80_infinity_low
);
4648 isTiny
= status
->tininess_before_rounding
4651 || (zSig0
< UINT64_C(0xFFFFFFFFFFFFFFFF));
4652 shift64ExtraRightJamming( zSig0
, zSig1
, 1 - zExp
, &zSig0
, &zSig1
);
4654 if (isTiny
&& zSig1
) {
4655 float_raise(float_flag_underflow
, status
);
4658 float_raise(float_flag_inexact
, status
);
4660 switch (roundingMode
) {
4661 case float_round_nearest_even
:
4662 case float_round_ties_away
:
4663 increment
= ((int64_t)zSig1
< 0);
4665 case float_round_to_zero
:
4668 case float_round_up
:
4669 increment
= !zSign
&& zSig1
;
4671 case float_round_down
:
4672 increment
= zSign
&& zSig1
;
4679 if (!(zSig1
<< 1) && roundNearestEven
) {
4682 if ( (int64_t) zSig0
< 0 ) zExp
= 1;
4684 return packFloatx80( zSign
, zExp
, zSig0
);
4688 float_raise(float_flag_inexact
, status
);
4694 zSig0
= UINT64_C(0x8000000000000000);
4697 if (!(zSig1
<< 1) && roundNearestEven
) {
4703 if ( zSig0
== 0 ) zExp
= 0;
4705 return packFloatx80( zSign
, zExp
, zSig0
);
4709 /*----------------------------------------------------------------------------
4710 | Takes an abstract floating-point value having sign `zSign', exponent
4711 | `zExp', and significand formed by the concatenation of `zSig0' and `zSig1',
4712 | and returns the proper extended double-precision floating-point value
4713 | corresponding to the abstract input. This routine is just like
4714 | `roundAndPackFloatx80' except that the input significand does not have to be
4716 *----------------------------------------------------------------------------*/
4718 floatx80
normalizeRoundAndPackFloatx80(int8_t roundingPrecision
,
4719 bool zSign
, int32_t zExp
,
4720 uint64_t zSig0
, uint64_t zSig1
,
4721 float_status
*status
)
4730 shiftCount
= clz64(zSig0
);
4731 shortShift128Left( zSig0
, zSig1
, shiftCount
, &zSig0
, &zSig1
);
4733 return roundAndPackFloatx80(roundingPrecision
, zSign
, zExp
,
4734 zSig0
, zSig1
, status
);
4738 /*----------------------------------------------------------------------------
4739 | Returns the least-significant 64 fraction bits of the quadruple-precision
4740 | floating-point value `a'.
4741 *----------------------------------------------------------------------------*/
4743 static inline uint64_t extractFloat128Frac1( float128 a
)
4750 /*----------------------------------------------------------------------------
4751 | Returns the most-significant 48 fraction bits of the quadruple-precision
4752 | floating-point value `a'.
4753 *----------------------------------------------------------------------------*/
4755 static inline uint64_t extractFloat128Frac0( float128 a
)
4758 return a
.high
& UINT64_C(0x0000FFFFFFFFFFFF);
4762 /*----------------------------------------------------------------------------
4763 | Returns the exponent bits of the quadruple-precision floating-point value
4765 *----------------------------------------------------------------------------*/
4767 static inline int32_t extractFloat128Exp( float128 a
)
4770 return ( a
.high
>>48 ) & 0x7FFF;
4774 /*----------------------------------------------------------------------------
4775 | Returns the sign bit of the quadruple-precision floating-point value `a'.
4776 *----------------------------------------------------------------------------*/
4778 static inline bool extractFloat128Sign(float128 a
)
4780 return a
.high
>> 63;
4783 /*----------------------------------------------------------------------------
4784 | Normalizes the subnormal quadruple-precision floating-point value
4785 | represented by the denormalized significand formed by the concatenation of
4786 | `aSig0' and `aSig1'. The normalized exponent is stored at the location
4787 | pointed to by `zExpPtr'. The most significant 49 bits of the normalized
4788 | significand are stored at the location pointed to by `zSig0Ptr', and the
4789 | least significant 64 bits of the normalized significand are stored at the
4790 | location pointed to by `zSig1Ptr'.
4791 *----------------------------------------------------------------------------*/
4794 normalizeFloat128Subnormal(
4805 shiftCount
= clz64(aSig1
) - 15;
4806 if ( shiftCount
< 0 ) {
4807 *zSig0Ptr
= aSig1
>>( - shiftCount
);
4808 *zSig1Ptr
= aSig1
<<( shiftCount
& 63 );
4811 *zSig0Ptr
= aSig1
<<shiftCount
;
4814 *zExpPtr
= - shiftCount
- 63;
4817 shiftCount
= clz64(aSig0
) - 15;
4818 shortShift128Left( aSig0
, aSig1
, shiftCount
, zSig0Ptr
, zSig1Ptr
);
4819 *zExpPtr
= 1 - shiftCount
;
4824 /*----------------------------------------------------------------------------
4825 | Packs the sign `zSign', the exponent `zExp', and the significand formed
4826 | by the concatenation of `zSig0' and `zSig1' into a quadruple-precision
4827 | floating-point value, returning the result. After being shifted into the
4828 | proper positions, the three fields `zSign', `zExp', and `zSig0' are simply
4829 | added together to form the most significant 32 bits of the result. This
4830 | means that any integer portion of `zSig0' will be added into the exponent.
4831 | Since a properly normalized significand will have an integer portion equal
4832 | to 1, the `zExp' input should be 1 less than the desired result exponent
4833 | whenever `zSig0' and `zSig1' concatenated form a complete, normalized
4835 *----------------------------------------------------------------------------*/
4837 static inline float128
4838 packFloat128(bool zSign
, int32_t zExp
, uint64_t zSig0
, uint64_t zSig1
)
4843 z
.high
= ((uint64_t)zSign
<< 63) + ((uint64_t)zExp
<< 48) + zSig0
;
4847 /*----------------------------------------------------------------------------
4848 | Takes an abstract floating-point value having sign `zSign', exponent `zExp',
4849 | and extended significand formed by the concatenation of `zSig0', `zSig1',
4850 | and `zSig2', and returns the proper quadruple-precision floating-point value
4851 | corresponding to the abstract input. Ordinarily, the abstract value is
4852 | simply rounded and packed into the quadruple-precision format, with the
4853 | inexact exception raised if the abstract input cannot be represented
4854 | exactly. However, if the abstract value is too large, the overflow and
4855 | inexact exceptions are raised and an infinity or maximal finite value is
4856 | returned. If the abstract value is too small, the input value is rounded to
4857 | a subnormal number, and the underflow and inexact exceptions are raised if
4858 | the abstract input cannot be represented exactly as a subnormal quadruple-
4859 | precision floating-point number.
4860 | The input significand must be normalized or smaller. If the input
4861 | significand is not normalized, `zExp' must be 0; in that case, the result
4862 | returned is a subnormal number, and it must not require rounding. In the
4863 | usual case that the input significand is normalized, `zExp' must be 1 less
4864 | than the ``true'' floating-point exponent. The handling of underflow and
4865 | overflow follows the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
4866 *----------------------------------------------------------------------------*/
4868 static float128
roundAndPackFloat128(bool zSign
, int32_t zExp
,
4869 uint64_t zSig0
, uint64_t zSig1
,
4870 uint64_t zSig2
, float_status
*status
)
4872 int8_t roundingMode
;
4873 bool roundNearestEven
, increment
, isTiny
;
4875 roundingMode
= status
->float_rounding_mode
;
4876 roundNearestEven
= ( roundingMode
== float_round_nearest_even
);
4877 switch (roundingMode
) {
4878 case float_round_nearest_even
:
4879 case float_round_ties_away
:
4880 increment
= ((int64_t)zSig2
< 0);
4882 case float_round_to_zero
:
4885 case float_round_up
:
4886 increment
= !zSign
&& zSig2
;
4888 case float_round_down
:
4889 increment
= zSign
&& zSig2
;
4891 case float_round_to_odd
:
4892 increment
= !(zSig1
& 0x1) && zSig2
;
4897 if ( 0x7FFD <= (uint32_t) zExp
) {
4898 if ( ( 0x7FFD < zExp
)
4899 || ( ( zExp
== 0x7FFD )
4901 UINT64_C(0x0001FFFFFFFFFFFF),
4902 UINT64_C(0xFFFFFFFFFFFFFFFF),
4909 float_raise(float_flag_overflow
| float_flag_inexact
, status
);
4910 if ( ( roundingMode
== float_round_to_zero
)
4911 || ( zSign
&& ( roundingMode
== float_round_up
) )
4912 || ( ! zSign
&& ( roundingMode
== float_round_down
) )
4913 || (roundingMode
== float_round_to_odd
)
4919 UINT64_C(0x0000FFFFFFFFFFFF),
4920 UINT64_C(0xFFFFFFFFFFFFFFFF)
4923 return packFloat128( zSign
, 0x7FFF, 0, 0 );
4926 if (status
->flush_to_zero
) {
4927 float_raise(float_flag_output_denormal
, status
);
4928 return packFloat128(zSign
, 0, 0, 0);
4930 isTiny
= status
->tininess_before_rounding
4933 || lt128(zSig0
, zSig1
,
4934 UINT64_C(0x0001FFFFFFFFFFFF),
4935 UINT64_C(0xFFFFFFFFFFFFFFFF));
4936 shift128ExtraRightJamming(
4937 zSig0
, zSig1
, zSig2
, - zExp
, &zSig0
, &zSig1
, &zSig2
);
4939 if (isTiny
&& zSig2
) {
4940 float_raise(float_flag_underflow
, status
);
4942 switch (roundingMode
) {
4943 case float_round_nearest_even
:
4944 case float_round_ties_away
:
4945 increment
= ((int64_t)zSig2
< 0);
4947 case float_round_to_zero
:
4950 case float_round_up
:
4951 increment
= !zSign
&& zSig2
;
4953 case float_round_down
:
4954 increment
= zSign
&& zSig2
;
4956 case float_round_to_odd
:
4957 increment
= !(zSig1
& 0x1) && zSig2
;
4965 float_raise(float_flag_inexact
, status
);
4968 add128( zSig0
, zSig1
, 0, 1, &zSig0
, &zSig1
);
4969 if ((zSig2
+ zSig2
== 0) && roundNearestEven
) {
4974 if ( ( zSig0
| zSig1
) == 0 ) zExp
= 0;
4976 return packFloat128( zSign
, zExp
, zSig0
, zSig1
);
4980 /*----------------------------------------------------------------------------
4981 | Takes an abstract floating-point value having sign `zSign', exponent `zExp',
4982 | and significand formed by the concatenation of `zSig0' and `zSig1', and
4983 | returns the proper quadruple-precision floating-point value corresponding
4984 | to the abstract input. This routine is just like `roundAndPackFloat128'
4985 | except that the input significand has fewer bits and does not have to be
4986 | normalized. In all cases, `zExp' must be 1 less than the ``true'' floating-
4988 *----------------------------------------------------------------------------*/
4990 static float128
normalizeRoundAndPackFloat128(bool zSign
, int32_t zExp
,
4991 uint64_t zSig0
, uint64_t zSig1
,
4992 float_status
*status
)
5002 shiftCount
= clz64(zSig0
) - 15;
5003 if ( 0 <= shiftCount
) {
5005 shortShift128Left( zSig0
, zSig1
, shiftCount
, &zSig0
, &zSig1
);
5008 shift128ExtraRightJamming(
5009 zSig0
, zSig1
, 0, - shiftCount
, &zSig0
, &zSig1
, &zSig2
);
5012 return roundAndPackFloat128(zSign
, zExp
, zSig0
, zSig1
, zSig2
, status
);
5017 /*----------------------------------------------------------------------------
5018 | Returns the result of converting the 32-bit two's complement integer `a'
5019 | to the extended double-precision floating-point format. The conversion
5020 | is performed according to the IEC/IEEE Standard for Binary Floating-Point
5022 *----------------------------------------------------------------------------*/
5024 floatx80
int32_to_floatx80(int32_t a
, float_status
*status
)
5031 if ( a
== 0 ) return packFloatx80( 0, 0, 0 );
5033 absA
= zSign
? - a
: a
;
5034 shiftCount
= clz32(absA
) + 32;
5036 return packFloatx80( zSign
, 0x403E - shiftCount
, zSig
<<shiftCount
);
5040 /*----------------------------------------------------------------------------
5041 | Returns the result of converting the 32-bit two's complement integer `a' to
5042 | the quadruple-precision floating-point format. The conversion is performed
5043 | according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
5044 *----------------------------------------------------------------------------*/
5046 float128
int32_to_float128(int32_t a
, float_status
*status
)
5053 if ( a
== 0 ) return packFloat128( 0, 0, 0, 0 );
5055 absA
= zSign
? - a
: a
;
5056 shiftCount
= clz32(absA
) + 17;
5058 return packFloat128( zSign
, 0x402E - shiftCount
, zSig0
<<shiftCount
, 0 );
5062 /*----------------------------------------------------------------------------
5063 | Returns the result of converting the 64-bit two's complement integer `a'
5064 | to the extended double-precision floating-point format. The conversion
5065 | is performed according to the IEC/IEEE Standard for Binary Floating-Point
5067 *----------------------------------------------------------------------------*/
5069 floatx80
int64_to_floatx80(int64_t a
, float_status
*status
)
5075 if ( a
== 0 ) return packFloatx80( 0, 0, 0 );
5077 absA
= zSign
? - a
: a
;
5078 shiftCount
= clz64(absA
);
5079 return packFloatx80( zSign
, 0x403E - shiftCount
, absA
<<shiftCount
);
5083 /*----------------------------------------------------------------------------
5084 | Returns the result of converting the 64-bit two's complement integer `a' to
5085 | the quadruple-precision floating-point format. The conversion is performed
5086 | according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
5087 *----------------------------------------------------------------------------*/
5089 float128
int64_to_float128(int64_t a
, float_status
*status
)
5095 uint64_t zSig0
, zSig1
;
5097 if ( a
== 0 ) return packFloat128( 0, 0, 0, 0 );
5099 absA
= zSign
? - a
: a
;
5100 shiftCount
= clz64(absA
) + 49;
5101 zExp
= 0x406E - shiftCount
;
5102 if ( 64 <= shiftCount
) {
5111 shortShift128Left( zSig0
, zSig1
, shiftCount
, &zSig0
, &zSig1
);
5112 return packFloat128( zSign
, zExp
, zSig0
, zSig1
);
5116 /*----------------------------------------------------------------------------
5117 | Returns the result of converting the 64-bit unsigned integer `a'
5118 | to the quadruple-precision floating-point format. The conversion is performed
5119 | according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
5120 *----------------------------------------------------------------------------*/
5122 float128
uint64_to_float128(uint64_t a
, float_status
*status
)
5125 return float128_zero
;
5127 return normalizeRoundAndPackFloat128(0, 0x406E, 0, a
, status
);
5130 /*----------------------------------------------------------------------------
5131 | Returns the result of converting the single-precision floating-point value
5132 | `a' to the extended double-precision floating-point format. The conversion
5133 | is performed according to the IEC/IEEE Standard for Binary Floating-Point
5135 *----------------------------------------------------------------------------*/
5137 floatx80
float32_to_floatx80(float32 a
, float_status
*status
)
5143 a
= float32_squash_input_denormal(a
, status
);
5144 aSig
= extractFloat32Frac( a
);
5145 aExp
= extractFloat32Exp( a
);
5146 aSign
= extractFloat32Sign( a
);
5147 if ( aExp
== 0xFF ) {
5149 floatx80 res
= commonNaNToFloatx80(float32ToCommonNaN(a
, status
),
5151 return floatx80_silence_nan(res
, status
);
5153 return packFloatx80(aSign
,
5154 floatx80_infinity_high
,
5155 floatx80_infinity_low
);
5158 if ( aSig
== 0 ) return packFloatx80( aSign
, 0, 0 );
5159 normalizeFloat32Subnormal( aSig
, &aExp
, &aSig
);
5162 return packFloatx80( aSign
, aExp
+ 0x3F80, ( (uint64_t) aSig
)<<40 );
5166 /*----------------------------------------------------------------------------
5167 | Returns the remainder of the single-precision floating-point value `a'
5168 | with respect to the corresponding value `b'. The operation is performed
5169 | according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
5170 *----------------------------------------------------------------------------*/
5172 float32
float32_rem(float32 a
, float32 b
, float_status
*status
)
5175 int aExp
, bExp
, expDiff
;
5176 uint32_t aSig
, bSig
;
5178 uint64_t aSig64
, bSig64
, q64
;
5179 uint32_t alternateASig
;
5181 a
= float32_squash_input_denormal(a
, status
);
5182 b
= float32_squash_input_denormal(b
, status
);
5184 aSig
= extractFloat32Frac( a
);
5185 aExp
= extractFloat32Exp( a
);
5186 aSign
= extractFloat32Sign( a
);
5187 bSig
= extractFloat32Frac( b
);
5188 bExp
= extractFloat32Exp( b
);
5189 if ( aExp
== 0xFF ) {
5190 if ( aSig
|| ( ( bExp
== 0xFF ) && bSig
) ) {
5191 return propagateFloat32NaN(a
, b
, status
);
5193 float_raise(float_flag_invalid
, status
);
5194 return float32_default_nan(status
);
5196 if ( bExp
== 0xFF ) {
5198 return propagateFloat32NaN(a
, b
, status
);
5204 float_raise(float_flag_invalid
, status
);
5205 return float32_default_nan(status
);
5207 normalizeFloat32Subnormal( bSig
, &bExp
, &bSig
);
5210 if ( aSig
== 0 ) return a
;
5211 normalizeFloat32Subnormal( aSig
, &aExp
, &aSig
);
5213 expDiff
= aExp
- bExp
;
5216 if ( expDiff
< 32 ) {
5219 if ( expDiff
< 0 ) {
5220 if ( expDiff
< -1 ) return a
;
5223 q
= ( bSig
<= aSig
);
5224 if ( q
) aSig
-= bSig
;
5225 if ( 0 < expDiff
) {
5226 q
= ( ( (uint64_t) aSig
)<<32 ) / bSig
;
5229 aSig
= ( ( aSig
>>1 )<<( expDiff
- 1 ) ) - bSig
* q
;
5237 if ( bSig
<= aSig
) aSig
-= bSig
;
5238 aSig64
= ( (uint64_t) aSig
)<<40;
5239 bSig64
= ( (uint64_t) bSig
)<<40;
5241 while ( 0 < expDiff
) {
5242 q64
= estimateDiv128To64( aSig64
, 0, bSig64
);
5243 q64
= ( 2 < q64
) ? q64
- 2 : 0;
5244 aSig64
= - ( ( bSig
* q64
)<<38 );
5248 q64
= estimateDiv128To64( aSig64
, 0, bSig64
);
5249 q64
= ( 2 < q64
) ? q64
- 2 : 0;
5250 q
= q64
>>( 64 - expDiff
);
5252 aSig
= ( ( aSig64
>>33 )<<( expDiff
- 1 ) ) - bSig
* q
;
5255 alternateASig
= aSig
;
5258 } while ( 0 <= (int32_t) aSig
);
5259 sigMean
= aSig
+ alternateASig
;
5260 if ( ( sigMean
< 0 ) || ( ( sigMean
== 0 ) && ( q
& 1 ) ) ) {
5261 aSig
= alternateASig
;
5263 zSign
= ( (int32_t) aSig
< 0 );
5264 if ( zSign
) aSig
= - aSig
;
5265 return normalizeRoundAndPackFloat32(aSign
^ zSign
, bExp
, aSig
, status
);
5270 /*----------------------------------------------------------------------------
5271 | Returns the binary exponential of the single-precision floating-point value
5272 | `a'. The operation is performed according to the IEC/IEEE Standard for
5273 | Binary Floating-Point Arithmetic.
5275 | Uses the following identities:
5277 | 1. -------------------------------------------------------------------------
5281 | 2. -------------------------------------------------------------------------
5284 | e = 1 + --- + --- + --- + --- + --- + ... + --- + ...
5286 *----------------------------------------------------------------------------*/
5288 static const float64 float32_exp2_coefficients
[15] =
5290 const_float64( 0x3ff0000000000000ll
), /* 1 */
5291 const_float64( 0x3fe0000000000000ll
), /* 2 */
5292 const_float64( 0x3fc5555555555555ll
), /* 3 */
5293 const_float64( 0x3fa5555555555555ll
), /* 4 */
5294 const_float64( 0x3f81111111111111ll
), /* 5 */
5295 const_float64( 0x3f56c16c16c16c17ll
), /* 6 */
5296 const_float64( 0x3f2a01a01a01a01all
), /* 7 */
5297 const_float64( 0x3efa01a01a01a01all
), /* 8 */
5298 const_float64( 0x3ec71de3a556c734ll
), /* 9 */
5299 const_float64( 0x3e927e4fb7789f5cll
), /* 10 */
5300 const_float64( 0x3e5ae64567f544e4ll
), /* 11 */
5301 const_float64( 0x3e21eed8eff8d898ll
), /* 12 */
5302 const_float64( 0x3de6124613a86d09ll
), /* 13 */
5303 const_float64( 0x3da93974a8c07c9dll
), /* 14 */
5304 const_float64( 0x3d6ae7f3e733b81fll
), /* 15 */
5307 float32
float32_exp2(float32 a
, float_status
*status
)
5314 a
= float32_squash_input_denormal(a
, status
);
5316 aSig
= extractFloat32Frac( a
);
5317 aExp
= extractFloat32Exp( a
);
5318 aSign
= extractFloat32Sign( a
);
5320 if ( aExp
== 0xFF) {
5322 return propagateFloat32NaN(a
, float32_zero
, status
);
5324 return (aSign
) ? float32_zero
: a
;
5327 if (aSig
== 0) return float32_one
;
5330 float_raise(float_flag_inexact
, status
);
5332 /* ******************************* */
5333 /* using float64 for approximation */
5334 /* ******************************* */
5335 x
= float32_to_float64(a
, status
);
5336 x
= float64_mul(x
, float64_ln2
, status
);
5340 for (i
= 0 ; i
< 15 ; i
++) {
5343 f
= float64_mul(xn
, float32_exp2_coefficients
[i
], status
);
5344 r
= float64_add(r
, f
, status
);
5346 xn
= float64_mul(xn
, x
, status
);
5349 return float64_to_float32(r
, status
);
5352 /*----------------------------------------------------------------------------
5353 | Returns the binary log of the single-precision floating-point value `a'.
5354 | The operation is performed according to the IEC/IEEE Standard for Binary
5355 | Floating-Point Arithmetic.
5356 *----------------------------------------------------------------------------*/
5357 float32
float32_log2(float32 a
, float_status
*status
)
5361 uint32_t aSig
, zSig
, i
;
5363 a
= float32_squash_input_denormal(a
, status
);
5364 aSig
= extractFloat32Frac( a
);
5365 aExp
= extractFloat32Exp( a
);
5366 aSign
= extractFloat32Sign( a
);
5369 if ( aSig
== 0 ) return packFloat32( 1, 0xFF, 0 );
5370 normalizeFloat32Subnormal( aSig
, &aExp
, &aSig
);
5373 float_raise(float_flag_invalid
, status
);
5374 return float32_default_nan(status
);
5376 if ( aExp
== 0xFF ) {
5378 return propagateFloat32NaN(a
, float32_zero
, status
);
5388 for (i
= 1 << 22; i
> 0; i
>>= 1) {
5389 aSig
= ( (uint64_t)aSig
* aSig
) >> 23;
5390 if ( aSig
& 0x01000000 ) {
5399 return normalizeRoundAndPackFloat32(zSign
, 0x85, zSig
, status
);
5402 /*----------------------------------------------------------------------------
5403 | Returns the result of converting the double-precision floating-point value
5404 | `a' to the extended double-precision floating-point format. The conversion
5405 | is performed according to the IEC/IEEE Standard for Binary Floating-Point
5407 *----------------------------------------------------------------------------*/
5409 floatx80
float64_to_floatx80(float64 a
, float_status
*status
)
5415 a
= float64_squash_input_denormal(a
, status
);
5416 aSig
= extractFloat64Frac( a
);
5417 aExp
= extractFloat64Exp( a
);
5418 aSign
= extractFloat64Sign( a
);
5419 if ( aExp
== 0x7FF ) {
5421 floatx80 res
= commonNaNToFloatx80(float64ToCommonNaN(a
, status
),
5423 return floatx80_silence_nan(res
, status
);
5425 return packFloatx80(aSign
,
5426 floatx80_infinity_high
,
5427 floatx80_infinity_low
);
5430 if ( aSig
== 0 ) return packFloatx80( aSign
, 0, 0 );
5431 normalizeFloat64Subnormal( aSig
, &aExp
, &aSig
);
5435 aSign
, aExp
+ 0x3C00, (aSig
| UINT64_C(0x0010000000000000)) << 11);
5439 /*----------------------------------------------------------------------------
5440 | Returns the remainder of the double-precision floating-point value `a'
5441 | with respect to the corresponding value `b'. The operation is performed
5442 | according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
5443 *----------------------------------------------------------------------------*/
5445 float64
float64_rem(float64 a
, float64 b
, float_status
*status
)
5448 int aExp
, bExp
, expDiff
;
5449 uint64_t aSig
, bSig
;
5450 uint64_t q
, alternateASig
;
5453 a
= float64_squash_input_denormal(a
, status
);
5454 b
= float64_squash_input_denormal(b
, status
);
5455 aSig
= extractFloat64Frac( a
);
5456 aExp
= extractFloat64Exp( a
);
5457 aSign
= extractFloat64Sign( a
);
5458 bSig
= extractFloat64Frac( b
);
5459 bExp
= extractFloat64Exp( b
);
5460 if ( aExp
== 0x7FF ) {
5461 if ( aSig
|| ( ( bExp
== 0x7FF ) && bSig
) ) {
5462 return propagateFloat64NaN(a
, b
, status
);
5464 float_raise(float_flag_invalid
, status
);
5465 return float64_default_nan(status
);
5467 if ( bExp
== 0x7FF ) {
5469 return propagateFloat64NaN(a
, b
, status
);
5475 float_raise(float_flag_invalid
, status
);
5476 return float64_default_nan(status
);
5478 normalizeFloat64Subnormal( bSig
, &bExp
, &bSig
);
5481 if ( aSig
== 0 ) return a
;
5482 normalizeFloat64Subnormal( aSig
, &aExp
, &aSig
);
5484 expDiff
= aExp
- bExp
;
5485 aSig
= (aSig
| UINT64_C(0x0010000000000000)) << 11;
5486 bSig
= (bSig
| UINT64_C(0x0010000000000000)) << 11;
5487 if ( expDiff
< 0 ) {
5488 if ( expDiff
< -1 ) return a
;
5491 q
= ( bSig
<= aSig
);
5492 if ( q
) aSig
-= bSig
;
5494 while ( 0 < expDiff
) {
5495 q
= estimateDiv128To64( aSig
, 0, bSig
);
5496 q
= ( 2 < q
) ? q
- 2 : 0;
5497 aSig
= - ( ( bSig
>>2 ) * q
);
5501 if ( 0 < expDiff
) {
5502 q
= estimateDiv128To64( aSig
, 0, bSig
);
5503 q
= ( 2 < q
) ? q
- 2 : 0;
5506 aSig
= ( ( aSig
>>1 )<<( expDiff
- 1 ) ) - bSig
* q
;
5513 alternateASig
= aSig
;
5516 } while ( 0 <= (int64_t) aSig
);
5517 sigMean
= aSig
+ alternateASig
;
5518 if ( ( sigMean
< 0 ) || ( ( sigMean
== 0 ) && ( q
& 1 ) ) ) {
5519 aSig
= alternateASig
;
5521 zSign
= ( (int64_t) aSig
< 0 );
5522 if ( zSign
) aSig
= - aSig
;
5523 return normalizeRoundAndPackFloat64(aSign
^ zSign
, bExp
, aSig
, status
);
5527 /*----------------------------------------------------------------------------
5528 | Returns the binary log of the double-precision floating-point value `a'.
5529 | The operation is performed according to the IEC/IEEE Standard for Binary
5530 | Floating-Point Arithmetic.
5531 *----------------------------------------------------------------------------*/
5532 float64
float64_log2(float64 a
, float_status
*status
)
5536 uint64_t aSig
, aSig0
, aSig1
, zSig
, i
;
5537 a
= float64_squash_input_denormal(a
, status
);
5539 aSig
= extractFloat64Frac( a
);
5540 aExp
= extractFloat64Exp( a
);
5541 aSign
= extractFloat64Sign( a
);
5544 if ( aSig
== 0 ) return packFloat64( 1, 0x7FF, 0 );
5545 normalizeFloat64Subnormal( aSig
, &aExp
, &aSig
);
5548 float_raise(float_flag_invalid
, status
);
5549 return float64_default_nan(status
);
5551 if ( aExp
== 0x7FF ) {
5553 return propagateFloat64NaN(a
, float64_zero
, status
);
5559 aSig
|= UINT64_C(0x0010000000000000);
5561 zSig
= (uint64_t)aExp
<< 52;
5562 for (i
= 1LL << 51; i
> 0; i
>>= 1) {
5563 mul64To128( aSig
, aSig
, &aSig0
, &aSig1
);
5564 aSig
= ( aSig0
<< 12 ) | ( aSig1
>> 52 );
5565 if ( aSig
& UINT64_C(0x0020000000000000) ) {
5573 return normalizeRoundAndPackFloat64(zSign
, 0x408, zSig
, status
);
5576 /*----------------------------------------------------------------------------
5577 | Returns the result of converting the extended double-precision floating-
5578 | point value `a' to the 32-bit two's complement integer format. The
5579 | conversion is performed according to the IEC/IEEE Standard for Binary
5580 | Floating-Point Arithmetic---which means in particular that the conversion
5581 | is rounded according to the current rounding mode. If `a' is a NaN, the
5582 | largest positive integer is returned. Otherwise, if the conversion
5583 | overflows, the largest integer with the same sign as `a' is returned.
5584 *----------------------------------------------------------------------------*/
5586 int32_t floatx80_to_int32(floatx80 a
, float_status
*status
)
5589 int32_t aExp
, shiftCount
;
5592 if (floatx80_invalid_encoding(a
)) {
5593 float_raise(float_flag_invalid
, status
);
5596 aSig
= extractFloatx80Frac( a
);
5597 aExp
= extractFloatx80Exp( a
);
5598 aSign
= extractFloatx80Sign( a
);
5599 if ( ( aExp
== 0x7FFF ) && (uint64_t) ( aSig
<<1 ) ) aSign
= 0;
5600 shiftCount
= 0x4037 - aExp
;
5601 if ( shiftCount
<= 0 ) shiftCount
= 1;
5602 shift64RightJamming( aSig
, shiftCount
, &aSig
);
5603 return roundAndPackInt32(aSign
, aSig
, status
);
5607 /*----------------------------------------------------------------------------
5608 | Returns the result of converting the extended double-precision floating-
5609 | point value `a' to the 32-bit two's complement integer format. The
5610 | conversion is performed according to the IEC/IEEE Standard for Binary
5611 | Floating-Point Arithmetic, except that the conversion is always rounded
5612 | toward zero. If `a' is a NaN, the largest positive integer is returned.
5613 | Otherwise, if the conversion overflows, the largest integer with the same
5614 | sign as `a' is returned.
5615 *----------------------------------------------------------------------------*/
5617 int32_t floatx80_to_int32_round_to_zero(floatx80 a
, float_status
*status
)
5620 int32_t aExp
, shiftCount
;
5621 uint64_t aSig
, savedASig
;
5624 if (floatx80_invalid_encoding(a
)) {
5625 float_raise(float_flag_invalid
, status
);
5628 aSig
= extractFloatx80Frac( a
);
5629 aExp
= extractFloatx80Exp( a
);
5630 aSign
= extractFloatx80Sign( a
);
5631 if ( 0x401E < aExp
) {
5632 if ( ( aExp
== 0x7FFF ) && (uint64_t) ( aSig
<<1 ) ) aSign
= 0;
5635 else if ( aExp
< 0x3FFF ) {
5637 float_raise(float_flag_inexact
, status
);
5641 shiftCount
= 0x403E - aExp
;
5643 aSig
>>= shiftCount
;
5645 if ( aSign
) z
= - z
;
5646 if ( ( z
< 0 ) ^ aSign
) {
5648 float_raise(float_flag_invalid
, status
);
5649 return aSign
? (int32_t) 0x80000000 : 0x7FFFFFFF;
5651 if ( ( aSig
<<shiftCount
) != savedASig
) {
5652 float_raise(float_flag_inexact
, status
);
5658 /*----------------------------------------------------------------------------
5659 | Returns the result of converting the extended double-precision floating-
5660 | point value `a' to the 64-bit two's complement integer format. The
5661 | conversion is performed according to the IEC/IEEE Standard for Binary
5662 | Floating-Point Arithmetic---which means in particular that the conversion
5663 | is rounded according to the current rounding mode. If `a' is a NaN,
5664 | the largest positive integer is returned. Otherwise, if the conversion
5665 | overflows, the largest integer with the same sign as `a' is returned.
5666 *----------------------------------------------------------------------------*/
5668 int64_t floatx80_to_int64(floatx80 a
, float_status
*status
)
5671 int32_t aExp
, shiftCount
;
5672 uint64_t aSig
, aSigExtra
;
5674 if (floatx80_invalid_encoding(a
)) {
5675 float_raise(float_flag_invalid
, status
);
5678 aSig
= extractFloatx80Frac( a
);
5679 aExp
= extractFloatx80Exp( a
);
5680 aSign
= extractFloatx80Sign( a
);
5681 shiftCount
= 0x403E - aExp
;
5682 if ( shiftCount
<= 0 ) {
5684 float_raise(float_flag_invalid
, status
);
5685 if (!aSign
|| floatx80_is_any_nan(a
)) {
5693 shift64ExtraRightJamming( aSig
, 0, shiftCount
, &aSig
, &aSigExtra
);
5695 return roundAndPackInt64(aSign
, aSig
, aSigExtra
, status
);
5699 /*----------------------------------------------------------------------------
5700 | Returns the result of converting the extended double-precision floating-
5701 | point value `a' to the 64-bit two's complement integer format. The
5702 | conversion is performed according to the IEC/IEEE Standard for Binary
5703 | Floating-Point Arithmetic, except that the conversion is always rounded
5704 | toward zero. If `a' is a NaN, the largest positive integer is returned.
5705 | Otherwise, if the conversion overflows, the largest integer with the same
5706 | sign as `a' is returned.
5707 *----------------------------------------------------------------------------*/
5709 int64_t floatx80_to_int64_round_to_zero(floatx80 a
, float_status
*status
)
5712 int32_t aExp
, shiftCount
;
5716 if (floatx80_invalid_encoding(a
)) {
5717 float_raise(float_flag_invalid
, status
);
5720 aSig
= extractFloatx80Frac( a
);
5721 aExp
= extractFloatx80Exp( a
);
5722 aSign
= extractFloatx80Sign( a
);
5723 shiftCount
= aExp
- 0x403E;
5724 if ( 0 <= shiftCount
) {
5725 aSig
&= UINT64_C(0x7FFFFFFFFFFFFFFF);
5726 if ( ( a
.high
!= 0xC03E ) || aSig
) {
5727 float_raise(float_flag_invalid
, status
);
5728 if ( ! aSign
|| ( ( aExp
== 0x7FFF ) && aSig
) ) {
5734 else if ( aExp
< 0x3FFF ) {
5736 float_raise(float_flag_inexact
, status
);
5740 z
= aSig
>>( - shiftCount
);
5741 if ( (uint64_t) ( aSig
<<( shiftCount
& 63 ) ) ) {
5742 float_raise(float_flag_inexact
, status
);
5744 if ( aSign
) z
= - z
;
5749 /*----------------------------------------------------------------------------
5750 | Returns the result of converting the extended double-precision floating-
5751 | point value `a' to the single-precision floating-point format. The
5752 | conversion is performed according to the IEC/IEEE Standard for Binary
5753 | Floating-Point Arithmetic.
5754 *----------------------------------------------------------------------------*/
5756 float32
floatx80_to_float32(floatx80 a
, float_status
*status
)
5762 if (floatx80_invalid_encoding(a
)) {
5763 float_raise(float_flag_invalid
, status
);
5764 return float32_default_nan(status
);
5766 aSig
= extractFloatx80Frac( a
);
5767 aExp
= extractFloatx80Exp( a
);
5768 aSign
= extractFloatx80Sign( a
);
5769 if ( aExp
== 0x7FFF ) {
5770 if ( (uint64_t) ( aSig
<<1 ) ) {
5771 float32 res
= commonNaNToFloat32(floatx80ToCommonNaN(a
, status
),
5773 return float32_silence_nan(res
, status
);
5775 return packFloat32( aSign
, 0xFF, 0 );
5777 shift64RightJamming( aSig
, 33, &aSig
);
5778 if ( aExp
|| aSig
) aExp
-= 0x3F81;
5779 return roundAndPackFloat32(aSign
, aExp
, aSig
, status
);
5783 /*----------------------------------------------------------------------------
5784 | Returns the result of converting the extended double-precision floating-
5785 | point value `a' to the double-precision floating-point format. The
5786 | conversion is performed according to the IEC/IEEE Standard for Binary
5787 | Floating-Point Arithmetic.
5788 *----------------------------------------------------------------------------*/
5790 float64
floatx80_to_float64(floatx80 a
, float_status
*status
)
5794 uint64_t aSig
, zSig
;
5796 if (floatx80_invalid_encoding(a
)) {
5797 float_raise(float_flag_invalid
, status
);
5798 return float64_default_nan(status
);
5800 aSig
= extractFloatx80Frac( a
);
5801 aExp
= extractFloatx80Exp( a
);
5802 aSign
= extractFloatx80Sign( a
);
5803 if ( aExp
== 0x7FFF ) {
5804 if ( (uint64_t) ( aSig
<<1 ) ) {
5805 float64 res
= commonNaNToFloat64(floatx80ToCommonNaN(a
, status
),
5807 return float64_silence_nan(res
, status
);
5809 return packFloat64( aSign
, 0x7FF, 0 );
5811 shift64RightJamming( aSig
, 1, &zSig
);
5812 if ( aExp
|| aSig
) aExp
-= 0x3C01;
5813 return roundAndPackFloat64(aSign
, aExp
, zSig
, status
);
5817 /*----------------------------------------------------------------------------
5818 | Returns the result of converting the extended double-precision floating-
5819 | point value `a' to the quadruple-precision floating-point format. The
5820 | conversion is performed according to the IEC/IEEE Standard for Binary
5821 | Floating-Point Arithmetic.
5822 *----------------------------------------------------------------------------*/
5824 float128
floatx80_to_float128(floatx80 a
, float_status
*status
)
5828 uint64_t aSig
, zSig0
, zSig1
;
5830 if (floatx80_invalid_encoding(a
)) {
5831 float_raise(float_flag_invalid
, status
);
5832 return float128_default_nan(status
);
5834 aSig
= extractFloatx80Frac( a
);
5835 aExp
= extractFloatx80Exp( a
);
5836 aSign
= extractFloatx80Sign( a
);
5837 if ( ( aExp
== 0x7FFF ) && (uint64_t) ( aSig
<<1 ) ) {
5838 float128 res
= commonNaNToFloat128(floatx80ToCommonNaN(a
, status
),
5840 return float128_silence_nan(res
, status
);
5842 shift128Right( aSig
<<1, 0, 16, &zSig0
, &zSig1
);
5843 return packFloat128( aSign
, aExp
, zSig0
, zSig1
);
5847 /*----------------------------------------------------------------------------
5848 | Rounds the extended double-precision floating-point value `a'
5849 | to the precision provided by floatx80_rounding_precision and returns the
5850 | result as an extended double-precision floating-point value.
5851 | The operation is performed according to the IEC/IEEE Standard for Binary
5852 | Floating-Point Arithmetic.
5853 *----------------------------------------------------------------------------*/
5855 floatx80
floatx80_round(floatx80 a
, float_status
*status
)
5857 return roundAndPackFloatx80(status
->floatx80_rounding_precision
,
5858 extractFloatx80Sign(a
),
5859 extractFloatx80Exp(a
),
5860 extractFloatx80Frac(a
), 0, status
);
5863 /*----------------------------------------------------------------------------
5864 | Rounds the extended double-precision floating-point value `a' to an integer,
5865 | and returns the result as an extended quadruple-precision floating-point
5866 | value. The operation is performed according to the IEC/IEEE Standard for
5867 | Binary Floating-Point Arithmetic.
5868 *----------------------------------------------------------------------------*/
5870 floatx80
floatx80_round_to_int(floatx80 a
, float_status
*status
)
5874 uint64_t lastBitMask
, roundBitsMask
;
5877 if (floatx80_invalid_encoding(a
)) {
5878 float_raise(float_flag_invalid
, status
);
5879 return floatx80_default_nan(status
);
5881 aExp
= extractFloatx80Exp( a
);
5882 if ( 0x403E <= aExp
) {
5883 if ( ( aExp
== 0x7FFF ) && (uint64_t) ( extractFloatx80Frac( a
)<<1 ) ) {
5884 return propagateFloatx80NaN(a
, a
, status
);
5888 if ( aExp
< 0x3FFF ) {
5890 && ( (uint64_t) ( extractFloatx80Frac( a
) ) == 0 ) ) {
5893 float_raise(float_flag_inexact
, status
);
5894 aSign
= extractFloatx80Sign( a
);
5895 switch (status
->float_rounding_mode
) {
5896 case float_round_nearest_even
:
5897 if ( ( aExp
== 0x3FFE ) && (uint64_t) ( extractFloatx80Frac( a
)<<1 )
5900 packFloatx80( aSign
, 0x3FFF, UINT64_C(0x8000000000000000));
5903 case float_round_ties_away
:
5904 if (aExp
== 0x3FFE) {
5905 return packFloatx80(aSign
, 0x3FFF, UINT64_C(0x8000000000000000));
5908 case float_round_down
:
5911 packFloatx80( 1, 0x3FFF, UINT64_C(0x8000000000000000))
5912 : packFloatx80( 0, 0, 0 );
5913 case float_round_up
:
5915 aSign
? packFloatx80( 1, 0, 0 )
5916 : packFloatx80( 0, 0x3FFF, UINT64_C(0x8000000000000000));
5918 case float_round_to_zero
:
5921 g_assert_not_reached();
5923 return packFloatx80( aSign
, 0, 0 );
5926 lastBitMask
<<= 0x403E - aExp
;
5927 roundBitsMask
= lastBitMask
- 1;
5929 switch (status
->float_rounding_mode
) {
5930 case float_round_nearest_even
:
5931 z
.low
+= lastBitMask
>>1;
5932 if ((z
.low
& roundBitsMask
) == 0) {
5933 z
.low
&= ~lastBitMask
;
5936 case float_round_ties_away
:
5937 z
.low
+= lastBitMask
>> 1;
5939 case float_round_to_zero
:
5941 case float_round_up
:
5942 if (!extractFloatx80Sign(z
)) {
5943 z
.low
+= roundBitsMask
;
5946 case float_round_down
:
5947 if (extractFloatx80Sign(z
)) {
5948 z
.low
+= roundBitsMask
;
5954 z
.low
&= ~ roundBitsMask
;
5957 z
.low
= UINT64_C(0x8000000000000000);
5959 if (z
.low
!= a
.low
) {
5960 float_raise(float_flag_inexact
, status
);
5966 /*----------------------------------------------------------------------------
5967 | Returns the result of adding the absolute values of the extended double-
5968 | precision floating-point values `a' and `b'. If `zSign' is 1, the sum is
5969 | negated before being returned. `zSign' is ignored if the result is a NaN.
5970 | The addition is performed according to the IEC/IEEE Standard for Binary
5971 | Floating-Point Arithmetic.
5972 *----------------------------------------------------------------------------*/
5974 static floatx80
addFloatx80Sigs(floatx80 a
, floatx80 b
, bool zSign
,
5975 float_status
*status
)
5977 int32_t aExp
, bExp
, zExp
;
5978 uint64_t aSig
, bSig
, zSig0
, zSig1
;
5981 aSig
= extractFloatx80Frac( a
);
5982 aExp
= extractFloatx80Exp( a
);
5983 bSig
= extractFloatx80Frac( b
);
5984 bExp
= extractFloatx80Exp( b
);
5985 expDiff
= aExp
- bExp
;
5986 if ( 0 < expDiff
) {
5987 if ( aExp
== 0x7FFF ) {
5988 if ((uint64_t)(aSig
<< 1)) {
5989 return propagateFloatx80NaN(a
, b
, status
);
5993 if ( bExp
== 0 ) --expDiff
;
5994 shift64ExtraRightJamming( bSig
, 0, expDiff
, &bSig
, &zSig1
);
5997 else if ( expDiff
< 0 ) {
5998 if ( bExp
== 0x7FFF ) {
5999 if ((uint64_t)(bSig
<< 1)) {
6000 return propagateFloatx80NaN(a
, b
, status
);
6002 return packFloatx80(zSign
,
6003 floatx80_infinity_high
,
6004 floatx80_infinity_low
);
6006 if ( aExp
== 0 ) ++expDiff
;
6007 shift64ExtraRightJamming( aSig
, 0, - expDiff
, &aSig
, &zSig1
);
6011 if ( aExp
== 0x7FFF ) {
6012 if ( (uint64_t) ( ( aSig
| bSig
)<<1 ) ) {
6013 return propagateFloatx80NaN(a
, b
, status
);
6018 zSig0
= aSig
+ bSig
;
6020 if ((aSig
| bSig
) & UINT64_C(0x8000000000000000) && zSig0
< aSig
) {
6021 /* At least one of the values is a pseudo-denormal,
6022 * and there is a carry out of the result. */
6027 return packFloatx80(zSign
, 0, 0);
6029 normalizeFloatx80Subnormal( zSig0
, &zExp
, &zSig0
);
6035 zSig0
= aSig
+ bSig
;
6036 if ( (int64_t) zSig0
< 0 ) goto roundAndPack
;
6038 shift64ExtraRightJamming( zSig0
, zSig1
, 1, &zSig0
, &zSig1
);
6039 zSig0
|= UINT64_C(0x8000000000000000);
6042 return roundAndPackFloatx80(status
->floatx80_rounding_precision
,
6043 zSign
, zExp
, zSig0
, zSig1
, status
);
6046 /*----------------------------------------------------------------------------
6047 | Returns the result of subtracting the absolute values of the extended
6048 | double-precision floating-point values `a' and `b'. If `zSign' is 1, the
6049 | difference is negated before being returned. `zSign' is ignored if the
6050 | result is a NaN. The subtraction is performed according to the IEC/IEEE
6051 | Standard for Binary Floating-Point Arithmetic.
6052 *----------------------------------------------------------------------------*/
6054 static floatx80
subFloatx80Sigs(floatx80 a
, floatx80 b
, bool zSign
,
6055 float_status
*status
)
6057 int32_t aExp
, bExp
, zExp
;
6058 uint64_t aSig
, bSig
, zSig0
, zSig1
;
6061 aSig
= extractFloatx80Frac( a
);
6062 aExp
= extractFloatx80Exp( a
);
6063 bSig
= extractFloatx80Frac( b
);
6064 bExp
= extractFloatx80Exp( b
);
6065 expDiff
= aExp
- bExp
;
6066 if ( 0 < expDiff
) goto aExpBigger
;
6067 if ( expDiff
< 0 ) goto bExpBigger
;
6068 if ( aExp
== 0x7FFF ) {
6069 if ( (uint64_t) ( ( aSig
| bSig
)<<1 ) ) {
6070 return propagateFloatx80NaN(a
, b
, status
);
6072 float_raise(float_flag_invalid
, status
);
6073 return floatx80_default_nan(status
);
6080 if ( bSig
< aSig
) goto aBigger
;
6081 if ( aSig
< bSig
) goto bBigger
;
6082 return packFloatx80(status
->float_rounding_mode
== float_round_down
, 0, 0);
6084 if ( bExp
== 0x7FFF ) {
6085 if ((uint64_t)(bSig
<< 1)) {
6086 return propagateFloatx80NaN(a
, b
, status
);
6088 return packFloatx80(zSign
^ 1, floatx80_infinity_high
,
6089 floatx80_infinity_low
);
6091 if ( aExp
== 0 ) ++expDiff
;
6092 shift128RightJamming( aSig
, 0, - expDiff
, &aSig
, &zSig1
);
6094 sub128( bSig
, 0, aSig
, zSig1
, &zSig0
, &zSig1
);
6097 goto normalizeRoundAndPack
;
6099 if ( aExp
== 0x7FFF ) {
6100 if ((uint64_t)(aSig
<< 1)) {
6101 return propagateFloatx80NaN(a
, b
, status
);
6105 if ( bExp
== 0 ) --expDiff
;
6106 shift128RightJamming( bSig
, 0, expDiff
, &bSig
, &zSig1
);
6108 sub128( aSig
, 0, bSig
, zSig1
, &zSig0
, &zSig1
);
6110 normalizeRoundAndPack
:
6111 return normalizeRoundAndPackFloatx80(status
->floatx80_rounding_precision
,
6112 zSign
, zExp
, zSig0
, zSig1
, status
);
6115 /*----------------------------------------------------------------------------
6116 | Returns the result of adding the extended double-precision floating-point
6117 | values `a' and `b'. The operation is performed according to the IEC/IEEE
6118 | Standard for Binary Floating-Point Arithmetic.
6119 *----------------------------------------------------------------------------*/
6121 floatx80
floatx80_add(floatx80 a
, floatx80 b
, float_status
*status
)
6125 if (floatx80_invalid_encoding(a
) || floatx80_invalid_encoding(b
)) {
6126 float_raise(float_flag_invalid
, status
);
6127 return floatx80_default_nan(status
);
6129 aSign
= extractFloatx80Sign( a
);
6130 bSign
= extractFloatx80Sign( b
);
6131 if ( aSign
== bSign
) {
6132 return addFloatx80Sigs(a
, b
, aSign
, status
);
6135 return subFloatx80Sigs(a
, b
, aSign
, status
);
6140 /*----------------------------------------------------------------------------
6141 | Returns the result of subtracting the extended double-precision floating-
6142 | point values `a' and `b'. The operation is performed according to the
6143 | IEC/IEEE Standard for Binary Floating-Point Arithmetic.
6144 *----------------------------------------------------------------------------*/
6146 floatx80
floatx80_sub(floatx80 a
, floatx80 b
, float_status
*status
)
6150 if (floatx80_invalid_encoding(a
) || floatx80_invalid_encoding(b
)) {
6151 float_raise(float_flag_invalid
, status
);
6152 return floatx80_default_nan(status
);
6154 aSign
= extractFloatx80Sign( a
);
6155 bSign
= extractFloatx80Sign( b
);
6156 if ( aSign
== bSign
) {
6157 return subFloatx80Sigs(a
, b
, aSign
, status
);
6160 return addFloatx80Sigs(a
, b
, aSign
, status
);
6165 /*----------------------------------------------------------------------------
6166 | Returns the result of multiplying the extended double-precision floating-
6167 | point values `a' and `b'. The operation is performed according to the
6168 | IEC/IEEE Standard for Binary Floating-Point Arithmetic.
6169 *----------------------------------------------------------------------------*/
6171 floatx80
floatx80_mul(floatx80 a
, floatx80 b
, float_status
*status
)
6173 bool aSign
, bSign
, zSign
;
6174 int32_t aExp
, bExp
, zExp
;
6175 uint64_t aSig
, bSig
, zSig0
, zSig1
;
6177 if (floatx80_invalid_encoding(a
) || floatx80_invalid_encoding(b
)) {
6178 float_raise(float_flag_invalid
, status
);
6179 return floatx80_default_nan(status
);
6181 aSig
= extractFloatx80Frac( a
);
6182 aExp
= extractFloatx80Exp( a
);
6183 aSign
= extractFloatx80Sign( a
);
6184 bSig
= extractFloatx80Frac( b
);
6185 bExp
= extractFloatx80Exp( b
);
6186 bSign
= extractFloatx80Sign( b
);
6187 zSign
= aSign
^ bSign
;
6188 if ( aExp
== 0x7FFF ) {
6189 if ( (uint64_t) ( aSig
<<1 )
6190 || ( ( bExp
== 0x7FFF ) && (uint64_t) ( bSig
<<1 ) ) ) {
6191 return propagateFloatx80NaN(a
, b
, status
);
6193 if ( ( bExp
| bSig
) == 0 ) goto invalid
;
6194 return packFloatx80(zSign
, floatx80_infinity_high
,
6195 floatx80_infinity_low
);
6197 if ( bExp
== 0x7FFF ) {
6198 if ((uint64_t)(bSig
<< 1)) {
6199 return propagateFloatx80NaN(a
, b
, status
);
6201 if ( ( aExp
| aSig
) == 0 ) {
6203 float_raise(float_flag_invalid
, status
);
6204 return floatx80_default_nan(status
);
6206 return packFloatx80(zSign
, floatx80_infinity_high
,
6207 floatx80_infinity_low
);
6210 if ( aSig
== 0 ) return packFloatx80( zSign
, 0, 0 );
6211 normalizeFloatx80Subnormal( aSig
, &aExp
, &aSig
);
6214 if ( bSig
== 0 ) return packFloatx80( zSign
, 0, 0 );
6215 normalizeFloatx80Subnormal( bSig
, &bExp
, &bSig
);
6217 zExp
= aExp
+ bExp
- 0x3FFE;
6218 mul64To128( aSig
, bSig
, &zSig0
, &zSig1
);
6219 if ( 0 < (int64_t) zSig0
) {
6220 shortShift128Left( zSig0
, zSig1
, 1, &zSig0
, &zSig1
);
6223 return roundAndPackFloatx80(status
->floatx80_rounding_precision
,
6224 zSign
, zExp
, zSig0
, zSig1
, status
);
6227 /*----------------------------------------------------------------------------
6228 | Returns the result of dividing the extended double-precision floating-point
6229 | value `a' by the corresponding value `b'. The operation is performed
6230 | according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
6231 *----------------------------------------------------------------------------*/
6233 floatx80
floatx80_div(floatx80 a
, floatx80 b
, float_status
*status
)
6235 bool aSign
, bSign
, zSign
;
6236 int32_t aExp
, bExp
, zExp
;
6237 uint64_t aSig
, bSig
, zSig0
, zSig1
;
6238 uint64_t rem0
, rem1
, rem2
, term0
, term1
, term2
;
6240 if (floatx80_invalid_encoding(a
) || floatx80_invalid_encoding(b
)) {
6241 float_raise(float_flag_invalid
, status
);
6242 return floatx80_default_nan(status
);
6244 aSig
= extractFloatx80Frac( a
);
6245 aExp
= extractFloatx80Exp( a
);
6246 aSign
= extractFloatx80Sign( a
);
6247 bSig
= extractFloatx80Frac( b
);
6248 bExp
= extractFloatx80Exp( b
);
6249 bSign
= extractFloatx80Sign( b
);
6250 zSign
= aSign
^ bSign
;
6251 if ( aExp
== 0x7FFF ) {
6252 if ((uint64_t)(aSig
<< 1)) {
6253 return propagateFloatx80NaN(a
, b
, status
);
6255 if ( bExp
== 0x7FFF ) {
6256 if ((uint64_t)(bSig
<< 1)) {
6257 return propagateFloatx80NaN(a
, b
, status
);
6261 return packFloatx80(zSign
, floatx80_infinity_high
,
6262 floatx80_infinity_low
);
6264 if ( bExp
== 0x7FFF ) {
6265 if ((uint64_t)(bSig
<< 1)) {
6266 return propagateFloatx80NaN(a
, b
, status
);
6268 return packFloatx80( zSign
, 0, 0 );
6272 if ( ( aExp
| aSig
) == 0 ) {
6274 float_raise(float_flag_invalid
, status
);
6275 return floatx80_default_nan(status
);
6277 float_raise(float_flag_divbyzero
, status
);
6278 return packFloatx80(zSign
, floatx80_infinity_high
,
6279 floatx80_infinity_low
);
6281 normalizeFloatx80Subnormal( bSig
, &bExp
, &bSig
);
6284 if ( aSig
== 0 ) return packFloatx80( zSign
, 0, 0 );
6285 normalizeFloatx80Subnormal( aSig
, &aExp
, &aSig
);
6287 zExp
= aExp
- bExp
+ 0x3FFE;
6289 if ( bSig
<= aSig
) {
6290 shift128Right( aSig
, 0, 1, &aSig
, &rem1
);
6293 zSig0
= estimateDiv128To64( aSig
, rem1
, bSig
);
6294 mul64To128( bSig
, zSig0
, &term0
, &term1
);
6295 sub128( aSig
, rem1
, term0
, term1
, &rem0
, &rem1
);
6296 while ( (int64_t) rem0
< 0 ) {
6298 add128( rem0
, rem1
, 0, bSig
, &rem0
, &rem1
);
6300 zSig1
= estimateDiv128To64( rem1
, 0, bSig
);
6301 if ( (uint64_t) ( zSig1
<<1 ) <= 8 ) {
6302 mul64To128( bSig
, zSig1
, &term1
, &term2
);
6303 sub128( rem1
, 0, term1
, term2
, &rem1
, &rem2
);
6304 while ( (int64_t) rem1
< 0 ) {
6306 add128( rem1
, rem2
, 0, bSig
, &rem1
, &rem2
);
6308 zSig1
|= ( ( rem1
| rem2
) != 0 );
6310 return roundAndPackFloatx80(status
->floatx80_rounding_precision
,
6311 zSign
, zExp
, zSig0
, zSig1
, status
);
6314 /*----------------------------------------------------------------------------
6315 | Returns the remainder of the extended double-precision floating-point value
6316 | `a' with respect to the corresponding value `b'. The operation is performed
6317 | according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic,
6318 | if 'mod' is false; if 'mod' is true, return the remainder based on truncating
6319 | the quotient toward zero instead. '*quotient' is set to the low 64 bits of
6320 | the absolute value of the integer quotient.
6321 *----------------------------------------------------------------------------*/
6323 floatx80
floatx80_modrem(floatx80 a
, floatx80 b
, bool mod
, uint64_t *quotient
,
6324 float_status
*status
)
6327 int32_t aExp
, bExp
, expDiff
, aExpOrig
;
6328 uint64_t aSig0
, aSig1
, bSig
;
6329 uint64_t q
, term0
, term1
, alternateASig0
, alternateASig1
;
6332 if (floatx80_invalid_encoding(a
) || floatx80_invalid_encoding(b
)) {
6333 float_raise(float_flag_invalid
, status
);
6334 return floatx80_default_nan(status
);
6336 aSig0
= extractFloatx80Frac( a
);
6337 aExpOrig
= aExp
= extractFloatx80Exp( a
);
6338 aSign
= extractFloatx80Sign( a
);
6339 bSig
= extractFloatx80Frac( b
);
6340 bExp
= extractFloatx80Exp( b
);
6341 if ( aExp
== 0x7FFF ) {
6342 if ( (uint64_t) ( aSig0
<<1 )
6343 || ( ( bExp
== 0x7FFF ) && (uint64_t) ( bSig
<<1 ) ) ) {
6344 return propagateFloatx80NaN(a
, b
, status
);
6348 if ( bExp
== 0x7FFF ) {
6349 if ((uint64_t)(bSig
<< 1)) {
6350 return propagateFloatx80NaN(a
, b
, status
);
6352 if (aExp
== 0 && aSig0
>> 63) {
6354 * Pseudo-denormal argument must be returned in normalized
6357 return packFloatx80(aSign
, 1, aSig0
);
6364 float_raise(float_flag_invalid
, status
);
6365 return floatx80_default_nan(status
);
6367 normalizeFloatx80Subnormal( bSig
, &bExp
, &bSig
);
6370 if ( aSig0
== 0 ) return a
;
6371 normalizeFloatx80Subnormal( aSig0
, &aExp
, &aSig0
);
6374 expDiff
= aExp
- bExp
;
6376 if ( expDiff
< 0 ) {
6377 if ( mod
|| expDiff
< -1 ) {
6378 if (aExp
== 1 && aExpOrig
== 0) {
6380 * Pseudo-denormal argument must be returned in
6383 return packFloatx80(aSign
, aExp
, aSig0
);
6387 shift128Right( aSig0
, 0, 1, &aSig0
, &aSig1
);
6390 *quotient
= q
= ( bSig
<= aSig0
);
6391 if ( q
) aSig0
-= bSig
;
6393 while ( 0 < expDiff
) {
6394 q
= estimateDiv128To64( aSig0
, aSig1
, bSig
);
6395 q
= ( 2 < q
) ? q
- 2 : 0;
6396 mul64To128( bSig
, q
, &term0
, &term1
);
6397 sub128( aSig0
, aSig1
, term0
, term1
, &aSig0
, &aSig1
);
6398 shortShift128Left( aSig0
, aSig1
, 62, &aSig0
, &aSig1
);
6404 if ( 0 < expDiff
) {
6405 q
= estimateDiv128To64( aSig0
, aSig1
, bSig
);
6406 q
= ( 2 < q
) ? q
- 2 : 0;
6408 mul64To128( bSig
, q
<<( 64 - expDiff
), &term0
, &term1
);
6409 sub128( aSig0
, aSig1
, term0
, term1
, &aSig0
, &aSig1
);
6410 shortShift128Left( 0, bSig
, 64 - expDiff
, &term0
, &term1
);
6411 while ( le128( term0
, term1
, aSig0
, aSig1
) ) {
6413 sub128( aSig0
, aSig1
, term0
, term1
, &aSig0
, &aSig1
);
6416 *quotient
<<= expDiff
;
6427 sub128( term0
, term1
, aSig0
, aSig1
, &alternateASig0
, &alternateASig1
);
6428 if ( lt128( alternateASig0
, alternateASig1
, aSig0
, aSig1
)
6429 || ( eq128( alternateASig0
, alternateASig1
, aSig0
, aSig1
)
6432 aSig0
= alternateASig0
;
6433 aSig1
= alternateASig1
;
6439 normalizeRoundAndPackFloatx80(
6440 80, zSign
, bExp
+ expDiff
, aSig0
, aSig1
, status
);
6444 /*----------------------------------------------------------------------------
6445 | Returns the remainder of the extended double-precision floating-point value
6446 | `a' with respect to the corresponding value `b'. The operation is performed
6447 | according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
6448 *----------------------------------------------------------------------------*/
6450 floatx80
floatx80_rem(floatx80 a
, floatx80 b
, float_status
*status
)
6453 return floatx80_modrem(a
, b
, false, "ient
, status
);
6456 /*----------------------------------------------------------------------------
6457 | Returns the remainder of the extended double-precision floating-point value
6458 | `a' with respect to the corresponding value `b', with the quotient truncated
6460 *----------------------------------------------------------------------------*/
6462 floatx80
floatx80_mod(floatx80 a
, floatx80 b
, float_status
*status
)
6465 return floatx80_modrem(a
, b
, true, "ient
, status
);
6468 /*----------------------------------------------------------------------------
6469 | Returns the square root of the extended double-precision floating-point
6470 | value `a'. The operation is performed according to the IEC/IEEE Standard
6471 | for Binary Floating-Point Arithmetic.
6472 *----------------------------------------------------------------------------*/
6474 floatx80
floatx80_sqrt(floatx80 a
, float_status
*status
)
6478 uint64_t aSig0
, aSig1
, zSig0
, zSig1
, doubleZSig0
;
6479 uint64_t rem0
, rem1
, rem2
, rem3
, term0
, term1
, term2
, term3
;
6481 if (floatx80_invalid_encoding(a
)) {
6482 float_raise(float_flag_invalid
, status
);
6483 return floatx80_default_nan(status
);
6485 aSig0
= extractFloatx80Frac( a
);
6486 aExp
= extractFloatx80Exp( a
);
6487 aSign
= extractFloatx80Sign( a
);
6488 if ( aExp
== 0x7FFF ) {
6489 if ((uint64_t)(aSig0
<< 1)) {
6490 return propagateFloatx80NaN(a
, a
, status
);
6492 if ( ! aSign
) return a
;
6496 if ( ( aExp
| aSig0
) == 0 ) return a
;
6498 float_raise(float_flag_invalid
, status
);
6499 return floatx80_default_nan(status
);
6502 if ( aSig0
== 0 ) return packFloatx80( 0, 0, 0 );
6503 normalizeFloatx80Subnormal( aSig0
, &aExp
, &aSig0
);
6505 zExp
= ( ( aExp
- 0x3FFF )>>1 ) + 0x3FFF;
6506 zSig0
= estimateSqrt32( aExp
, aSig0
>>32 );
6507 shift128Right( aSig0
, 0, 2 + ( aExp
& 1 ), &aSig0
, &aSig1
);
6508 zSig0
= estimateDiv128To64( aSig0
, aSig1
, zSig0
<<32 ) + ( zSig0
<<30 );
6509 doubleZSig0
= zSig0
<<1;
6510 mul64To128( zSig0
, zSig0
, &term0
, &term1
);
6511 sub128( aSig0
, aSig1
, term0
, term1
, &rem0
, &rem1
);
6512 while ( (int64_t) rem0
< 0 ) {
6515 add128( rem0
, rem1
, zSig0
>>63, doubleZSig0
| 1, &rem0
, &rem1
);
6517 zSig1
= estimateDiv128To64( rem1
, 0, doubleZSig0
);
6518 if ( ( zSig1
& UINT64_C(0x3FFFFFFFFFFFFFFF) ) <= 5 ) {
6519 if ( zSig1
== 0 ) zSig1
= 1;
6520 mul64To128( doubleZSig0
, zSig1
, &term1
, &term2
);
6521 sub128( rem1
, 0, term1
, term2
, &rem1
, &rem2
);
6522 mul64To128( zSig1
, zSig1
, &term2
, &term3
);
6523 sub192( rem1
, rem2
, 0, 0, term2
, term3
, &rem1
, &rem2
, &rem3
);
6524 while ( (int64_t) rem1
< 0 ) {
6526 shortShift128Left( 0, zSig1
, 1, &term2
, &term3
);
6528 term2
|= doubleZSig0
;
6529 add192( rem1
, rem2
, rem3
, 0, term2
, term3
, &rem1
, &rem2
, &rem3
);
6531 zSig1
|= ( ( rem1
| rem2
| rem3
) != 0 );
6533 shortShift128Left( 0, zSig1
, 1, &zSig0
, &zSig1
);
6534 zSig0
|= doubleZSig0
;
6535 return roundAndPackFloatx80(status
->floatx80_rounding_precision
,
6536 0, zExp
, zSig0
, zSig1
, status
);
6539 /*----------------------------------------------------------------------------
6540 | Returns the result of converting the quadruple-precision floating-point value
6541 | `a' to the 64-bit unsigned integer format. The conversion is
6542 | performed according to the IEC/IEEE Standard for Binary Floating-Point
6543 | Arithmetic---which means in particular that the conversion is rounded
6544 | according to the current rounding mode. If `a' is a NaN, the largest
6545 | positive integer is returned. If the conversion overflows, the
6546 | largest unsigned integer is returned. If 'a' is negative, the value is
6547 | rounded and zero is returned; negative values that do not round to zero
6548 | will raise the inexact exception.
6549 *----------------------------------------------------------------------------*/
6551 uint64_t float128_to_uint64(float128 a
, float_status
*status
)
6556 uint64_t aSig0
, aSig1
;
6558 aSig0
= extractFloat128Frac0(a
);
6559 aSig1
= extractFloat128Frac1(a
);
6560 aExp
= extractFloat128Exp(a
);
6561 aSign
= extractFloat128Sign(a
);
6562 if (aSign
&& (aExp
> 0x3FFE)) {
6563 float_raise(float_flag_invalid
, status
);
6564 if (float128_is_any_nan(a
)) {
6571 aSig0
|= UINT64_C(0x0001000000000000);
6573 shiftCount
= 0x402F - aExp
;
6574 if (shiftCount
<= 0) {
6575 if (0x403E < aExp
) {
6576 float_raise(float_flag_invalid
, status
);
6579 shortShift128Left(aSig0
, aSig1
, -shiftCount
, &aSig0
, &aSig1
);
6581 shift64ExtraRightJamming(aSig0
, aSig1
, shiftCount
, &aSig0
, &aSig1
);
6583 return roundAndPackUint64(aSign
, aSig0
, aSig1
, status
);
6586 uint64_t float128_to_uint64_round_to_zero(float128 a
, float_status
*status
)
6589 signed char current_rounding_mode
= status
->float_rounding_mode
;
6591 set_float_rounding_mode(float_round_to_zero
, status
);
6592 v
= float128_to_uint64(a
, status
);
6593 set_float_rounding_mode(current_rounding_mode
, status
);
6598 /*----------------------------------------------------------------------------
6599 | Returns the result of converting the quadruple-precision floating-point
6600 | value `a' to the 32-bit unsigned integer format. The conversion
6601 | is performed according to the IEC/IEEE Standard for Binary Floating-Point
6602 | Arithmetic except that the conversion is always rounded toward zero.
6603 | If `a' is a NaN, the largest positive integer is returned. Otherwise,
6604 | if the conversion overflows, the largest unsigned integer is returned.
6605 | If 'a' is negative, the value is rounded and zero is returned; negative
6606 | values that do not round to zero will raise the inexact exception.
6607 *----------------------------------------------------------------------------*/
6609 uint32_t float128_to_uint32_round_to_zero(float128 a
, float_status
*status
)
6613 int old_exc_flags
= get_float_exception_flags(status
);
6615 v
= float128_to_uint64_round_to_zero(a
, status
);
6616 if (v
> 0xffffffff) {
6621 set_float_exception_flags(old_exc_flags
, status
);
6622 float_raise(float_flag_invalid
, status
);
6626 /*----------------------------------------------------------------------------
6627 | Returns the result of converting the quadruple-precision floating-point value
6628 | `a' to the 32-bit unsigned integer format. The conversion is
6629 | performed according to the IEC/IEEE Standard for Binary Floating-Point
6630 | Arithmetic---which means in particular that the conversion is rounded
6631 | according to the current rounding mode. If `a' is a NaN, the largest
6632 | positive integer is returned. If the conversion overflows, the
6633 | largest unsigned integer is returned. If 'a' is negative, the value is
6634 | rounded and zero is returned; negative values that do not round to zero
6635 | will raise the inexact exception.
6636 *----------------------------------------------------------------------------*/
6638 uint32_t float128_to_uint32(float128 a
, float_status
*status
)
6642 int old_exc_flags
= get_float_exception_flags(status
);
6644 v
= float128_to_uint64(a
, status
);
6645 if (v
> 0xffffffff) {
6650 set_float_exception_flags(old_exc_flags
, status
);
6651 float_raise(float_flag_invalid
, status
);
6655 /*----------------------------------------------------------------------------
6656 | Returns the result of converting the quadruple-precision floating-point
6657 | value `a' to the extended double-precision floating-point format. The
6658 | conversion is performed according to the IEC/IEEE Standard for Binary
6659 | Floating-Point Arithmetic.
6660 *----------------------------------------------------------------------------*/
6662 floatx80
float128_to_floatx80(float128 a
, float_status
*status
)
6666 uint64_t aSig0
, aSig1
;
6668 aSig1
= extractFloat128Frac1( a
);
6669 aSig0
= extractFloat128Frac0( a
);
6670 aExp
= extractFloat128Exp( a
);
6671 aSign
= extractFloat128Sign( a
);
6672 if ( aExp
== 0x7FFF ) {
6673 if ( aSig0
| aSig1
) {
6674 floatx80 res
= commonNaNToFloatx80(float128ToCommonNaN(a
, status
),
6676 return floatx80_silence_nan(res
, status
);
6678 return packFloatx80(aSign
, floatx80_infinity_high
,
6679 floatx80_infinity_low
);
6682 if ( ( aSig0
| aSig1
) == 0 ) return packFloatx80( aSign
, 0, 0 );
6683 normalizeFloat128Subnormal( aSig0
, aSig1
, &aExp
, &aSig0
, &aSig1
);
6686 aSig0
|= UINT64_C(0x0001000000000000);
6688 shortShift128Left( aSig0
, aSig1
, 15, &aSig0
, &aSig1
);
6689 return roundAndPackFloatx80(80, aSign
, aExp
, aSig0
, aSig1
, status
);
6693 /*----------------------------------------------------------------------------
6694 | Returns the remainder of the quadruple-precision floating-point value `a'
6695 | with respect to the corresponding value `b'. The operation is performed
6696 | according to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
6697 *----------------------------------------------------------------------------*/
6699 float128
float128_rem(float128 a
, float128 b
, float_status
*status
)
6702 int32_t aExp
, bExp
, expDiff
;
6703 uint64_t aSig0
, aSig1
, bSig0
, bSig1
, q
, term0
, term1
, term2
;
6704 uint64_t allZero
, alternateASig0
, alternateASig1
, sigMean1
;
6707 aSig1
= extractFloat128Frac1( a
);
6708 aSig0
= extractFloat128Frac0( a
);
6709 aExp
= extractFloat128Exp( a
);
6710 aSign
= extractFloat128Sign( a
);
6711 bSig1
= extractFloat128Frac1( b
);
6712 bSig0
= extractFloat128Frac0( b
);
6713 bExp
= extractFloat128Exp( b
);
6714 if ( aExp
== 0x7FFF ) {
6715 if ( ( aSig0
| aSig1
)
6716 || ( ( bExp
== 0x7FFF ) && ( bSig0
| bSig1
) ) ) {
6717 return propagateFloat128NaN(a
, b
, status
);
6721 if ( bExp
== 0x7FFF ) {
6722 if (bSig0
| bSig1
) {
6723 return propagateFloat128NaN(a
, b
, status
);
6728 if ( ( bSig0
| bSig1
) == 0 ) {
6730 float_raise(float_flag_invalid
, status
);
6731 return float128_default_nan(status
);
6733 normalizeFloat128Subnormal( bSig0
, bSig1
, &bExp
, &bSig0
, &bSig1
);
6736 if ( ( aSig0
| aSig1
) == 0 ) return a
;
6737 normalizeFloat128Subnormal( aSig0
, aSig1
, &aExp
, &aSig0
, &aSig1
);
6739 expDiff
= aExp
- bExp
;
6740 if ( expDiff
< -1 ) return a
;
6742 aSig0
| UINT64_C(0x0001000000000000),
6744 15 - ( expDiff
< 0 ),
6749 bSig0
| UINT64_C(0x0001000000000000), bSig1
, 15, &bSig0
, &bSig1
);
6750 q
= le128( bSig0
, bSig1
, aSig0
, aSig1
);
6751 if ( q
) sub128( aSig0
, aSig1
, bSig0
, bSig1
, &aSig0
, &aSig1
);
6753 while ( 0 < expDiff
) {
6754 q
= estimateDiv128To64( aSig0
, aSig1
, bSig0
);
6755 q
= ( 4 < q
) ? q
- 4 : 0;
6756 mul128By64To192( bSig0
, bSig1
, q
, &term0
, &term1
, &term2
);
6757 shortShift192Left( term0
, term1
, term2
, 61, &term1
, &term2
, &allZero
);
6758 shortShift128Left( aSig0
, aSig1
, 61, &aSig0
, &allZero
);
6759 sub128( aSig0
, 0, term1
, term2
, &aSig0
, &aSig1
);
6762 if ( -64 < expDiff
) {
6763 q
= estimateDiv128To64( aSig0
, aSig1
, bSig0
);
6764 q
= ( 4 < q
) ? q
- 4 : 0;
6766 shift128Right( bSig0
, bSig1
, 12, &bSig0
, &bSig1
);
6768 if ( expDiff
< 0 ) {
6769 shift128Right( aSig0
, aSig1
, - expDiff
, &aSig0
, &aSig1
);
6772 shortShift128Left( aSig0
, aSig1
, expDiff
, &aSig0
, &aSig1
);
6774 mul128By64To192( bSig0
, bSig1
, q
, &term0
, &term1
, &term2
);
6775 sub128( aSig0
, aSig1
, term1
, term2
, &aSig0
, &aSig1
);
6778 shift128Right( aSig0
, aSig1
, 12, &aSig0
, &aSig1
);
6779 shift128Right( bSig0
, bSig1
, 12, &bSig0
, &bSig1
);
6782 alternateASig0
= aSig0
;
6783 alternateASig1
= aSig1
;
6785 sub128( aSig0
, aSig1
, bSig0
, bSig1
, &aSig0
, &aSig1
);
6786 } while ( 0 <= (int64_t) aSig0
);
6788 aSig0
, aSig1
, alternateASig0
, alternateASig1
, (uint64_t *)&sigMean0
, &sigMean1
);
6789 if ( ( sigMean0
< 0 )
6790 || ( ( ( sigMean0
| sigMean1
) == 0 ) && ( q
& 1 ) ) ) {
6791 aSig0
= alternateASig0
;
6792 aSig1
= alternateASig1
;
6794 zSign
= ( (int64_t) aSig0
< 0 );
6795 if ( zSign
) sub128( 0, 0, aSig0
, aSig1
, &aSig0
, &aSig1
);
6796 return normalizeRoundAndPackFloat128(aSign
^ zSign
, bExp
- 4, aSig0
, aSig1
,
6800 /*----------------------------------------------------------------------------
6801 | Returns the square root of the quadruple-precision floating-point value `a'.
6802 | The operation is performed according to the IEC/IEEE Standard for Binary
6803 | Floating-Point Arithmetic.
6804 *----------------------------------------------------------------------------*/
6806 float128
float128_sqrt(float128 a
, float_status
*status
)
6810 uint64_t aSig0
, aSig1
, zSig0
, zSig1
, zSig2
, doubleZSig0
;
6811 uint64_t rem0
, rem1
, rem2
, rem3
, term0
, term1
, term2
, term3
;
6813 aSig1
= extractFloat128Frac1( a
);
6814 aSig0
= extractFloat128Frac0( a
);
6815 aExp
= extractFloat128Exp( a
);
6816 aSign
= extractFloat128Sign( a
);
6817 if ( aExp
== 0x7FFF ) {
6818 if (aSig0
| aSig1
) {
6819 return propagateFloat128NaN(a
, a
, status
);
6821 if ( ! aSign
) return a
;
6825 if ( ( aExp
| aSig0
| aSig1
) == 0 ) return a
;
6827 float_raise(float_flag_invalid
, status
);
6828 return float128_default_nan(status
);
6831 if ( ( aSig0
| aSig1
) == 0 ) return packFloat128( 0, 0, 0, 0 );
6832 normalizeFloat128Subnormal( aSig0
, aSig1
, &aExp
, &aSig0
, &aSig1
);
6834 zExp
= ( ( aExp
- 0x3FFF )>>1 ) + 0x3FFE;
6835 aSig0
|= UINT64_C(0x0001000000000000);
6836 zSig0
= estimateSqrt32( aExp
, aSig0
>>17 );
6837 shortShift128Left( aSig0
, aSig1
, 13 - ( aExp
& 1 ), &aSig0
, &aSig1
);
6838 zSig0
= estimateDiv128To64( aSig0
, aSig1
, zSig0
<<32 ) + ( zSig0
<<30 );
6839 doubleZSig0
= zSig0
<<1;
6840 mul64To128( zSig0
, zSig0
, &term0
, &term1
);
6841 sub128( aSig0
, aSig1
, term0
, term1
, &rem0
, &rem1
);
6842 while ( (int64_t) rem0
< 0 ) {
6845 add128( rem0
, rem1
, zSig0
>>63, doubleZSig0
| 1, &rem0
, &rem1
);
6847 zSig1
= estimateDiv128To64( rem1
, 0, doubleZSig0
);
6848 if ( ( zSig1
& 0x1FFF ) <= 5 ) {
6849 if ( zSig1
== 0 ) zSig1
= 1;
6850 mul64To128( doubleZSig0
, zSig1
, &term1
, &term2
);
6851 sub128( rem1
, 0, term1
, term2
, &rem1
, &rem2
);
6852 mul64To128( zSig1
, zSig1
, &term2
, &term3
);
6853 sub192( rem1
, rem2
, 0, 0, term2
, term3
, &rem1
, &rem2
, &rem3
);
6854 while ( (int64_t) rem1
< 0 ) {
6856 shortShift128Left( 0, zSig1
, 1, &term2
, &term3
);
6858 term2
|= doubleZSig0
;
6859 add192( rem1
, rem2
, rem3
, 0, term2
, term3
, &rem1
, &rem2
, &rem3
);
6861 zSig1
|= ( ( rem1
| rem2
| rem3
) != 0 );
6863 shift128ExtraRightJamming( zSig0
, zSig1
, 0, 14, &zSig0
, &zSig1
, &zSig2
);
6864 return roundAndPackFloat128(0, zExp
, zSig0
, zSig1
, zSig2
, status
);
6868 static inline FloatRelation
6869 floatx80_compare_internal(floatx80 a
, floatx80 b
, bool is_quiet
,
6870 float_status
*status
)
6874 if (floatx80_invalid_encoding(a
) || floatx80_invalid_encoding(b
)) {
6875 float_raise(float_flag_invalid
, status
);
6876 return float_relation_unordered
;
6878 if (( ( extractFloatx80Exp( a
) == 0x7fff ) &&
6879 ( extractFloatx80Frac( a
)<<1 ) ) ||
6880 ( ( extractFloatx80Exp( b
) == 0x7fff ) &&
6881 ( extractFloatx80Frac( b
)<<1 ) )) {
6883 floatx80_is_signaling_nan(a
, status
) ||
6884 floatx80_is_signaling_nan(b
, status
)) {
6885 float_raise(float_flag_invalid
, status
);
6887 return float_relation_unordered
;
6889 aSign
= extractFloatx80Sign( a
);
6890 bSign
= extractFloatx80Sign( b
);
6891 if ( aSign
!= bSign
) {
6893 if ( ( ( (uint16_t) ( ( a
.high
| b
.high
) << 1 ) ) == 0) &&
6894 ( ( a
.low
| b
.low
) == 0 ) ) {
6896 return float_relation_equal
;
6898 return 1 - (2 * aSign
);
6901 /* Normalize pseudo-denormals before comparison. */
6902 if ((a
.high
& 0x7fff) == 0 && a
.low
& UINT64_C(0x8000000000000000)) {
6905 if ((b
.high
& 0x7fff) == 0 && b
.low
& UINT64_C(0x8000000000000000)) {
6908 if (a
.low
== b
.low
&& a
.high
== b
.high
) {
6909 return float_relation_equal
;
6911 return 1 - 2 * (aSign
^ ( lt128( a
.high
, a
.low
, b
.high
, b
.low
) ));
6916 FloatRelation
floatx80_compare(floatx80 a
, floatx80 b
, float_status
*status
)
6918 return floatx80_compare_internal(a
, b
, 0, status
);
6921 FloatRelation
floatx80_compare_quiet(floatx80 a
, floatx80 b
,
6922 float_status
*status
)
6924 return floatx80_compare_internal(a
, b
, 1, status
);
6927 static inline FloatRelation
6928 float128_compare_internal(float128 a
, float128 b
, bool is_quiet
,
6929 float_status
*status
)
6933 if (( ( extractFloat128Exp( a
) == 0x7fff ) &&
6934 ( extractFloat128Frac0( a
) | extractFloat128Frac1( a
) ) ) ||
6935 ( ( extractFloat128Exp( b
) == 0x7fff ) &&
6936 ( extractFloat128Frac0( b
) | extractFloat128Frac1( b
) ) )) {
6938 float128_is_signaling_nan(a
, status
) ||
6939 float128_is_signaling_nan(b
, status
)) {
6940 float_raise(float_flag_invalid
, status
);
6942 return float_relation_unordered
;
6944 aSign
= extractFloat128Sign( a
);
6945 bSign
= extractFloat128Sign( b
);
6946 if ( aSign
!= bSign
) {
6947 if ( ( ( ( a
.high
| b
.high
)<<1 ) | a
.low
| b
.low
) == 0 ) {
6949 return float_relation_equal
;
6951 return 1 - (2 * aSign
);
6954 if (a
.low
== b
.low
&& a
.high
== b
.high
) {
6955 return float_relation_equal
;
6957 return 1 - 2 * (aSign
^ ( lt128( a
.high
, a
.low
, b
.high
, b
.low
) ));
6962 FloatRelation
float128_compare(float128 a
, float128 b
, float_status
*status
)
6964 return float128_compare_internal(a
, b
, 0, status
);
6967 FloatRelation
float128_compare_quiet(float128 a
, float128 b
,
6968 float_status
*status
)
6970 return float128_compare_internal(a
, b
, 1, status
);
6973 floatx80
floatx80_scalbn(floatx80 a
, int n
, float_status
*status
)
6979 if (floatx80_invalid_encoding(a
)) {
6980 float_raise(float_flag_invalid
, status
);
6981 return floatx80_default_nan(status
);
6983 aSig
= extractFloatx80Frac( a
);
6984 aExp
= extractFloatx80Exp( a
);
6985 aSign
= extractFloatx80Sign( a
);
6987 if ( aExp
== 0x7FFF ) {
6989 return propagateFloatx80NaN(a
, a
, status
);
7003 } else if (n
< -0x10000) {
7008 return normalizeRoundAndPackFloatx80(status
->floatx80_rounding_precision
,
7009 aSign
, aExp
, aSig
, 0, status
);
7012 float128
float128_scalbn(float128 a
, int n
, float_status
*status
)
7016 uint64_t aSig0
, aSig1
;
7018 aSig1
= extractFloat128Frac1( a
);
7019 aSig0
= extractFloat128Frac0( a
);
7020 aExp
= extractFloat128Exp( a
);
7021 aSign
= extractFloat128Sign( a
);
7022 if ( aExp
== 0x7FFF ) {
7023 if ( aSig0
| aSig1
) {
7024 return propagateFloat128NaN(a
, a
, status
);
7029 aSig0
|= UINT64_C(0x0001000000000000);
7030 } else if (aSig0
== 0 && aSig1
== 0) {
7038 } else if (n
< -0x10000) {
7043 return normalizeRoundAndPackFloat128( aSign
, aExp
, aSig0
, aSig1
7048 static void __attribute__((constructor
)) softfloat_init(void)
7050 union_float64 ua
, ub
, uc
, ur
;
7052 if (QEMU_NO_HARDFLOAT
) {
7056 * Test that the host's FMA is not obviously broken. For example,
7057 * glibc < 2.23 can perform an incorrect FMA on certain hosts; see
7058 * https://sourceware.org/bugzilla/show_bug.cgi?id=13304
7060 ua
.s
= 0x0020000000000001ULL
;
7061 ub
.s
= 0x3ca0000000000000ULL
;
7062 uc
.s
= 0x0020000000000000ULL
;
7063 ur
.h
= fma(ua
.h
, ub
.h
, uc
.h
);
7064 if (ur
.s
!= 0x0020000000000001ULL
) {
7065 force_soft_fma
= true;