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;
218 if (s->rebias_overflow) {
219 exp -= fmt->exp_re_bias;
220 } else if (overflow_norm) {
221 flags |= float_flag_inexact;
224 p->frac_lo &= ~round_mask;
226 flags |= float_flag_inexact;
227 p->cls = float_class_inf;
232 frac_shr(p, frac_shift);
233 } else if (unlikely(s->rebias_underflow)) {
234 flags |= float_flag_underflow;
235 exp += fmt->exp_re_bias;
236 if (p->frac_lo & round_mask) {
237 flags |= float_flag_inexact;
238 if (frac_addi(p, p, inc)) {
240 p->frac_hi |= DECOMPOSED_IMPLICIT_BIT;
243 p->frac_lo &= ~round_mask;
245 frac_shr(p, frac_shift);
246 } else if (s->flush_to_zero) {
247 flags |= float_flag_output_denormal;
248 p->cls = float_class_zero;
252 bool is_tiny = s->tininess_before_rounding || exp < 0;
256 is_tiny = !frac_addi(&discard, p, inc);
259 frac_shrjam(p, 1 - exp);
261 if (p->frac_lo & round_mask) {
262 /* Need to recompute round-to-even/round-to-odd. */
263 switch (s->float_rounding_mode) {
264 case float_round_nearest_even:
265 if (N > 64 && frac_lsb == 0) {
266 inc = ((p->frac_hi & 1) ||
267 (p->frac_lo & round_mask) != frac_lsbm1
270 inc = ((p->frac_lo & roundeven_mask) != frac_lsbm1
274 case float_round_to_odd:
275 case float_round_to_odd_inf:
276 if (N > 64 && frac_lsb == 0) {
277 inc = p->frac_hi & 1 ? 0 : round_mask;
279 inc = p->frac_lo & frac_lsb ? 0 : round_mask;
285 flags |= float_flag_inexact;
286 frac_addi(p, p, inc);
287 p->frac_lo &= ~round_mask;
290 exp = (p->frac_hi & DECOMPOSED_IMPLICIT_BIT) != 0;
291 frac_shr(p, frac_shift);
293 if (is_tiny && (flags & float_flag_inexact)) {
294 flags |= float_flag_underflow;
296 if (exp == 0 && frac_eqz(p)) {
297 p->cls = float_class_zero;
301 float_raise(flags, s);
304 static void partsN(uncanon)(FloatPartsN *p, float_status *s,
307 if (likely(p->cls == float_class_normal)) {
308 parts_uncanon_normal(p, s, fmt);
311 case float_class_zero:
315 case float_class_inf:
316 g_assert(!fmt->arm_althp);
317 p->exp = fmt->exp_max;
320 case float_class_qnan:
321 case float_class_snan:
322 g_assert(!fmt->arm_althp);
323 p->exp = fmt->exp_max;
324 frac_shr(p, fmt->frac_shift);
329 g_assert_not_reached();
334 * Returns the result of adding or subtracting the values of the
335 * floating-point values `a' and `b'. The operation is performed
336 * according to the IEC/IEEE Standard for Binary Floating-Point
339 static FloatPartsN *partsN(addsub)(FloatPartsN *a, FloatPartsN *b,
340 float_status *s, bool subtract)
342 bool b_sign = b->sign ^ subtract;
343 int ab_mask = float_cmask(a->cls) | float_cmask(b->cls);
345 if (a->sign != b_sign) {
347 if (likely(ab_mask == float_cmask_normal)) {
348 if (parts_sub_normal(a, b)) {
351 /* Subtract was exact, fall through to set sign. */
352 ab_mask = float_cmask_zero;
355 if (ab_mask == float_cmask_zero) {
356 a->sign = s->float_rounding_mode == float_round_down;
360 if (unlikely(ab_mask & float_cmask_anynan)) {
364 if (ab_mask & float_cmask_inf) {
365 if (a->cls != float_class_inf) {
369 if (b->cls != float_class_inf) {
374 float_raise(float_flag_invalid | float_flag_invalid_isi, s);
375 parts_default_nan(a, s);
380 if (likely(ab_mask == float_cmask_normal)) {
381 parts_add_normal(a, b);
385 if (ab_mask == float_cmask_zero) {
389 if (unlikely(ab_mask & float_cmask_anynan)) {
393 if (ab_mask & float_cmask_inf) {
394 a->cls = float_class_inf;
399 if (b->cls == float_class_zero) {
400 g_assert(a->cls == float_class_normal);
404 g_assert(a->cls == float_class_zero);
405 g_assert(b->cls == float_class_normal);
411 return parts_pick_nan(a, b, s);
415 * Returns the result of multiplying the floating-point values `a' and
416 * `b'. The operation is performed according to the IEC/IEEE Standard
417 * for Binary Floating-Point Arithmetic.
419 static FloatPartsN *partsN(mul)(FloatPartsN *a, FloatPartsN *b,
422 int ab_mask = float_cmask(a->cls) | float_cmask(b->cls);
423 bool sign = a->sign ^ b->sign;
425 if (likely(ab_mask == float_cmask_normal)) {
428 frac_mulw(&tmp, a, b);
429 frac_truncjam(a, &tmp);
431 a->exp += b->exp + 1;
432 if (!(a->frac_hi & DECOMPOSED_IMPLICIT_BIT)) {
441 /* Inf * Zero == NaN */
442 if (unlikely(ab_mask == float_cmask_infzero)) {
443 float_raise(float_flag_invalid | float_flag_invalid_imz, s);
444 parts_default_nan(a, s);
448 if (unlikely(ab_mask & float_cmask_anynan)) {
449 return parts_pick_nan(a, b, s);
452 /* Multiply by 0 or Inf */
453 if (ab_mask & float_cmask_inf) {
454 a->cls = float_class_inf;
459 g_assert(ab_mask & float_cmask_zero);
460 a->cls = float_class_zero;
466 * Returns the result of multiplying the floating-point values `a' and
467 * `b' then adding 'c', with no intermediate rounding step after the
468 * multiplication. The operation is performed according to the
469 * IEC/IEEE Standard for Binary Floating-Point Arithmetic 754-2008.
470 * The flags argument allows the caller to select negation of the
471 * addend, the intermediate product, or the final result. (The
472 * difference between this and having the caller do a separate
473 * negation is that negating externally will flip the sign bit on NaNs.)
475 * Requires A and C extracted into a double-sized structure to provide the
476 * extra space for the widening multiply.
478 static FloatPartsN *partsN(muladd)(FloatPartsN *a, FloatPartsN *b,
479 FloatPartsN *c, int flags, float_status *s)
481 int ab_mask, abc_mask;
482 FloatPartsW p_widen, c_widen;
484 ab_mask = float_cmask(a->cls) | float_cmask(b->cls);
485 abc_mask = float_cmask(c->cls) | ab_mask;
488 * It is implementation-defined whether the cases of (0,inf,qnan)
489 * and (inf,0,qnan) raise InvalidOperation or not (and what QNaN
490 * they return if they do), so we have to hand this information
491 * off to the target-specific pick-a-NaN routine.
493 if (unlikely(abc_mask & float_cmask_anynan)) {
494 return parts_pick_nan_muladd(a, b, c, s, ab_mask, abc_mask);
497 if (flags & float_muladd_negate_c) {
501 /* Compute the sign of the product into A. */
503 if (flags & float_muladd_negate_product) {
507 if (unlikely(ab_mask != float_cmask_normal)) {
508 if (unlikely(ab_mask == float_cmask_infzero)) {
509 float_raise(float_flag_invalid | float_flag_invalid_imz, s);
513 if (ab_mask & float_cmask_inf) {
514 if (c->cls == float_class_inf && a->sign != c->sign) {
515 float_raise(float_flag_invalid | float_flag_invalid_isi, s);
521 g_assert(ab_mask & float_cmask_zero);
522 if (c->cls == float_class_normal) {
526 if (c->cls == float_class_zero) {
527 if (a->sign != c->sign) {
528 goto return_sub_zero;
532 g_assert(c->cls == float_class_inf);
535 if (unlikely(c->cls == float_class_inf)) {
540 /* Perform the multiplication step. */
541 p_widen.sign = a->sign;
542 p_widen.exp = a->exp + b->exp + 1;
543 frac_mulw(&p_widen, a, b);
544 if (!(p_widen.frac_hi & DECOMPOSED_IMPLICIT_BIT)) {
545 frac_add(&p_widen, &p_widen, &p_widen);
549 /* Perform the addition step. */
550 if (c->cls != float_class_zero) {
551 /* Zero-extend C to less significant bits. */
552 frac_widen(&c_widen, c);
553 c_widen.exp = c->exp;
555 if (a->sign == c->sign) {
556 parts_add_normal(&p_widen, &c_widen);
557 } else if (!parts_sub_normal(&p_widen, &c_widen)) {
558 goto return_sub_zero;
562 /* Narrow with sticky bit, for proper rounding later. */
563 frac_truncjam(a, &p_widen);
564 a->sign = p_widen.sign;
565 a->exp = p_widen.exp;
568 if (flags & float_muladd_halve_result) {
572 if (flags & float_muladd_negate_result) {
578 a->sign = s->float_rounding_mode == float_round_down;
580 a->cls = float_class_zero;
584 a->cls = float_class_inf;
588 parts_default_nan(a, s);
593 * Returns the result of dividing the floating-point value `a' by the
594 * corresponding value `b'. The operation is performed according to
595 * the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
597 static FloatPartsN *partsN(div)(FloatPartsN *a, FloatPartsN *b,
600 int ab_mask = float_cmask(a->cls) | float_cmask(b->cls);
601 bool sign = a->sign ^ b->sign;
603 if (likely(ab_mask == float_cmask_normal)) {
605 a->exp -= b->exp + frac_div(a, b);
609 /* 0/0 or Inf/Inf => NaN */
610 if (unlikely(ab_mask == float_cmask_zero)) {
611 float_raise(float_flag_invalid | float_flag_invalid_zdz, s);
614 if (unlikely(ab_mask == float_cmask_inf)) {
615 float_raise(float_flag_invalid | float_flag_invalid_idi, s);
619 /* All the NaN cases */
620 if (unlikely(ab_mask & float_cmask_anynan)) {
621 return parts_pick_nan(a, b, s);
627 if (a->cls == float_class_inf) {
632 if (a->cls == float_class_zero) {
637 if (b->cls == float_class_inf) {
638 a->cls = float_class_zero;
643 g_assert(b->cls == float_class_zero);
644 float_raise(float_flag_divbyzero, s);
645 a->cls = float_class_inf;
649 parts_default_nan(a, s);
654 * Floating point remainder, per IEC/IEEE, or modulus.
656 static FloatPartsN *partsN(modrem)(FloatPartsN *a, FloatPartsN *b,
657 uint64_t *mod_quot, float_status *s)
659 int ab_mask = float_cmask(a->cls) | float_cmask(b->cls);
661 if (likely(ab_mask == float_cmask_normal)) {
662 frac_modrem(a, b, mod_quot);
670 /* All the NaN cases */
671 if (unlikely(ab_mask & float_cmask_anynan)) {
672 return parts_pick_nan(a, b, s);
676 if (a->cls == float_class_inf || b->cls == float_class_zero) {
677 float_raise(float_flag_invalid, s);
678 parts_default_nan(a, s);
683 g_assert(b->cls == float_class_inf || a->cls == float_class_zero);
690 * The base algorithm is lifted from
691 * https://git.musl-libc.org/cgit/musl/tree/src/math/sqrtf.c
692 * https://git.musl-libc.org/cgit/musl/tree/src/math/sqrt.c
693 * https://git.musl-libc.org/cgit/musl/tree/src/math/sqrtl.c
694 * and is thus MIT licenced.
696 static void partsN(sqrt)(FloatPartsN *a, float_status *status,
699 const uint32_t three32 = 3u << 30;
700 const uint64_t three64 = 3ull << 62;
701 uint32_t d32, m32, r32, s32, u32; /* 32-bit computation */
702 uint64_t d64, m64, r64, s64, u64; /* 64-bit computation */
703 uint64_t dh, dl, rh, rl, sh, sl, uh, ul; /* 128-bit computation */
704 uint64_t d0h, d0l, d1h, d1l, d2h, d2l;
709 if (unlikely(a->cls != float_class_normal)) {
711 case float_class_snan:
712 case float_class_qnan:
713 parts_return_nan(a, status);
715 case float_class_zero:
717 case float_class_inf:
718 if (unlikely(a->sign)) {
723 g_assert_not_reached();
727 if (unlikely(a->sign)) {
732 * Argument reduction.
733 * x = 4^e frac; with integer e, and frac in [1, 4)
734 * m = frac fixed point at bit 62, since we're in base 4.
735 * If base-2 exponent is odd, exchange that for multiply by 2,
736 * which results in no shift.
738 exp_odd = a->exp & 1;
739 index = extract64(a->frac_hi, 57, 6) | (!exp_odd << 6);
745 * Approximate r ~= 1/sqrt(m) and s ~= sqrt(m) when m in [1, 4).
748 * 7-bit lookup table (1-bit exponent and 6-bit significand).
750 * The relative error (e = r0*sqrt(m)-1) of a linear estimate
751 * (r0 = a*m + b) is |e| < 0.085955 ~ 0x1.6p-4 at best;
752 * a table lookup is faster and needs one less iteration.
753 * The 7-bit table gives |e| < 0x1.fdp-9.
755 * A Newton-Raphson iteration for r is
761 * Fixed point representations:
762 * m, s, d, u, three are all 2.30; r is 0.32
767 r32 = rsqrt_tab[index] << 16;
768 /* |r*sqrt(m) - 1| < 0x1.FDp-9 */
770 s32 = ((uint64_t)m32 * r32) >> 32;
771 d32 = ((uint64_t)s32 * r32) >> 32;
775 /* float64 or smaller */
777 r32 = ((uint64_t)r32 * u32) >> 31;
778 /* |r*sqrt(m) - 1| < 0x1.7Bp-16 */
780 s32 = ((uint64_t)m32 * r32) >> 32;
781 d32 = ((uint64_t)s32 * r32) >> 32;
784 if (fmt->frac_size <= 23) {
785 /* float32 or smaller */
787 s32 = ((uint64_t)s32 * u32) >> 32; /* 3.29 */
788 s32 = (s32 - 1) >> 6; /* 9.23 */
789 /* s < sqrt(m) < s + 0x1.08p-23 */
791 /* compute nearest rounded result to 2.23 bits */
792 uint32_t d0 = (m32 << 16) - s32 * s32;
793 uint32_t d1 = s32 - d0;
794 uint32_t d2 = d1 + s32 + 1;
796 a->frac_hi = (uint64_t)s32 << (64 - 25);
798 /* increment or decrement for inexact */
800 a->frac_hi += ((int32_t)(d1 ^ d2) < 0 ? -1 : 1);
807 r64 = (uint64_t)r32 * u32 * 2;
808 /* |r*sqrt(m) - 1| < 0x1.37-p29; convert to 64-bit arithmetic */
809 mul64To128(m64, r64, &s64, &discard);
810 mul64To128(s64, r64, &d64, &discard);
813 mul64To128(s64, u64, &s64, &discard); /* 3.61 */
814 s64 = (s64 - 2) >> 9; /* 12.52 */
816 /* Compute nearest rounded result */
817 uint64_t d0 = (m64 << 42) - s64 * s64;
818 uint64_t d1 = s64 - d0;
819 uint64_t d2 = d1 + s64 + 1;
821 a->frac_hi = s64 << (64 - 54);
823 /* increment or decrement for inexact */
825 a->frac_hi += ((int64_t)(d1 ^ d2) < 0 ? -1 : 1);
830 r64 = (uint64_t)r32 * u32 * 2;
831 /* |r*sqrt(m) - 1| < 0x1.7Bp-16; convert to 64-bit arithmetic */
833 mul64To128(m64, r64, &s64, &discard);
834 mul64To128(s64, r64, &d64, &discard);
836 mul64To128(u64, r64, &r64, &discard);
838 /* |r*sqrt(m) - 1| < 0x1.a5p-31 */
840 mul64To128(m64, r64, &s64, &discard);
841 mul64To128(s64, r64, &d64, &discard);
843 mul64To128(u64, r64, &rh, &rl);
844 add128(rh, rl, rh, rl, &rh, &rl);
845 /* |r*sqrt(m) - 1| < 0x1.c001p-59; change to 128-bit arithmetic */
847 mul128To256(a->frac_hi, a->frac_lo, rh, rl, &sh, &sl, &discard, &discard);
848 mul128To256(sh, sl, rh, rl, &dh, &dl, &discard, &discard);
849 sub128(three64, 0, dh, dl, &uh, &ul);
850 mul128To256(uh, ul, sh, sl, &sh, &sl, &discard, &discard); /* 3.125 */
851 /* -0x1p-116 < s - sqrt(m) < 0x3.8001p-125 */
853 sub128(sh, sl, 0, 4, &sh, &sl);
854 shift128Right(sh, sl, 13, &sh, &sl); /* 16.112 */
855 /* s < sqrt(m) < s + 1ulp */
857 /* Compute nearest rounded result */
858 mul64To128(sl, sl, &d0h, &d0l);
860 sub128(a->frac_lo << 34, 0, d0h, d0l, &d0h, &d0l);
861 sub128(sh, sl, d0h, d0l, &d1h, &d1l);
862 add128(sh, sl, 0, 1, &d2h, &d2l);
863 add128(d2h, d2l, d1h, d1l, &d2h, &d2l);
864 add128(sh, sl, 0, d1h >> 63, &sh, &sl);
865 shift128Left(sh, sl, 128 - 114, &sh, &sl);
867 /* increment or decrement for inexact */
869 if ((int64_t)(d1h ^ d2h) < 0) {
870 sub128(sh, sl, 0, 1, &sh, &sl);
872 add128(sh, sl, 0, 1, &sh, &sl);
879 /* Convert back from base 4 to base 2. */
881 if (!(a->frac_hi & DECOMPOSED_IMPLICIT_BIT)) {
889 float_raise(float_flag_invalid | float_flag_invalid_sqrt, status);
890 parts_default_nan(a, status);
894 * Rounds the floating-point value `a' to an integer, and returns the
895 * result as a floating-point value. The operation is performed
896 * according to the IEC/IEEE Standard for Binary Floating-Point
899 * parts_round_to_int_normal is an internal helper function for
900 * normal numbers only, returning true for inexact but not directly
901 * raising float_flag_inexact.
903 static bool partsN(round_to_int_normal)(FloatPartsN *a, FloatRoundMode rmode,
904 int scale, int frac_size)
906 uint64_t frac_lsb, frac_lsbm1, rnd_even_mask, rnd_mask, inc;
909 scale = MIN(MAX(scale, -0x10000), 0x10000);
917 case float_round_nearest_even:
921 /* Shift left one, discarding DECOMPOSED_IMPLICIT_BIT */
922 frac_add(&tmp, a, a);
923 /* Anything remaining means frac > 0.5. */
924 one = !frac_eqz(&tmp);
927 case float_round_ties_away:
930 case float_round_to_zero:
936 case float_round_down:
939 case float_round_to_odd:
943 g_assert_not_reached();
949 a->frac_hi = DECOMPOSED_IMPLICIT_BIT;
951 a->cls = float_class_zero;
956 if (a->exp >= frac_size) {
961 if (N > 64 && a->exp < N - 64) {
963 * Rounding is not in the low word -- shift lsb to bit 2,
964 * which leaves room for sticky and rounding bit.
966 shift_adj = (N - 1) - (a->exp + 2);
967 frac_shrjam(a, shift_adj);
971 frac_lsb = DECOMPOSED_IMPLICIT_BIT >> (a->exp & 63);
974 frac_lsbm1 = frac_lsb >> 1;
975 rnd_mask = frac_lsb - 1;
976 rnd_even_mask = rnd_mask | frac_lsb;
978 if (!(a->frac_lo & rnd_mask)) {
979 /* Fractional bits already clear, undo the shift above. */
980 frac_shl(a, shift_adj);
985 case float_round_nearest_even:
986 inc = ((a->frac_lo & rnd_even_mask) != frac_lsbm1 ? frac_lsbm1 : 0);
988 case float_round_ties_away:
991 case float_round_to_zero:
995 inc = a->sign ? 0 : rnd_mask;
997 case float_round_down:
998 inc = a->sign ? rnd_mask : 0;
1000 case float_round_to_odd:
1001 inc = a->frac_lo & frac_lsb ? 0 : rnd_mask;
1004 g_assert_not_reached();
1007 if (shift_adj == 0) {
1008 if (frac_addi(a, a, inc)) {
1010 a->frac_hi |= DECOMPOSED_IMPLICIT_BIT;
1013 a->frac_lo &= ~rnd_mask;
1015 frac_addi(a, a, inc);
1016 a->frac_lo &= ~rnd_mask;
1017 /* Be careful shifting back, not to overflow */
1018 frac_shl(a, shift_adj - 1);
1019 if (a->frac_hi & DECOMPOSED_IMPLICIT_BIT) {
1028 static void partsN(round_to_int)(FloatPartsN *a, FloatRoundMode rmode,
1029 int scale, float_status *s,
1030 const FloatFmt *fmt)
1033 case float_class_qnan:
1034 case float_class_snan:
1035 parts_return_nan(a, s);
1037 case float_class_zero:
1038 case float_class_inf:
1040 case float_class_normal:
1041 if (parts_round_to_int_normal(a, rmode, scale, fmt->frac_size)) {
1042 float_raise(float_flag_inexact, s);
1046 g_assert_not_reached();
1051 * Returns the result of converting the floating-point value `a' to
1052 * the two's complement integer format. The conversion is performed
1053 * according to the IEC/IEEE Standard for Binary Floating-Point
1054 * Arithmetic---which means in particular that the conversion is
1055 * rounded according to the current rounding mode. If `a' is a NaN,
1056 * the largest positive integer is returned. Otherwise, if the
1057 * conversion overflows, the largest integer with the same sign as `a'
1060 static int64_t partsN(float_to_sint)(FloatPartsN *p, FloatRoundMode rmode,
1061 int scale, int64_t min, int64_t max,
1068 case float_class_snan:
1069 flags |= float_flag_invalid_snan;
1071 case float_class_qnan:
1072 flags |= float_flag_invalid;
1076 case float_class_inf:
1077 flags = float_flag_invalid | float_flag_invalid_cvti;
1078 r = p->sign ? min : max;
1081 case float_class_zero:
1084 case float_class_normal:
1085 /* TODO: N - 2 is frac_size for rounding; could use input fmt. */
1086 if (parts_round_to_int_normal(p, rmode, scale, N - 2)) {
1087 flags = float_flag_inexact;
1090 if (p->exp <= DECOMPOSED_BINARY_POINT) {
1091 r = p->frac_hi >> (DECOMPOSED_BINARY_POINT - p->exp);
1096 if (r <= -(uint64_t)min) {
1099 flags = float_flag_invalid | float_flag_invalid_cvti;
1102 } else if (r > max) {
1103 flags = float_flag_invalid | float_flag_invalid_cvti;
1109 g_assert_not_reached();
1112 float_raise(flags, s);
1117 * Returns the result of converting the floating-point value `a' to
1118 * the unsigned integer format. The conversion is performed according
1119 * to the IEC/IEEE Standard for Binary Floating-Point
1120 * Arithmetic---which means in particular that the conversion is
1121 * rounded according to the current rounding mode. If `a' is a NaN,
1122 * the largest unsigned integer is returned. Otherwise, if the
1123 * conversion overflows, the largest unsigned integer is returned. If
1124 * the 'a' is negative, the result is rounded and zero is returned;
1125 * values that do not round to zero will raise the inexact exception
1128 static uint64_t partsN(float_to_uint)(FloatPartsN *p, FloatRoundMode rmode,
1129 int scale, uint64_t max, float_status *s)
1135 case float_class_snan:
1136 flags |= float_flag_invalid_snan;
1138 case float_class_qnan:
1139 flags |= float_flag_invalid;
1143 case float_class_inf:
1144 flags = float_flag_invalid | float_flag_invalid_cvti;
1145 r = p->sign ? 0 : max;
1148 case float_class_zero:
1151 case float_class_normal:
1152 /* TODO: N - 2 is frac_size for rounding; could use input fmt. */
1153 if (parts_round_to_int_normal(p, rmode, scale, N - 2)) {
1154 flags = float_flag_inexact;
1155 if (p->cls == float_class_zero) {
1162 flags = float_flag_invalid | float_flag_invalid_cvti;
1164 } else if (p->exp > DECOMPOSED_BINARY_POINT) {
1165 flags = float_flag_invalid | float_flag_invalid_cvti;
1168 r = p->frac_hi >> (DECOMPOSED_BINARY_POINT - p->exp);
1170 flags = float_flag_invalid | float_flag_invalid_cvti;
1177 g_assert_not_reached();
1180 float_raise(flags, s);
1185 * Integer to float conversions
1187 * Returns the result of converting the two's complement integer `a'
1188 * to the floating-point format. The conversion is performed according
1189 * to the IEC/IEEE Standard for Binary Floating-Point Arithmetic.
1191 static void partsN(sint_to_float)(FloatPartsN *p, int64_t a,
1192 int scale, float_status *s)
1197 memset(p, 0, sizeof(*p));
1200 p->cls = float_class_zero;
1204 p->cls = float_class_normal;
1210 scale = MIN(MAX(scale, -0x10000), 0x10000);
1212 p->exp = DECOMPOSED_BINARY_POINT - shift + scale;
1213 p->frac_hi = f << shift;
1217 * Unsigned Integer to float conversions
1219 * Returns the result of converting the unsigned integer `a' to the
1220 * floating-point format. The conversion is performed according to the
1221 * IEC/IEEE Standard for Binary Floating-Point Arithmetic.
1223 static void partsN(uint_to_float)(FloatPartsN *p, uint64_t a,
1224 int scale, float_status *status)
1226 memset(p, 0, sizeof(*p));
1229 p->cls = float_class_zero;
1231 int shift = clz64(a);
1232 scale = MIN(MAX(scale, -0x10000), 0x10000);
1233 p->cls = float_class_normal;
1234 p->exp = DECOMPOSED_BINARY_POINT - shift + scale;
1235 p->frac_hi = a << shift;
1242 static FloatPartsN *partsN(minmax)(FloatPartsN *a, FloatPartsN *b,
1243 float_status *s, int flags)
1245 int ab_mask = float_cmask(a->cls) | float_cmask(b->cls);
1246 int a_exp, b_exp, cmp;
1248 if (unlikely(ab_mask & float_cmask_anynan)) {
1250 * For minNum/maxNum (IEEE 754-2008)
1251 * or minimumNumber/maximumNumber (IEEE 754-2019),
1252 * if one operand is a QNaN, and the other
1253 * operand is numerical, then return numerical argument.
1255 if ((flags & (minmax_isnum | minmax_isnumber))
1256 && !(ab_mask & float_cmask_snan)
1257 && (ab_mask & ~float_cmask_qnan)) {
1258 return is_nan(a->cls) ? b : a;
1262 * In IEEE 754-2019, minNum, maxNum, minNumMag and maxNumMag
1263 * are removed and replaced with minimum, minimumNumber, maximum
1264 * and maximumNumber.
1265 * minimumNumber/maximumNumber behavior for SNaN is changed to:
1266 * If both operands are NaNs, a QNaN is returned.
1267 * If either operand is a SNaN,
1268 * an invalid operation exception is signaled,
1269 * but unless both operands are NaNs,
1270 * the SNaN is otherwise ignored and not converted to a QNaN.
1272 if ((flags & minmax_isnumber)
1273 && (ab_mask & float_cmask_snan)
1274 && (ab_mask & ~float_cmask_anynan)) {
1275 float_raise(float_flag_invalid, s);
1276 return is_nan(a->cls) ? b : a;
1279 return parts_pick_nan(a, b, s);
1285 if (unlikely(ab_mask != float_cmask_normal)) {
1287 case float_class_normal:
1289 case float_class_inf:
1292 case float_class_zero:
1296 g_assert_not_reached();
1300 case float_class_normal:
1302 case float_class_inf:
1305 case float_class_zero:
1309 g_assert_not_reached();
1314 /* Compare magnitudes. */
1315 cmp = a_exp - b_exp;
1317 cmp = frac_cmp(a, b);
1321 * Take the sign into account.
1322 * For ismag, only do this if the magnitudes are equal.
1324 if (!(flags & minmax_ismag) || cmp == 0) {
1325 if (a->sign != b->sign) {
1326 /* For differing signs, the negative operand is less. */
1327 cmp = a->sign ? -1 : 1;
1328 } else if (a->sign) {
1329 /* For two negative operands, invert the magnitude comparison. */
1334 if (flags & minmax_ismin) {
1337 return cmp < 0 ? b : a;
1341 * Floating point compare
1343 static FloatRelation partsN(compare)(FloatPartsN *a, FloatPartsN *b,
1344 float_status *s, bool is_quiet)
1346 int ab_mask = float_cmask(a->cls) | float_cmask(b->cls);
1348 if (likely(ab_mask == float_cmask_normal)) {
1351 if (a->sign != b->sign) {
1354 if (a->exp == b->exp) {
1355 cmp = frac_cmp(a, b);
1356 } else if (a->exp < b->exp) {
1357 cmp = float_relation_less;
1359 cmp = float_relation_greater;
1367 if (unlikely(ab_mask & float_cmask_anynan)) {
1368 if (ab_mask & float_cmask_snan) {
1369 float_raise(float_flag_invalid | float_flag_invalid_snan, s);
1370 } else if (!is_quiet) {
1371 float_raise(float_flag_invalid, s);
1373 return float_relation_unordered;
1376 if (ab_mask & float_cmask_zero) {
1377 if (ab_mask == float_cmask_zero) {
1378 return float_relation_equal;
1379 } else if (a->cls == float_class_zero) {
1386 if (ab_mask == float_cmask_inf) {
1387 if (a->sign == b->sign) {
1388 return float_relation_equal;
1390 } else if (b->cls == float_class_inf) {
1393 g_assert(a->cls == float_class_inf);
1397 return a->sign ? float_relation_less : float_relation_greater;
1399 return b->sign ? float_relation_greater : float_relation_less;
1403 * Multiply A by 2 raised to the power N.
1405 static void partsN(scalbn)(FloatPartsN *a, int n, float_status *s)
1408 case float_class_snan:
1409 case float_class_qnan:
1410 parts_return_nan(a, s);
1412 case float_class_zero:
1413 case float_class_inf:
1415 case float_class_normal:
1416 a->exp += MIN(MAX(n, -0x10000), 0x10000);
1419 g_assert_not_reached();
1426 static void partsN(log2)(FloatPartsN *a, float_status *s, const FloatFmt *fmt)
1428 uint64_t a0, a1, r, t, ign;
1430 int i, n, a_exp, f_exp;
1432 if (unlikely(a->cls != float_class_normal)) {
1434 case float_class_snan:
1435 case float_class_qnan:
1436 parts_return_nan(a, s);
1438 case float_class_zero:
1439 /* log2(0) = -inf */
1440 a->cls = float_class_inf;
1443 case float_class_inf:
1444 if (unlikely(a->sign)) {
1451 g_assert_not_reached();
1453 if (unlikely(a->sign)) {
1457 /* TODO: This algorithm looses bits too quickly for float128. */
1464 t = DECOMPOSED_IMPLICIT_BIT;
1468 n = fmt->frac_size + 2;
1469 if (unlikely(a_exp == -1)) {
1471 * When a_exp == -1, we're computing the log2 of a value [0.5,1.0).
1472 * When the value is very close to 1.0, there are lots of 1's in
1473 * the msb parts of the fraction. At the end, when we subtract
1474 * this value from -1.0, we can see a catastrophic loss of precision,
1475 * as 0x800..000 - 0x7ff..ffx becomes 0x000..00y, leaving only the
1476 * bits of y in the final result. To minimize this, compute as many
1478 * ??? This case needs another algorithm to avoid this.
1480 n = fmt->frac_size * 2 + 2;
1481 /* Don't compute a value overlapping the sticky bit */
1485 for (i = 0; i < n; i++) {
1487 mul128To256(a0, a1, a0, a1, &a0, &a1, &ign, &ign);
1488 } else if (a0 & 0xffffffffull) {
1489 mul64To128(a0, a0, &a0, &a1);
1490 } else if (a0 & ~DECOMPOSED_IMPLICIT_BIT) {
1497 if (a0 & DECOMPOSED_IMPLICIT_BIT) {
1498 if (unlikely(a_exp == 0 && r == 0)) {
1500 * When a_exp == 0, we're computing the log2 of a value
1501 * [1.0,2.0). When the value is very close to 1.0, there
1502 * are lots of 0's in the msb parts of the fraction.
1503 * We need to compute more digits to produce a correct
1504 * result -- restart at the top of the fraction.
1505 * ??? This is likely to lose precision quickly, as for
1506 * float128; we may need another method.
1509 t = r = DECOMPOSED_IMPLICIT_BIT;
1515 add128(a0, a1, a0, a1, &a0, &a1);
1520 /* Set sticky for inexact. */
1521 r |= (a1 || a0 & ~DECOMPOSED_IMPLICIT_BIT);
1524 parts_sint_to_float(a, a_exp, 0, s);
1529 memset(&f, 0, sizeof(f));
1530 f.cls = float_class_normal;
1532 f.exp = f_exp - frac_normalize(&f);
1535 parts_sub_normal(a, &f);
1536 } else if (a_exp > 0) {
1537 parts_add_normal(a, &f);
1544 float_raise(float_flag_invalid, s);
1545 parts_default_nan(a, s);