85
#include "qemu/osdep.h"
87
#include "qemu/bitops.h"
88
#include "fpu/softfloat.h"
97
#include "fpu/softfloat-macros.h"
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
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
212
#if defined(__x86_64__) || defined(__aarch64__)
213
# define QEMU_HARDFLOAT_USE_ISINF 1
215
# define QEMU_HARDFLOAT_USE_ISINF 0
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);
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);
275
static inline bool f32_is_zon2(union_float32 a, union_float32 b)
277
if (QEMU_HARDFLOAT_2F32_USE_FP) {
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);
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);
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);
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);
409
typedef enum __attribute__ ((__packed__)) {
410
float_class_unclassified,
418
#define float_cmask(bit) (1u << (bit))
421
float_cmask_zero = float_cmask(float_class_zero),
422
float_cmask_normal = float_cmask(float_class_normal),
423
float_cmask_inf = float_cmask(float_class_inf),
424
float_cmask_qnan = float_cmask(float_class_qnan),
425
float_cmask_snan = float_cmask(float_class_snan),
427
float_cmask_infzero = float_cmask_zero | float_cmask_inf,
428
float_cmask_anynan = float_cmask_qnan | float_cmask_snan,
447
static inline __attribute__((unused)) bool is_nan(FloatClass c)
449
return unlikely(c >= float_class_qnan);
452
static inline __attribute__((unused)) bool is_snan(FloatClass c)
454
return c == float_class_snan;
457
static inline __attribute__((unused)) bool is_qnan(FloatClass c)
459
return c == float_class_qnan;
507
#define DECOMPOSED_BINARY_POINT 63
508
#define DECOMPOSED_IMPLICIT_BIT (1ull << DECOMPOSED_BINARY_POINT)
535
#define FLOAT_PARAMS_(E) \
537
.exp_bias = ((1 << E) - 1) >> 1, \
538
.exp_re_bias = (1 << (E - 1)) + (1 << (E - 2)), \
539
.exp_max = (1 << E) - 1
541
#define FLOAT_PARAMS(E, F) \
544
.frac_shift = (-F - 1) & 63, \
545
.round_mask = (1ull << ((-F - 1) & 63)) - 1
547
static const FloatFmt float16_params = {
551
static const FloatFmt float16_params_ahp = {
556
static const FloatFmt bfloat16_params = {
560
static const FloatFmt float32_params = {
564
static const FloatFmt float64_params = {
568
static const FloatFmt float128_params = {
569
FLOAT_PARAMS(15, 112)
572
#define FLOATX80_PARAMS(R) \
574
.frac_size = R == 64 ? 63 : R, \
576
.round_mask = R == 64 ? -1 : (1ull << ((-R - 1) & 63)) - 1
578
static const FloatFmt floatx80_params[3] = {
579
[floatx80_precision_s] = { FLOATX80_PARAMS(23) },
580
[floatx80_precision_d] = { FLOATX80_PARAMS(52) },
581
[floatx80_precision_x] = {
584
.m68k_denormal = true,
590
static void unpack_raw64(FloatParts64 *r, const FloatFmt *fmt, uint64_t raw)
592
const int f_size = fmt->frac_size;
593
const int e_size = fmt->exp_size;
595
*r = (FloatParts64) {
596
.cls = float_class_unclassified,
597
.sign = extract64(raw, f_size + e_size, 1),
598
.exp = extract64(raw, f_size, e_size),
599
.frac = extract64(raw, 0, f_size)
603
static void QEMU_FLATTEN float16_unpack_raw(FloatParts64 *p, float16 f)
605
unpack_raw64(p, &float16_params, f);
608
static void QEMU_FLATTEN bfloat16_unpack_raw(FloatParts64 *p, bfloat16 f)
610
unpack_raw64(p, &bfloat16_params, f);
613
static void QEMU_FLATTEN float32_unpack_raw(FloatParts64 *p, float32 f)
615
unpack_raw64(p, &float32_params, f);
618
static void QEMU_FLATTEN float64_unpack_raw(FloatParts64 *p, float64 f)
620
unpack_raw64(p, &float64_params, f);
623
static void QEMU_FLATTEN floatx80_unpack_raw(FloatParts128 *p, floatx80 f)
625
*p = (FloatParts128) {
626
.cls = float_class_unclassified,
627
.sign = extract32(f.high, 15, 1),
628
.exp = extract32(f.high, 0, 15),
633
static void QEMU_FLATTEN float128_unpack_raw(FloatParts128 *p, float128 f)
635
const int f_size = float128_params.frac_size - 64;
636
const int e_size = float128_params.exp_size;
638
*p = (FloatParts128) {
639
.cls = float_class_unclassified,
640
.sign = extract64(f.high, f_size + e_size, 1),
641
.exp = extract64(f.high, f_size, e_size),
642
.frac_hi = extract64(f.high, 0, f_size),
648
static uint64_t pack_raw64(const FloatParts64 *p, const FloatFmt *fmt)
650
const int f_size = fmt->frac_size;
651
const int e_size = fmt->exp_size;
654
ret = (uint64_t)p->sign << (f_size + e_size);
655
ret = deposit64(ret, f_size, e_size, p->exp);
656
ret = deposit64(ret, 0, f_size, p->frac);
660
static float16 QEMU_FLATTEN float16_pack_raw(const FloatParts64 *p)
662
return make_float16(pack_raw64(p, &float16_params));
665
static bfloat16 QEMU_FLATTEN bfloat16_pack_raw(const FloatParts64 *p)
667
return pack_raw64(p, &bfloat16_params);
670
static float32 QEMU_FLATTEN float32_pack_raw(const FloatParts64 *p)
672
return make_float32(pack_raw64(p, &float32_params));
675
static float64 QEMU_FLATTEN float64_pack_raw(const FloatParts64 *p)
677
return make_float64(pack_raw64(p, &float64_params));
680
static float128 QEMU_FLATTEN float128_pack_raw(const FloatParts128 *p)
682
const int f_size = float128_params.frac_size - 64;
683
const int e_size = float128_params.exp_size;
686
hi = (uint64_t)p->sign << (f_size + e_size);
687
hi = deposit64(hi, f_size, e_size, p->exp);
688
hi = deposit64(hi, 0, f_size, p->frac_hi);
689
return make_float128(hi, p->frac_lo);
700
#include "softfloat-specialize.c.inc"
702
#define PARTS_GENERIC_64_128(NAME, P) \
703
_Generic((P), FloatParts64 *: parts64_##NAME, \
704
FloatParts128 *: parts128_##NAME)
706
#define PARTS_GENERIC_64_128_256(NAME, P) \
707
_Generic((P), FloatParts64 *: parts64_##NAME, \
708
FloatParts128 *: parts128_##NAME, \
709
FloatParts256 *: parts256_##NAME)
711
#define parts_default_nan(P, S) PARTS_GENERIC_64_128(default_nan, P)(P, S)
712
#define parts_silence_nan(P, S) PARTS_GENERIC_64_128(silence_nan, P)(P, S)
714
static void parts64_return_nan(FloatParts64 *a, float_status *s);
715
static void parts128_return_nan(FloatParts128 *a, float_status *s);
717
#define parts_return_nan(P, S) PARTS_GENERIC_64_128(return_nan, P)(P, S)
719
static FloatParts64 *parts64_pick_nan(FloatParts64 *a, FloatParts64 *b,
721
static FloatParts128 *parts128_pick_nan(FloatParts128 *a, FloatParts128 *b,
724
#define parts_pick_nan(A, B, S) PARTS_GENERIC_64_128(pick_nan, A)(A, B, S)
726
static FloatParts64 *parts64_pick_nan_muladd(FloatParts64 *a, FloatParts64 *b,
727
FloatParts64 *c, float_status *s,
728
int ab_mask, int abc_mask);
729
static FloatParts128 *parts128_pick_nan_muladd(FloatParts128 *a,
733
int ab_mask, int abc_mask);
735
#define parts_pick_nan_muladd(A, B, C, S, ABM, ABCM) \
736
PARTS_GENERIC_64_128(pick_nan_muladd, A)(A, B, C, S, ABM, ABCM)
738
static void parts64_canonicalize(FloatParts64 *p, float_status *status,
739
const FloatFmt *fmt);
740
static void parts128_canonicalize(FloatParts128 *p, float_status *status,
741
const FloatFmt *fmt);
743
#define parts_canonicalize(A, S, F) \
744
PARTS_GENERIC_64_128(canonicalize, A)(A, S, F)
746
static void parts64_uncanon_normal(FloatParts64 *p, float_status *status,
747
const FloatFmt *fmt);
748
static void parts128_uncanon_normal(FloatParts128 *p, float_status *status,
749
const FloatFmt *fmt);
751
#define parts_uncanon_normal(A, S, F) \
752
PARTS_GENERIC_64_128(uncanon_normal, A)(A, S, F)
754
static void parts64_uncanon(FloatParts64 *p, float_status *status,
755
const FloatFmt *fmt);
756
static void parts128_uncanon(FloatParts128 *p, float_status *status,
757
const FloatFmt *fmt);
759
#define parts_uncanon(A, S, F) \
760
PARTS_GENERIC_64_128(uncanon, A)(A, S, F)
762
static void parts64_add_normal(FloatParts64 *a, FloatParts64 *b);
763
static void parts128_add_normal(FloatParts128 *a, FloatParts128 *b);
764
static void parts256_add_normal(FloatParts256 *a, FloatParts256 *b);
766
#define parts_add_normal(A, B) \
767
PARTS_GENERIC_64_128_256(add_normal, A)(A, B)
769
static bool parts64_sub_normal(FloatParts64 *a, FloatParts64 *b);
770
static bool parts128_sub_normal(FloatParts128 *a, FloatParts128 *b);
771
static bool parts256_sub_normal(FloatParts256 *a, FloatParts256 *b);
773
#define parts_sub_normal(A, B) \
774
PARTS_GENERIC_64_128_256(sub_normal, A)(A, B)
776
static FloatParts64 *parts64_addsub(FloatParts64 *a, FloatParts64 *b,
777
float_status *s, bool subtract);
778
static FloatParts128 *parts128_addsub(FloatParts128 *a, FloatParts128 *b,
779
float_status *s, bool subtract);
781
#define parts_addsub(A, B, S, Z) \
782
PARTS_GENERIC_64_128(addsub, A)(A, B, S, Z)
784
static FloatParts64 *parts64_mul(FloatParts64 *a, FloatParts64 *b,
786
static FloatParts128 *parts128_mul(FloatParts128 *a, FloatParts128 *b,
789
#define parts_mul(A, B, S) \
790
PARTS_GENERIC_64_128(mul, A)(A, B, S)
792
static FloatParts64 *parts64_muladd(FloatParts64 *a, FloatParts64 *b,
793
FloatParts64 *c, int flags,
795
static FloatParts128 *parts128_muladd(FloatParts128 *a, FloatParts128 *b,
796
FloatParts128 *c, int flags,
799
#define parts_muladd(A, B, C, Z, S) \
800
PARTS_GENERIC_64_128(muladd, A)(A, B, C, Z, S)
802
static FloatParts64 *parts64_div(FloatParts64 *a, FloatParts64 *b,
804
static FloatParts128 *parts128_div(FloatParts128 *a, FloatParts128 *b,
807
#define parts_div(A, B, S) \
808
PARTS_GENERIC_64_128(div, A)(A, B, S)
810
static FloatParts64 *parts64_modrem(FloatParts64 *a, FloatParts64 *b,
811
uint64_t *mod_quot, float_status *s);
812
static FloatParts128 *parts128_modrem(FloatParts128 *a, FloatParts128 *b,
813
uint64_t *mod_quot, float_status *s);
815
#define parts_modrem(A, B, Q, S) \
816
PARTS_GENERIC_64_128(modrem, A)(A, B, Q, S)
818
static void parts64_sqrt(FloatParts64 *a, float_status *s, const FloatFmt *f);
819
static void parts128_sqrt(FloatParts128 *a, float_status *s, const FloatFmt *f);
821
#define parts_sqrt(A, S, F) \
822
PARTS_GENERIC_64_128(sqrt, A)(A, S, F)
824
static bool parts64_round_to_int_normal(FloatParts64 *a, FloatRoundMode rm,
825
int scale, int frac_size);
826
static bool parts128_round_to_int_normal(FloatParts128 *a, FloatRoundMode r,
827
int scale, int frac_size);
829
#define parts_round_to_int_normal(A, R, C, F) \
830
PARTS_GENERIC_64_128(round_to_int_normal, A)(A, R, C, F)
832
static void parts64_round_to_int(FloatParts64 *a, FloatRoundMode rm,
833
int scale, float_status *s,
834
const FloatFmt *fmt);
835
static void parts128_round_to_int(FloatParts128 *a, FloatRoundMode r,
836
int scale, float_status *s,
837
const FloatFmt *fmt);
839
#define parts_round_to_int(A, R, C, S, F) \
840
PARTS_GENERIC_64_128(round_to_int, A)(A, R, C, S, F)
842
static int64_t parts64_float_to_sint(FloatParts64 *p, FloatRoundMode rmode,
843
int scale, int64_t min, int64_t max,
845
static int64_t parts128_float_to_sint(FloatParts128 *p, FloatRoundMode rmode,
846
int scale, int64_t min, int64_t max,
849
#define parts_float_to_sint(P, R, Z, MN, MX, S) \
850
PARTS_GENERIC_64_128(float_to_sint, P)(P, R, Z, MN, MX, S)
852
static uint64_t parts64_float_to_uint(FloatParts64 *p, FloatRoundMode rmode,
853
int scale, uint64_t max,
855
static uint64_t parts128_float_to_uint(FloatParts128 *p, FloatRoundMode rmode,
856
int scale, uint64_t max,
859
#define parts_float_to_uint(P, R, Z, M, S) \
860
PARTS_GENERIC_64_128(float_to_uint, P)(P, R, Z, M, S)
862
static int64_t parts64_float_to_sint_modulo(FloatParts64 *p,
863
FloatRoundMode rmode,
864
int bitsm1, float_status *s);
865
static int64_t parts128_float_to_sint_modulo(FloatParts128 *p,
866
FloatRoundMode rmode,
867
int bitsm1, float_status *s);
869
#define parts_float_to_sint_modulo(P, R, M, S) \
870
PARTS_GENERIC_64_128(float_to_sint_modulo, P)(P, R, M, S)
872
static void parts64_sint_to_float(FloatParts64 *p, int64_t a,
873
int scale, float_status *s);
874
static void parts128_sint_to_float(FloatParts128 *p, int64_t a,
875
int scale, float_status *s);
877
#define parts_float_to_sint(P, R, Z, MN, MX, S) \
878
PARTS_GENERIC_64_128(float_to_sint, P)(P, R, Z, MN, MX, S)
880
#define parts_sint_to_float(P, I, Z, S) \
881
PARTS_GENERIC_64_128(sint_to_float, P)(P, I, Z, S)
883
static void parts64_uint_to_float(FloatParts64 *p, uint64_t a,
884
int scale, float_status *s);
885
static void parts128_uint_to_float(FloatParts128 *p, uint64_t a,
886
int scale, float_status *s);
888
#define parts_uint_to_float(P, I, Z, S) \
889
PARTS_GENERIC_64_128(uint_to_float, P)(P, I, Z, S)
891
static FloatParts64 *parts64_minmax(FloatParts64 *a, FloatParts64 *b,
892
float_status *s, int flags);
893
static FloatParts128 *parts128_minmax(FloatParts128 *a, FloatParts128 *b,
894
float_status *s, int flags);
896
#define parts_minmax(A, B, S, F) \
897
PARTS_GENERIC_64_128(minmax, A)(A, B, S, F)
899
static FloatRelation parts64_compare(FloatParts64 *a, FloatParts64 *b,
900
float_status *s, bool q);
901
static FloatRelation parts128_compare(FloatParts128 *a, FloatParts128 *b,
902
float_status *s, bool q);
904
#define parts_compare(A, B, S, Q) \
905
PARTS_GENERIC_64_128(compare, A)(A, B, S, Q)
907
static void parts64_scalbn(FloatParts64 *a, int n, float_status *s);
908
static void parts128_scalbn(FloatParts128 *a, int n, float_status *s);
910
#define parts_scalbn(A, N, S) \
911
PARTS_GENERIC_64_128(scalbn, A)(A, N, S)
913
static void parts64_log2(FloatParts64 *a, float_status *s, const FloatFmt *f);
914
static void parts128_log2(FloatParts128 *a, float_status *s, const FloatFmt *f);
916
#define parts_log2(A, S, F) \
917
PARTS_GENERIC_64_128(log2, A)(A, S, F)
923
#define FRAC_GENERIC_64_128(NAME, P) \
924
_Generic((P), FloatParts64 *: frac64_##NAME, \
925
FloatParts128 *: frac128_##NAME)
927
#define FRAC_GENERIC_64_128_256(NAME, P) \
928
_Generic((P), FloatParts64 *: frac64_##NAME, \
929
FloatParts128 *: frac128_##NAME, \
930
FloatParts256 *: frac256_##NAME)
932
static bool frac64_add(FloatParts64 *r, FloatParts64 *a, FloatParts64 *b)
934
return uadd64_overflow(a->frac, b->frac, &r->frac);
937
static bool frac128_add(FloatParts128 *r, FloatParts128 *a, FloatParts128 *b)
940
r->frac_lo = uadd64_carry(a->frac_lo, b->frac_lo, &c);
941
r->frac_hi = uadd64_carry(a->frac_hi, b->frac_hi, &c);
945
static bool frac256_add(FloatParts256 *r, FloatParts256 *a, FloatParts256 *b)
948
r->frac_lo = uadd64_carry(a->frac_lo, b->frac_lo, &c);
949
r->frac_lm = uadd64_carry(a->frac_lm, b->frac_lm, &c);
950
r->frac_hm = uadd64_carry(a->frac_hm, b->frac_hm, &c);
951
r->frac_hi = uadd64_carry(a->frac_hi, b->frac_hi, &c);
955
#define frac_add(R, A, B) FRAC_GENERIC_64_128_256(add, R)(R, A, B)
957
static bool frac64_addi(FloatParts64 *r, FloatParts64 *a, uint64_t c)
959
return uadd64_overflow(a->frac, c, &r->frac);
962
static bool frac128_addi(FloatParts128 *r, FloatParts128 *a, uint64_t c)
964
c = uadd64_overflow(a->frac_lo, c, &r->frac_lo);
965
return uadd64_overflow(a->frac_hi, c, &r->frac_hi);
968
#define frac_addi(R, A, C) FRAC_GENERIC_64_128(addi, R)(R, A, C)
970
static void frac64_allones(FloatParts64 *a)
975
static void frac128_allones(FloatParts128 *a)
977
a->frac_hi = a->frac_lo = -1;
980
#define frac_allones(A) FRAC_GENERIC_64_128(allones, A)(A)
982
static FloatRelation frac64_cmp(FloatParts64 *a, FloatParts64 *b)
984
return (a->frac == b->frac ? float_relation_equal
985
: a->frac < b->frac ? float_relation_less
986
: float_relation_greater);
989
static FloatRelation frac128_cmp(FloatParts128 *a, FloatParts128 *b)
991
uint64_t ta = a->frac_hi, tb = b->frac_hi;
993
ta = a->frac_lo, tb = b->frac_lo;
995
return float_relation_equal;
998
return ta < tb ? float_relation_less : float_relation_greater;
1001
#define frac_cmp(A, B) FRAC_GENERIC_64_128(cmp, A)(A, B)
1003
static void frac64_clear(FloatParts64 *a)
1008
static void frac128_clear(FloatParts128 *a)
1010
a->frac_hi = a->frac_lo = 0;
1013
#define frac_clear(A) FRAC_GENERIC_64_128(clear, A)(A)
1015
static bool frac64_div(FloatParts64 *a, FloatParts64 *b)
1017
uint64_t n1, n0, r, q;
1031
ret = a->frac < b->frac;
1039
q = udiv_qrnnd(&r, n0, n1, b->frac);
1042
a->frac = q | (r != 0);
1047
static bool frac128_div(FloatParts128 *a, FloatParts128 *b)
1049
uint64_t q0, q1, a0, a1, b0, b1;
1050
uint64_t r0, r1, r2, r3, t0, t1, t2, t3;
1053
a0 = a->frac_hi, a1 = a->frac_lo;
1054
b0 = b->frac_hi, b1 = b->frac_lo;
1056
ret = lt128(a0, a1, b0, b1);
1058
a1 = shr_double(a0, a1, 1);
1063
q0 = estimateDiv128To64(a0, a1, b0);
1070
mul128By64To192(b0, b1, q0, &t0, &t1, &t2);
1071
sub192(a0, a1, 0, t0, t1, t2, &r0, &r1, &r2);
1074
add192(r0, r1, r2, 0, b0, b1, &r0, &r1, &r2);
1078
q1 = estimateDiv128To64(r1, r2, b0);
1079
mul128By64To192(b0, b1, q1, &t1, &t2, &t3);
1080
sub192(r1, r2, 0, t1, t2, t3, &r1, &r2, &r3);
1083
add192(r1, r2, r3, 0, b0, b1, &r1, &r2, &r3);
1087
q1 |= (r2 | r3) != 0;
1094
#define frac_div(A, B) FRAC_GENERIC_64_128(div, A)(A, B)
1096
static bool frac64_eqz(FloatParts64 *a)
1098
return a->frac == 0;
1101
static bool frac128_eqz(FloatParts128 *a)
1103
return (a->frac_hi | a->frac_lo) == 0;
1106
#define frac_eqz(A) FRAC_GENERIC_64_128(eqz, A)(A)
1108
static void frac64_mulw(FloatParts128 *r, FloatParts64 *a, FloatParts64 *b)
1110
mulu64(&r->frac_lo, &r->frac_hi, a->frac, b->frac);
1113
static void frac128_mulw(FloatParts256 *r, FloatParts128 *a, FloatParts128 *b)
1115
mul128To256(a->frac_hi, a->frac_lo, b->frac_hi, b->frac_lo,
1116
&r->frac_hi, &r->frac_hm, &r->frac_lm, &r->frac_lo);
1119
#define frac_mulw(R, A, B) FRAC_GENERIC_64_128(mulw, A)(R, A, B)
1121
static void frac64_neg(FloatParts64 *a)
1126
static void frac128_neg(FloatParts128 *a)
1129
a->frac_lo = usub64_borrow(0, a->frac_lo, &c);
1130
a->frac_hi = usub64_borrow(0, a->frac_hi, &c);
1133
static void frac256_neg(FloatParts256 *a)
1136
a->frac_lo = usub64_borrow(0, a->frac_lo, &c);
1137
a->frac_lm = usub64_borrow(0, a->frac_lm, &c);
1138
a->frac_hm = usub64_borrow(0, a->frac_hm, &c);
1139
a->frac_hi = usub64_borrow(0, a->frac_hi, &c);
1142
#define frac_neg(A) FRAC_GENERIC_64_128_256(neg, A)(A)
1144
static int frac64_normalize(FloatParts64 *a)
1147
int shift = clz64(a->frac);
1154
static int frac128_normalize(FloatParts128 *a)
1157
int shl = clz64(a->frac_hi);
1158
a->frac_hi = shl_double(a->frac_hi, a->frac_lo, shl);
1161
} else if (a->frac_lo) {
1162
int shl = clz64(a->frac_lo);
1163
a->frac_hi = a->frac_lo << shl;
1170
static int frac256_normalize(FloatParts256 *a)
1172
uint64_t a0 = a->frac_hi, a1 = a->frac_hm;
1173
uint64_t a2 = a->frac_lm, a3 = a->frac_lo;
1185
a0 = a1, a1 = a2, a2 = a3, a3 = 0;
1188
a0 = a2, a1 = a3, a2 = 0, a3 = 0;
1191
a0 = a3, a1 = 0, a2 = 0, a3 = 0;
1194
a0 = 0, a1 = 0, a2 = 0, a3 = 0;
1204
a0 = shl_double(a0, a1, shl);
1205
a1 = shl_double(a1, a2, shl);
1206
a2 = shl_double(a2, a3, shl);
1217
#define frac_normalize(A) FRAC_GENERIC_64_128_256(normalize, A)(A)
1219
static void frac64_modrem(FloatParts64 *a, FloatParts64 *b, uint64_t *mod_quot)
1221
uint64_t a0, a1, b0, t0, t1, q, quot;
1222
int exp_diff = a->exp - b->exp;
1228
if (exp_diff < -1) {
1234
if (exp_diff == -1) {
1240
quot = q = b0 <= a0;
1246
while (exp_diff > 0) {
1247
q = estimateDiv128To64(a0, a1, b0);
1248
q = q > 2 ? q - 2 : 0;
1249
mul64To128(b0, q, &t0, &t1);
1250
sub128(a0, a1, t0, t1, &a0, &a1);
1251
shortShift128Left(a0, a1, 62, &a0, &a1);
1253
quot = (quot << 62) + q;
1258
q = estimateDiv128To64(a0, a1, b0);
1259
q = q > 2 ? (q - 2) >> (64 - exp_diff) : 0;
1260
mul64To128(b0, q << (64 - exp_diff), &t0, &t1);
1261
sub128(a0, a1, t0, t1, &a0, &a1);
1262
shortShift128Left(0, b0, 64 - exp_diff, &t0, &t1);
1263
while (le128(t0, t1, a0, a1)) {
1265
sub128(a0, a1, t0, t1, &a0, &a1);
1267
quot = (exp_diff < 64 ? quot << exp_diff : 0) + q;
1276
sub128(t0, t1, a0, a1, &t0, &t1);
1277
if (lt128(t0, t1, a0, a1) ||
1278
(eq128(t0, t1, a0, a1) && (q & 1))) {
1287
shortShift128Left(a0, a1, shift, &a0, &a1);
1288
} else if (likely(a1)) {
1294
a->cls = float_class_zero;
1298
a->exp = b->exp + exp_diff - shift;
1299
a->frac = a0 | (a1 != 0);
1302
static void frac128_modrem(FloatParts128 *a, FloatParts128 *b,
1305
uint64_t a0, a1, a2, b0, b1, t0, t1, t2, q, quot;
1306
int exp_diff = a->exp - b->exp;
1313
if (exp_diff < -1) {
1319
if (exp_diff == -1) {
1320
shift128Right(a0, a1, 1, &a0, &a1);
1327
quot = q = le128(b0, b1, a0, a1);
1329
sub128(a0, a1, b0, b1, &a0, &a1);
1333
while (exp_diff > 0) {
1334
q = estimateDiv128To64(a0, a1, b0);
1335
q = q > 4 ? q - 4 : 0;
1336
mul128By64To192(b0, b1, q, &t0, &t1, &t2);
1337
sub192(a0, a1, a2, t0, t1, t2, &a0, &a1, &a2);
1338
shortShift192Left(a0, a1, a2, 61, &a0, &a1, &a2);
1340
quot = (quot << 61) + q;
1345
q = estimateDiv128To64(a0, a1, b0);
1346
q = q > 4 ? (q - 4) >> (64 - exp_diff) : 0;
1347
mul128By64To192(b0, b1, q << (64 - exp_diff), &t0, &t1, &t2);
1348
sub192(a0, a1, a2, t0, t1, t2, &a0, &a1, &a2);
1349
shortShift192Left(0, b0, b1, 64 - exp_diff, &t0, &t1, &t2);
1350
while (le192(t0, t1, t2, a0, a1, a2)) {
1352
sub192(a0, a1, a2, t0, t1, t2, &a0, &a1, &a2);
1354
quot = (exp_diff < 64 ? quot << exp_diff : 0) + q;
1364
sub192(t0, t1, t2, a0, a1, a2, &t0, &t1, &t2);
1365
if (lt192(t0, t1, t2, a0, a1, a2) ||
1366
(eq192(t0, t1, t2, a0, a1, a2) && (q & 1))) {
1376
shortShift192Left(a0, a1, a2, shift, &a0, &a1, &a2);
1377
} else if (likely(a1)) {
1379
shortShift128Left(a1, a2, shift, &a0, &a1);
1382
} else if (likely(a2)) {
1388
a->cls = float_class_zero;
1392
a->exp = b->exp + exp_diff - shift;
1394
a->frac_lo = a1 | (a2 != 0);
1397
#define frac_modrem(A, B, Q) FRAC_GENERIC_64_128(modrem, A)(A, B, Q)
1399
static void frac64_shl(FloatParts64 *a, int c)
1404
static void frac128_shl(FloatParts128 *a, int c)
1406
uint64_t a0 = a->frac_hi, a1 = a->frac_lo;
1414
a0 = shl_double(a0, a1, c);
1422
#define frac_shl(A, C) FRAC_GENERIC_64_128(shl, A)(A, C)
1424
static void frac64_shr(FloatParts64 *a, int c)
1429
static void frac128_shr(FloatParts128 *a, int c)
1431
uint64_t a0 = a->frac_hi, a1 = a->frac_lo;
1439
a1 = shr_double(a0, a1, c);
1447
#define frac_shr(A, C) FRAC_GENERIC_64_128(shr, A)(A, C)
1449
static void frac64_shrjam(FloatParts64 *a, int c)
1451
uint64_t a0 = a->frac;
1453
if (likely(c != 0)) {
1454
if (likely(c < 64)) {
1455
a0 = (a0 >> c) | (shr_double(a0, 0, c) != 0);
1463
static void frac128_shrjam(FloatParts128 *a, int c)
1465
uint64_t a0 = a->frac_hi, a1 = a->frac_lo;
1466
uint64_t sticky = 0;
1468
if (unlikely(c == 0)) {
1470
} else if (likely(c < 64)) {
1472
} else if (likely(c < 128)) {
1486
sticky |= shr_double(a1, 0, c);
1487
a1 = shr_double(a0, a1, c);
1491
a->frac_lo = a1 | (sticky != 0);
1495
static void frac256_shrjam(FloatParts256 *a, int c)
1497
uint64_t a0 = a->frac_hi, a1 = a->frac_hm;
1498
uint64_t a2 = a->frac_lm, a3 = a->frac_lo;
1499
uint64_t sticky = 0;
1501
if (unlikely(c == 0)) {
1503
} else if (likely(c < 64)) {
1505
} else if (likely(c < 256)) {
1506
if (unlikely(c & 128)) {
1508
a3 = a1, a2 = a0, a1 = 0, a0 = 0;
1510
if (unlikely(c & 64)) {
1512
a3 = a2, a2 = a1, a1 = a0, a0 = 0;
1519
sticky = a0 | a1 | a2 | a3;
1520
a0 = a1 = a2 = a3 = 0;
1524
sticky |= shr_double(a3, 0, c);
1525
a3 = shr_double(a2, a3, c);
1526
a2 = shr_double(a1, a2, c);
1527
a1 = shr_double(a0, a1, c);
1531
a->frac_lo = a3 | (sticky != 0);
1537
#define frac_shrjam(A, C) FRAC_GENERIC_64_128_256(shrjam, A)(A, C)
1539
static bool frac64_sub(FloatParts64 *r, FloatParts64 *a, FloatParts64 *b)
1541
return usub64_overflow(a->frac, b->frac, &r->frac);
1544
static bool frac128_sub(FloatParts128 *r, FloatParts128 *a, FloatParts128 *b)
1547
r->frac_lo = usub64_borrow(a->frac_lo, b->frac_lo, &c);
1548
r->frac_hi = usub64_borrow(a->frac_hi, b->frac_hi, &c);
1552
static bool frac256_sub(FloatParts256 *r, FloatParts256 *a, FloatParts256 *b)
1555
r->frac_lo = usub64_borrow(a->frac_lo, b->frac_lo, &c);
1556
r->frac_lm = usub64_borrow(a->frac_lm, b->frac_lm, &c);
1557
r->frac_hm = usub64_borrow(a->frac_hm, b->frac_hm, &c);
1558
r->frac_hi = usub64_borrow(a->frac_hi, b->frac_hi, &c);
1562
#define frac_sub(R, A, B) FRAC_GENERIC_64_128_256(sub, R)(R, A, B)
1564
static void frac64_truncjam(FloatParts64 *r, FloatParts128 *a)
1566
r->frac = a->frac_hi | (a->frac_lo != 0);
1569
static void frac128_truncjam(FloatParts128 *r, FloatParts256 *a)
1571
r->frac_hi = a->frac_hi;
1572
r->frac_lo = a->frac_hm | ((a->frac_lm | a->frac_lo) != 0);
1575
#define frac_truncjam(R, A) FRAC_GENERIC_64_128(truncjam, R)(R, A)
1577
static void frac64_widen(FloatParts128 *r, FloatParts64 *a)
1579
r->frac_hi = a->frac;
1583
static void frac128_widen(FloatParts256 *r, FloatParts128 *a)
1585
r->frac_hi = a->frac_hi;
1586
r->frac_hm = a->frac_lo;
1591
#define frac_widen(A, B) FRAC_GENERIC_64_128(widen, B)(A, B)
1598
static const uint16_t rsqrt_tab[128] = {
1599
0xb451, 0xb2f0, 0xb196, 0xb044, 0xaef9, 0xadb6, 0xac79, 0xab43,
1600
0xaa14, 0xa8eb, 0xa7c8, 0xa6aa, 0xa592, 0xa480, 0xa373, 0xa26b,
1601
0xa168, 0xa06a, 0x9f70, 0x9e7b, 0x9d8a, 0x9c9d, 0x9bb5, 0x9ad1,
1602
0x99f0, 0x9913, 0x983a, 0x9765, 0x9693, 0x95c4, 0x94f8, 0x9430,
1603
0x936b, 0x92a9, 0x91ea, 0x912e, 0x9075, 0x8fbe, 0x8f0a, 0x8e59,
1604
0x8daa, 0x8cfe, 0x8c54, 0x8bac, 0x8b07, 0x8a64, 0x89c4, 0x8925,
1605
0x8889, 0x87ee, 0x8756, 0x86c0, 0x862b, 0x8599, 0x8508, 0x8479,
1606
0x83ec, 0x8361, 0x82d8, 0x8250, 0x81c9, 0x8145, 0x80c2, 0x8040,
1607
0xff02, 0xfd0e, 0xfb25, 0xf947, 0xf773, 0xf5aa, 0xf3ea, 0xf234,
1608
0xf087, 0xeee3, 0xed47, 0xebb3, 0xea27, 0xe8a3, 0xe727, 0xe5b2,
1609
0xe443, 0xe2dc, 0xe17a, 0xe020, 0xdecb, 0xdd7d, 0xdc34, 0xdaf1,
1610
0xd9b3, 0xd87b, 0xd748, 0xd61a, 0xd4f1, 0xd3cd, 0xd2ad, 0xd192,
1611
0xd07b, 0xcf69, 0xce5b, 0xcd51, 0xcc4a, 0xcb48, 0xca4a, 0xc94f,
1612
0xc858, 0xc764, 0xc674, 0xc587, 0xc49d, 0xc3b7, 0xc2d4, 0xc1f4,
1613
0xc116, 0xc03c, 0xbf65, 0xbe90, 0xbdbe, 0xbcef, 0xbc23, 0xbb59,
1614
0xba91, 0xb9cc, 0xb90a, 0xb84a, 0xb78c, 0xb6d0, 0xb617, 0xb560,
1617
#define partsN(NAME) glue(glue(glue(parts,N),_),NAME)
1618
#define FloatPartsN glue(FloatParts,N)
1619
#define FloatPartsW glue(FloatParts,W)
1624
#include "softfloat-parts-addsub.c.inc"
1625
#include "softfloat-parts.c.inc"
1632
#include "softfloat-parts-addsub.c.inc"
1633
#include "softfloat-parts.c.inc"
1639
#include "softfloat-parts-addsub.c.inc"
1651
static void float16a_unpack_canonical(FloatParts64 *p, float16 f,
1652
float_status *s, const FloatFmt *params)
1654
float16_unpack_raw(p, f);
1655
parts_canonicalize(p, s, params);
1658
static void float16_unpack_canonical(FloatParts64 *p, float16 f,
1661
float16a_unpack_canonical(p, f, s, &float16_params);
1664
static void bfloat16_unpack_canonical(FloatParts64 *p, bfloat16 f,
1667
bfloat16_unpack_raw(p, f);
1668
parts_canonicalize(p, s, &bfloat16_params);
1671
static float16 float16a_round_pack_canonical(FloatParts64 *p,
1673
const FloatFmt *params)
1675
parts_uncanon(p, s, params);
1676
return float16_pack_raw(p);
1679
static float16 float16_round_pack_canonical(FloatParts64 *p,
1682
return float16a_round_pack_canonical(p, s, &float16_params);
1685
static bfloat16 bfloat16_round_pack_canonical(FloatParts64 *p,
1688
parts_uncanon(p, s, &bfloat16_params);
1689
return bfloat16_pack_raw(p);
1692
static void float32_unpack_canonical(FloatParts64 *p, float32 f,
1695
float32_unpack_raw(p, f);
1696
parts_canonicalize(p, s, &float32_params);
1699
static float32 float32_round_pack_canonical(FloatParts64 *p,
1702
parts_uncanon(p, s, &float32_params);
1703
return float32_pack_raw(p);
1706
static void float64_unpack_canonical(FloatParts64 *p, float64 f,
1709
float64_unpack_raw(p, f);
1710
parts_canonicalize(p, s, &float64_params);
1713
static float64 float64_round_pack_canonical(FloatParts64 *p,
1716
parts_uncanon(p, s, &float64_params);
1717
return float64_pack_raw(p);
1720
static float64 float64r32_round_pack_canonical(FloatParts64 *p,
1723
parts_uncanon(p, s, &float32_params);
1731
case float_class_normal:
1732
if (unlikely(p->exp == 0)) {
1737
int shift = frac_normalize(p);
1738
p->exp = (float32_params.frac_shift -
1739
float32_params.exp_bias - shift + 1 +
1740
float64_params.exp_bias);
1741
frac_shr(p, float64_params.frac_shift);
1743
frac_shl(p, float32_params.frac_shift - float64_params.frac_shift);
1744
p->exp += float64_params.exp_bias - float32_params.exp_bias;
1747
case float_class_snan:
1748
case float_class_qnan:
1749
frac_shl(p, float32_params.frac_shift - float64_params.frac_shift);
1750
p->exp = float64_params.exp_max;
1752
case float_class_inf:
1753
p->exp = float64_params.exp_max;
1755
case float_class_zero:
1758
g_assert_not_reached();
1761
return float64_pack_raw(p);
1764
static void float128_unpack_canonical(FloatParts128 *p, float128 f,
1767
float128_unpack_raw(p, f);
1768
parts_canonicalize(p, s, &float128_params);
1771
static float128 float128_round_pack_canonical(FloatParts128 *p,
1774
parts_uncanon(p, s, &float128_params);
1775
return float128_pack_raw(p);
1779
static bool floatx80_unpack_canonical(FloatParts128 *p, floatx80 f,
1783
switch (s->floatx80_rounding_precision) {
1784
case floatx80_precision_x:
1785
case floatx80_precision_d:
1786
case floatx80_precision_s:
1789
g_assert_not_reached();
1792
if (unlikely(floatx80_invalid_encoding(f))) {
1793
float_raise(float_flag_invalid, s);
1797
floatx80_unpack_raw(p, f);
1799
if (likely(p->exp != floatx80_params[floatx80_precision_x].exp_max)) {
1800
parts_canonicalize(p, s, &floatx80_params[floatx80_precision_x]);
1803
p->frac_hi &= MAKE_64BIT_MASK(0, 63);
1804
p->cls = (p->frac_hi == 0 ? float_class_inf
1805
: parts_is_snan_frac(p->frac_hi, s)
1806
? float_class_snan : float_class_qnan);
1811
static floatx80 floatx80_round_pack_canonical(FloatParts128 *p,
1814
const FloatFmt *fmt = &floatx80_params[s->floatx80_rounding_precision];
1819
case float_class_normal:
1820
if (s->floatx80_rounding_precision == floatx80_precision_x) {
1821
parts_uncanon_normal(p, s, fmt);
1829
frac_truncjam(&p64, p);
1830
parts_uncanon_normal(&p64, s, fmt);
1834
if (exp != fmt->exp_max) {
1839
case float_class_inf:
1841
frac = floatx80_infinity_low;
1845
case float_class_zero:
1850
case float_class_snan:
1851
case float_class_qnan:
1853
frac = p->frac_hi | (1ull << 63);
1858
g_assert_not_reached();
1861
return packFloatx80(p->sign, exp, frac);
1868
static float16 QEMU_FLATTEN
1869
float16_addsub(float16 a, float16 b, float_status *status, bool subtract)
1871
FloatParts64 pa, pb, *pr;
1873
float16_unpack_canonical(&pa, a, status);
1874
float16_unpack_canonical(&pb, b, status);
1875
pr = parts_addsub(&pa, &pb, status, subtract);
1877
return float16_round_pack_canonical(pr, status);
1880
float16 float16_add(float16 a, float16 b, float_status *status)
1882
return float16_addsub(a, b, status, false);
1885
float16 float16_sub(float16 a, float16 b, float_status *status)
1887
return float16_addsub(a, b, status, true);
1890
static float32 QEMU_SOFTFLOAT_ATTR
1891
soft_f32_addsub(float32 a, float32 b, float_status *status, bool subtract)
1893
FloatParts64 pa, pb, *pr;
1895
float32_unpack_canonical(&pa, a, status);
1896
float32_unpack_canonical(&pb, b, status);
1897
pr = parts_addsub(&pa, &pb, status, subtract);
1899
return float32_round_pack_canonical(pr, status);
1902
static float32 soft_f32_add(float32 a, float32 b, float_status *status)
1904
return soft_f32_addsub(a, b, status, false);
1907
static float32 soft_f32_sub(float32 a, float32 b, float_status *status)
1909
return soft_f32_addsub(a, b, status, true);
1912
static float64 QEMU_SOFTFLOAT_ATTR
1913
soft_f64_addsub(float64 a, float64 b, float_status *status, bool subtract)
1915
FloatParts64 pa, pb, *pr;
1917
float64_unpack_canonical(&pa, a, status);
1918
float64_unpack_canonical(&pb, b, status);
1919
pr = parts_addsub(&pa, &pb, status, subtract);
1921
return float64_round_pack_canonical(pr, status);
1924
static float64 soft_f64_add(float64 a, float64 b, float_status *status)
1926
return soft_f64_addsub(a, b, status, false);
1929
static float64 soft_f64_sub(float64 a, float64 b, float_status *status)
1931
return soft_f64_addsub(a, b, status, true);
1934
static float hard_f32_add(float a, float b)
1939
static float hard_f32_sub(float a, float b)
1944
static double hard_f64_add(double a, double b)
1949
static double hard_f64_sub(double a, double b)
1954
static bool f32_addsubmul_post(union_float32 a, union_float32 b)
1956
if (QEMU_HARDFLOAT_2F32_USE_FP) {
1957
return !(fpclassify(a.h) == FP_ZERO && fpclassify(b.h) == FP_ZERO);
1959
return !(float32_is_zero(a.s) && float32_is_zero(b.s));
1962
static bool f64_addsubmul_post(union_float64 a, union_float64 b)
1964
if (QEMU_HARDFLOAT_2F64_USE_FP) {
1965
return !(fpclassify(a.h) == FP_ZERO && fpclassify(b.h) == FP_ZERO);
1967
return !(float64_is_zero(a.s) && float64_is_zero(b.s));
1971
static float32 float32_addsub(float32 a, float32 b, float_status *s,
1972
hard_f32_op2_fn hard, soft_f32_op2_fn soft)
1974
return float32_gen2(a, b, s, hard, soft,
1975
f32_is_zon2, f32_addsubmul_post);
1978
static float64 float64_addsub(float64 a, float64 b, float_status *s,
1979
hard_f64_op2_fn hard, soft_f64_op2_fn soft)
1981
return float64_gen2(a, b, s, hard, soft,
1982
f64_is_zon2, f64_addsubmul_post);
1986
float32_add(float32 a, float32 b, float_status *s)
1988
return float32_addsub(a, b, s, hard_f32_add, soft_f32_add);
1992
float32_sub(float32 a, float32 b, float_status *s)
1994
return float32_addsub(a, b, s, hard_f32_sub, soft_f32_sub);
1998
float64_add(float64 a, float64 b, float_status *s)
2000
return float64_addsub(a, b, s, hard_f64_add, soft_f64_add);
2004
float64_sub(float64 a, float64 b, float_status *s)
2006
return float64_addsub(a, b, s, hard_f64_sub, soft_f64_sub);
2009
static float64 float64r32_addsub(float64 a, float64 b, float_status *status,
2012
FloatParts64 pa, pb, *pr;
2014
float64_unpack_canonical(&pa, a, status);
2015
float64_unpack_canonical(&pb, b, status);
2016
pr = parts_addsub(&pa, &pb, status, subtract);
2018
return float64r32_round_pack_canonical(pr, status);
2021
float64 float64r32_add(float64 a, float64 b, float_status *status)
2023
return float64r32_addsub(a, b, status, false);
2026
float64 float64r32_sub(float64 a, float64 b, float_status *status)
2028
return float64r32_addsub(a, b, status, true);
2031
static bfloat16 QEMU_FLATTEN
2032
bfloat16_addsub(bfloat16 a, bfloat16 b, float_status *status, bool subtract)
2034
FloatParts64 pa, pb, *pr;
2036
bfloat16_unpack_canonical(&pa, a, status);
2037
bfloat16_unpack_canonical(&pb, b, status);
2038
pr = parts_addsub(&pa, &pb, status, subtract);
2040
return bfloat16_round_pack_canonical(pr, status);
2043
bfloat16 bfloat16_add(bfloat16 a, bfloat16 b, float_status *status)
2045
return bfloat16_addsub(a, b, status, false);
2048
bfloat16 bfloat16_sub(bfloat16 a, bfloat16 b, float_status *status)
2050
return bfloat16_addsub(a, b, status, true);
2053
static float128 QEMU_FLATTEN
2054
float128_addsub(float128 a, float128 b, float_status *status, bool subtract)
2056
FloatParts128 pa, pb, *pr;
2058
float128_unpack_canonical(&pa, a, status);
2059
float128_unpack_canonical(&pb, b, status);
2060
pr = parts_addsub(&pa, &pb, status, subtract);
2062
return float128_round_pack_canonical(pr, status);
2065
float128 float128_add(float128 a, float128 b, float_status *status)
2067
return float128_addsub(a, b, status, false);
2070
float128 float128_sub(float128 a, float128 b, float_status *status)
2072
return float128_addsub(a, b, status, true);
2075
static floatx80 QEMU_FLATTEN
2076
floatx80_addsub(floatx80 a, floatx80 b, float_status *status, bool subtract)
2078
FloatParts128 pa, pb, *pr;
2080
if (!floatx80_unpack_canonical(&pa, a, status) ||
2081
!floatx80_unpack_canonical(&pb, b, status)) {
2082
return floatx80_default_nan(status);
2085
pr = parts_addsub(&pa, &pb, status, subtract);
2086
return floatx80_round_pack_canonical(pr, status);
2089
floatx80 floatx80_add(floatx80 a, floatx80 b, float_status *status)
2091
return floatx80_addsub(a, b, status, false);
2094
floatx80 floatx80_sub(floatx80 a, floatx80 b, float_status *status)
2096
return floatx80_addsub(a, b, status, true);
2103
float16 QEMU_FLATTEN float16_mul(float16 a, float16 b, float_status *status)
2105
FloatParts64 pa, pb, *pr;
2107
float16_unpack_canonical(&pa, a, status);
2108
float16_unpack_canonical(&pb, b, status);
2109
pr = parts_mul(&pa, &pb, status);
2111
return float16_round_pack_canonical(pr, status);
2114
static float32 QEMU_SOFTFLOAT_ATTR
2115
soft_f32_mul(float32 a, float32 b, float_status *status)
2117
FloatParts64 pa, pb, *pr;
2119
float32_unpack_canonical(&pa, a, status);
2120
float32_unpack_canonical(&pb, b, status);
2121
pr = parts_mul(&pa, &pb, status);
2123
return float32_round_pack_canonical(pr, status);
2126
static float64 QEMU_SOFTFLOAT_ATTR
2127
soft_f64_mul(float64 a, float64 b, float_status *status)
2129
FloatParts64 pa, pb, *pr;
2131
float64_unpack_canonical(&pa, a, status);
2132
float64_unpack_canonical(&pb, b, status);
2133
pr = parts_mul(&pa, &pb, status);
2135
return float64_round_pack_canonical(pr, status);
2138
static float hard_f32_mul(float a, float b)
2143
static double hard_f64_mul(double a, double b)
2149
float32_mul(float32 a, float32 b, float_status *s)
2151
return float32_gen2(a, b, s, hard_f32_mul, soft_f32_mul,
2152
f32_is_zon2, f32_addsubmul_post);
2156
float64_mul(float64 a, float64 b, float_status *s)
2158
return float64_gen2(a, b, s, hard_f64_mul, soft_f64_mul,
2159
f64_is_zon2, f64_addsubmul_post);
2162
float64 float64r32_mul(float64 a, float64 b, float_status *status)
2164
FloatParts64 pa, pb, *pr;
2166
float64_unpack_canonical(&pa, a, status);
2167
float64_unpack_canonical(&pb, b, status);
2168
pr = parts_mul(&pa, &pb, status);
2170
return float64r32_round_pack_canonical(pr, status);
2173
bfloat16 QEMU_FLATTEN
2174
bfloat16_mul(bfloat16 a, bfloat16 b, float_status *status)
2176
FloatParts64 pa, pb, *pr;
2178
bfloat16_unpack_canonical(&pa, a, status);
2179
bfloat16_unpack_canonical(&pb, b, status);
2180
pr = parts_mul(&pa, &pb, status);
2182
return bfloat16_round_pack_canonical(pr, status);
2185
float128 QEMU_FLATTEN
2186
float128_mul(float128 a, float128 b, float_status *status)
2188
FloatParts128 pa, pb, *pr;
2190
float128_unpack_canonical(&pa, a, status);
2191
float128_unpack_canonical(&pb, b, status);
2192
pr = parts_mul(&pa, &pb, status);
2194
return float128_round_pack_canonical(pr, status);
2197
floatx80 QEMU_FLATTEN
2198
floatx80_mul(floatx80 a, floatx80 b, float_status *status)
2200
FloatParts128 pa, pb, *pr;
2202
if (!floatx80_unpack_canonical(&pa, a, status) ||
2203
!floatx80_unpack_canonical(&pb, b, status)) {
2204
return floatx80_default_nan(status);
2207
pr = parts_mul(&pa, &pb, status);
2208
return floatx80_round_pack_canonical(pr, status);
2215
float16 QEMU_FLATTEN float16_muladd(float16 a, float16 b, float16 c,
2216
int flags, float_status *status)
2218
FloatParts64 pa, pb, pc, *pr;
2220
float16_unpack_canonical(&pa, a, status);
2221
float16_unpack_canonical(&pb, b, status);
2222
float16_unpack_canonical(&pc, c, status);
2223
pr = parts_muladd(&pa, &pb, &pc, flags, status);
2225
return float16_round_pack_canonical(pr, status);
2228
static float32 QEMU_SOFTFLOAT_ATTR
2229
soft_f32_muladd(float32 a, float32 b, float32 c, int flags,
2230
float_status *status)
2232
FloatParts64 pa, pb, pc, *pr;
2234
float32_unpack_canonical(&pa, a, status);
2235
float32_unpack_canonical(&pb, b, status);
2236
float32_unpack_canonical(&pc, c, status);
2237
pr = parts_muladd(&pa, &pb, &pc, flags, status);
2239
return float32_round_pack_canonical(pr, status);
2242
static float64 QEMU_SOFTFLOAT_ATTR
2243
soft_f64_muladd(float64 a, float64 b, float64 c, int flags,
2244
float_status *status)
2246
FloatParts64 pa, pb, pc, *pr;
2248
float64_unpack_canonical(&pa, a, status);
2249
float64_unpack_canonical(&pb, b, status);
2250
float64_unpack_canonical(&pc, c, status);
2251
pr = parts_muladd(&pa, &pb, &pc, flags, status);
2253
return float64_round_pack_canonical(pr, status);
2256
static bool force_soft_fma;
2259
float32_muladd(float32 xa, float32 xb, float32 xc, int flags, float_status *s)
2261
union_float32 ua, ub, uc, ur;
2267
if (unlikely(!can_use_fpu(s))) {
2270
if (unlikely(flags & float_muladd_halve_result)) {
2274
float32_input_flush3(&ua.s, &ub.s, &uc.s, s);
2275
if (unlikely(!f32_is_zon3(ua, ub, uc))) {
2279
if (unlikely(force_soft_fma)) {
2287
if (float32_is_zero(ua.s) || float32_is_zero(ub.s)) {
2291
prod_sign = float32_is_neg(ua.s) ^ float32_is_neg(ub.s);
2292
prod_sign ^= !!(flags & float_muladd_negate_product);
2293
up.s = float32_set_sign(float32_zero, prod_sign);
2295
if (flags & float_muladd_negate_c) {
2300
union_float32 ua_orig = ua;
2301
union_float32 uc_orig = uc;
2303
if (flags & float_muladd_negate_product) {
2306
if (flags & float_muladd_negate_c) {
2310
ur.h = fmaf(ua.h, ub.h, uc.h);
2312
if (unlikely(f32_is_inf(ur))) {
2313
float_raise(float_flag_overflow, s);
2314
} else if (unlikely(fabsf(ur.h) <= FLT_MIN)) {
2320
if (flags & float_muladd_negate_result) {
2321
return float32_chs(ur.s);
2326
return soft_f32_muladd(ua.s, ub.s, uc.s, flags, s);
2330
float64_muladd(float64 xa, float64 xb, float64 xc, int flags, float_status *s)
2332
union_float64 ua, ub, uc, ur;
2338
if (unlikely(!can_use_fpu(s))) {
2341
if (unlikely(flags & float_muladd_halve_result)) {
2345
float64_input_flush3(&ua.s, &ub.s, &uc.s, s);
2346
if (unlikely(!f64_is_zon3(ua, ub, uc))) {
2350
if (unlikely(force_soft_fma)) {
2358
if (float64_is_zero(ua.s) || float64_is_zero(ub.s)) {
2362
prod_sign = float64_is_neg(ua.s) ^ float64_is_neg(ub.s);
2363
prod_sign ^= !!(flags & float_muladd_negate_product);
2364
up.s = float64_set_sign(float64_zero, prod_sign);
2366
if (flags & float_muladd_negate_c) {
2371
union_float64 ua_orig = ua;
2372
union_float64 uc_orig = uc;
2374
if (flags & float_muladd_negate_product) {
2377
if (flags & float_muladd_negate_c) {
2381
ur.h = fma(ua.h, ub.h, uc.h);
2383
if (unlikely(f64_is_inf(ur))) {
2384
float_raise(float_flag_overflow, s);
2385
} else if (unlikely(fabs(ur.h) <= FLT_MIN)) {
2391
if (flags & float_muladd_negate_result) {
2392
return float64_chs(ur.s);
2397
return soft_f64_muladd(ua.s, ub.s, uc.s, flags, s);
2400
float64 float64r32_muladd(float64 a, float64 b, float64 c,
2401
int flags, float_status *status)
2403
FloatParts64 pa, pb, pc, *pr;
2405
float64_unpack_canonical(&pa, a, status);
2406
float64_unpack_canonical(&pb, b, status);
2407
float64_unpack_canonical(&pc, c, status);
2408
pr = parts_muladd(&pa, &pb, &pc, flags, status);
2410
return float64r32_round_pack_canonical(pr, status);
2413
bfloat16 QEMU_FLATTEN bfloat16_muladd(bfloat16 a, bfloat16 b, bfloat16 c,
2414
int flags, float_status *status)
2416
FloatParts64 pa, pb, pc, *pr;
2418
bfloat16_unpack_canonical(&pa, a, status);
2419
bfloat16_unpack_canonical(&pb, b, status);
2420
bfloat16_unpack_canonical(&pc, c, status);
2421
pr = parts_muladd(&pa, &pb, &pc, flags, status);
2423
return bfloat16_round_pack_canonical(pr, status);
2426
float128 QEMU_FLATTEN float128_muladd(float128 a, float128 b, float128 c,
2427
int flags, float_status *status)
2429
FloatParts128 pa, pb, pc, *pr;
2431
float128_unpack_canonical(&pa, a, status);
2432
float128_unpack_canonical(&pb, b, status);
2433
float128_unpack_canonical(&pc, c, status);
2434
pr = parts_muladd(&pa, &pb, &pc, flags, status);
2436
return float128_round_pack_canonical(pr, status);
2443
float16 float16_div(float16 a, float16 b, float_status *status)
2445
FloatParts64 pa, pb, *pr;
2447
float16_unpack_canonical(&pa, a, status);
2448
float16_unpack_canonical(&pb, b, status);
2449
pr = parts_div(&pa, &pb, status);
2451
return float16_round_pack_canonical(pr, status);
2454
static float32 QEMU_SOFTFLOAT_ATTR
2455
soft_f32_div(float32 a, float32 b, float_status *status)
2457
FloatParts64 pa, pb, *pr;
2459
float32_unpack_canonical(&pa, a, status);
2460
float32_unpack_canonical(&pb, b, status);
2461
pr = parts_div(&pa, &pb, status);
2463
return float32_round_pack_canonical(pr, status);
2466
static float64 QEMU_SOFTFLOAT_ATTR
2467
soft_f64_div(float64 a, float64 b, float_status *status)
2469
FloatParts64 pa, pb, *pr;
2471
float64_unpack_canonical(&pa, a, status);
2472
float64_unpack_canonical(&pb, b, status);
2473
pr = parts_div(&pa, &pb, status);
2475
return float64_round_pack_canonical(pr, status);
2478
static float hard_f32_div(float a, float b)
2483
static double hard_f64_div(double a, double b)
2488
static bool f32_div_pre(union_float32 a, union_float32 b)
2490
if (QEMU_HARDFLOAT_2F32_USE_FP) {
2491
return (fpclassify(a.h) == FP_NORMAL || fpclassify(a.h) == FP_ZERO) &&
2492
fpclassify(b.h) == FP_NORMAL;
2494
return float32_is_zero_or_normal(a.s) && float32_is_normal(b.s);
2497
static bool f64_div_pre(union_float64 a, union_float64 b)
2499
if (QEMU_HARDFLOAT_2F64_USE_FP) {
2500
return (fpclassify(a.h) == FP_NORMAL || fpclassify(a.h) == FP_ZERO) &&
2501
fpclassify(b.h) == FP_NORMAL;
2503
return float64_is_zero_or_normal(a.s) && float64_is_normal(b.s);
2506
static bool f32_div_post(union_float32 a, union_float32 b)
2508
if (QEMU_HARDFLOAT_2F32_USE_FP) {
2509
return fpclassify(a.h) != FP_ZERO;
2511
return !float32_is_zero(a.s);
2514
static bool f64_div_post(union_float64 a, union_float64 b)
2516
if (QEMU_HARDFLOAT_2F64_USE_FP) {
2517
return fpclassify(a.h) != FP_ZERO;
2519
return !float64_is_zero(a.s);
2523
float32_div(float32 a, float32 b, float_status *s)
2525
return float32_gen2(a, b, s, hard_f32_div, soft_f32_div,
2526
f32_div_pre, f32_div_post);
2530
float64_div(float64 a, float64 b, float_status *s)
2532
return float64_gen2(a, b, s, hard_f64_div, soft_f64_div,
2533
f64_div_pre, f64_div_post);
2536
float64 float64r32_div(float64 a, float64 b, float_status *status)
2538
FloatParts64 pa, pb, *pr;
2540
float64_unpack_canonical(&pa, a, status);
2541
float64_unpack_canonical(&pb, b, status);
2542
pr = parts_div(&pa, &pb, status);
2544
return float64r32_round_pack_canonical(pr, status);
2547
bfloat16 QEMU_FLATTEN
2548
bfloat16_div(bfloat16 a, bfloat16 b, float_status *status)
2550
FloatParts64 pa, pb, *pr;
2552
bfloat16_unpack_canonical(&pa, a, status);
2553
bfloat16_unpack_canonical(&pb, b, status);
2554
pr = parts_div(&pa, &pb, status);
2556
return bfloat16_round_pack_canonical(pr, status);
2559
float128 QEMU_FLATTEN
2560
float128_div(float128 a, float128 b, float_status *status)
2562
FloatParts128 pa, pb, *pr;
2564
float128_unpack_canonical(&pa, a, status);
2565
float128_unpack_canonical(&pb, b, status);
2566
pr = parts_div(&pa, &pb, status);
2568
return float128_round_pack_canonical(pr, status);
2571
floatx80 floatx80_div(floatx80 a, floatx80 b, float_status *status)
2573
FloatParts128 pa, pb, *pr;
2575
if (!floatx80_unpack_canonical(&pa, a, status) ||
2576
!floatx80_unpack_canonical(&pb, b, status)) {
2577
return floatx80_default_nan(status);
2580
pr = parts_div(&pa, &pb, status);
2581
return floatx80_round_pack_canonical(pr, status);
2588
float32 float32_rem(float32 a, float32 b, float_status *status)
2590
FloatParts64 pa, pb, *pr;
2592
float32_unpack_canonical(&pa, a, status);
2593
float32_unpack_canonical(&pb, b, status);
2594
pr = parts_modrem(&pa, &pb, NULL, status);
2596
return float32_round_pack_canonical(pr, status);
2599
float64 float64_rem(float64 a, float64 b, float_status *status)
2601
FloatParts64 pa, pb, *pr;
2603
float64_unpack_canonical(&pa, a, status);
2604
float64_unpack_canonical(&pb, b, status);
2605
pr = parts_modrem(&pa, &pb, NULL, status);
2607
return float64_round_pack_canonical(pr, status);
2610
float128 float128_rem(float128 a, float128 b, float_status *status)
2612
FloatParts128 pa, pb, *pr;
2614
float128_unpack_canonical(&pa, a, status);
2615
float128_unpack_canonical(&pb, b, status);
2616
pr = parts_modrem(&pa, &pb, NULL, status);
2618
return float128_round_pack_canonical(pr, status);
2630
floatx80 floatx80_modrem(floatx80 a, floatx80 b, bool mod,
2631
uint64_t *quotient, float_status *status)
2633
FloatParts128 pa, pb, *pr;
2636
if (!floatx80_unpack_canonical(&pa, a, status) ||
2637
!floatx80_unpack_canonical(&pb, b, status)) {
2638
return floatx80_default_nan(status);
2640
pr = parts_modrem(&pa, &pb, mod ? quotient : NULL, status);
2642
return floatx80_round_pack_canonical(pr, status);
2645
floatx80 floatx80_rem(floatx80 a, floatx80 b, float_status *status)
2648
return floatx80_modrem(a, b, false, "ient, status);
2651
floatx80 floatx80_mod(floatx80 a, floatx80 b, float_status *status)
2654
return floatx80_modrem(a, b, true, "ient, status);
2668
static void parts_float_to_ahp(FloatParts64 *a, float_status *s)
2671
case float_class_snan:
2672
float_raise(float_flag_invalid_snan, s);
2674
case float_class_qnan:
2679
float_raise(float_flag_invalid, s);
2680
a->cls = float_class_zero;
2683
case float_class_inf:
2688
float_raise(float_flag_invalid, s);
2689
a->cls = float_class_normal;
2690
a->exp = float16_params_ahp.exp_max;
2691
a->frac = MAKE_64BIT_MASK(float16_params_ahp.frac_shift,
2692
float16_params_ahp.frac_size + 1);
2695
case float_class_normal:
2696
case float_class_zero:
2700
g_assert_not_reached();
2704
static void parts64_float_to_float(FloatParts64 *a, float_status *s)
2706
if (is_nan(a->cls)) {
2707
parts_return_nan(a, s);
2711
static void parts128_float_to_float(FloatParts128 *a, float_status *s)
2713
if (is_nan(a->cls)) {
2714
parts_return_nan(a, s);
2718
#define parts_float_to_float(P, S) \
2719
PARTS_GENERIC_64_128(float_to_float, P)(P, S)
2721
static void parts_float_to_float_narrow(FloatParts64 *a, FloatParts128 *b,
2728
if (a->cls == float_class_normal) {
2729
frac_truncjam(a, b);
2730
} else if (is_nan(a->cls)) {
2732
a->frac = b->frac_hi;
2733
parts_return_nan(a, s);
2737
static void parts_float_to_float_widen(FloatParts128 *a, FloatParts64 *b,
2745
if (is_nan(a->cls)) {
2746
parts_return_nan(a, s);
2750
float32 float16_to_float32(float16 a, bool ieee, float_status *s)
2752
const FloatFmt *fmt16 = ieee ? &float16_params : &float16_params_ahp;
2755
float16a_unpack_canonical(&p, a, s, fmt16);
2756
parts_float_to_float(&p, s);
2757
return float32_round_pack_canonical(&p, s);
2760
float64 float16_to_float64(float16 a, bool ieee, float_status *s)
2762
const FloatFmt *fmt16 = ieee ? &float16_params : &float16_params_ahp;
2765
float16a_unpack_canonical(&p, a, s, fmt16);
2766
parts_float_to_float(&p, s);
2767
return float64_round_pack_canonical(&p, s);
2770
float16 float32_to_float16(float32 a, bool ieee, float_status *s)
2773
const FloatFmt *fmt;
2775
float32_unpack_canonical(&p, a, s);
2777
parts_float_to_float(&p, s);
2778
fmt = &float16_params;
2780
parts_float_to_ahp(&p, s);
2781
fmt = &float16_params_ahp;
2783
return float16a_round_pack_canonical(&p, s, fmt);
2786
static float64 QEMU_SOFTFLOAT_ATTR
2787
soft_float32_to_float64(float32 a, float_status *s)
2791
float32_unpack_canonical(&p, a, s);
2792
parts_float_to_float(&p, s);
2793
return float64_round_pack_canonical(&p, s);
2796
float64 float32_to_float64(float32 a, float_status *s)
2798
if (likely(float32_is_normal(a))) {
2805
} else if (float32_is_zero(a)) {
2806
return float64_set_sign(float64_zero, float32_is_neg(a));
2808
return soft_float32_to_float64(a, s);
2812
float16 float64_to_float16(float64 a, bool ieee, float_status *s)
2815
const FloatFmt *fmt;
2817
float64_unpack_canonical(&p, a, s);
2819
parts_float_to_float(&p, s);
2820
fmt = &float16_params;
2822
parts_float_to_ahp(&p, s);
2823
fmt = &float16_params_ahp;
2825
return float16a_round_pack_canonical(&p, s, fmt);
2828
float32 float64_to_float32(float64 a, float_status *s)
2832
float64_unpack_canonical(&p, a, s);
2833
parts_float_to_float(&p, s);
2834
return float32_round_pack_canonical(&p, s);
2837
float32 bfloat16_to_float32(bfloat16 a, float_status *s)
2841
bfloat16_unpack_canonical(&p, a, s);
2842
parts_float_to_float(&p, s);
2843
return float32_round_pack_canonical(&p, s);
2846
float64 bfloat16_to_float64(bfloat16 a, float_status *s)
2850
bfloat16_unpack_canonical(&p, a, s);
2851
parts_float_to_float(&p, s);
2852
return float64_round_pack_canonical(&p, s);
2855
bfloat16 float32_to_bfloat16(float32 a, float_status *s)
2859
float32_unpack_canonical(&p, a, s);
2860
parts_float_to_float(&p, s);
2861
return bfloat16_round_pack_canonical(&p, s);
2864
bfloat16 float64_to_bfloat16(float64 a, float_status *s)
2868
float64_unpack_canonical(&p, a, s);
2869
parts_float_to_float(&p, s);
2870
return bfloat16_round_pack_canonical(&p, s);
2873
float32 float128_to_float32(float128 a, float_status *s)
2878
float128_unpack_canonical(&p128, a, s);
2879
parts_float_to_float_narrow(&p64, &p128, s);
2880
return float32_round_pack_canonical(&p64, s);
2883
float64 float128_to_float64(float128 a, float_status *s)
2888
float128_unpack_canonical(&p128, a, s);
2889
parts_float_to_float_narrow(&p64, &p128, s);
2890
return float64_round_pack_canonical(&p64, s);
2893
float128 float32_to_float128(float32 a, float_status *s)
2898
float32_unpack_canonical(&p64, a, s);
2899
parts_float_to_float_widen(&p128, &p64, s);
2900
return float128_round_pack_canonical(&p128, s);
2903
float128 float64_to_float128(float64 a, float_status *s)
2908
float64_unpack_canonical(&p64, a, s);
2909
parts_float_to_float_widen(&p128, &p64, s);
2910
return float128_round_pack_canonical(&p128, s);
2913
float32 floatx80_to_float32(floatx80 a, float_status *s)
2918
if (floatx80_unpack_canonical(&p128, a, s)) {
2919
parts_float_to_float_narrow(&p64, &p128, s);
2921
parts_default_nan(&p64, s);
2923
return float32_round_pack_canonical(&p64, s);
2926
float64 floatx80_to_float64(floatx80 a, float_status *s)
2931
if (floatx80_unpack_canonical(&p128, a, s)) {
2932
parts_float_to_float_narrow(&p64, &p128, s);
2934
parts_default_nan(&p64, s);
2936
return float64_round_pack_canonical(&p64, s);
2939
float128 floatx80_to_float128(floatx80 a, float_status *s)
2943
if (floatx80_unpack_canonical(&p, a, s)) {
2944
parts_float_to_float(&p, s);
2946
parts_default_nan(&p, s);
2948
return float128_round_pack_canonical(&p, s);
2951
floatx80 float32_to_floatx80(float32 a, float_status *s)
2956
float32_unpack_canonical(&p64, a, s);
2957
parts_float_to_float_widen(&p128, &p64, s);
2958
return floatx80_round_pack_canonical(&p128, s);
2961
floatx80 float64_to_floatx80(float64 a, float_status *s)
2966
float64_unpack_canonical(&p64, a, s);
2967
parts_float_to_float_widen(&p128, &p64, s);
2968
return floatx80_round_pack_canonical(&p128, s);
2971
floatx80 float128_to_floatx80(float128 a, float_status *s)
2975
float128_unpack_canonical(&p, a, s);
2976
parts_float_to_float(&p, s);
2977
return floatx80_round_pack_canonical(&p, s);
2984
float16 float16_round_to_int(float16 a, float_status *s)
2988
float16_unpack_canonical(&p, a, s);
2989
parts_round_to_int(&p, s->float_rounding_mode, 0, s, &float16_params);
2990
return float16_round_pack_canonical(&p, s);
2993
float32 float32_round_to_int(float32 a, float_status *s)
2997
float32_unpack_canonical(&p, a, s);
2998
parts_round_to_int(&p, s->float_rounding_mode, 0, s, &float32_params);
2999
return float32_round_pack_canonical(&p, s);
3002
float64 float64_round_to_int(float64 a, float_status *s)
3006
float64_unpack_canonical(&p, a, s);
3007
parts_round_to_int(&p, s->float_rounding_mode, 0, s, &float64_params);
3008
return float64_round_pack_canonical(&p, s);
3011
bfloat16 bfloat16_round_to_int(bfloat16 a, float_status *s)
3015
bfloat16_unpack_canonical(&p, a, s);
3016
parts_round_to_int(&p, s->float_rounding_mode, 0, s, &bfloat16_params);
3017
return bfloat16_round_pack_canonical(&p, s);
3020
float128 float128_round_to_int(float128 a, float_status *s)
3024
float128_unpack_canonical(&p, a, s);
3025
parts_round_to_int(&p, s->float_rounding_mode, 0, s, &float128_params);
3026
return float128_round_pack_canonical(&p, s);
3029
floatx80 floatx80_round_to_int(floatx80 a, float_status *status)
3033
if (!floatx80_unpack_canonical(&p, a, status)) {
3034
return floatx80_default_nan(status);
3037
parts_round_to_int(&p, status->float_rounding_mode, 0, status,
3038
&floatx80_params[status->floatx80_rounding_precision]);
3039
return floatx80_round_pack_canonical(&p, status);
3046
int8_t float16_to_int8_scalbn(float16 a, FloatRoundMode rmode, int scale,
3051
float16_unpack_canonical(&p, a, s);
3052
return parts_float_to_sint(&p, rmode, scale, INT8_MIN, INT8_MAX, s);
3055
int16_t float16_to_int16_scalbn(float16 a, FloatRoundMode rmode, int scale,
3060
float16_unpack_canonical(&p, a, s);
3061
return parts_float_to_sint(&p, rmode, scale, INT16_MIN, INT16_MAX, s);
3064
int32_t float16_to_int32_scalbn(float16 a, FloatRoundMode rmode, int scale,
3069
float16_unpack_canonical(&p, a, s);
3070
return parts_float_to_sint(&p, rmode, scale, INT32_MIN, INT32_MAX, s);
3073
int64_t float16_to_int64_scalbn(float16 a, FloatRoundMode rmode, int scale,
3078
float16_unpack_canonical(&p, a, s);
3079
return parts_float_to_sint(&p, rmode, scale, INT64_MIN, INT64_MAX, s);
3082
int16_t float32_to_int16_scalbn(float32 a, FloatRoundMode rmode, int scale,
3087
float32_unpack_canonical(&p, a, s);
3088
return parts_float_to_sint(&p, rmode, scale, INT16_MIN, INT16_MAX, s);
3091
int32_t float32_to_int32_scalbn(float32 a, FloatRoundMode rmode, int scale,
3096
float32_unpack_canonical(&p, a, s);
3097
return parts_float_to_sint(&p, rmode, scale, INT32_MIN, INT32_MAX, s);
3100
int64_t float32_to_int64_scalbn(float32 a, FloatRoundMode rmode, int scale,
3105
float32_unpack_canonical(&p, a, s);
3106
return parts_float_to_sint(&p, rmode, scale, INT64_MIN, INT64_MAX, s);
3109
int16_t float64_to_int16_scalbn(float64 a, FloatRoundMode rmode, int scale,
3114
float64_unpack_canonical(&p, a, s);
3115
return parts_float_to_sint(&p, rmode, scale, INT16_MIN, INT16_MAX, s);
3118
int32_t float64_to_int32_scalbn(float64 a, FloatRoundMode rmode, int scale,
3123
float64_unpack_canonical(&p, a, s);
3124
return parts_float_to_sint(&p, rmode, scale, INT32_MIN, INT32_MAX, s);
3127
int64_t float64_to_int64_scalbn(float64 a, FloatRoundMode rmode, int scale,
3132
float64_unpack_canonical(&p, a, s);
3133
return parts_float_to_sint(&p, rmode, scale, INT64_MIN, INT64_MAX, s);
3136
int8_t bfloat16_to_int8_scalbn(bfloat16 a, FloatRoundMode rmode, int scale,
3141
bfloat16_unpack_canonical(&p, a, s);
3142
return parts_float_to_sint(&p, rmode, scale, INT8_MIN, INT8_MAX, s);
3145
int16_t bfloat16_to_int16_scalbn(bfloat16 a, FloatRoundMode rmode, int scale,
3150
bfloat16_unpack_canonical(&p, a, s);
3151
return parts_float_to_sint(&p, rmode, scale, INT16_MIN, INT16_MAX, s);
3154
int32_t bfloat16_to_int32_scalbn(bfloat16 a, FloatRoundMode rmode, int scale,
3159
bfloat16_unpack_canonical(&p, a, s);
3160
return parts_float_to_sint(&p, rmode, scale, INT32_MIN, INT32_MAX, s);
3163
int64_t bfloat16_to_int64_scalbn(bfloat16 a, FloatRoundMode rmode, int scale,
3168
bfloat16_unpack_canonical(&p, a, s);
3169
return parts_float_to_sint(&p, rmode, scale, INT64_MIN, INT64_MAX, s);
3172
static int32_t float128_to_int32_scalbn(float128 a, FloatRoundMode rmode,
3173
int scale, float_status *s)
3177
float128_unpack_canonical(&p, a, s);
3178
return parts_float_to_sint(&p, rmode, scale, INT32_MIN, INT32_MAX, s);
3181
static int64_t float128_to_int64_scalbn(float128 a, FloatRoundMode rmode,
3182
int scale, float_status *s)
3186
float128_unpack_canonical(&p, a, s);
3187
return parts_float_to_sint(&p, rmode, scale, INT64_MIN, INT64_MAX, s);
3190
static Int128 float128_to_int128_scalbn(float128 a, FloatRoundMode rmode,
3191
int scale, float_status *s)
3197
float128_unpack_canonical(&p, a, s);
3200
case float_class_snan:
3201
flags |= float_flag_invalid_snan;
3203
case float_class_qnan:
3204
flags |= float_flag_invalid;
3208
case float_class_inf:
3209
flags = float_flag_invalid | float_flag_invalid_cvti;
3210
r = p.sign ? INT128_MIN : INT128_MAX;
3213
case float_class_zero:
3214
return int128_zero();
3216
case float_class_normal:
3217
if (parts_round_to_int_normal(&p, rmode, scale, 128 - 2)) {
3218
flags = float_flag_inexact;
3222
int shift = 127 - p.exp;
3223
r = int128_urshift(int128_make128(p.frac_lo, p.frac_hi), shift);
3227
} else if (p.exp == 127 && p.sign && p.frac_lo == 0 &&
3228
p.frac_hi == DECOMPOSED_IMPLICIT_BIT) {
3231
flags = float_flag_invalid | float_flag_invalid_cvti;
3232
r = p.sign ? INT128_MIN : INT128_MAX;
3237
g_assert_not_reached();
3240
float_raise(flags, s);
3244
static int32_t floatx80_to_int32_scalbn(floatx80 a, FloatRoundMode rmode,
3245
int scale, float_status *s)
3249
if (!floatx80_unpack_canonical(&p, a, s)) {
3250
parts_default_nan(&p, s);
3252
return parts_float_to_sint(&p, rmode, scale, INT32_MIN, INT32_MAX, s);
3255
static int64_t floatx80_to_int64_scalbn(floatx80 a, FloatRoundMode rmode,
3256
int scale, float_status *s)
3260
if (!floatx80_unpack_canonical(&p, a, s)) {
3261
parts_default_nan(&p, s);
3263
return parts_float_to_sint(&p, rmode, scale, INT64_MIN, INT64_MAX, s);
3266
int8_t float16_to_int8(float16 a, float_status *s)
3268
return float16_to_int8_scalbn(a, s->float_rounding_mode, 0, s);
3271
int16_t float16_to_int16(float16 a, float_status *s)
3273
return float16_to_int16_scalbn(a, s->float_rounding_mode, 0, s);
3276
int32_t float16_to_int32(float16 a, float_status *s)
3278
return float16_to_int32_scalbn(a, s->float_rounding_mode, 0, s);
3281
int64_t float16_to_int64(float16 a, float_status *s)
3283
return float16_to_int64_scalbn(a, s->float_rounding_mode, 0, s);
3286
int16_t float32_to_int16(float32 a, float_status *s)
3288
return float32_to_int16_scalbn(a, s->float_rounding_mode, 0, s);
3291
int32_t float32_to_int32(float32 a, float_status *s)
3293
return float32_to_int32_scalbn(a, s->float_rounding_mode, 0, s);
3296
int64_t float32_to_int64(float32 a, float_status *s)
3298
return float32_to_int64_scalbn(a, s->float_rounding_mode, 0, s);
3301
int16_t float64_to_int16(float64 a, float_status *s)
3303
return float64_to_int16_scalbn(a, s->float_rounding_mode, 0, s);
3306
int32_t float64_to_int32(float64 a, float_status *s)
3308
return float64_to_int32_scalbn(a, s->float_rounding_mode, 0, s);
3311
int64_t float64_to_int64(float64 a, float_status *s)
3313
return float64_to_int64_scalbn(a, s->float_rounding_mode, 0, s);
3316
int32_t float128_to_int32(float128 a, float_status *s)
3318
return float128_to_int32_scalbn(a, s->float_rounding_mode, 0, s);
3321
int64_t float128_to_int64(float128 a, float_status *s)
3323
return float128_to_int64_scalbn(a, s->float_rounding_mode, 0, s);
3326
Int128 float128_to_int128(float128 a, float_status *s)
3328
return float128_to_int128_scalbn(a, s->float_rounding_mode, 0, s);
3331
int32_t floatx80_to_int32(floatx80 a, float_status *s)
3333
return floatx80_to_int32_scalbn(a, s->float_rounding_mode, 0, s);
3336
int64_t floatx80_to_int64(floatx80 a, float_status *s)
3338
return floatx80_to_int64_scalbn(a, s->float_rounding_mode, 0, s);
3341
int16_t float16_to_int16_round_to_zero(float16 a, float_status *s)
3343
return float16_to_int16_scalbn(a, float_round_to_zero, 0, s);
3346
int32_t float16_to_int32_round_to_zero(float16 a, float_status *s)
3348
return float16_to_int32_scalbn(a, float_round_to_zero, 0, s);
3351
int64_t float16_to_int64_round_to_zero(float16 a, float_status *s)
3353
return float16_to_int64_scalbn(a, float_round_to_zero, 0, s);
3356
int16_t float32_to_int16_round_to_zero(float32 a, float_status *s)
3358
return float32_to_int16_scalbn(a, float_round_to_zero, 0, s);
3361
int32_t float32_to_int32_round_to_zero(float32 a, float_status *s)
3363
return float32_to_int32_scalbn(a, float_round_to_zero, 0, s);
3366
int64_t float32_to_int64_round_to_zero(float32 a, float_status *s)
3368
return float32_to_int64_scalbn(a, float_round_to_zero, 0, s);
3371
int16_t float64_to_int16_round_to_zero(float64 a, float_status *s)
3373
return float64_to_int16_scalbn(a, float_round_to_zero, 0, s);
3376
int32_t float64_to_int32_round_to_zero(float64 a, float_status *s)
3378
return float64_to_int32_scalbn(a, float_round_to_zero, 0, s);
3381
int64_t float64_to_int64_round_to_zero(float64 a, float_status *s)
3383
return float64_to_int64_scalbn(a, float_round_to_zero, 0, s);
3386
int32_t float128_to_int32_round_to_zero(float128 a, float_status *s)
3388
return float128_to_int32_scalbn(a, float_round_to_zero, 0, s);
3391
int64_t float128_to_int64_round_to_zero(float128 a, float_status *s)
3393
return float128_to_int64_scalbn(a, float_round_to_zero, 0, s);
3396
Int128 float128_to_int128_round_to_zero(float128 a, float_status *s)
3398
return float128_to_int128_scalbn(a, float_round_to_zero, 0, s);
3401
int32_t floatx80_to_int32_round_to_zero(floatx80 a, float_status *s)
3403
return floatx80_to_int32_scalbn(a, float_round_to_zero, 0, s);
3406
int64_t floatx80_to_int64_round_to_zero(floatx80 a, float_status *s)
3408
return floatx80_to_int64_scalbn(a, float_round_to_zero, 0, s);
3411
int8_t bfloat16_to_int8(bfloat16 a, float_status *s)
3413
return bfloat16_to_int8_scalbn(a, s->float_rounding_mode, 0, s);
3416
int16_t bfloat16_to_int16(bfloat16 a, float_status *s)
3418
return bfloat16_to_int16_scalbn(a, s->float_rounding_mode, 0, s);
3421
int32_t bfloat16_to_int32(bfloat16 a, float_status *s)
3423
return bfloat16_to_int32_scalbn(a, s->float_rounding_mode, 0, s);
3426
int64_t bfloat16_to_int64(bfloat16 a, float_status *s)
3428
return bfloat16_to_int64_scalbn(a, s->float_rounding_mode, 0, s);
3431
int8_t bfloat16_to_int8_round_to_zero(bfloat16 a, float_status *s)
3433
return bfloat16_to_int8_scalbn(a, float_round_to_zero, 0, s);
3436
int16_t bfloat16_to_int16_round_to_zero(bfloat16 a, float_status *s)
3438
return bfloat16_to_int16_scalbn(a, float_round_to_zero, 0, s);
3441
int32_t bfloat16_to_int32_round_to_zero(bfloat16 a, float_status *s)
3443
return bfloat16_to_int32_scalbn(a, float_round_to_zero, 0, s);
3446
int64_t bfloat16_to_int64_round_to_zero(bfloat16 a, float_status *s)
3448
return bfloat16_to_int64_scalbn(a, float_round_to_zero, 0, s);
3451
int32_t float64_to_int32_modulo(float64 a, FloatRoundMode rmode,
3456
float64_unpack_canonical(&p, a, s);
3457
return parts_float_to_sint_modulo(&p, rmode, 31, s);
3460
int64_t float64_to_int64_modulo(float64 a, FloatRoundMode rmode,
3465
float64_unpack_canonical(&p, a, s);
3466
return parts_float_to_sint_modulo(&p, rmode, 63, s);
3473
uint8_t float16_to_uint8_scalbn(float16 a, FloatRoundMode rmode, int scale,
3478
float16_unpack_canonical(&p, a, s);
3479
return parts_float_to_uint(&p, rmode, scale, UINT8_MAX, s);
3482
uint16_t float16_to_uint16_scalbn(float16 a, FloatRoundMode rmode, int scale,
3487
float16_unpack_canonical(&p, a, s);
3488
return parts_float_to_uint(&p, rmode, scale, UINT16_MAX, s);
3491
uint32_t float16_to_uint32_scalbn(float16 a, FloatRoundMode rmode, int scale,
3496
float16_unpack_canonical(&p, a, s);
3497
return parts_float_to_uint(&p, rmode, scale, UINT32_MAX, s);
3500
uint64_t float16_to_uint64_scalbn(float16 a, FloatRoundMode rmode, int scale,
3505
float16_unpack_canonical(&p, a, s);
3506
return parts_float_to_uint(&p, rmode, scale, UINT64_MAX, s);
3509
uint16_t float32_to_uint16_scalbn(float32 a, FloatRoundMode rmode, int scale,
3514
float32_unpack_canonical(&p, a, s);
3515
return parts_float_to_uint(&p, rmode, scale, UINT16_MAX, s);
3518
uint32_t float32_to_uint32_scalbn(float32 a, FloatRoundMode rmode, int scale,
3523
float32_unpack_canonical(&p, a, s);
3524
return parts_float_to_uint(&p, rmode, scale, UINT32_MAX, s);
3527
uint64_t float32_to_uint64_scalbn(float32 a, FloatRoundMode rmode, int scale,
3532
float32_unpack_canonical(&p, a, s);
3533
return parts_float_to_uint(&p, rmode, scale, UINT64_MAX, s);
3536
uint16_t float64_to_uint16_scalbn(float64 a, FloatRoundMode rmode, int scale,
3541
float64_unpack_canonical(&p, a, s);
3542
return parts_float_to_uint(&p, rmode, scale, UINT16_MAX, s);
3545
uint32_t float64_to_uint32_scalbn(float64 a, FloatRoundMode rmode, int scale,
3550
float64_unpack_canonical(&p, a, s);
3551
return parts_float_to_uint(&p, rmode, scale, UINT32_MAX, s);
3554
uint64_t float64_to_uint64_scalbn(float64 a, FloatRoundMode rmode, int scale,
3559
float64_unpack_canonical(&p, a, s);
3560
return parts_float_to_uint(&p, rmode, scale, UINT64_MAX, s);
3563
uint8_t bfloat16_to_uint8_scalbn(bfloat16 a, FloatRoundMode rmode,
3564
int scale, float_status *s)
3568
bfloat16_unpack_canonical(&p, a, s);
3569
return parts_float_to_uint(&p, rmode, scale, UINT8_MAX, s);
3572
uint16_t bfloat16_to_uint16_scalbn(bfloat16 a, FloatRoundMode rmode,
3573
int scale, float_status *s)
3577
bfloat16_unpack_canonical(&p, a, s);
3578
return parts_float_to_uint(&p, rmode, scale, UINT16_MAX, s);
3581
uint32_t bfloat16_to_uint32_scalbn(bfloat16 a, FloatRoundMode rmode,
3582
int scale, float_status *s)
3586
bfloat16_unpack_canonical(&p, a, s);
3587
return parts_float_to_uint(&p, rmode, scale, UINT32_MAX, s);
3590
uint64_t bfloat16_to_uint64_scalbn(bfloat16 a, FloatRoundMode rmode,
3591
int scale, float_status *s)
3595
bfloat16_unpack_canonical(&p, a, s);
3596
return parts_float_to_uint(&p, rmode, scale, UINT64_MAX, s);
3599
static uint32_t float128_to_uint32_scalbn(float128 a, FloatRoundMode rmode,
3600
int scale, float_status *s)
3604
float128_unpack_canonical(&p, a, s);
3605
return parts_float_to_uint(&p, rmode, scale, UINT32_MAX, s);
3608
static uint64_t float128_to_uint64_scalbn(float128 a, FloatRoundMode rmode,
3609
int scale, float_status *s)
3613
float128_unpack_canonical(&p, a, s);
3614
return parts_float_to_uint(&p, rmode, scale, UINT64_MAX, s);
3617
static Int128 float128_to_uint128_scalbn(float128 a, FloatRoundMode rmode,
3618
int scale, float_status *s)
3624
float128_unpack_canonical(&p, a, s);
3627
case float_class_snan:
3628
flags |= float_flag_invalid_snan;
3630
case float_class_qnan:
3631
flags |= float_flag_invalid;
3635
case float_class_inf:
3636
flags = float_flag_invalid | float_flag_invalid_cvti;
3637
r = p.sign ? int128_zero() : UINT128_MAX;
3640
case float_class_zero:
3641
return int128_zero();
3643
case float_class_normal:
3644
if (parts_round_to_int_normal(&p, rmode, scale, 128 - 2)) {
3645
flags = float_flag_inexact;
3646
if (p.cls == float_class_zero) {
3653
flags = float_flag_invalid | float_flag_invalid_cvti;
3655
} else if (p.exp <= 127) {
3656
int shift = 127 - p.exp;
3657
r = int128_urshift(int128_make128(p.frac_lo, p.frac_hi), shift);
3659
flags = float_flag_invalid | float_flag_invalid_cvti;
3665
g_assert_not_reached();
3668
float_raise(flags, s);
3672
uint8_t float16_to_uint8(float16 a, float_status *s)
3674
return float16_to_uint8_scalbn(a, s->float_rounding_mode, 0, s);
3677
uint16_t float16_to_uint16(float16 a, float_status *s)
3679
return float16_to_uint16_scalbn(a, s->float_rounding_mode, 0, s);
3682
uint32_t float16_to_uint32(float16 a, float_status *s)
3684
return float16_to_uint32_scalbn(a, s->float_rounding_mode, 0, s);
3687
uint64_t float16_to_uint64(float16 a, float_status *s)
3689
return float16_to_uint64_scalbn(a, s->float_rounding_mode, 0, s);
3692
uint16_t float32_to_uint16(float32 a, float_status *s)
3694
return float32_to_uint16_scalbn(a, s->float_rounding_mode, 0, s);
3697
uint32_t float32_to_uint32(float32 a, float_status *s)
3699
return float32_to_uint32_scalbn(a, s->float_rounding_mode, 0, s);
3702
uint64_t float32_to_uint64(float32 a, float_status *s)
3704
return float32_to_uint64_scalbn(a, s->float_rounding_mode, 0, s);
3707
uint16_t float64_to_uint16(float64 a, float_status *s)
3709
return float64_to_uint16_scalbn(a, s->float_rounding_mode, 0, s);
3712
uint32_t float64_to_uint32(float64 a, float_status *s)
3714
return float64_to_uint32_scalbn(a, s->float_rounding_mode, 0, s);
3717
uint64_t float64_to_uint64(float64 a, float_status *s)
3719
return float64_to_uint64_scalbn(a, s->float_rounding_mode, 0, s);
3722
uint32_t float128_to_uint32(float128 a, float_status *s)
3724
return float128_to_uint32_scalbn(a, s->float_rounding_mode, 0, s);
3727
uint64_t float128_to_uint64(float128 a, float_status *s)
3729
return float128_to_uint64_scalbn(a, s->float_rounding_mode, 0, s);
3732
Int128 float128_to_uint128(float128 a, float_status *s)
3734
return float128_to_uint128_scalbn(a, s->float_rounding_mode, 0, s);
3737
uint16_t float16_to_uint16_round_to_zero(float16 a, float_status *s)
3739
return float16_to_uint16_scalbn(a, float_round_to_zero, 0, s);
3742
uint32_t float16_to_uint32_round_to_zero(float16 a, float_status *s)
3744
return float16_to_uint32_scalbn(a, float_round_to_zero, 0, s);
3747
uint64_t float16_to_uint64_round_to_zero(float16 a, float_status *s)
3749
return float16_to_uint64_scalbn(a, float_round_to_zero, 0, s);
3752
uint16_t float32_to_uint16_round_to_zero(float32 a, float_status *s)
3754
return float32_to_uint16_scalbn(a, float_round_to_zero, 0, s);
3757
uint32_t float32_to_uint32_round_to_zero(float32 a, float_status *s)
3759
return float32_to_uint32_scalbn(a, float_round_to_zero, 0, s);
3762
uint64_t float32_to_uint64_round_to_zero(float32 a, float_status *s)
3764
return float32_to_uint64_scalbn(a, float_round_to_zero, 0, s);
3767
uint16_t float64_to_uint16_round_to_zero(float64 a, float_status *s)
3769
return float64_to_uint16_scalbn(a, float_round_to_zero, 0, s);
3772
uint32_t float64_to_uint32_round_to_zero(float64 a, float_status *s)
3774
return float64_to_uint32_scalbn(a, float_round_to_zero, 0, s);
3777
uint64_t float64_to_uint64_round_to_zero(float64 a, float_status *s)
3779
return float64_to_uint64_scalbn(a, float_round_to_zero, 0, s);
3782
uint32_t float128_to_uint32_round_to_zero(float128 a, float_status *s)
3784
return float128_to_uint32_scalbn(a, float_round_to_zero, 0, s);
3787
uint64_t float128_to_uint64_round_to_zero(float128 a, float_status *s)
3789
return float128_to_uint64_scalbn(a, float_round_to_zero, 0, s);
3792
Int128 float128_to_uint128_round_to_zero(float128 a, float_status *s)
3794
return float128_to_uint128_scalbn(a, float_round_to_zero, 0, s);
3797
uint8_t bfloat16_to_uint8(bfloat16 a, float_status *s)
3799
return bfloat16_to_uint8_scalbn(a, s->float_rounding_mode, 0, s);
3802
uint16_t bfloat16_to_uint16(bfloat16 a, float_status *s)
3804
return bfloat16_to_uint16_scalbn(a, s->float_rounding_mode, 0, s);
3807
uint32_t bfloat16_to_uint32(bfloat16 a, float_status *s)
3809
return bfloat16_to_uint32_scalbn(a, s->float_rounding_mode, 0, s);
3812
uint64_t bfloat16_to_uint64(bfloat16 a, float_status *s)
3814
return bfloat16_to_uint64_scalbn(a, s->float_rounding_mode, 0, s);
3817
uint8_t bfloat16_to_uint8_round_to_zero(bfloat16 a, float_status *s)
3819
return bfloat16_to_uint8_scalbn(a, float_round_to_zero, 0, s);
3822
uint16_t bfloat16_to_uint16_round_to_zero(bfloat16 a, float_status *s)
3824
return bfloat16_to_uint16_scalbn(a, float_round_to_zero, 0, s);
3827
uint32_t bfloat16_to_uint32_round_to_zero(bfloat16 a, float_status *s)
3829
return bfloat16_to_uint32_scalbn(a, float_round_to_zero, 0, s);
3832
uint64_t bfloat16_to_uint64_round_to_zero(bfloat16 a, float_status *s)
3834
return bfloat16_to_uint64_scalbn(a, float_round_to_zero, 0, s);
3841
float16 int64_to_float16_scalbn(int64_t a, int scale, float_status *status)
3845
parts_sint_to_float(&p, a, scale, status);
3846
return float16_round_pack_canonical(&p, status);
3849
float16 int32_to_float16_scalbn(int32_t a, int scale, float_status *status)
3851
return int64_to_float16_scalbn(a, scale, status);
3854
float16 int16_to_float16_scalbn(int16_t a, int scale, float_status *status)
3856
return int64_to_float16_scalbn(a, scale, status);
3859
float16 int64_to_float16(int64_t a, float_status *status)
3861
return int64_to_float16_scalbn(a, 0, status);
3864
float16 int32_to_float16(int32_t a, float_status *status)
3866
return int64_to_float16_scalbn(a, 0, status);
3869
float16 int16_to_float16(int16_t a, float_status *status)
3871
return int64_to_float16_scalbn(a, 0, status);
3874
float16 int8_to_float16(int8_t a, float_status *status)
3876
return int64_to_float16_scalbn(a, 0, status);
3879
float32 int64_to_float32_scalbn(int64_t a, int scale, float_status *status)
3884
if (likely(scale == 0) && can_use_fpu(status)) {
3890
parts64_sint_to_float(&p, a, scale, status);
3891
return float32_round_pack_canonical(&p, status);
3894
float32 int32_to_float32_scalbn(int32_t a, int scale, float_status *status)
3896
return int64_to_float32_scalbn(a, scale, status);
3899
float32 int16_to_float32_scalbn(int16_t a, int scale, float_status *status)
3901
return int64_to_float32_scalbn(a, scale, status);
3904
float32 int64_to_float32(int64_t a, float_status *status)
3906
return int64_to_float32_scalbn(a, 0, status);
3909
float32 int32_to_float32(int32_t a, float_status *status)
3911
return int64_to_float32_scalbn(a, 0, status);
3914
float32 int16_to_float32(int16_t a, float_status *status)
3916
return int64_to_float32_scalbn(a, 0, status);
3919
float64 int64_to_float64_scalbn(int64_t a, int scale, float_status *status)
3924
if (likely(scale == 0) && can_use_fpu(status)) {
3930
parts_sint_to_float(&p, a, scale, status);
3931
return float64_round_pack_canonical(&p, status);
3934
float64 int32_to_float64_scalbn(int32_t a, int scale, float_status *status)
3936
return int64_to_float64_scalbn(a, scale, status);
3939
float64 int16_to_float64_scalbn(int16_t a, int scale, float_status *status)
3941
return int64_to_float64_scalbn(a, scale, status);
3944
float64 int64_to_float64(int64_t a, float_status *status)
3946
return int64_to_float64_scalbn(a, 0, status);
3949
float64 int32_to_float64(int32_t a, float_status *status)
3951
return int64_to_float64_scalbn(a, 0, status);
3954
float64 int16_to_float64(int16_t a, float_status *status)
3956
return int64_to_float64_scalbn(a, 0, status);
3959
bfloat16 int64_to_bfloat16_scalbn(int64_t a, int scale, float_status *status)
3963
parts_sint_to_float(&p, a, scale, status);
3964
return bfloat16_round_pack_canonical(&p, status);
3967
bfloat16 int32_to_bfloat16_scalbn(int32_t a, int scale, float_status *status)
3969
return int64_to_bfloat16_scalbn(a, scale, status);
3972
bfloat16 int16_to_bfloat16_scalbn(int16_t a, int scale, float_status *status)
3974
return int64_to_bfloat16_scalbn(a, scale, status);
3977
bfloat16 int8_to_bfloat16_scalbn(int8_t a, int scale, float_status *status)
3979
return int64_to_bfloat16_scalbn(a, scale, status);
3982
bfloat16 int64_to_bfloat16(int64_t a, float_status *status)
3984
return int64_to_bfloat16_scalbn(a, 0, status);
3987
bfloat16 int32_to_bfloat16(int32_t a, float_status *status)
3989
return int64_to_bfloat16_scalbn(a, 0, status);
3992
bfloat16 int16_to_bfloat16(int16_t a, float_status *status)
3994
return int64_to_bfloat16_scalbn(a, 0, status);
3997
bfloat16 int8_to_bfloat16(int8_t a, float_status *status)
3999
return int64_to_bfloat16_scalbn(a, 0, status);
4002
float128 int128_to_float128(Int128 a, float_status *status)
4004
FloatParts128 p = { };
4008
p.cls = float_class_normal;
4009
if (!int128_nonneg(a)) {
4014
shift = clz64(int128_gethi(a));
4016
shift += clz64(int128_getlo(a));
4019
p.exp = 127 - shift;
4020
a = int128_lshift(a, shift);
4022
p.frac_hi = int128_gethi(a);
4023
p.frac_lo = int128_getlo(a);
4025
p.cls = float_class_zero;
4028
return float128_round_pack_canonical(&p, status);
4031
float128 int64_to_float128(int64_t a, float_status *status)
4035
parts_sint_to_float(&p, a, 0, status);
4036
return float128_round_pack_canonical(&p, status);
4039
float128 int32_to_float128(int32_t a, float_status *status)
4041
return int64_to_float128(a, status);
4044
floatx80 int64_to_floatx80(int64_t a, float_status *status)
4048
parts_sint_to_float(&p, a, 0, status);
4049
return floatx80_round_pack_canonical(&p, status);
4052
floatx80 int32_to_floatx80(int32_t a, float_status *status)
4054
return int64_to_floatx80(a, status);
4061
float16 uint64_to_float16_scalbn(uint64_t a, int scale, float_status *status)
4065
parts_uint_to_float(&p, a, scale, status);
4066
return float16_round_pack_canonical(&p, status);
4069
float16 uint32_to_float16_scalbn(uint32_t a, int scale, float_status *status)
4071
return uint64_to_float16_scalbn(a, scale, status);
4074
float16 uint16_to_float16_scalbn(uint16_t a, int scale, float_status *status)
4076
return uint64_to_float16_scalbn(a, scale, status);
4079
float16 uint64_to_float16(uint64_t a, float_status *status)
4081
return uint64_to_float16_scalbn(a, 0, status);
4084
float16 uint32_to_float16(uint32_t a, float_status *status)
4086
return uint64_to_float16_scalbn(a, 0, status);
4089
float16 uint16_to_float16(uint16_t a, float_status *status)
4091
return uint64_to_float16_scalbn(a, 0, status);
4094
float16 uint8_to_float16(uint8_t a, float_status *status)
4096
return uint64_to_float16_scalbn(a, 0, status);
4099
float32 uint64_to_float32_scalbn(uint64_t a, int scale, float_status *status)
4104
if (likely(scale == 0) && can_use_fpu(status)) {
4110
parts_uint_to_float(&p, a, scale, status);
4111
return float32_round_pack_canonical(&p, status);
4114
float32 uint32_to_float32_scalbn(uint32_t a, int scale, float_status *status)
4116
return uint64_to_float32_scalbn(a, scale, status);
4119
float32 uint16_to_float32_scalbn(uint16_t a, int scale, float_status *status)
4121
return uint64_to_float32_scalbn(a, scale, status);
4124
float32 uint64_to_float32(uint64_t a, float_status *status)
4126
return uint64_to_float32_scalbn(a, 0, status);
4129
float32 uint32_to_float32(uint32_t a, float_status *status)
4131
return uint64_to_float32_scalbn(a, 0, status);
4134
float32 uint16_to_float32(uint16_t a, float_status *status)
4136
return uint64_to_float32_scalbn(a, 0, status);
4139
float64 uint64_to_float64_scalbn(uint64_t a, int scale, float_status *status)
4144
if (likely(scale == 0) && can_use_fpu(status)) {
4150
parts_uint_to_float(&p, a, scale, status);
4151
return float64_round_pack_canonical(&p, status);
4154
float64 uint32_to_float64_scalbn(uint32_t a, int scale, float_status *status)
4156
return uint64_to_float64_scalbn(a, scale, status);
4159
float64 uint16_to_float64_scalbn(uint16_t a, int scale, float_status *status)
4161
return uint64_to_float64_scalbn(a, scale, status);
4164
float64 uint64_to_float64(uint64_t a, float_status *status)
4166
return uint64_to_float64_scalbn(a, 0, status);
4169
float64 uint32_to_float64(uint32_t a, float_status *status)
4171
return uint64_to_float64_scalbn(a, 0, status);
4174
float64 uint16_to_float64(uint16_t a, float_status *status)
4176
return uint64_to_float64_scalbn(a, 0, status);
4179
bfloat16 uint64_to_bfloat16_scalbn(uint64_t a, int scale, float_status *status)
4183
parts_uint_to_float(&p, a, scale, status);
4184
return bfloat16_round_pack_canonical(&p, status);
4187
bfloat16 uint32_to_bfloat16_scalbn(uint32_t a, int scale, float_status *status)
4189
return uint64_to_bfloat16_scalbn(a, scale, status);
4192
bfloat16 uint16_to_bfloat16_scalbn(uint16_t a, int scale, float_status *status)
4194
return uint64_to_bfloat16_scalbn(a, scale, status);
4197
bfloat16 uint8_to_bfloat16_scalbn(uint8_t a, int scale, float_status *status)
4199
return uint64_to_bfloat16_scalbn(a, scale, status);
4202
bfloat16 uint64_to_bfloat16(uint64_t a, float_status *status)
4204
return uint64_to_bfloat16_scalbn(a, 0, status);
4207
bfloat16 uint32_to_bfloat16(uint32_t a, float_status *status)
4209
return uint64_to_bfloat16_scalbn(a, 0, status);
4212
bfloat16 uint16_to_bfloat16(uint16_t a, float_status *status)
4214
return uint64_to_bfloat16_scalbn(a, 0, status);
4217
bfloat16 uint8_to_bfloat16(uint8_t a, float_status *status)
4219
return uint64_to_bfloat16_scalbn(a, 0, status);
4222
float128 uint64_to_float128(uint64_t a, float_status *status)
4226
parts_uint_to_float(&p, a, 0, status);
4227
return float128_round_pack_canonical(&p, status);
4230
float128 uint128_to_float128(Int128 a, float_status *status)
4232
FloatParts128 p = { };
4236
p.cls = float_class_normal;
4238
shift = clz64(int128_gethi(a));
4240
shift += clz64(int128_getlo(a));
4243
p.exp = 127 - shift;
4244
a = int128_lshift(a, shift);
4246
p.frac_hi = int128_gethi(a);
4247
p.frac_lo = int128_getlo(a);
4249
p.cls = float_class_zero;
4252
return float128_round_pack_canonical(&p, status);
4259
static float16 float16_minmax(float16 a, float16 b, float_status *s, int flags)
4261
FloatParts64 pa, pb, *pr;
4263
float16_unpack_canonical(&pa, a, s);
4264
float16_unpack_canonical(&pb, b, s);
4265
pr = parts_minmax(&pa, &pb, s, flags);
4267
return float16_round_pack_canonical(pr, s);
4270
static bfloat16 bfloat16_minmax(bfloat16 a, bfloat16 b,
4271
float_status *s, int flags)
4273
FloatParts64 pa, pb, *pr;
4275
bfloat16_unpack_canonical(&pa, a, s);
4276
bfloat16_unpack_canonical(&pb, b, s);
4277
pr = parts_minmax(&pa, &pb, s, flags);
4279
return bfloat16_round_pack_canonical(pr, s);
4282
static float32 float32_minmax(float32 a, float32 b, float_status *s, int flags)
4284
FloatParts64 pa, pb, *pr;
4286
float32_unpack_canonical(&pa, a, s);
4287
float32_unpack_canonical(&pb, b, s);
4288
pr = parts_minmax(&pa, &pb, s, flags);
4290
return float32_round_pack_canonical(pr, s);
4293
static float64 float64_minmax(float64 a, float64 b, float_status *s, int flags)
4295
FloatParts64 pa, pb, *pr;
4297
float64_unpack_canonical(&pa, a, s);
4298
float64_unpack_canonical(&pb, b, s);
4299
pr = parts_minmax(&pa, &pb, s, flags);
4301
return float64_round_pack_canonical(pr, s);
4304
static float128 float128_minmax(float128 a, float128 b,
4305
float_status *s, int flags)
4307
FloatParts128 pa, pb, *pr;
4309
float128_unpack_canonical(&pa, a, s);
4310
float128_unpack_canonical(&pb, b, s);
4311
pr = parts_minmax(&pa, &pb, s, flags);
4313
return float128_round_pack_canonical(pr, s);
4316
#define MINMAX_1(type, name, flags) \
4317
type type##_##name(type a, type b, float_status *s) \
4318
{ return type##_minmax(a, b, s, flags); }
4320
#define MINMAX_2(type) \
4321
MINMAX_1(type, max, 0) \
4322
MINMAX_1(type, maxnum, minmax_isnum) \
4323
MINMAX_1(type, maxnummag, minmax_isnum | minmax_ismag) \
4324
MINMAX_1(type, maximum_number, minmax_isnumber) \
4325
MINMAX_1(type, min, minmax_ismin) \
4326
MINMAX_1(type, minnum, minmax_ismin | minmax_isnum) \
4327
MINMAX_1(type, minnummag, minmax_ismin | minmax_isnum | minmax_ismag) \
4328
MINMAX_1(type, minimum_number, minmax_ismin | minmax_isnumber) \
4343
static FloatRelation QEMU_FLATTEN
4344
float16_do_compare(float16 a, float16 b, float_status *s, bool is_quiet)
4346
FloatParts64 pa, pb;
4348
float16_unpack_canonical(&pa, a, s);
4349
float16_unpack_canonical(&pb, b, s);
4350
return parts_compare(&pa, &pb, s, is_quiet);
4353
FloatRelation float16_compare(float16 a, float16 b, float_status *s)
4355
return float16_do_compare(a, b, s, false);
4358
FloatRelation float16_compare_quiet(float16 a, float16 b, float_status *s)
4360
return float16_do_compare(a, b, s, true);
4363
static FloatRelation QEMU_SOFTFLOAT_ATTR
4364
float32_do_compare(float32 a, float32 b, float_status *s, bool is_quiet)
4366
FloatParts64 pa, pb;
4368
float32_unpack_canonical(&pa, a, s);
4369
float32_unpack_canonical(&pb, b, s);
4370
return parts_compare(&pa, &pb, s, is_quiet);
4373
static FloatRelation QEMU_FLATTEN
4374
float32_hs_compare(float32 xa, float32 xb, float_status *s, bool is_quiet)
4376
union_float32 ua, ub;
4381
if (QEMU_NO_HARDFLOAT) {
4385
float32_input_flush2(&ua.s, &ub.s, s);
4386
if (isgreaterequal(ua.h, ub.h)) {
4387
if (isgreater(ua.h, ub.h)) {
4388
return float_relation_greater;
4390
return float_relation_equal;
4392
if (likely(isless(ua.h, ub.h))) {
4393
return float_relation_less;
4400
return float32_do_compare(ua.s, ub.s, s, is_quiet);
4403
FloatRelation float32_compare(float32 a, float32 b, float_status *s)
4405
return float32_hs_compare(a, b, s, false);
4408
FloatRelation float32_compare_quiet(float32 a, float32 b, float_status *s)
4410
return float32_hs_compare(a, b, s, true);
4413
static FloatRelation QEMU_SOFTFLOAT_ATTR
4414
float64_do_compare(float64 a, float64 b, float_status *s, bool is_quiet)
4416
FloatParts64 pa, pb;
4418
float64_unpack_canonical(&pa, a, s);
4419
float64_unpack_canonical(&pb, b, s);
4420
return parts_compare(&pa, &pb, s, is_quiet);
4423
static FloatRelation QEMU_FLATTEN
4424
float64_hs_compare(float64 xa, float64 xb, float_status *s, bool is_quiet)
4426
union_float64 ua, ub;
4431
if (QEMU_NO_HARDFLOAT) {
4435
float64_input_flush2(&ua.s, &ub.s, s);
4436
if (isgreaterequal(ua.h, ub.h)) {
4437
if (isgreater(ua.h, ub.h)) {
4438
return float_relation_greater;
4440
return float_relation_equal;
4442
if (likely(isless(ua.h, ub.h))) {
4443
return float_relation_less;
4450
return float64_do_compare(ua.s, ub.s, s, is_quiet);
4453
FloatRelation float64_compare(float64 a, float64 b, float_status *s)
4455
return float64_hs_compare(a, b, s, false);
4458
FloatRelation float64_compare_quiet(float64 a, float64 b, float_status *s)
4460
return float64_hs_compare(a, b, s, true);
4463
static FloatRelation QEMU_FLATTEN
4464
bfloat16_do_compare(bfloat16 a, bfloat16 b, float_status *s, bool is_quiet)
4466
FloatParts64 pa, pb;
4468
bfloat16_unpack_canonical(&pa, a, s);
4469
bfloat16_unpack_canonical(&pb, b, s);
4470
return parts_compare(&pa, &pb, s, is_quiet);
4473
FloatRelation bfloat16_compare(bfloat16 a, bfloat16 b, float_status *s)
4475
return bfloat16_do_compare(a, b, s, false);
4478
FloatRelation bfloat16_compare_quiet(bfloat16 a, bfloat16 b, float_status *s)
4480
return bfloat16_do_compare(a, b, s, true);
4483
static FloatRelation QEMU_FLATTEN
4484
float128_do_compare(float128 a, float128 b, float_status *s, bool is_quiet)
4486
FloatParts128 pa, pb;
4488
float128_unpack_canonical(&pa, a, s);
4489
float128_unpack_canonical(&pb, b, s);
4490
return parts_compare(&pa, &pb, s, is_quiet);
4493
FloatRelation float128_compare(float128 a, float128 b, float_status *s)
4495
return float128_do_compare(a, b, s, false);
4498
FloatRelation float128_compare_quiet(float128 a, float128 b, float_status *s)
4500
return float128_do_compare(a, b, s, true);
4503
static FloatRelation QEMU_FLATTEN
4504
floatx80_do_compare(floatx80 a, floatx80 b, float_status *s, bool is_quiet)
4506
FloatParts128 pa, pb;
4508
if (!floatx80_unpack_canonical(&pa, a, s) ||
4509
!floatx80_unpack_canonical(&pb, b, s)) {
4510
return float_relation_unordered;
4512
return parts_compare(&pa, &pb, s, is_quiet);
4515
FloatRelation floatx80_compare(floatx80 a, floatx80 b, float_status *s)
4517
return floatx80_do_compare(a, b, s, false);
4520
FloatRelation floatx80_compare_quiet(floatx80 a, floatx80 b, float_status *s)
4522
return floatx80_do_compare(a, b, s, true);
4529
float16 float16_scalbn(float16 a, int n, float_status *status)
4533
float16_unpack_canonical(&p, a, status);
4534
parts_scalbn(&p, n, status);
4535
return float16_round_pack_canonical(&p, status);
4538
float32 float32_scalbn(float32 a, int n, float_status *status)
4542
float32_unpack_canonical(&p, a, status);
4543
parts_scalbn(&p, n, status);
4544
return float32_round_pack_canonical(&p, status);
4547
float64 float64_scalbn(float64 a, int n, float_status *status)
4551
float64_unpack_canonical(&p, a, status);
4552
parts_scalbn(&p, n, status);
4553
return float64_round_pack_canonical(&p, status);
4556
bfloat16 bfloat16_scalbn(bfloat16 a, int n, float_status *status)
4560
bfloat16_unpack_canonical(&p, a, status);
4561
parts_scalbn(&p, n, status);
4562
return bfloat16_round_pack_canonical(&p, status);
4565
float128 float128_scalbn(float128 a, int n, float_status *status)
4569
float128_unpack_canonical(&p, a, status);
4570
parts_scalbn(&p, n, status);
4571
return float128_round_pack_canonical(&p, status);
4574
floatx80 floatx80_scalbn(floatx80 a, int n, float_status *status)
4578
if (!floatx80_unpack_canonical(&p, a, status)) {
4579
return floatx80_default_nan(status);
4581
parts_scalbn(&p, n, status);
4582
return floatx80_round_pack_canonical(&p, status);
4589
float16 QEMU_FLATTEN float16_sqrt(float16 a, float_status *status)
4593
float16_unpack_canonical(&p, a, status);
4594
parts_sqrt(&p, status, &float16_params);
4595
return float16_round_pack_canonical(&p, status);
4598
static float32 QEMU_SOFTFLOAT_ATTR
4599
soft_f32_sqrt(float32 a, float_status *status)
4603
float32_unpack_canonical(&p, a, status);
4604
parts_sqrt(&p, status, &float32_params);
4605
return float32_round_pack_canonical(&p, status);
4608
static float64 QEMU_SOFTFLOAT_ATTR
4609
soft_f64_sqrt(float64 a, float_status *status)
4613
float64_unpack_canonical(&p, a, status);
4614
parts_sqrt(&p, status, &float64_params);
4615
return float64_round_pack_canonical(&p, status);
4618
float32 QEMU_FLATTEN float32_sqrt(float32 xa, float_status *s)
4620
union_float32 ua, ur;
4623
if (unlikely(!can_use_fpu(s))) {
4627
float32_input_flush1(&ua.s, s);
4628
if (QEMU_HARDFLOAT_1F32_USE_FP) {
4629
if (unlikely(!(fpclassify(ua.h) == FP_NORMAL ||
4630
fpclassify(ua.h) == FP_ZERO) ||
4634
} else if (unlikely(!float32_is_zero_or_normal(ua.s) ||
4635
float32_is_neg(ua.s))) {
4642
return soft_f32_sqrt(ua.s, s);
4645
float64 QEMU_FLATTEN float64_sqrt(float64 xa, float_status *s)
4647
union_float64 ua, ur;
4650
if (unlikely(!can_use_fpu(s))) {
4654
float64_input_flush1(&ua.s, s);
4655
if (QEMU_HARDFLOAT_1F64_USE_FP) {
4656
if (unlikely(!(fpclassify(ua.h) == FP_NORMAL ||
4657
fpclassify(ua.h) == FP_ZERO) ||
4661
} else if (unlikely(!float64_is_zero_or_normal(ua.s) ||
4662
float64_is_neg(ua.s))) {
4669
return soft_f64_sqrt(ua.s, s);
4672
float64 float64r32_sqrt(float64 a, float_status *status)
4676
float64_unpack_canonical(&p, a, status);
4677
parts_sqrt(&p, status, &float64_params);
4678
return float64r32_round_pack_canonical(&p, status);
4681
bfloat16 QEMU_FLATTEN bfloat16_sqrt(bfloat16 a, float_status *status)
4685
bfloat16_unpack_canonical(&p, a, status);
4686
parts_sqrt(&p, status, &bfloat16_params);
4687
return bfloat16_round_pack_canonical(&p, status);
4690
float128 QEMU_FLATTEN float128_sqrt(float128 a, float_status *status)
4694
float128_unpack_canonical(&p, a, status);
4695
parts_sqrt(&p, status, &float128_params);
4696
return float128_round_pack_canonical(&p, status);
4699
floatx80 floatx80_sqrt(floatx80 a, float_status *s)
4703
if (!floatx80_unpack_canonical(&p, a, s)) {
4704
return floatx80_default_nan(s);
4706
parts_sqrt(&p, s, &floatx80_params[s->floatx80_rounding_precision]);
4707
return floatx80_round_pack_canonical(&p, s);
4713
float32 float32_log2(float32 a, float_status *status)
4717
float32_unpack_canonical(&p, a, status);
4718
parts_log2(&p, status, &float32_params);
4719
return float32_round_pack_canonical(&p, status);
4722
float64 float64_log2(float64 a, float_status *status)
4726
float64_unpack_canonical(&p, a, status);
4727
parts_log2(&p, status, &float64_params);
4728
return float64_round_pack_canonical(&p, status);
4735
float16 float16_default_nan(float_status *status)
4739
parts_default_nan(&p, status);
4740
p.frac >>= float16_params.frac_shift;
4741
return float16_pack_raw(&p);
4744
float32 float32_default_nan(float_status *status)
4748
parts_default_nan(&p, status);
4749
p.frac >>= float32_params.frac_shift;
4750
return float32_pack_raw(&p);
4753
float64 float64_default_nan(float_status *status)
4757
parts_default_nan(&p, status);
4758
p.frac >>= float64_params.frac_shift;
4759
return float64_pack_raw(&p);
4762
float128 float128_default_nan(float_status *status)
4766
parts_default_nan(&p, status);
4767
frac_shr(&p, float128_params.frac_shift);
4768
return float128_pack_raw(&p);
4771
bfloat16 bfloat16_default_nan(float_status *status)
4775
parts_default_nan(&p, status);
4776
p.frac >>= bfloat16_params.frac_shift;
4777
return bfloat16_pack_raw(&p);
4784
float16 float16_silence_nan(float16 a, float_status *status)
4788
float16_unpack_raw(&p, a);
4789
p.frac <<= float16_params.frac_shift;
4790
parts_silence_nan(&p, status);
4791
p.frac >>= float16_params.frac_shift;
4792
return float16_pack_raw(&p);
4795
float32 float32_silence_nan(float32 a, float_status *status)
4799
float32_unpack_raw(&p, a);
4800
p.frac <<= float32_params.frac_shift;
4801
parts_silence_nan(&p, status);
4802
p.frac >>= float32_params.frac_shift;
4803
return float32_pack_raw(&p);
4806
float64 float64_silence_nan(float64 a, float_status *status)
4810
float64_unpack_raw(&p, a);
4811
p.frac <<= float64_params.frac_shift;
4812
parts_silence_nan(&p, status);
4813
p.frac >>= float64_params.frac_shift;
4814
return float64_pack_raw(&p);
4817
bfloat16 bfloat16_silence_nan(bfloat16 a, float_status *status)
4821
bfloat16_unpack_raw(&p, a);
4822
p.frac <<= bfloat16_params.frac_shift;
4823
parts_silence_nan(&p, status);
4824
p.frac >>= bfloat16_params.frac_shift;
4825
return bfloat16_pack_raw(&p);
4828
float128 float128_silence_nan(float128 a, float_status *status)
4832
float128_unpack_raw(&p, a);
4833
frac_shl(&p, float128_params.frac_shift);
4834
parts_silence_nan(&p, status);
4835
frac_shr(&p, float128_params.frac_shift);
4836
return float128_pack_raw(&p);
4844
static bool parts_squash_denormal(FloatParts64 p, float_status *status)
4846
if (p.exp == 0 && p.frac != 0) {
4847
float_raise(float_flag_input_denormal, status);
4854
float16 float16_squash_input_denormal(float16 a, float_status *status)
4856
if (status->flush_inputs_to_zero) {
4859
float16_unpack_raw(&p, a);
4860
if (parts_squash_denormal(p, status)) {
4861
return float16_set_sign(float16_zero, p.sign);
4867
float32 float32_squash_input_denormal(float32 a, float_status *status)
4869
if (status->flush_inputs_to_zero) {
4872
float32_unpack_raw(&p, a);
4873
if (parts_squash_denormal(p, status)) {
4874
return float32_set_sign(float32_zero, p.sign);
4880
float64 float64_squash_input_denormal(float64 a, float_status *status)
4882
if (status->flush_inputs_to_zero) {
4885
float64_unpack_raw(&p, a);
4886
if (parts_squash_denormal(p, status)) {
4887
return float64_set_sign(float64_zero, p.sign);
4893
bfloat16 bfloat16_squash_input_denormal(bfloat16 a, float_status *status)
4895
if (status->flush_inputs_to_zero) {
4898
bfloat16_unpack_raw(&p, a);
4899
if (parts_squash_denormal(p, status)) {
4900
return bfloat16_set_sign(bfloat16_zero, p.sign);
4913
void normalizeFloatx80Subnormal(uint64_t aSig, int32_t *zExpPtr,
4918
shiftCount = clz64(aSig);
4919
*zSigPtr = aSig<<shiftCount;
4920
*zExpPtr = 1 - shiftCount;
4947
floatx80 roundAndPackFloatx80(FloatX80RoundPrec roundingPrecision, bool zSign,
4948
int32_t zExp, uint64_t zSig0, uint64_t zSig1,
4949
float_status *status)
4951
FloatRoundMode roundingMode;
4952
bool roundNearestEven, increment, isTiny;
4953
int64_t roundIncrement, roundMask, roundBits;
4955
roundingMode = status->float_rounding_mode;
4956
roundNearestEven = ( roundingMode == float_round_nearest_even );
4957
switch (roundingPrecision) {
4958
case floatx80_precision_x:
4960
case floatx80_precision_d:
4961
roundIncrement = UINT64_C(0x0000000000000400);
4962
roundMask = UINT64_C(0x00000000000007FF);
4964
case floatx80_precision_s:
4965
roundIncrement = UINT64_C(0x0000008000000000);
4966
roundMask = UINT64_C(0x000000FFFFFFFFFF);
4969
g_assert_not_reached();
4971
zSig0 |= ( zSig1 != 0 );
4972
switch (roundingMode) {
4973
case float_round_nearest_even:
4974
case float_round_ties_away:
4976
case float_round_to_zero:
4979
case float_round_up:
4980
roundIncrement = zSign ? 0 : roundMask;
4982
case float_round_down:
4983
roundIncrement = zSign ? roundMask : 0;
4988
roundBits = zSig0 & roundMask;
4989
if ( 0x7FFD <= (uint32_t) ( zExp - 1 ) ) {
4990
if ( ( 0x7FFE < zExp )
4991
|| ( ( zExp == 0x7FFE ) && ( zSig0 + roundIncrement < zSig0 ) )
4996
if (status->flush_to_zero) {
4997
float_raise(float_flag_output_denormal, status);
4998
return packFloatx80(zSign, 0, 0);
5000
isTiny = status->tininess_before_rounding
5002
|| (zSig0 <= zSig0 + roundIncrement);
5003
shift64RightJamming( zSig0, 1 - zExp, &zSig0 );
5005
roundBits = zSig0 & roundMask;
5006
if (isTiny && roundBits) {
5007
float_raise(float_flag_underflow, status);
5010
float_raise(float_flag_inexact, status);
5012
zSig0 += roundIncrement;
5013
if ( (int64_t) zSig0 < 0 ) zExp = 1;
5014
roundIncrement = roundMask + 1;
5015
if ( roundNearestEven && ( roundBits<<1 == roundIncrement ) ) {
5016
roundMask |= roundIncrement;
5018
zSig0 &= ~ roundMask;
5019
return packFloatx80( zSign, zExp, zSig0 );
5023
float_raise(float_flag_inexact, status);
5025
zSig0 += roundIncrement;
5026
if ( zSig0 < roundIncrement ) {
5028
zSig0 = UINT64_C(0x8000000000000000);
5030
roundIncrement = roundMask + 1;
5031
if ( roundNearestEven && ( roundBits<<1 == roundIncrement ) ) {
5032
roundMask |= roundIncrement;
5034
zSig0 &= ~ roundMask;
5035
if ( zSig0 == 0 ) zExp = 0;
5036
return packFloatx80( zSign, zExp, zSig0 );
5038
switch (roundingMode) {
5039
case float_round_nearest_even:
5040
case float_round_ties_away:
5041
increment = ((int64_t)zSig1 < 0);
5043
case float_round_to_zero:
5046
case float_round_up:
5047
increment = !zSign && zSig1;
5049
case float_round_down:
5050
increment = zSign && zSig1;
5055
if ( 0x7FFD <= (uint32_t) ( zExp - 1 ) ) {
5056
if ( ( 0x7FFE < zExp )
5057
|| ( ( zExp == 0x7FFE )
5058
&& ( zSig0 == UINT64_C(0xFFFFFFFFFFFFFFFF) )
5064
float_raise(float_flag_overflow | float_flag_inexact, status);
5065
if ( ( roundingMode == float_round_to_zero )
5066
|| ( zSign && ( roundingMode == float_round_up ) )
5067
|| ( ! zSign && ( roundingMode == float_round_down ) )
5069
return packFloatx80( zSign, 0x7FFE, ~ roundMask );
5071
return packFloatx80(zSign,
5072
floatx80_infinity_high,
5073
floatx80_infinity_low);
5076
isTiny = status->tininess_before_rounding
5079
|| (zSig0 < UINT64_C(0xFFFFFFFFFFFFFFFF));
5080
shift64ExtraRightJamming( zSig0, zSig1, 1 - zExp, &zSig0, &zSig1 );
5082
if (isTiny && zSig1) {
5083
float_raise(float_flag_underflow, status);
5086
float_raise(float_flag_inexact, status);
5088
switch (roundingMode) {
5089
case float_round_nearest_even:
5090
case float_round_ties_away:
5091
increment = ((int64_t)zSig1 < 0);
5093
case float_round_to_zero:
5096
case float_round_up:
5097
increment = !zSign && zSig1;
5099
case float_round_down:
5100
increment = zSign && zSig1;
5107
if (!(zSig1 << 1) && roundNearestEven) {
5110
if ( (int64_t) zSig0 < 0 ) zExp = 1;
5112
return packFloatx80( zSign, zExp, zSig0 );
5116
float_raise(float_flag_inexact, status);
5122
zSig0 = UINT64_C(0x8000000000000000);
5125
if (!(zSig1 << 1) && roundNearestEven) {
5131
if ( zSig0 == 0 ) zExp = 0;
5133
return packFloatx80( zSign, zExp, zSig0 );
5146
floatx80 normalizeRoundAndPackFloatx80(FloatX80RoundPrec roundingPrecision,
5147
bool zSign, int32_t zExp,
5148
uint64_t zSig0, uint64_t zSig1,
5149
float_status *status)
5158
shiftCount = clz64(zSig0);
5159
shortShift128Left( zSig0, zSig1, shiftCount, &zSig0, &zSig1 );
5161
return roundAndPackFloatx80(roundingPrecision, zSign, zExp,
5162
zSig0, zSig1, status);
5184
static const float64 float32_exp2_coefficients[15] =
5186
const_float64( 0x3ff0000000000000ll ),
5187
const_float64( 0x3fe0000000000000ll ),
5188
const_float64( 0x3fc5555555555555ll ),
5189
const_float64( 0x3fa5555555555555ll ),
5190
const_float64( 0x3f81111111111111ll ),
5191
const_float64( 0x3f56c16c16c16c17ll ),
5192
const_float64( 0x3f2a01a01a01a01all ),
5193
const_float64( 0x3efa01a01a01a01all ),
5194
const_float64( 0x3ec71de3a556c734ll ),
5195
const_float64( 0x3e927e4fb7789f5cll ),
5196
const_float64( 0x3e5ae64567f544e4ll ),
5197
const_float64( 0x3e21eed8eff8d898ll ),
5198
const_float64( 0x3de6124613a86d09ll ),
5199
const_float64( 0x3da93974a8c07c9dll ),
5200
const_float64( 0x3d6ae7f3e733b81fll ),
5203
float32 float32_exp2(float32 a, float_status *status)
5205
FloatParts64 xp, xnp, tp, rp;
5208
float32_unpack_canonical(&xp, a, status);
5209
if (unlikely(xp.cls != float_class_normal)) {
5211
case float_class_snan:
5212
case float_class_qnan:
5213
parts_return_nan(&xp, status);
5214
return float32_round_pack_canonical(&xp, status);
5215
case float_class_inf:
5216
return xp.sign ? float32_zero : a;
5217
case float_class_zero:
5222
g_assert_not_reached();
5225
float_raise(float_flag_inexact, status);
5227
float64_unpack_canonical(&tp, float64_ln2, status);
5228
xp = *parts_mul(&xp, &tp, status);
5231
float64_unpack_canonical(&rp, float64_one, status);
5232
for (i = 0 ; i < 15 ; i++) {
5233
float64_unpack_canonical(&tp, float32_exp2_coefficients[i], status);
5234
rp = *parts_muladd(&tp, &xnp, &rp, 0, status);
5235
xnp = *parts_mul(&xnp, &xp, status);
5238
return float32_round_pack_canonical(&rp, status);
5249
floatx80 floatx80_round(floatx80 a, float_status *status)
5253
if (!floatx80_unpack_canonical(&p, a, status)) {
5254
return floatx80_default_nan(status);
5256
return floatx80_round_pack_canonical(&p, status);
5259
static void __attribute__((constructor)) softfloat_init(void)
5261
union_float64 ua, ub, uc, ur;
5263
if (QEMU_NO_HARDFLOAT) {
5271
ua.s = 0x0020000000000001ULL;
5272
ub.s = 0x3ca0000000000000ULL;
5273
uc.s = 0x0020000000000000ULL;
5274
ur.h = fma(ua.h, ub.h, uc.h);
5275
if (ur.s != 0x0020000000000001ULL) {
5276
force_soft_fma = true;