4 * The code in this source file is derived from release 2a of the SoftFloat
5 * IEC/IEEE Floating-point Arithmetic Package. Those parts of the code (and
6 * some later contributions) are provided under that license, as detailed below.
7 * It has subsequently been modified by contributors to the QEMU Project,
8 * so some portions are provided under:
9 * the SoftFloat-2a license
13 * Any future contributions to this file after December 1st 2014 will be
14 * taken to be licensed under the Softfloat-2a license unless specifically
15 * indicated otherwise.
18 static void partsN(return_nan)(FloatPartsN *a, float_status *s)
21 case float_class_snan:
22 float_raise(float_flag_invalid | float_flag_invalid_snan, s);
23 if (s->default_nan_mode) {
24 parts_default_nan(a, s);
26 parts_silence_nan(a, s);
29 case float_class_qnan:
30 if (s->default_nan_mode) {
31 parts_default_nan(a, s);
35 g_assert_not_reached();
39 static FloatPartsN *partsN(pick_nan)(FloatPartsN *a, FloatPartsN *b,
42 if (is_snan(a->cls) || is_snan(b->cls)) {
43 float_raise(float_flag_invalid | float_flag_invalid_snan, s);
46 if (s->default_nan_mode) {
47 parts_default_nan(a, s);
49 int cmp = frac_cmp(a, b);
51 cmp = a->sign < b->sign;
54 if (pickNaN(a->cls, b->cls, cmp > 0, s)) {
57 if (is_snan(a->cls)) {
58 parts_silence_nan(a, s);
64 static FloatPartsN *partsN(pick_nan_muladd)(FloatPartsN *a, FloatPartsN *b,
65 FloatPartsN *c, float_status *s,
66 int ab_mask, int abc_mask)
70 if (unlikely(abc_mask & float_cmask_snan)) {
71 float_raise(float_flag_invalid | float_flag_invalid_snan, s);
74 which = pickNaNMulAdd(a->cls, b->cls, c->cls,
75 ab_mask == float_cmask_infzero, s);
77 if (s->default_nan_mode || which == 3) {
79 * Note that this check is after pickNaNMulAdd so that function
80 * has an opportunity to set the Invalid flag for infzero.
82 parts_default_nan(a, s);
96 g_assert_not_reached();
98 if (is_snan(a->cls)) {
99 parts_silence_nan(a, s);
105 * Canonicalize the FloatParts structure. Determine the class,
106 * unbias the exponent, and normalize the fraction.
108 static void partsN(canonicalize)(FloatPartsN *p, float_status *status,
111 if (unlikely(p->exp == 0)) {
112 if (likely(frac_eqz(p))) {
113 p->cls = float_class_zero;
114 } else if (status->flush_inputs_to_zero) {
115 float_raise(float_flag_input_denormal, status);
116 p->cls = float_class_zero;
119 int shift = frac_normalize(p);
120 p->cls = float_class_normal;
121 p->exp = fmt->frac_shift - fmt->exp_bias - shift + 1;
123 } else if (likely(p->exp < fmt->exp_max) || fmt->arm_althp) {
124 p->cls = float_class_normal;
125 p->exp -= fmt->exp_bias;
126 frac_shl(p, fmt->frac_shift);
127 p->frac_hi |= DECOMPOSED_IMPLICIT_BIT;
128 } else if (likely(frac_eqz(p))) {
129 p->cls = float_class_inf;
131 frac_shl(p, fmt->frac_shift);
132 p->cls = (parts_is_snan_frac(p->frac_hi, status)
133 ? float_class_snan : float_class_qnan);
138 * Round and uncanonicalize a floating-point number by parts. There
139 * are FRAC_SHIFT bits that may require rounding at the bottom of the
140 * fraction; these bits will be removed. The exponent will be biased
141 * by EXP_BIAS and must be bounded by [EXP_MAX-1, 0].
143 static void partsN(uncanon_normal)(FloatPartsN *p, float_status *s,
146 const int exp_max = fmt->exp_max;
147 const int frac_shift = fmt->frac_shift;
148 const uint64_t round_mask = fmt->round_mask;
149 const uint64_t frac_lsb = round_mask + 1;
150 const uint64_t frac_lsbm1 = round_mask ^ (round_mask >> 1);
151 const uint64_t roundeven_mask = round_mask | frac_lsb;
153 bool overflow_norm = false;
156 switch (s->float_rounding_mode) {
157 case float_round_nearest_even:
158 if (N > 64 && frac_lsb == 0) {
159 inc = ((p->frac_hi & 1) || (p->frac_lo & round_mask) != frac_lsbm1
162 inc = ((p->frac_lo & roundeven_mask) != frac_lsbm1
166 case float_round_ties_away:
169 case float_round_to_zero:
170 overflow_norm = true;
174 inc = p->sign ? 0 : round_mask;
175 overflow_norm = p->sign;
177 case float_round_down:
178 inc = p->sign ? round_mask : 0;
179 overflow_norm = !p->sign;
181 case float_round_to_odd:
182 overflow_norm = true;
184 case float_round_to_odd_inf:
185 if (N > 64 && frac_lsb == 0) {
186 inc = p->frac_hi & 1 ? 0 : round_mask;
188 inc = p->frac_lo & frac_lsb ? 0 : round_mask;
192 g_assert_not_reached();
195 exp = p->exp + fmt->exp_bias;
196 if (likely(exp > 0)) {
197 if (p->frac_lo & round_mask) {
198 flags |= float_flag_inexact;
199 if (frac_addi(p, p, inc)) {
201 p->frac_hi |= DECOMPOSED_IMPLICIT_BIT;
204 p->frac_lo &= ~round_mask;
207 if (fmt->arm_althp) {
208 /* ARM Alt HP eschews Inf and NaN for a wider exponent. */
209 if (unlikely(exp > exp_max)) {
210 /* Overflow. Return the maximum normal. */
211 flags = float_flag_invalid;
214 p->frac_lo &= ~round_mask;
216 } else if (unlikely(exp >= exp_max)) {
217 flags |= float_flag_overflow | float_flag_inexact;
221 p->frac_lo &= ~round_mask;
223 p->cls = float_class_inf;
228 frac_shr(p, frac_shift);
229 } else if (s->flush_to_zero) {
230 flags |= float_flag_output_denormal;
231 p->cls = float_class_zero;
235 bool is_tiny = s->tininess_before_rounding || exp < 0;
239 is_tiny = !frac_addi(&discard, p, inc);
242 frac_shrjam(p, 1 - exp);
244 if (p->frac_lo & round_mask) {
245 /* Need to recompute round-to-even/round-to-odd. */
246 switch (s->float_rounding_mode) {
247 case float_round_nearest_even:
248 if (N > 64 && frac_lsb == 0) {
249 inc = ((p->frac_hi & 1) ||
250 (p->frac_lo & round_mask) != frac_lsbm1
253 inc = ((p->frac_lo & roundeven_mask) != frac_lsbm1
257 case float_round_to_odd:
258 case float_round_to_odd_inf:
259 if (N > 64 && frac_lsb == 0) {
260 inc = p->frac_hi & 1 ? 0 : round_mask;
262 inc = p->frac_lo & frac_lsb ? 0 : round_mask;
268 flags |= float_flag_inexact;
269 frac_addi(p, p, inc);
270 p->frac_lo &= ~round_mask;
273 exp = (p->frac_hi & DECOMPOSED_IMPLICIT_BIT) != 0;
274 frac_shr(p, frac_shift);
276 if (is_tiny && (flags & float_flag_inexact)) {
277 flags |= float_flag_underflow;
279 if (exp == 0 && frac_eqz(p)) {
280 p->cls = float_class_zero;
284 float_raise(flags, s);
287 static void partsN(uncanon)(FloatPartsN *p, float_status *s,
290 if (likely(p->cls == float_class_normal)) {
291 parts_uncanon_normal(p, s, fmt);
294 case float_class_zero:
298 case float_class_inf:
299 g_assert(!fmt->arm_althp);
300 p->exp = fmt->exp_max;
303 case float_class_qnan:
304 case float_class_snan:
305 g_assert(!fmt->arm_althp);
306 p->exp = fmt->exp_max;
307 frac_shr(p, fmt->frac_shift);
312 g_assert_not_reached();
317 * Returns the result of adding or subtracting the values of the
318 * floating-point values `a' and `b'. The operation is performed
319 * according to the IEC/IEEE Standard for Binary Floating-Point
322 static FloatPartsN *partsN(addsub)(FloatPartsN *a, FloatPartsN *b,
323 float_status *s, bool subtract)
325 bool b_sign = b->sign ^ subtract;
326 int ab_mask = float_cmask(a->cls) | float_cmask(b->cls);
328 if (a->sign != b_sign) {
330 if (likely(ab_mask == float_cmask_normal)) {
331 if (parts_sub_normal(a, b)) {
334 /* Subtract was exact, fall through to set sign. */
335 ab_mask = float_cmask_zero;
338 if (ab_mask == float_cmask_zero) {
339 a->sign = s->float_rounding_mode == float_round_down;
343 if (unlikely(ab_mask & float_cmask_anynan)) {
347 if (ab_mask & float_cmask_inf) {
348 if (a->cls != float_class_inf) {
352 if (b->cls != float_class_inf) {
357 float_raise(float_flag_invalid | float_flag_invalid_isi, s);
358 parts_default_nan(a, s);
363 if (likely(ab_mask == float_cmask_normal)) {
364 parts_add_normal(a, b);
368 if (ab_mask == float_cmask_zero) {
372 if (unlikely(ab_mask & float_cmask_anynan)) {
376 if (ab_mask & float_cmask_inf) {
377 a->cls = float_class_inf;
382 if (b->cls == float_class_zero) {
383 g_assert(a->cls == float_class_normal);
387 g_assert(a->cls == float_class_zero);
388 g_assert(b->cls == float_class_normal);
394 return parts_pick_nan(a, b, s);
398 * Returns the result of multiplying the floating-point values `a' and
399 * `b'. The operation is performed according to the IEC/IEEE Standard
400 * for Binary Floating-Point Arithmetic.
402 static FloatPartsN *partsN(mul)(FloatPartsN *a, FloatPartsN *b,
405 int ab_mask = float_cmask(a->cls) | float_cmask(b->cls);
406 bool sign = a->sign ^ b->sign;
408 if (likely(ab_mask == float_cmask_normal)) {
411 frac_mulw(&tmp, a, b);
412 frac_truncjam(a, &tmp);
414 a->exp += b->exp + 1;
415 if (!(a->frac_hi & DECOMPOSED_IMPLICIT_BIT)) {
424 /* Inf * Zero == NaN */
425 if (unlikely(ab_mask == float_cmask_infzero)) {
426 float_raise(float_flag_invalid | float_flag_invalid_imz, s);
427 parts_default_nan(a, s);
431 if (unlikely(ab_mask & float_cmask_anynan)) {
432 return parts_pick_nan(a, b, s);
435 /* Multiply by 0 or Inf */
436 if (ab_mask & float_cmask_inf) {
437 a->cls = float_class_inf;
442 g_assert(ab_mask & float_cmask_zero);
443 a->cls = float_class_zero;
449 * Returns the result of multiplying the floating-point values `a' and
450 * `b' then adding 'c', with no intermediate rounding step after the
451 * multiplication. The operation is performed according to the
452 * IEC/IEEE Standard for Binary Floating-Point Arithmetic 754-2008.
453 * The flags argument allows the caller to select negation of the
454 * addend, the intermediate product, or the final result. (The
455 * difference between this and having the caller do a separate
456 * negation is that negating externally will flip the sign bit on NaNs.)
458 * Requires A and C extracted into a double-sized structure to provide the
459 * extra space for the widening multiply.
461 static FloatPartsN *partsN(muladd)(FloatPartsN *a, FloatPartsN *b,
462 FloatPartsN *c, int flags, float_status *s)
464 int ab_mask, abc_mask;
465 FloatPartsW p_widen, c_widen;
467 ab_mask = float_cmask(a->cls) | float_cmask(b->cls);
468 abc_mask = float_cmask(c->cls) | ab_mask;
471 * It is implementation-defined whether the cases of (0,inf,qnan)
472 * and (inf,0,qnan) raise InvalidOperation or not (and what QNaN
473 * they return if they do), so we have to hand this information
474 * off to the target-specific pick-a-NaN routine.
476 if (unlikely(abc_mask & float_cmask_anynan)) {
477 return parts_pick_nan_muladd(a, b, c, s, ab_mask, abc_mask);
480 if (flags & float_muladd_negate_c) {
484 /* Compute the sign of the product into A. */
486 if (flags & float_muladd_negate_product) {
490 if (unlikely(ab_mask != float_cmask_normal)) {
491 if (unlikely(ab_mask == float_cmask_infzero)) {
492 float_raise(float_flag_invalid | float_flag_invalid_imz, s);
496 if (ab_mask & float_cmask_inf) {
497 if (c->cls == float_class_inf && a->sign != c->sign) {
498 float_raise(float_flag_invalid | float_flag_invalid_isi, s);
504 g_assert(ab_mask & float_cmask_zero);
505 if (c->cls == float_class_normal) {
509 if (c->cls == float_class_zero) {
510 if (a->sign != c->sign) {
511 goto return_sub_zero;
515 g_assert(c->cls == float_class_inf);
518 if (unlikely(c->cls == float_class_inf)) {
523 /* Perform the multiplication step. */
524 p_widen.sign = a->sign;
525 p_widen.exp = a->exp + b->exp + 1;
526 frac_mulw(&p_widen, a, b);
527 if (!(p_widen.frac_hi & DECOMPOSED_IMPLICIT_BIT)) {
528 frac_add(&p_widen, &p_widen, &p_widen);
532 /* Perform the addition step. */
533 if (c->cls != float_class_zero) {
534 /* Zero-extend C to less significant bits. */
535 frac_widen(&c_widen, c);
536 c_widen.exp = c->exp;
538 if (a->sign == c->sign) {
539 parts_add_normal(&p_widen, &c_widen);
540 } else if (!parts_sub_normal(&p_widen, &c_widen)) {
541 goto return_sub_zero;
545 /* Narrow with sticky bit, for proper rounding later. */
546 frac_truncjam(a, &p_widen);
547 a->sign = p_widen.sign;
548 a->exp = p_widen.exp;
551 if (flags & float_muladd_halve_result) {
555 if (flags & float_muladd_negate_result) {
561 a->sign = s->float_rounding_mode == float_round_down;
563 a->cls = float_class_zero;
567 a->cls = float_class_inf;
571 parts_default_nan(a, s);
576 * Returns the result of dividing the floating-point value `a' by the
577 * corresponding value `b'. The operation is performed according to
578 * the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
580 static FloatPartsN *partsN(div)(FloatPartsN *a, FloatPartsN *b,
583 int ab_mask = float_cmask(a->cls) | float_cmask(b->cls);
584 bool sign = a->sign ^ b->sign;
586 if (likely(ab_mask == float_cmask_normal)) {
588 a->exp -= b->exp + frac_div(a, b);
592 /* 0/0 or Inf/Inf => NaN */
593 if (unlikely(ab_mask == float_cmask_zero)) {
594 float_raise(float_flag_invalid | float_flag_invalid_zdz, s);
597 if (unlikely(ab_mask == float_cmask_inf)) {
598 float_raise(float_flag_invalid | float_flag_invalid_idi, s);
602 /* All the NaN cases */
603 if (unlikely(ab_mask & float_cmask_anynan)) {
604 return parts_pick_nan(a, b, s);
610 if (a->cls == float_class_inf) {
615 if (a->cls == float_class_zero) {
620 if (b->cls == float_class_inf) {
621 a->cls = float_class_zero;
626 g_assert(b->cls == float_class_zero);
627 float_raise(float_flag_divbyzero, s);
628 a->cls = float_class_inf;
632 parts_default_nan(a, s);
637 * Floating point remainder, per IEC/IEEE, or modulus.
639 static FloatPartsN *partsN(modrem)(FloatPartsN *a, FloatPartsN *b,
640 uint64_t *mod_quot, float_status *s)
642 int ab_mask = float_cmask(a->cls) | float_cmask(b->cls);
644 if (likely(ab_mask == float_cmask_normal)) {
645 frac_modrem(a, b, mod_quot);
653 /* All the NaN cases */
654 if (unlikely(ab_mask & float_cmask_anynan)) {
655 return parts_pick_nan(a, b, s);
659 if (a->cls == float_class_inf || b->cls == float_class_zero) {
660 float_raise(float_flag_invalid, s);
661 parts_default_nan(a, s);
666 g_assert(b->cls == float_class_inf || a->cls == float_class_zero);
673 * The base algorithm is lifted from
674 * https://git.musl-libc.org/cgit/musl/tree/src/math/sqrtf.c
675 * https://git.musl-libc.org/cgit/musl/tree/src/math/sqrt.c
676 * https://git.musl-libc.org/cgit/musl/tree/src/math/sqrtl.c
677 * and is thus MIT licenced.
679 static void partsN(sqrt)(FloatPartsN *a, float_status *status,
682 const uint32_t three32 = 3u << 30;
683 const uint64_t three64 = 3ull << 62;
684 uint32_t d32, m32, r32, s32, u32; /* 32-bit computation */
685 uint64_t d64, m64, r64, s64, u64; /* 64-bit computation */
686 uint64_t dh, dl, rh, rl, sh, sl, uh, ul; /* 128-bit computation */
687 uint64_t d0h, d0l, d1h, d1l, d2h, d2l;
692 if (unlikely(a->cls != float_class_normal)) {
694 case float_class_snan:
695 case float_class_qnan:
696 parts_return_nan(a, status);
698 case float_class_zero:
700 case float_class_inf:
701 if (unlikely(a->sign)) {
706 g_assert_not_reached();
710 if (unlikely(a->sign)) {
715 * Argument reduction.
716 * x = 4^e frac; with integer e, and frac in [1, 4)
717 * m = frac fixed point at bit 62, since we're in base 4.
718 * If base-2 exponent is odd, exchange that for multiply by 2,
719 * which results in no shift.
721 exp_odd = a->exp & 1;
722 index = extract64(a->frac_hi, 57, 6) | (!exp_odd << 6);
728 * Approximate r ~= 1/sqrt(m) and s ~= sqrt(m) when m in [1, 4).
731 * 7-bit lookup table (1-bit exponent and 6-bit significand).
733 * The relative error (e = r0*sqrt(m)-1) of a linear estimate
734 * (r0 = a*m + b) is |e| < 0.085955 ~ 0x1.6p-4 at best;
735 * a table lookup is faster and needs one less iteration.
736 * The 7-bit table gives |e| < 0x1.fdp-9.
738 * A Newton-Raphson iteration for r is
744 * Fixed point representations:
745 * m, s, d, u, three are all 2.30; r is 0.32
750 r32 = rsqrt_tab[index] << 16;
751 /* |r*sqrt(m) - 1| < 0x1.FDp-9 */
753 s32 = ((uint64_t)m32 * r32) >> 32;
754 d32 = ((uint64_t)s32 * r32) >> 32;
758 /* float64 or smaller */
760 r32 = ((uint64_t)r32 * u32) >> 31;
761 /* |r*sqrt(m) - 1| < 0x1.7Bp-16 */
763 s32 = ((uint64_t)m32 * r32) >> 32;
764 d32 = ((uint64_t)s32 * r32) >> 32;
767 if (fmt->frac_size <= 23) {
768 /* float32 or smaller */
770 s32 = ((uint64_t)s32 * u32) >> 32; /* 3.29 */
771 s32 = (s32 - 1) >> 6; /* 9.23 */
772 /* s < sqrt(m) < s + 0x1.08p-23 */
774 /* compute nearest rounded result to 2.23 bits */
775 uint32_t d0 = (m32 << 16) - s32 * s32;
776 uint32_t d1 = s32 - d0;
777 uint32_t d2 = d1 + s32 + 1;
779 a->frac_hi = (uint64_t)s32 << (64 - 25);
781 /* increment or decrement for inexact */
783 a->frac_hi += ((int32_t)(d1 ^ d2) < 0 ? -1 : 1);
790 r64 = (uint64_t)r32 * u32 * 2;
791 /* |r*sqrt(m) - 1| < 0x1.37-p29; convert to 64-bit arithmetic */
792 mul64To128(m64, r64, &s64, &discard);
793 mul64To128(s64, r64, &d64, &discard);
796 mul64To128(s64, u64, &s64, &discard); /* 3.61 */
797 s64 = (s64 - 2) >> 9; /* 12.52 */
799 /* Compute nearest rounded result */
800 uint64_t d0 = (m64 << 42) - s64 * s64;
801 uint64_t d1 = s64 - d0;
802 uint64_t d2 = d1 + s64 + 1;
804 a->frac_hi = s64 << (64 - 54);
806 /* increment or decrement for inexact */
808 a->frac_hi += ((int64_t)(d1 ^ d2) < 0 ? -1 : 1);
813 r64 = (uint64_t)r32 * u32 * 2;
814 /* |r*sqrt(m) - 1| < 0x1.7Bp-16; convert to 64-bit arithmetic */
816 mul64To128(m64, r64, &s64, &discard);
817 mul64To128(s64, r64, &d64, &discard);
819 mul64To128(u64, r64, &r64, &discard);
821 /* |r*sqrt(m) - 1| < 0x1.a5p-31 */
823 mul64To128(m64, r64, &s64, &discard);
824 mul64To128(s64, r64, &d64, &discard);
826 mul64To128(u64, r64, &rh, &rl);
827 add128(rh, rl, rh, rl, &rh, &rl);
828 /* |r*sqrt(m) - 1| < 0x1.c001p-59; change to 128-bit arithmetic */
830 mul128To256(a->frac_hi, a->frac_lo, rh, rl, &sh, &sl, &discard, &discard);
831 mul128To256(sh, sl, rh, rl, &dh, &dl, &discard, &discard);
832 sub128(three64, 0, dh, dl, &uh, &ul);
833 mul128To256(uh, ul, sh, sl, &sh, &sl, &discard, &discard); /* 3.125 */
834 /* -0x1p-116 < s - sqrt(m) < 0x3.8001p-125 */
836 sub128(sh, sl, 0, 4, &sh, &sl);
837 shift128Right(sh, sl, 13, &sh, &sl); /* 16.112 */
838 /* s < sqrt(m) < s + 1ulp */
840 /* Compute nearest rounded result */
841 mul64To128(sl, sl, &d0h, &d0l);
843 sub128(a->frac_lo << 34, 0, d0h, d0l, &d0h, &d0l);
844 sub128(sh, sl, d0h, d0l, &d1h, &d1l);
845 add128(sh, sl, 0, 1, &d2h, &d2l);
846 add128(d2h, d2l, d1h, d1l, &d2h, &d2l);
847 add128(sh, sl, 0, d1h >> 63, &sh, &sl);
848 shift128Left(sh, sl, 128 - 114, &sh, &sl);
850 /* increment or decrement for inexact */
852 if ((int64_t)(d1h ^ d2h) < 0) {
853 sub128(sh, sl, 0, 1, &sh, &sl);
855 add128(sh, sl, 0, 1, &sh, &sl);
862 /* Convert back from base 4 to base 2. */
864 if (!(a->frac_hi & DECOMPOSED_IMPLICIT_BIT)) {
872 float_raise(float_flag_invalid | float_flag_invalid_sqrt, status);
873 parts_default_nan(a, status);
877 * Rounds the floating-point value `a' to an integer, and returns the
878 * result as a floating-point value. The operation is performed
879 * according to the IEC/IEEE Standard for Binary Floating-Point
882 * parts_round_to_int_normal is an internal helper function for
883 * normal numbers only, returning true for inexact but not directly
884 * raising float_flag_inexact.
886 static bool partsN(round_to_int_normal)(FloatPartsN *a, FloatRoundMode rmode,
887 int scale, int frac_size)
889 uint64_t frac_lsb, frac_lsbm1, rnd_even_mask, rnd_mask, inc;
892 scale = MIN(MAX(scale, -0x10000), 0x10000);
900 case float_round_nearest_even:
904 /* Shift left one, discarding DECOMPOSED_IMPLICIT_BIT */
905 frac_add(&tmp, a, a);
906 /* Anything remaining means frac > 0.5. */
907 one = !frac_eqz(&tmp);
910 case float_round_ties_away:
913 case float_round_to_zero:
919 case float_round_down:
922 case float_round_to_odd:
926 g_assert_not_reached();
932 a->frac_hi = DECOMPOSED_IMPLICIT_BIT;
934 a->cls = float_class_zero;
939 if (a->exp >= frac_size) {
944 if (N > 64 && a->exp < N - 64) {
946 * Rounding is not in the low word -- shift lsb to bit 2,
947 * which leaves room for sticky and rounding bit.
949 shift_adj = (N - 1) - (a->exp + 2);
950 frac_shrjam(a, shift_adj);
954 frac_lsb = DECOMPOSED_IMPLICIT_BIT >> (a->exp & 63);
957 frac_lsbm1 = frac_lsb >> 1;
958 rnd_mask = frac_lsb - 1;
959 rnd_even_mask = rnd_mask | frac_lsb;
961 if (!(a->frac_lo & rnd_mask)) {
962 /* Fractional bits already clear, undo the shift above. */
963 frac_shl(a, shift_adj);
968 case float_round_nearest_even:
969 inc = ((a->frac_lo & rnd_even_mask) != frac_lsbm1 ? frac_lsbm1 : 0);
971 case float_round_ties_away:
974 case float_round_to_zero:
978 inc = a->sign ? 0 : rnd_mask;
980 case float_round_down:
981 inc = a->sign ? rnd_mask : 0;
983 case float_round_to_odd:
984 inc = a->frac_lo & frac_lsb ? 0 : rnd_mask;
987 g_assert_not_reached();
990 if (shift_adj == 0) {
991 if (frac_addi(a, a, inc)) {
993 a->frac_hi |= DECOMPOSED_IMPLICIT_BIT;
996 a->frac_lo &= ~rnd_mask;
998 frac_addi(a, a, inc);
999 a->frac_lo &= ~rnd_mask;
1000 /* Be careful shifting back, not to overflow */
1001 frac_shl(a, shift_adj - 1);
1002 if (a->frac_hi & DECOMPOSED_IMPLICIT_BIT) {
1011 static void partsN(round_to_int)(FloatPartsN *a, FloatRoundMode rmode,
1012 int scale, float_status *s,
1013 const FloatFmt *fmt)
1016 case float_class_qnan:
1017 case float_class_snan:
1018 parts_return_nan(a, s);
1020 case float_class_zero:
1021 case float_class_inf:
1023 case float_class_normal:
1024 if (parts_round_to_int_normal(a, rmode, scale, fmt->frac_size)) {
1025 float_raise(float_flag_inexact, s);
1029 g_assert_not_reached();
1034 * Returns the result of converting the floating-point value `a' to
1035 * the two's complement integer format. The conversion is performed
1036 * according to the IEC/IEEE Standard for Binary Floating-Point
1037 * Arithmetic---which means in particular that the conversion is
1038 * rounded according to the current rounding mode. If `a' is a NaN,
1039 * the largest positive integer is returned. Otherwise, if the
1040 * conversion overflows, the largest integer with the same sign as `a'
1043 static int64_t partsN(float_to_sint)(FloatPartsN *p, FloatRoundMode rmode,
1044 int scale, int64_t min, int64_t max,
1051 case float_class_snan:
1052 flags |= float_flag_invalid_snan;
1054 case float_class_qnan:
1055 flags |= float_flag_invalid;
1059 case float_class_inf:
1060 flags = float_flag_invalid | float_flag_invalid_cvti;
1061 r = p->sign ? min : max;
1064 case float_class_zero:
1067 case float_class_normal:
1068 /* TODO: N - 2 is frac_size for rounding; could use input fmt. */
1069 if (parts_round_to_int_normal(p, rmode, scale, N - 2)) {
1070 flags = float_flag_inexact;
1073 if (p->exp <= DECOMPOSED_BINARY_POINT) {
1074 r = p->frac_hi >> (DECOMPOSED_BINARY_POINT - p->exp);
1079 if (r <= -(uint64_t)min) {
1082 flags = float_flag_invalid | float_flag_invalid_cvti;
1085 } else if (r > max) {
1086 flags = float_flag_invalid | float_flag_invalid_cvti;
1092 g_assert_not_reached();
1095 float_raise(flags, s);
1100 * Returns the result of converting the floating-point value `a' to
1101 * the unsigned integer format. The conversion is performed according
1102 * to the IEC/IEEE Standard for Binary Floating-Point
1103 * Arithmetic---which means in particular that the conversion is
1104 * rounded according to the current rounding mode. If `a' is a NaN,
1105 * the largest unsigned integer is returned. Otherwise, if the
1106 * conversion overflows, the largest unsigned integer is returned. If
1107 * the 'a' is negative, the result is rounded and zero is returned;
1108 * values that do not round to zero will raise the inexact exception
1111 static uint64_t partsN(float_to_uint)(FloatPartsN *p, FloatRoundMode rmode,
1112 int scale, uint64_t max, float_status *s)
1118 case float_class_snan:
1119 flags |= float_flag_invalid_snan;
1121 case float_class_qnan:
1122 flags |= float_flag_invalid;
1126 case float_class_inf:
1127 flags = float_flag_invalid | float_flag_invalid_cvti;
1128 r = p->sign ? 0 : max;
1131 case float_class_zero:
1134 case float_class_normal:
1135 /* TODO: N - 2 is frac_size for rounding; could use input fmt. */
1136 if (parts_round_to_int_normal(p, rmode, scale, N - 2)) {
1137 flags = float_flag_inexact;
1138 if (p->cls == float_class_zero) {
1145 flags = float_flag_invalid | float_flag_invalid_cvti;
1147 } else if (p->exp > DECOMPOSED_BINARY_POINT) {
1148 flags = float_flag_invalid | float_flag_invalid_cvti;
1151 r = p->frac_hi >> (DECOMPOSED_BINARY_POINT - p->exp);
1153 flags = float_flag_invalid | float_flag_invalid_cvti;
1160 g_assert_not_reached();
1163 float_raise(flags, s);
1168 * Integer to float conversions
1170 * Returns the result of converting the two's complement integer `a'
1171 * to the floating-point format. The conversion is performed according
1172 * to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
1174 static void partsN(sint_to_float)(FloatPartsN *p, int64_t a,
1175 int scale, float_status *s)
1180 memset(p, 0, sizeof(*p));
1183 p->cls = float_class_zero;
1187 p->cls = float_class_normal;
1193 scale = MIN(MAX(scale, -0x10000), 0x10000);
1195 p->exp = DECOMPOSED_BINARY_POINT - shift + scale;
1196 p->frac_hi = f << shift;
1200 * Unsigned Integer to float conversions
1202 * Returns the result of converting the unsigned integer `a' to the
1203 * floating-point format. The conversion is performed according to the
1204 * IEC/IEEE Standard for Binary Floating-Point Arithmetic.
1206 static void partsN(uint_to_float)(FloatPartsN *p, uint64_t a,
1207 int scale, float_status *status)
1209 memset(p, 0, sizeof(*p));
1212 p->cls = float_class_zero;
1214 int shift = clz64(a);
1215 scale = MIN(MAX(scale, -0x10000), 0x10000);
1216 p->cls = float_class_normal;
1217 p->exp = DECOMPOSED_BINARY_POINT - shift + scale;
1218 p->frac_hi = a << shift;
1225 static FloatPartsN *partsN(minmax)(FloatPartsN *a, FloatPartsN *b,
1226 float_status *s, int flags)
1228 int ab_mask = float_cmask(a->cls) | float_cmask(b->cls);
1229 int a_exp, b_exp, cmp;
1231 if (unlikely(ab_mask & float_cmask_anynan)) {
1233 * For minNum/maxNum (IEEE 754-2008)
1234 * or minimumNumber/maximumNumber (IEEE 754-2019),
1235 * if one operand is a QNaN, and the other
1236 * operand is numerical, then return numerical argument.
1238 if ((flags & (minmax_isnum | minmax_isnumber))
1239 && !(ab_mask & float_cmask_snan)
1240 && (ab_mask & ~float_cmask_qnan)) {
1241 return is_nan(a->cls) ? b : a;
1245 * In IEEE 754-2019, minNum, maxNum, minNumMag and maxNumMag
1246 * are removed and replaced with minimum, minimumNumber, maximum
1247 * and maximumNumber.
1248 * minimumNumber/maximumNumber behavior for SNaN is changed to:
1249 * If both operands are NaNs, a QNaN is returned.
1250 * If either operand is a SNaN,
1251 * an invalid operation exception is signaled,
1252 * but unless both operands are NaNs,
1253 * the SNaN is otherwise ignored and not converted to a QNaN.
1255 if ((flags & minmax_isnumber)
1256 && (ab_mask & float_cmask_snan)
1257 && (ab_mask & ~float_cmask_anynan)) {
1258 float_raise(float_flag_invalid, s);
1259 return is_nan(a->cls) ? b : a;
1262 return parts_pick_nan(a, b, s);
1268 if (unlikely(ab_mask != float_cmask_normal)) {
1270 case float_class_normal:
1272 case float_class_inf:
1275 case float_class_zero:
1279 g_assert_not_reached();
1283 case float_class_normal:
1285 case float_class_inf:
1288 case float_class_zero:
1292 g_assert_not_reached();
1297 /* Compare magnitudes. */
1298 cmp = a_exp - b_exp;
1300 cmp = frac_cmp(a, b);
1304 * Take the sign into account.
1305 * For ismag, only do this if the magnitudes are equal.
1307 if (!(flags & minmax_ismag) || cmp == 0) {
1308 if (a->sign != b->sign) {
1309 /* For differing signs, the negative operand is less. */
1310 cmp = a->sign ? -1 : 1;
1311 } else if (a->sign) {
1312 /* For two negative operands, invert the magnitude comparison. */
1317 if (flags & minmax_ismin) {
1320 return cmp < 0 ? b : a;
1324 * Floating point compare
1326 static FloatRelation partsN(compare)(FloatPartsN *a, FloatPartsN *b,
1327 float_status *s, bool is_quiet)
1329 int ab_mask = float_cmask(a->cls) | float_cmask(b->cls);
1332 if (likely(ab_mask == float_cmask_normal)) {
1333 if (a->sign != b->sign) {
1336 if (a->exp != b->exp) {
1337 cmp = a->exp < b->exp ? -1 : 1;
1339 cmp = frac_cmp(a, b);
1347 if (unlikely(ab_mask & float_cmask_anynan)) {
1348 if (ab_mask & float_cmask_snan) {
1349 float_raise(float_flag_invalid | float_flag_invalid_snan, s);
1350 } else if (!is_quiet) {
1351 float_raise(float_flag_invalid, s);
1353 return float_relation_unordered;
1356 if (ab_mask & float_cmask_zero) {
1357 if (ab_mask == float_cmask_zero) {
1358 return float_relation_equal;
1359 } else if (a->cls == float_class_zero) {
1366 if (ab_mask == float_cmask_inf) {
1367 if (a->sign == b->sign) {
1368 return float_relation_equal;
1370 } else if (b->cls == float_class_inf) {
1373 g_assert(a->cls == float_class_inf);
1377 return a->sign ? float_relation_less : float_relation_greater;
1379 return b->sign ? float_relation_greater : float_relation_less;
1383 * Multiply A by 2 raised to the power N.
1385 static void partsN(scalbn)(FloatPartsN *a, int n, float_status *s)
1388 case float_class_snan:
1389 case float_class_qnan:
1390 parts_return_nan(a, s);
1392 case float_class_zero:
1393 case float_class_inf:
1395 case float_class_normal:
1396 a->exp += MIN(MAX(n, -0x10000), 0x10000);
1399 g_assert_not_reached();
1406 static void partsN(log2)(FloatPartsN *a, float_status *s, const FloatFmt *fmt)
1408 uint64_t a0, a1, r, t, ign;
1410 int i, n, a_exp, f_exp;
1412 if (unlikely(a->cls != float_class_normal)) {
1414 case float_class_snan:
1415 case float_class_qnan:
1416 parts_return_nan(a, s);
1418 case float_class_zero:
1419 /* log2(0) = -inf */
1420 a->cls = float_class_inf;
1423 case float_class_inf:
1424 if (unlikely(a->sign)) {
1431 g_assert_not_reached();
1433 if (unlikely(a->sign)) {
1437 /* TODO: This algorithm looses bits too quickly for float128. */
1444 t = DECOMPOSED_IMPLICIT_BIT;
1448 n = fmt->frac_size + 2;
1449 if (unlikely(a_exp == -1)) {
1451 * When a_exp == -1, we're computing the log2 of a value [0.5,1.0).
1452 * When the value is very close to 1.0, there are lots of 1's in
1453 * the msb parts of the fraction. At the end, when we subtract
1454 * this value from -1.0, we can see a catastrophic loss of precision,
1455 * as 0x800..000 - 0x7ff..ffx becomes 0x000..00y, leaving only the
1456 * bits of y in the final result. To minimize this, compute as many
1458 * ??? This case needs another algorithm to avoid this.
1460 n = fmt->frac_size * 2 + 2;
1461 /* Don't compute a value overlapping the sticky bit */
1465 for (i = 0; i < n; i++) {
1467 mul128To256(a0, a1, a0, a1, &a0, &a1, &ign, &ign);
1468 } else if (a0 & 0xffffffffull) {
1469 mul64To128(a0, a0, &a0, &a1);
1470 } else if (a0 & ~DECOMPOSED_IMPLICIT_BIT) {
1477 if (a0 & DECOMPOSED_IMPLICIT_BIT) {
1478 if (unlikely(a_exp == 0 && r == 0)) {
1480 * When a_exp == 0, we're computing the log2 of a value
1481 * [1.0,2.0). When the value is very close to 1.0, there
1482 * are lots of 0's in the msb parts of the fraction.
1483 * We need to compute more digits to produce a correct
1484 * result -- restart at the top of the fraction.
1485 * ??? This is likely to lose precision quickly, as for
1486 * float128; we may need another method.
1489 t = r = DECOMPOSED_IMPLICIT_BIT;
1495 add128(a0, a1, a0, a1, &a0, &a1);
1500 /* Set sticky for inexact. */
1501 r |= (a1 || a0 & ~DECOMPOSED_IMPLICIT_BIT);
1504 parts_sint_to_float(a, a_exp, 0, s);
1509 memset(&f, 0, sizeof(f));
1510 f.cls = float_class_normal;
1512 f.exp = f_exp - frac_normalize(&f);
1515 parts_sub_normal(a, &f);
1516 } else if (a_exp > 0) {
1517 parts_add_normal(a, &f);
1524 float_raise(float_flag_invalid, s);
1525 parts_default_nan(a, s);