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, 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, 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, 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, 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, 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)) {
495 if (ab_mask & float_cmask_inf) {
496 if (c->cls == float_class_inf && a->sign != c->sign) {
502 g_assert(ab_mask & float_cmask_zero);
503 if (c->cls == float_class_normal) {
507 if (c->cls == float_class_zero) {
508 if (a->sign != c->sign) {
509 goto return_sub_zero;
513 g_assert(c->cls == float_class_inf);
516 if (unlikely(c->cls == float_class_inf)) {
521 /* Perform the multiplication step. */
522 p_widen.sign = a->sign;
523 p_widen.exp = a->exp + b->exp + 1;
524 frac_mulw(&p_widen, a, b);
525 if (!(p_widen.frac_hi & DECOMPOSED_IMPLICIT_BIT)) {
526 frac_add(&p_widen, &p_widen, &p_widen);
530 /* Perform the addition step. */
531 if (c->cls != float_class_zero) {
532 /* Zero-extend C to less significant bits. */
533 frac_widen(&c_widen, c);
534 c_widen.exp = c->exp;
536 if (a->sign == c->sign) {
537 parts_add_normal(&p_widen, &c_widen);
538 } else if (!parts_sub_normal(&p_widen, &c_widen)) {
539 goto return_sub_zero;
543 /* Narrow with sticky bit, for proper rounding later. */
544 frac_truncjam(a, &p_widen);
545 a->sign = p_widen.sign;
546 a->exp = p_widen.exp;
549 if (flags & float_muladd_halve_result) {
553 if (flags & float_muladd_negate_result) {
559 a->sign = s->float_rounding_mode == float_round_down;
561 a->cls = float_class_zero;
565 a->cls = float_class_inf;
569 float_raise(float_flag_invalid, s);
570 parts_default_nan(a, s);
575 * Returns the result of dividing the floating-point value `a' by the
576 * corresponding value `b'. The operation is performed according to
577 * the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
579 static FloatPartsN *partsN(div)(FloatPartsN *a, FloatPartsN *b,
582 int ab_mask = float_cmask(a->cls) | float_cmask(b->cls);
583 bool sign = a->sign ^ b->sign;
585 if (likely(ab_mask == float_cmask_normal)) {
587 a->exp -= b->exp + frac_div(a, b);
591 /* 0/0 or Inf/Inf => NaN */
592 if (unlikely(ab_mask == float_cmask_zero) ||
593 unlikely(ab_mask == float_cmask_inf)) {
594 float_raise(float_flag_invalid, s);
595 parts_default_nan(a, s);
599 /* All the NaN cases */
600 if (unlikely(ab_mask & float_cmask_anynan)) {
601 return parts_pick_nan(a, b, s);
607 if (a->cls == float_class_inf) {
612 if (a->cls == float_class_zero) {
617 if (b->cls == float_class_inf) {
618 a->cls = float_class_zero;
623 g_assert(b->cls == float_class_zero);
624 float_raise(float_flag_divbyzero, s);
625 a->cls = float_class_inf;
630 * Floating point remainder, per IEC/IEEE, or modulus.
632 static FloatPartsN *partsN(modrem)(FloatPartsN *a, FloatPartsN *b,
633 uint64_t *mod_quot, float_status *s)
635 int ab_mask = float_cmask(a->cls) | float_cmask(b->cls);
637 if (likely(ab_mask == float_cmask_normal)) {
638 frac_modrem(a, b, mod_quot);
646 /* All the NaN cases */
647 if (unlikely(ab_mask & float_cmask_anynan)) {
648 return parts_pick_nan(a, b, s);
652 if (a->cls == float_class_inf || b->cls == float_class_zero) {
653 float_raise(float_flag_invalid, s);
654 parts_default_nan(a, s);
659 g_assert(b->cls == float_class_inf || a->cls == float_class_zero);
666 * The base algorithm is lifted from
667 * https://git.musl-libc.org/cgit/musl/tree/src/math/sqrtf.c
668 * https://git.musl-libc.org/cgit/musl/tree/src/math/sqrt.c
669 * https://git.musl-libc.org/cgit/musl/tree/src/math/sqrtl.c
670 * and is thus MIT licenced.
672 static void partsN(sqrt)(FloatPartsN *a, float_status *status,
675 const uint32_t three32 = 3u << 30;
676 const uint64_t three64 = 3ull << 62;
677 uint32_t d32, m32, r32, s32, u32; /* 32-bit computation */
678 uint64_t d64, m64, r64, s64, u64; /* 64-bit computation */
679 uint64_t dh, dl, rh, rl, sh, sl, uh, ul; /* 128-bit computation */
680 uint64_t d0h, d0l, d1h, d1l, d2h, d2l;
685 if (unlikely(a->cls != float_class_normal)) {
687 case float_class_snan:
688 case float_class_qnan:
689 parts_return_nan(a, status);
691 case float_class_zero:
693 case float_class_inf:
694 if (unlikely(a->sign)) {
699 g_assert_not_reached();
703 if (unlikely(a->sign)) {
708 * Argument reduction.
709 * x = 4^e frac; with integer e, and frac in [1, 4)
710 * m = frac fixed point at bit 62, since we're in base 4.
711 * If base-2 exponent is odd, exchange that for multiply by 2,
712 * which results in no shift.
714 exp_odd = a->exp & 1;
715 index = extract64(a->frac_hi, 57, 6) | (!exp_odd << 6);
721 * Approximate r ~= 1/sqrt(m) and s ~= sqrt(m) when m in [1, 4).
724 * 7-bit lookup table (1-bit exponent and 6-bit significand).
726 * The relative error (e = r0*sqrt(m)-1) of a linear estimate
727 * (r0 = a*m + b) is |e| < 0.085955 ~ 0x1.6p-4 at best;
728 * a table lookup is faster and needs one less iteration.
729 * The 7-bit table gives |e| < 0x1.fdp-9.
731 * A Newton-Raphson iteration for r is
737 * Fixed point representations:
738 * m, s, d, u, three are all 2.30; r is 0.32
743 r32 = rsqrt_tab[index] << 16;
744 /* |r*sqrt(m) - 1| < 0x1.FDp-9 */
746 s32 = ((uint64_t)m32 * r32) >> 32;
747 d32 = ((uint64_t)s32 * r32) >> 32;
751 /* float64 or smaller */
753 r32 = ((uint64_t)r32 * u32) >> 31;
754 /* |r*sqrt(m) - 1| < 0x1.7Bp-16 */
756 s32 = ((uint64_t)m32 * r32) >> 32;
757 d32 = ((uint64_t)s32 * r32) >> 32;
760 if (fmt->frac_size <= 23) {
761 /* float32 or smaller */
763 s32 = ((uint64_t)s32 * u32) >> 32; /* 3.29 */
764 s32 = (s32 - 1) >> 6; /* 9.23 */
765 /* s < sqrt(m) < s + 0x1.08p-23 */
767 /* compute nearest rounded result to 2.23 bits */
768 uint32_t d0 = (m32 << 16) - s32 * s32;
769 uint32_t d1 = s32 - d0;
770 uint32_t d2 = d1 + s32 + 1;
772 a->frac_hi = (uint64_t)s32 << (64 - 25);
774 /* increment or decrement for inexact */
776 a->frac_hi += ((int32_t)(d1 ^ d2) < 0 ? -1 : 1);
783 r64 = (uint64_t)r32 * u32 * 2;
784 /* |r*sqrt(m) - 1| < 0x1.37-p29; convert to 64-bit arithmetic */
785 mul64To128(m64, r64, &s64, &discard);
786 mul64To128(s64, r64, &d64, &discard);
789 mul64To128(s64, u64, &s64, &discard); /* 3.61 */
790 s64 = (s64 - 2) >> 9; /* 12.52 */
792 /* Compute nearest rounded result */
793 uint64_t d0 = (m64 << 42) - s64 * s64;
794 uint64_t d1 = s64 - d0;
795 uint64_t d2 = d1 + s64 + 1;
797 a->frac_hi = s64 << (64 - 54);
799 /* increment or decrement for inexact */
801 a->frac_hi += ((int64_t)(d1 ^ d2) < 0 ? -1 : 1);
806 r64 = (uint64_t)r32 * u32 * 2;
807 /* |r*sqrt(m) - 1| < 0x1.7Bp-16; convert to 64-bit arithmetic */
809 mul64To128(m64, r64, &s64, &discard);
810 mul64To128(s64, r64, &d64, &discard);
812 mul64To128(u64, r64, &r64, &discard);
814 /* |r*sqrt(m) - 1| < 0x1.a5p-31 */
816 mul64To128(m64, r64, &s64, &discard);
817 mul64To128(s64, r64, &d64, &discard);
819 mul64To128(u64, r64, &rh, &rl);
820 add128(rh, rl, rh, rl, &rh, &rl);
821 /* |r*sqrt(m) - 1| < 0x1.c001p-59; change to 128-bit arithmetic */
823 mul128To256(a->frac_hi, a->frac_lo, rh, rl, &sh, &sl, &discard, &discard);
824 mul128To256(sh, sl, rh, rl, &dh, &dl, &discard, &discard);
825 sub128(three64, 0, dh, dl, &uh, &ul);
826 mul128To256(uh, ul, sh, sl, &sh, &sl, &discard, &discard); /* 3.125 */
827 /* -0x1p-116 < s - sqrt(m) < 0x3.8001p-125 */
829 sub128(sh, sl, 0, 4, &sh, &sl);
830 shift128Right(sh, sl, 13, &sh, &sl); /* 16.112 */
831 /* s < sqrt(m) < s + 1ulp */
833 /* Compute nearest rounded result */
834 mul64To128(sl, sl, &d0h, &d0l);
836 sub128(a->frac_lo << 34, 0, d0h, d0l, &d0h, &d0l);
837 sub128(sh, sl, d0h, d0l, &d1h, &d1l);
838 add128(sh, sl, 0, 1, &d2h, &d2l);
839 add128(d2h, d2l, d1h, d1l, &d2h, &d2l);
840 add128(sh, sl, 0, d1h >> 63, &sh, &sl);
841 shift128Left(sh, sl, 128 - 114, &sh, &sl);
843 /* increment or decrement for inexact */
845 if ((int64_t)(d1h ^ d2h) < 0) {
846 sub128(sh, sl, 0, 1, &sh, &sl);
848 add128(sh, sl, 0, 1, &sh, &sl);
855 /* Convert back from base 4 to base 2. */
857 if (!(a->frac_hi & DECOMPOSED_IMPLICIT_BIT)) {
865 float_raise(float_flag_invalid, status);
866 parts_default_nan(a, status);
870 * Rounds the floating-point value `a' to an integer, and returns the
871 * result as a floating-point value. The operation is performed
872 * according to the IEC/IEEE Standard for Binary Floating-Point
875 * parts_round_to_int_normal is an internal helper function for
876 * normal numbers only, returning true for inexact but not directly
877 * raising float_flag_inexact.
879 static bool partsN(round_to_int_normal)(FloatPartsN *a, FloatRoundMode rmode,
880 int scale, int frac_size)
882 uint64_t frac_lsb, frac_lsbm1, rnd_even_mask, rnd_mask, inc;
885 scale = MIN(MAX(scale, -0x10000), 0x10000);
893 case float_round_nearest_even:
897 /* Shift left one, discarding DECOMPOSED_IMPLICIT_BIT */
898 frac_add(&tmp, a, a);
899 /* Anything remaining means frac > 0.5. */
900 one = !frac_eqz(&tmp);
903 case float_round_ties_away:
906 case float_round_to_zero:
912 case float_round_down:
915 case float_round_to_odd:
919 g_assert_not_reached();
925 a->frac_hi = DECOMPOSED_IMPLICIT_BIT;
927 a->cls = float_class_zero;
932 if (a->exp >= frac_size) {
937 if (N > 64 && a->exp < N - 64) {
939 * Rounding is not in the low word -- shift lsb to bit 2,
940 * which leaves room for sticky and rounding bit.
942 shift_adj = (N - 1) - (a->exp + 2);
943 frac_shrjam(a, shift_adj);
947 frac_lsb = DECOMPOSED_IMPLICIT_BIT >> (a->exp & 63);
950 frac_lsbm1 = frac_lsb >> 1;
951 rnd_mask = frac_lsb - 1;
952 rnd_even_mask = rnd_mask | frac_lsb;
954 if (!(a->frac_lo & rnd_mask)) {
955 /* Fractional bits already clear, undo the shift above. */
956 frac_shl(a, shift_adj);
961 case float_round_nearest_even:
962 inc = ((a->frac_lo & rnd_even_mask) != frac_lsbm1 ? frac_lsbm1 : 0);
964 case float_round_ties_away:
967 case float_round_to_zero:
971 inc = a->sign ? 0 : rnd_mask;
973 case float_round_down:
974 inc = a->sign ? rnd_mask : 0;
976 case float_round_to_odd:
977 inc = a->frac_lo & frac_lsb ? 0 : rnd_mask;
980 g_assert_not_reached();
983 if (shift_adj == 0) {
984 if (frac_addi(a, a, inc)) {
986 a->frac_hi |= DECOMPOSED_IMPLICIT_BIT;
989 a->frac_lo &= ~rnd_mask;
991 frac_addi(a, a, inc);
992 a->frac_lo &= ~rnd_mask;
993 /* Be careful shifting back, not to overflow */
994 frac_shl(a, shift_adj - 1);
995 if (a->frac_hi & DECOMPOSED_IMPLICIT_BIT) {
1004 static void partsN(round_to_int)(FloatPartsN *a, FloatRoundMode rmode,
1005 int scale, float_status *s,
1006 const FloatFmt *fmt)
1009 case float_class_qnan:
1010 case float_class_snan:
1011 parts_return_nan(a, s);
1013 case float_class_zero:
1014 case float_class_inf:
1016 case float_class_normal:
1017 if (parts_round_to_int_normal(a, rmode, scale, fmt->frac_size)) {
1018 float_raise(float_flag_inexact, s);
1022 g_assert_not_reached();
1027 * Returns the result of converting the floating-point value `a' to
1028 * the two's complement integer format. The conversion is performed
1029 * according to the IEC/IEEE Standard for Binary Floating-Point
1030 * Arithmetic---which means in particular that the conversion is
1031 * rounded according to the current rounding mode. If `a' is a NaN,
1032 * the largest positive integer is returned. Otherwise, if the
1033 * conversion overflows, the largest integer with the same sign as `a'
1036 static int64_t partsN(float_to_sint)(FloatPartsN *p, FloatRoundMode rmode,
1037 int scale, int64_t min, int64_t max,
1044 case float_class_snan:
1045 case float_class_qnan:
1046 flags = float_flag_invalid;
1050 case float_class_inf:
1051 flags = float_flag_invalid;
1052 r = p->sign ? min : max;
1055 case float_class_zero:
1058 case float_class_normal:
1059 /* TODO: N - 2 is frac_size for rounding; could use input fmt. */
1060 if (parts_round_to_int_normal(p, rmode, scale, N - 2)) {
1061 flags = float_flag_inexact;
1064 if (p->exp <= DECOMPOSED_BINARY_POINT) {
1065 r = p->frac_hi >> (DECOMPOSED_BINARY_POINT - p->exp);
1070 if (r <= -(uint64_t)min) {
1073 flags = float_flag_invalid;
1076 } else if (r > max) {
1077 flags = float_flag_invalid;
1083 g_assert_not_reached();
1086 float_raise(flags, s);
1091 * Returns the result of converting the floating-point value `a' to
1092 * the unsigned integer format. The conversion is performed according
1093 * to the IEC/IEEE Standard for Binary Floating-Point
1094 * Arithmetic---which means in particular that the conversion is
1095 * rounded according to the current rounding mode. If `a' is a NaN,
1096 * the largest unsigned integer is returned. Otherwise, if the
1097 * conversion overflows, the largest unsigned integer is returned. If
1098 * the 'a' is negative, the result is rounded and zero is returned;
1099 * values that do not round to zero will raise the inexact exception
1102 static uint64_t partsN(float_to_uint)(FloatPartsN *p, FloatRoundMode rmode,
1103 int scale, uint64_t max, float_status *s)
1109 case float_class_snan:
1110 case float_class_qnan:
1111 flags = float_flag_invalid;
1115 case float_class_inf:
1116 flags = float_flag_invalid;
1117 r = p->sign ? 0 : max;
1120 case float_class_zero:
1123 case float_class_normal:
1124 /* TODO: N - 2 is frac_size for rounding; could use input fmt. */
1125 if (parts_round_to_int_normal(p, rmode, scale, N - 2)) {
1126 flags = float_flag_inexact;
1127 if (p->cls == float_class_zero) {
1134 flags = float_flag_invalid;
1136 } else if (p->exp > DECOMPOSED_BINARY_POINT) {
1137 flags = float_flag_invalid;
1140 r = p->frac_hi >> (DECOMPOSED_BINARY_POINT - p->exp);
1142 flags = float_flag_invalid;
1149 g_assert_not_reached();
1152 float_raise(flags, s);
1157 * Integer to float conversions
1159 * Returns the result of converting the two's complement integer `a'
1160 * to the floating-point format. The conversion is performed according
1161 * to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
1163 static void partsN(sint_to_float)(FloatPartsN *p, int64_t a,
1164 int scale, float_status *s)
1169 memset(p, 0, sizeof(*p));
1172 p->cls = float_class_zero;
1176 p->cls = float_class_normal;
1182 scale = MIN(MAX(scale, -0x10000), 0x10000);
1184 p->exp = DECOMPOSED_BINARY_POINT - shift + scale;
1185 p->frac_hi = f << shift;
1189 * Unsigned Integer to float conversions
1191 * Returns the result of converting the unsigned integer `a' to the
1192 * floating-point format. The conversion is performed according to the
1193 * IEC/IEEE Standard for Binary Floating-Point Arithmetic.
1195 static void partsN(uint_to_float)(FloatPartsN *p, uint64_t a,
1196 int scale, float_status *status)
1198 memset(p, 0, sizeof(*p));
1201 p->cls = float_class_zero;
1203 int shift = clz64(a);
1204 scale = MIN(MAX(scale, -0x10000), 0x10000);
1205 p->cls = float_class_normal;
1206 p->exp = DECOMPOSED_BINARY_POINT - shift + scale;
1207 p->frac_hi = a << shift;
1214 static FloatPartsN *partsN(minmax)(FloatPartsN *a, FloatPartsN *b,
1215 float_status *s, int flags)
1217 int ab_mask = float_cmask(a->cls) | float_cmask(b->cls);
1218 int a_exp, b_exp, cmp;
1220 if (unlikely(ab_mask & float_cmask_anynan)) {
1222 * For minnum/maxnum, if one operand is a QNaN, and the other
1223 * operand is numerical, then return numerical argument.
1225 if ((flags & minmax_isnum)
1226 && !(ab_mask & float_cmask_snan)
1227 && (ab_mask & ~float_cmask_qnan)) {
1228 return is_nan(a->cls) ? b : a;
1230 return parts_pick_nan(a, b, s);
1236 if (unlikely(ab_mask != float_cmask_normal)) {
1238 case float_class_normal:
1240 case float_class_inf:
1243 case float_class_zero:
1247 g_assert_not_reached();
1251 case float_class_normal:
1253 case float_class_inf:
1256 case float_class_zero:
1260 g_assert_not_reached();
1265 /* Compare magnitudes. */
1266 cmp = a_exp - b_exp;
1268 cmp = frac_cmp(a, b);
1272 * Take the sign into account.
1273 * For ismag, only do this if the magnitudes are equal.
1275 if (!(flags & minmax_ismag) || cmp == 0) {
1276 if (a->sign != b->sign) {
1277 /* For differing signs, the negative operand is less. */
1278 cmp = a->sign ? -1 : 1;
1279 } else if (a->sign) {
1280 /* For two negative operands, invert the magnitude comparison. */
1285 if (flags & minmax_ismin) {
1288 return cmp < 0 ? b : a;
1292 * Floating point compare
1294 static FloatRelation partsN(compare)(FloatPartsN *a, FloatPartsN *b,
1295 float_status *s, bool is_quiet)
1297 int ab_mask = float_cmask(a->cls) | float_cmask(b->cls);
1300 if (likely(ab_mask == float_cmask_normal)) {
1301 if (a->sign != b->sign) {
1304 if (a->exp != b->exp) {
1305 cmp = a->exp < b->exp ? -1 : 1;
1307 cmp = frac_cmp(a, b);
1315 if (unlikely(ab_mask & float_cmask_anynan)) {
1316 if (!is_quiet || (ab_mask & float_cmask_snan)) {
1317 float_raise(float_flag_invalid, s);
1319 return float_relation_unordered;
1322 if (ab_mask & float_cmask_zero) {
1323 if (ab_mask == float_cmask_zero) {
1324 return float_relation_equal;
1325 } else if (a->cls == float_class_zero) {
1332 if (ab_mask == float_cmask_inf) {
1333 if (a->sign == b->sign) {
1334 return float_relation_equal;
1336 } else if (b->cls == float_class_inf) {
1339 g_assert(a->cls == float_class_inf);
1343 return a->sign ? float_relation_less : float_relation_greater;
1345 return b->sign ? float_relation_greater : float_relation_less;
1349 * Multiply A by 2 raised to the power N.
1351 static void partsN(scalbn)(FloatPartsN *a, int n, float_status *s)
1354 case float_class_snan:
1355 case float_class_qnan:
1356 parts_return_nan(a, s);
1358 case float_class_zero:
1359 case float_class_inf:
1361 case float_class_normal:
1362 a->exp += MIN(MAX(n, -0x10000), 0x10000);
1365 g_assert_not_reached();
1372 static void partsN(log2)(FloatPartsN *a, float_status *s, const FloatFmt *fmt)
1374 uint64_t a0, a1, r, t, ign;
1376 int i, n, a_exp, f_exp;
1378 if (unlikely(a->cls != float_class_normal)) {
1380 case float_class_snan:
1381 case float_class_qnan:
1382 parts_return_nan(a, s);
1384 case float_class_zero:
1385 /* log2(0) = -inf */
1386 a->cls = float_class_inf;
1389 case float_class_inf:
1390 if (unlikely(a->sign)) {
1397 g_assert_not_reached();
1399 if (unlikely(a->sign)) {
1403 /* TODO: This algorithm looses bits too quickly for float128. */
1410 t = DECOMPOSED_IMPLICIT_BIT;
1414 n = fmt->frac_size + 2;
1415 if (unlikely(a_exp == -1)) {
1417 * When a_exp == -1, we're computing the log2 of a value [0.5,1.0).
1418 * When the value is very close to 1.0, there are lots of 1's in
1419 * the msb parts of the fraction. At the end, when we subtract
1420 * this value from -1.0, we can see a catastrophic loss of precision,
1421 * as 0x800..000 - 0x7ff..ffx becomes 0x000..00y, leaving only the
1422 * bits of y in the final result. To minimize this, compute as many
1424 * ??? This case needs another algorithm to avoid this.
1426 n = fmt->frac_size * 2 + 2;
1427 /* Don't compute a value overlapping the sticky bit */
1431 for (i = 0; i < n; i++) {
1433 mul128To256(a0, a1, a0, a1, &a0, &a1, &ign, &ign);
1434 } else if (a0 & 0xffffffffull) {
1435 mul64To128(a0, a0, &a0, &a1);
1436 } else if (a0 & ~DECOMPOSED_IMPLICIT_BIT) {
1443 if (a0 & DECOMPOSED_IMPLICIT_BIT) {
1444 if (unlikely(a_exp == 0 && r == 0)) {
1446 * When a_exp == 0, we're computing the log2 of a value
1447 * [1.0,2.0). When the value is very close to 1.0, there
1448 * are lots of 0's in the msb parts of the fraction.
1449 * We need to compute more digits to produce a correct
1450 * result -- restart at the top of the fraction.
1451 * ??? This is likely to lose precision quickly, as for
1452 * float128; we may need another method.
1455 t = r = DECOMPOSED_IMPLICIT_BIT;
1461 add128(a0, a1, a0, a1, &a0, &a1);
1466 /* Set sticky for inexact. */
1467 r |= (a1 || a0 & ~DECOMPOSED_IMPLICIT_BIT);
1470 parts_sint_to_float(a, a_exp, 0, s);
1475 memset(&f, 0, sizeof(f));
1476 f.cls = float_class_normal;
1478 f.exp = f_exp - frac_normalize(&f);
1481 parts_sub_normal(a, &f);
1482 } else if (a_exp > 0) {
1483 parts_add_normal(a, &f);
1490 float_raise(float_flag_invalid, s);
1491 parts_default_nan(a, s);