https://github.com/lntue updated https://github.com/llvm/llvm-project/pull/243290
>From 69f5e684dd7fedb203a6754b5b9e14167f25240b Mon Sep 17 00:00:00 2001 From: Tue Ly <[email protected]> Date: Mon, 16 Nov 2026 23:21:04 +0000 Subject: [PATCH] [libc][math] Use multiply_sub and sub_multiply across math headers. Replace negated multiply_add calls (multiply_add(a, b, -c), multiply_add(-b, c, a), and multiply_add(b, -c, a)) across FPUtil and math implementation headers with multiply_sub and sub_multiply. --- libc/src/__support/FPUtil/double_double.h | 12 ++++++------ libc/src/__support/math/acos.h | 19 +++++++++---------- libc/src/__support/math/acosbf16.h | 4 ++-- libc/src/__support/math/acosf.h | 8 ++++---- libc/src/__support/math/acosf16.h | 6 +++--- libc/src/__support/math/acoshbf16.h | 2 +- libc/src/__support/math/acoshf.h | 4 ++-- libc/src/__support/math/acoshf16.h | 2 +- libc/src/__support/math/acoshf_utils.h | 2 +- libc/src/__support/math/acospif.h | 2 +- libc/src/__support/math/acospif16.h | 6 +++--- libc/src/__support/math/asin.h | 15 +++++++-------- libc/src/__support/math/asin_utils.h | 2 +- libc/src/__support/math/asinbf16.h | 4 ++-- libc/src/__support/math/asinf.h | 2 +- libc/src/__support/math/asinf16.h | 4 ++-- libc/src/__support/math/asinpi.h | 13 ++++++------- libc/src/__support/math/asinpif.h | 2 +- libc/src/__support/math/asinpif16.h | 4 ++-- libc/src/__support/math/atan.h | 4 ++-- libc/src/__support/math/atan2f.h | 12 ++++++------ libc/src/__support/math/atan2f128.h | 2 +- libc/src/__support/math/atan2f16.h | 2 +- libc/src/__support/math/atan2f_float.h | 2 +- libc/src/__support/math/atan_utils.h | 10 +++++----- libc/src/__support/math/atanbf16.h | 2 +- libc/src/__support/math/atanf.h | 8 ++++---- libc/src/__support/math/atanf16.h | 2 +- libc/src/__support/math/atanf_float.h | 6 +++--- libc/src/__support/math/atanpif16.h | 2 +- libc/src/__support/math/cbrt.h | 4 ++-- libc/src/__support/math/cosf16.h | 2 +- libc/src/__support/math/cosf_double_eval.h | 8 ++++---- libc/src/__support/math/cospif.h | 8 ++++---- libc/src/__support/math/cospif16.h | 2 +- libc/src/__support/math/exp10f16_utils.h | 2 +- libc/src/__support/math/exp10f_float_eval.h | 6 +++--- libc/src/__support/math/exp10m1f.h | 2 +- libc/src/__support/math/exp10m1f16.h | 2 +- libc/src/__support/math/exp2_double_eval.h | 2 +- libc/src/__support/math/exp2bf16.h | 2 +- libc/src/__support/math/exp2f_double_eval.h | 2 +- libc/src/__support/math/exp2f_float_eval.h | 2 +- libc/src/__support/math/exp2m1f.h | 4 ++-- libc/src/__support/math/exp2m1f16.h | 2 +- libc/src/__support/math/expbf16.h | 2 +- libc/src/__support/math/expf16_utils.h | 2 +- libc/src/__support/math/expf_double_eval.h | 2 +- libc/src/__support/math/expf_float_eval.h | 8 ++++---- libc/src/__support/math/expm1f.h | 4 ++-- libc/src/__support/math/expm1f16.h | 2 +- libc/src/__support/math/expxf16_utils.h | 4 ++-- libc/src/__support/math/hypot.h | 2 +- libc/src/__support/math/hypotf.h | 4 ++-- libc/src/__support/math/hypotf16.h | 2 +- libc/src/__support/math/lgammaf.h | 12 ++++++------ libc/src/__support/math/log.h | 2 +- libc/src/__support/math/log10.h | 2 +- libc/src/__support/math/log10f16.h | 2 +- libc/src/__support/math/log10f_double_eval.h | 6 +++--- libc/src/__support/math/log10p1bf16.h | 2 +- libc/src/__support/math/log10p1f16.h | 2 +- libc/src/__support/math/log1p.h | 2 +- libc/src/__support/math/log2.h | 2 +- libc/src/__support/math/log2f16.h | 2 +- libc/src/__support/math/log2f_double_eval.h | 4 ++-- libc/src/__support/math/log2p1f16.h | 2 +- libc/src/__support/math/logf16.h | 2 +- libc/src/__support/math/logf_double_eval.h | 4 ++-- libc/src/__support/math/pow.h | 2 +- libc/src/__support/math/pow_fast.h | 2 +- libc/src/__support/math/powf_double_eval.h | 8 ++++---- libc/src/__support/math/powf_float_eval.h | 4 ++-- libc/src/__support/math/rsqrtf16.h | 2 +- libc/src/__support/math/sin.h | 6 +++--- libc/src/__support/math/sincos.h | 6 +++--- libc/src/__support/math/sincos_eval.h | 14 +++++++------- libc/src/__support/math/sincosf.h | 14 +++++++------- libc/src/__support/math/sincosf16_utils.h | 2 +- libc/src/__support/math/sincosf_float_eval.h | 6 +++--- libc/src/__support/math/sincosf_utils.h | 2 +- libc/src/__support/math/sinf_double_eval.h | 4 ++-- libc/src/__support/math/sinhcosh_utils.h | 2 +- libc/src/__support/math/sinhcoshbf16_utils.h | 4 ++-- libc/src/__support/math/tanbf16.h | 4 ++-- libc/src/__support/math/tanf16.h | 2 +- libc/src/__support/math/tanf_double_eval.h | 2 +- libc/src/__support/math/tanf_float_eval.h | 2 +- libc/src/__support/math/tanh_double_eval.h | 10 +++++----- libc/src/__support/math/tanhf.h | 2 +- libc/src/__support/math/tanhf16.h | 2 +- libc/src/__support/math/tanpif.h | 6 +++--- libc/src/__support/math/tanpif16.h | 4 ++-- 93 files changed, 201 insertions(+), 204 deletions(-) diff --git a/libc/src/__support/FPUtil/double_double.h b/libc/src/__support/FPUtil/double_double.h index 3c1c052352a0a9..c74d5a9a038394 100644 --- a/libc/src/__support/FPUtil/double_double.h +++ b/libc/src/__support/FPUtil/double_double.h @@ -208,11 +208,11 @@ LIBC_INLINE LIBC_CONSTEXPR NumberPair<T> exact_mult(T a, T b) { if constexpr (TargetHasFmaInstruction<T>::VALUE) { r.hi = a * b; - // Prevent `-r.hi` below from being rewritten into `a * (-b)` when `a * b` - // is duplicated (e.g. by SLP vectorization), which breaks directed - // rounding modes (`RU(a * (-b)) != -RU(a * b)`). + // Prevent `-r.hi` inside `multiply_sub` from being rewritten into + // `a * (-b)` when `a * b` is duplicated (e.g. by SLP vectorization), which + // breaks directed rounding modes (`RU(a * (-b)) != -RU(a * b)`). LIBC_DO_NOT_OPTIMIZE_ROUNDING(r.hi); - r.lo = fputil::multiply_add(a, b, -r.hi); + r.lo = fputil::multiply_sub(a, b, r.hi); } else { // Dekker's Product. NumberPair<T> as = split(a); @@ -289,8 +289,8 @@ LIBC_INLINE NumberPair<T> div(const NumberPair<T> &a, const NumberPair<T> &b) { LIBC_DO_NOT_OPTIMIZE_ROUNDING(r.hi); #ifdef LIBC_TARGET_CPU_HAS_FMA - T e_hi = fputil::multiply_add(b.hi, -r.hi, a.hi); - T e_lo = fputil::multiply_add(b.lo, -r.hi, a.lo); + T e_hi = fputil::sub_multiply(a.hi, b.hi, r.hi); + T e_lo = fputil::sub_multiply(a.lo, b.lo, r.hi); #else NumberPair<T> b_hi_r_hi = fputil::exact_mult(b.hi, -r.hi); NumberPair<T> b_lo_r_hi = fputil::exact_mult(b.lo, -r.hi); diff --git a/libc/src/__support/math/acos.h b/libc/src/__support/math/acos.h index dbcee8f0df6f15..481d4d9e8768c1 100644 --- a/libc/src/__support/math/acos.h +++ b/libc/src/__support/math/acos.h @@ -52,7 +52,7 @@ LIBC_INLINE double acos(double x) { // = pi/2 - x - x^3 * P1(x^2) double xsq = x * x; return PI_OVER_TWO.hi + - fputil::multiply_add(-x * xsq, asin_eval(xsq), PI_OVER_TWO.lo - x); + fputil::sub_multiply(PI_OVER_TWO.lo - x, x * xsq, asin_eval(xsq)); #else unsigned idx = 0; DoubleDouble x_sq = fputil::exact_mult(x, x); @@ -65,10 +65,10 @@ LIBC_INLINE double acos(double x) { // acos(x) = pi/2 - asin(x) // ~ pi/2 - x * p // = pi/2 - x * (p.hi + p.lo) - double r_hi = fputil::multiply_add(-x, p.hi, PI_OVER_TWO.hi); + double r_hi = fputil::sub_multiply(PI_OVER_TWO.hi, x, p.hi); // Use Dekker's 2SUM algorithm to compute the lower part. double r_lo = ((PI_OVER_TWO.hi - r_hi) - r0.hi) - r0.lo; - r_lo = fputil::multiply_add(-x, p.lo, r_lo + PI_OVER_TWO.lo); + r_lo = fputil::sub_multiply(r_lo + PI_OVER_TWO.lo, x, p.lo); // Ziv's accuracy test. @@ -91,7 +91,7 @@ LIBC_INLINE double acos(double x) { #ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE // u = x^2 - idx/64 DFloat128 u_hi( - fputil::multiply_add(static_cast<double>(idx), -0x1.0p-6, x_sq.hi)); + fputil::sub_multiply(x_sq.hi, static_cast<double>(idx), 0x1.0p-6)); DFloat128 u = fputil::quick_add(u_hi, DFloat128(x_sq.lo)); #else DFloat128 x_sq_f128 = fputil::quick_mul(x_f128, x_f128); @@ -121,7 +121,7 @@ LIBC_INLINE double acos(double x) { if (x_abs == 1.0) { // x = 1, acos(x) = 0, // x = -1, acos(x) = pi - return x == 1.0 ? 0.0 : fputil::multiply_add(-x_sign, PI.hi, PI.lo); + return x == 1.0 ? 0.0 : fputil::sub_multiply(PI.lo, x_sign, PI.hi); } // |x| > 1, return NaN. if (xbits.is_quiet_nan()) @@ -164,7 +164,7 @@ LIBC_INLINE double acos(double x) { // where u = (1 - |x|)/2. // u = (1 - |x|)/2 - double u = fputil::multiply_add(x_abs, -0.5, 0.5); + double u = fputil::sub_multiply(0.5, x_abs, 0.5); // v_hi + v_lo ~ sqrt(u). // Let: // h = u - v_hi^2 = (sqrt(u) - v_hi) * (sqrt(u) + v_hi) @@ -186,7 +186,7 @@ LIBC_INLINE double acos(double x) { #else #ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE - double h = fputil::multiply_add(v_hi, -v_hi, u); + double h = fputil::sub_multiply(u, v_hi, v_hi); #else DoubleDouble v_hi_sq = fputil::exact_mult(v_hi, v_hi); double h = (u - v_hi_sq.hi) - v_hi_sq.lo; @@ -251,7 +251,7 @@ LIBC_INLINE double acos(double x) { // Get the rounding error of vl = 2 * v_lo ~ h / vh // Get full product of vh * vl #ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE - double vl_lo = fputil::multiply_add(-v_hi, vl, h) / v_hi; + double vl_lo = fputil::sub_multiply(h, v_hi, vl) / v_hi; #else DoubleDouble vh_vl = fputil::exact_mult(v_hi, vl); double vl_lo = ((h - vh_vl.hi) - vh_vl.lo) / v_hi; @@ -267,8 +267,7 @@ LIBC_INLINE double acos(double x) { // Perform computations in DFloat128: // acos(x) = (v_hi + v_lo + vll) * P(u) , when 0.5 <= x < 1, // = pi - (v_hi + v_lo + vll) * P(u) , when -1 < x <= -0.5. - DFloat128 y_f128( - fputil::multiply_add(static_cast<double>(idx), -0x1.0p-6, u)); + DFloat128 y_f128(fputil::sub_multiply(u, static_cast<double>(idx), 0x1.0p-6)); DFloat128 p_f128 = asin_eval(y_f128, idx); DFloat128 r_f128 = fputil::quick_mul(m_v, p_f128); diff --git a/libc/src/__support/math/acosbf16.h b/libc/src/__support/math/acosbf16.h index e0fb861accb475..4f6b73f20637cc 100644 --- a/libc/src/__support/math/acosbf16.h +++ b/libc/src/__support/math/acosbf16.h @@ -67,12 +67,12 @@ LIBC_INLINE bfloat16 acosbf16(bfloat16 x) { // using reduction for acos: // acos(|x|) = 2*asin(sqrt((1 - |x|)/2)), // and acos(x) = acos(|x|) for x >= 0, pi - acos(|x|) for x < 0 - float t = fputil::multiply_add<float>(xf_abs, -0.5f, 0.5f); + float t = fputil::sub_multiply<float>(0.5f, xf_abs, 0.5f); float t_sqrt = fputil::sqrt<float>(t); float asin_sqrt_t = t_sqrt * asin_internal::asin_eval_float(t); return fputil::cast<bfloat16>( - (sign) ? fputil::multiply_add(asin_sqrt_t, -2.0f, PI) + (sign) ? fputil::sub_multiply(PI, asin_sqrt_t, 2.0f) : 2.0f * asin_sqrt_t); } // case 3: NaN or |x| > 1 diff --git a/libc/src/__support/math/acosf.h b/libc/src/__support/math/acosf.h index 2f19ed4d99b292..6a047b0e8bda37 100644 --- a/libc/src/__support/math/acosf.h +++ b/libc/src/__support/math/acosf.h @@ -74,8 +74,8 @@ LIBC_INLINE float acosf(float x) { #endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS double xd = static_cast<double>(x); - return static_cast<float>(fputil::multiply_add( - -0x1.5555555555555p-3 * xd, xd * xd, M_MATH_PI_2 - xd)); + return static_cast<float>(fputil::sub_multiply( + M_MATH_PI_2 - xd, 0x1.5555555555555p-3 * xd, xd * xd)); } // For |x| <= 0.5, we approximate acosf(x) by: @@ -88,7 +88,7 @@ LIBC_INLINE float acosf(float x) { double xsq = xd * xd; double x3 = xd * xsq; double r = asin_eval(xsq); - return static_cast<float>(fputil::multiply_add(-x3, r, M_MATH_PI_2 - xd)); + return static_cast<float>(fputil::sub_multiply(M_MATH_PI_2 - xd, x3, r)); } // |x| >= 1, return 0, 2pi, or NaNs. @@ -136,7 +136,7 @@ LIBC_INLINE float acosf(float x) { xbits.set_sign(Sign::POS); double xd = static_cast<double>(xbits.get_val()); - double u = fputil::multiply_add(-0.5, xd, 0.5); + double u = fputil::sub_multiply(0.5, 0.5, xd); double cv = 2 * fputil::sqrt<double>(u); double r3 = asin_eval(u); diff --git a/libc/src/__support/math/acosf16.h b/libc/src/__support/math/acosf16.h index b7bbbd75d06afa..70a0bab9b8fbac 100644 --- a/libc/src/__support/math/acosf16.h +++ b/libc/src/__support/math/acosf16.h @@ -98,7 +98,7 @@ LIBC_INLINE constexpr float16 acosf16(float16 x) { float interm = fputil::polyeval(xsq, 0x1.000002p0f, 0x1.554c2ap-3f, 0x1.3541ccp-4f, 0x1.43b2d6p-5f, 0x1.a0d73ep-5f); - return fputil::cast<float16>(fputil::multiply_add(-xf, interm, PI_OVER_2)); + return fputil::cast<float16>(fputil::sub_multiply(PI_OVER_2, xf, interm)); } // When |x| > 0.5, assume that 0.5 < |x| <= 1 @@ -142,7 +142,7 @@ LIBC_INLINE constexpr float16 acosf16(float16 x) { // with a positive x value instead (rhs). float xf_abs = (xf < 0 ? -xf : xf); - float u = fputil::multiply_add(-0.5f, xf_abs, 0.5f); + float u = fputil::sub_multiply(0.5f, 0.5f, xf_abs); float sqrt_u = fputil::sqrt<float>(u); // Degree-6 minimax polynomial of asin(x) generated by Sollya with: @@ -152,7 +152,7 @@ LIBC_INLINE constexpr float16 acosf16(float16 x) { 0x1.3541ccp-4f, 0x1.43b2d6p-5f, 0x1.a0d73ep-5f); return fputil::cast<float16>( - x_sign ? fputil::multiply_add(-2.0f, asin_sqrt_u, PI) : 2 * asin_sqrt_u); + x_sign ? fputil::sub_multiply(PI, 2.0f, asin_sqrt_u) : 2 * asin_sqrt_u); } } // namespace math diff --git a/libc/src/__support/math/acoshbf16.h b/libc/src/__support/math/acoshbf16.h index aaf9b0c05f7868..008a69baad0bb0 100644 --- a/libc/src/__support/math/acoshbf16.h +++ b/libc/src/__support/math/acoshbf16.h @@ -136,7 +136,7 @@ LIBC_INLINE LIBC_CONSTEXPR bfloat16 acoshbf16(bfloat16 x) { // this avoids overflow when squaring the largest finite BFloat16 inputs. // Convert directly to BFloat16 to avoid an intermediate float rounding. double xd = static_cast<double>(xf); - double sqrt_term = fputil::sqrt<double>(fputil::multiply_add(xd, xd, -1.0)); + double sqrt_term = fputil::sqrt<double>(fputil::multiply_sub(xd, xd, 1.0)); double result = log_eval(xd + sqrt_term); return fputil::cast<bfloat16>(result); diff --git a/libc/src/__support/math/acoshf.h b/libc/src/__support/math/acoshf.h index 02b2a3d5a513db..c68eaafede7b38 100644 --- a/libc/src/__support/math/acoshf.h +++ b/libc/src/__support/math/acoshf.h @@ -79,7 +79,7 @@ LIBC_INLINE float acoshf(float x) { #endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS double y_inv = 0.5 / x_d; return static_cast<float>( - fputil::multiply_add(y_inv, -y_inv, log_eval(y))); + fputil::sub_multiply(log_eval(y), y_inv, y_inv)); } else { // x > 2^26 @@ -114,7 +114,7 @@ LIBC_INLINE float acoshf(float x) { // For 1 < x < 2^12, we use the formula: // acosh(x) = log(x + sqrt(x^2 - 1)) return static_cast<float>(log_eval( - x_d + fputil::sqrt<double>(fputil::multiply_add(x_d, x_d, -1.0)))); + x_d + fputil::sqrt<double>(fputil::multiply_sub(x_d, x_d, 1.0)))); } } // namespace math diff --git a/libc/src/__support/math/acoshf16.h b/libc/src/__support/math/acoshf16.h index eeb9e9f7afa9d5..567dd1a2d7bdaa 100644 --- a/libc/src/__support/math/acoshf16.h +++ b/libc/src/__support/math/acoshf16.h @@ -108,7 +108,7 @@ LIBC_INLINE constexpr float16 acoshf16(float16 x) { } // acosh(x) = log(x + sqrt(x^2 - 1)) - float sqrt_term = fputil::sqrt<float>(fputil::multiply_add(xf, xf, -1.0f)); + float sqrt_term = fputil::sqrt<float>(fputil::multiply_sub(xf, xf, 1.0f)); float result = static_cast<float>(log_eval(xf + sqrt_term)); return fputil::cast<float16>(result); diff --git a/libc/src/__support/math/acoshf_utils.h b/libc/src/__support/math/acoshf_utils.h index 3d760f98352490..5706d5a8652558 100644 --- a/libc/src/__support/math/acoshf_utils.h +++ b/libc/src/__support/math/acoshf_utils.h @@ -103,7 +103,7 @@ LIBC_INLINE LIBC_CONSTEXPR double log_eval(double x) { // Perform exact range reduction #ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE - dx = fputil::multiply_add(r, m, -1.0); // exact + dx = fputil::multiply_sub(r, m, 1.0); // exact #else uint64_t c_m = x_m & 0x3FFF'C000'0000'0000ULL; double c = FPBits(c_m).get_val(); diff --git a/libc/src/__support/math/acospif.h b/libc/src/__support/math/acospif.h index f193a34f95316e..ba937614beead6 100644 --- a/libc/src/__support/math/acospif.h +++ b/libc/src/__support/math/acospif.h @@ -78,7 +78,7 @@ LIBC_INLINE float acospif(float x) { constexpr double C0_MINUS_1OVERPI = (asin_internal::ASINPIF_COEFFS[0] - ONE_OVER_PI_HI) + ONE_OVER_PI_LO; - double u = fputil::multiply_add(-0.5, x_abs, 0.5); + double u = fputil::sub_multiply(0.5, 0.5, x_abs); double sqrt_u = fputil::sqrt<double>(u); double neg2_sqrt_u = -2.0 * sqrt_u; diff --git a/libc/src/__support/math/acospif16.h b/libc/src/__support/math/acospif16.h index 4b5c28084f7094..40bf3b8b04387f 100644 --- a/libc/src/__support/math/acospif16.h +++ b/libc/src/__support/math/acospif16.h @@ -79,7 +79,7 @@ LIBC_INLINE constexpr float16 acospif16(float16 x) { fputil::polyeval(xsq, POLY_COEFFS[0], POLY_COEFFS[1], POLY_COEFFS[2], POLY_COEFFS[3], POLY_COEFFS[4]); - return fputil::cast<float16>(fputil::multiply_add(-xf, interm, 0.5f)); + return fputil::cast<float16>(fputil::sub_multiply(0.5f, xf, interm)); } // When |x| > 0.5, assume that 0.5 < |x| <= 1 @@ -125,7 +125,7 @@ LIBC_INLINE constexpr float16 acospif16(float16 x) { // with a positive x value instead (rhs). float xf_abs = (xf < 0 ? -xf : xf); - float u = fputil::multiply_add(-0.5f, xf_abs, 0.5f); + float u = fputil::sub_multiply(0.5f, 0.5f, xf_abs); float sqrt_u = fputil::sqrt<float>(u); float asin_sqrt_u = @@ -134,7 +134,7 @@ LIBC_INLINE constexpr float16 acospif16(float16 x) { // Same as acos(x), but devided the expression with pi return fputil::cast<float16>( - x_sign ? fputil::multiply_add(-2.0f, asin_sqrt_u, 1.0f) + x_sign ? fputil::sub_multiply(1.0f, 2.0f, asin_sqrt_u) : 2.0f * asin_sqrt_u); } diff --git a/libc/src/__support/math/asin.h b/libc/src/__support/math/asin.h index cf4c2736c3e209..3e66db92f17dde 100644 --- a/libc/src/__support/math/asin.h +++ b/libc/src/__support/math/asin.h @@ -67,7 +67,7 @@ LIBC_INLINE double asin(double x) { double normalize_const = (x_exp == 0) ? eps : 0.0; double scaled_normal = fputil::multiply_add(x + normalize_const, 0x1.0p54, eps); - return fputil::multiply_add(scaled_normal, 0x1.0p-54, -normalize_const); + return fputil::multiply_sub(scaled_normal, 0x1.0p-54, normalize_const); #endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS } @@ -110,7 +110,7 @@ LIBC_INLINE double asin(double x) { #ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE // u = x^2 - idx/64 DFloat128 u_hi( - fputil::multiply_add(static_cast<double>(idx), -0x1.0p-6, x_sq.hi)); + fputil::sub_multiply(x_sq.hi, static_cast<double>(idx), 0x1.0p-6)); DFloat128 u = fputil::quick_add(u_hi, DFloat128(x_sq.lo)); #else DFloat128 x_sq_f128 = fputil::quick_mul(x_f128, x_f128); @@ -176,7 +176,7 @@ LIBC_INLINE double asin(double x) { // asin(x) ~ pi/2 - 2 * sqrt(u) * P(u), // u = (1 - |x|)/2 - double u = fputil::multiply_add(x_abs, -0.5, 0.5); + double u = fputil::sub_multiply(0.5, x_abs, 0.5); // v_hi + v_lo ~ sqrt(u). // Let: // h = u - v_hi^2 = (sqrt(u) - v_hi) * (sqrt(u) + v_hi) @@ -197,7 +197,7 @@ LIBC_INLINE double asin(double x) { #else #ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE - double h = fputil::multiply_add(v_hi, -v_hi, u); + double h = fputil::sub_multiply(u, v_hi, v_hi); #else DoubleDouble v_hi_sq = fputil::exact_mult(v_hi, v_hi); double h = (u - v_hi_sq.hi) - v_hi_sq.lo; @@ -229,7 +229,7 @@ LIBC_INLINE double asin(double x) { double r_upper = fputil::multiply_add( r.hi, x_sign, fputil::multiply_add(r_lo, x_sign, err)); double r_lower = fputil::multiply_add( - r.hi, x_sign, fputil::multiply_add(r_lo, x_sign, -err)); + r.hi, x_sign, fputil::multiply_sub(r_lo, x_sign, err)); #else r_lo *= x_sign; r.hi *= x_sign; @@ -264,7 +264,7 @@ LIBC_INLINE double asin(double x) { // Get the rounding error of vl = 2 * v_lo ~ h / vh // Get full product of vh * vl #ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE - double vl_lo = fputil::multiply_add(-v_hi, vl, h) / v_hi; + double vl_lo = fputil::sub_multiply(h, v_hi, vl) / v_hi; #else DoubleDouble vh_vl = fputil::exact_mult(v_hi, vl); double vl_lo = ((h - vh_vl.hi) - vh_vl.lo) / v_hi; @@ -279,8 +279,7 @@ LIBC_INLINE double asin(double x) { // Perform computations in DFloat128: // asin(x) = pi/2 - (v_hi + v_lo + vll) * P(u). - DFloat128 y_f128( - fputil::multiply_add(static_cast<double>(idx), -0x1.0p-6, u)); + DFloat128 y_f128(fputil::sub_multiply(u, static_cast<double>(idx), 0x1.0p-6)); DFloat128 p_f128 = asin_eval(y_f128, idx); DFloat128 r0_f128 = fputil::quick_mul(m_v, p_f128); diff --git a/libc/src/__support/math/asin_utils.h b/libc/src/__support/math/asin_utils.h index 59d3df2e32b161..f0c81b32b6195d 100644 --- a/libc/src/__support/math/asin_utils.h +++ b/libc/src/__support/math/asin_utils.h @@ -237,7 +237,7 @@ LIBC_INLINE DoubleDouble asin_eval(const DoubleDouble &u, unsigned &idx, double k = fputil::nearest_integer(u.hi * 0x1.0p5); idx = static_cast<unsigned>(k); // y = u - k/32. - double y_hi = multiply_add(k, -0x1.0p-5, u.hi); // Exact + double y_hi = fputil::sub_multiply(u.hi, k, 0x1.0p-5); // Exact DoubleDouble y = fputil::exact_add(y_hi, u.lo); double y2 = y.hi * y.hi; // Add double-double errors in addition to the relative errors from y2. diff --git a/libc/src/__support/math/asinbf16.h b/libc/src/__support/math/asinbf16.h index 9083ca9adcce91..dc67f642d602ab 100644 --- a/libc/src/__support/math/asinbf16.h +++ b/libc/src/__support/math/asinbf16.h @@ -67,10 +67,10 @@ LIBC_INLINE LIBC_CONSTEXPR bfloat16 asinbf16(bfloat16 x) { return fputil::cast<bfloat16>(x_sign * PI_2); } - float t = fputil::multiply_add<float>(xf_abs, -0.5f, 0.5f); + float t = fputil::sub_multiply<float>(0.5f, xf_abs, 0.5f); float t_sqrt = fputil::sqrt<float>(t); float asin_sqrt_t = t_sqrt * asin_internal::asin_eval_float(t); - float result = fputil::multiply_add<float>(-2.0f, asin_sqrt_t, PI_2); + float result = fputil::sub_multiply<float>(PI_2, 2.0f, asin_sqrt_t); return fputil::cast<bfloat16>(x_sign * result); } diff --git a/libc/src/__support/math/asinf.h b/libc/src/__support/math/asinf.h index fbcac68dfb55c7..9c452579b34b44 100644 --- a/libc/src/__support/math/asinf.h +++ b/libc/src/__support/math/asinf.h @@ -122,7 +122,7 @@ LIBC_INLINE float asinf(float x) { xbits.set_sign(Sign::POS); double sign_two = TWO[x_sign]; // sign * (-2) - double uf = fputil::multiply_add(-0.5f, xbits.get_val(), 0.5f); + double uf = fputil::sub_multiply(0.5f, 0.5f, xbits.get_val()); double u = static_cast<double>(uf); double c1 = sign_two * fputil::sqrt<double>(u); double c2 = fputil::multiply_add(sign_two, M_PI_OVER_4, c1); diff --git a/libc/src/__support/math/asinf16.h b/libc/src/__support/math/asinf16.h index 367a034521146d..943faa5e9f665d 100644 --- a/libc/src/__support/math/asinf16.h +++ b/libc/src/__support/math/asinf16.h @@ -126,7 +126,7 @@ LIBC_INLINE constexpr float16 asinf16(float16 x) { // 0x1p-1 < |x| <= 0x1p0, 0.5 < |x| <= 1.0 float xf_abs = (xf < 0 ? -xf : xf); float sign = (xbits.uintval() >> 15 == 1 ? -1.0 : 1.0); - float u = fputil::multiply_add(-0.5f, xf_abs, 0.5f); + float u = fputil::sub_multiply(0.5f, 0.5f, xf_abs); float u_sqrt = fputil::sqrt<float>(u); // Degree-6 minimax odd polynomial of asin(x) generated by Sollya with: @@ -136,7 +136,7 @@ LIBC_INLINE constexpr float16 asinf16(float16 x) { 0x1.3541ccp-4f, 0x1.43b2d6p-5f, 0x1.a0d73ep-5f); return fputil::cast<float16>(sign * - fputil::multiply_add(-2.0f, asin_sqrt_u, PI_2)); + fputil::sub_multiply(PI_2, 2.0f, asin_sqrt_u)); } } // namespace math diff --git a/libc/src/__support/math/asinpi.h b/libc/src/__support/math/asinpi.h index 9ed3cc419dd5ea..e9fd001e5d5bb6 100644 --- a/libc/src/__support/math/asinpi.h +++ b/libc/src/__support/math/asinpi.h @@ -147,7 +147,7 @@ LIBC_INLINE double asinpi(double x) { #ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE DFloat128 u_hi( - fputil::multiply_add(static_cast<double>(idx), -0x1.0p-6, x_sq.hi)); + fputil::sub_multiply(x_sq.hi, static_cast<double>(idx), 0x1.0p-6)); DFloat128 u = fputil::quick_add(u_hi, DFloat128(x_sq.lo)); #else DFloat128 x_sq_f128 = fputil::quick_mul(x_f128, x_f128); @@ -200,7 +200,7 @@ LIBC_INLINE double asinpi(double x) { // where u = (1 - |x|) / 2. // u = (1 - |x|)/2 - double u = fputil::multiply_add(x_abs, -0.5, 0.5); + double u = fputil::sub_multiply(0.5, x_abs, 0.5); // v_hi ~ sqrt(u). double v_hi = fputil::sqrt<double>(u); @@ -215,7 +215,7 @@ LIBC_INLINE double asinpi(double x) { using DoubleDouble = fputil::DoubleDouble; #ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE - double h = fputil::multiply_add(v_hi, -v_hi, u); + double h = fputil::sub_multiply(u, v_hi, v_hi); #else DoubleDouble v_hi_sq = fputil::exact_mult(v_hi, v_hi); double h = (u - v_hi_sq.hi) - v_hi_sq.lo; @@ -247,7 +247,7 @@ LIBC_INLINE double asinpi(double x) { double r_upper = fputil::multiply_add( r.hi, x_sign, fputil::multiply_add(r_lo, x_sign, err)); double r_lower = fputil::multiply_add( - r.hi, x_sign, fputil::multiply_add(r_lo, x_sign, -err)); + r.hi, x_sign, fputil::multiply_sub(r_lo, x_sign, err)); #else r_lo *= x_sign; r.hi *= x_sign; @@ -270,7 +270,7 @@ LIBC_INLINE double asinpi(double x) { // Get the rounding error of vl = 2 * v_lo ~ h / vh #ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE - double vl_lo = fputil::multiply_add(-v_hi, vl, h) / v_hi; + double vl_lo = fputil::sub_multiply(h, v_hi, vl) / v_hi; #else DoubleDouble vh_vl = fputil::exact_mult(v_hi, vl); double vl_lo = ((h - vh_vl.hi) - vh_vl.lo) / v_hi; @@ -285,8 +285,7 @@ LIBC_INLINE double asinpi(double x) { // Perform computations in DFloat128: // asinpi(x) = 0.5 - (v_hi + v_lo + vll) * P_pi(u). - DFloat128 y_f128( - fputil::multiply_add(static_cast<double>(idx), -0x1.0p-6, u)); + DFloat128 y_f128(fputil::sub_multiply(u, static_cast<double>(idx), 0x1.0p-6)); DFloat128 p_f128 = asinpi_eval(y_f128, idx); DFloat128 r0_f128 = fputil::quick_mul(m_v, p_f128); diff --git a/libc/src/__support/math/asinpif.h b/libc/src/__support/math/asinpif.h index 0a823c2bce627d..cd3096940121c6 100644 --- a/libc/src/__support/math/asinpif.h +++ b/libc/src/__support/math/asinpif.h @@ -76,7 +76,7 @@ LIBC_INLINE float asinpif(float x) { constexpr double C0_MINUS_1OVERPI = (asin_internal::ASINPIF_COEFFS[0] - ONE_OVER_PI_HI) + ONE_OVER_PI_LO; - double u = fputil::multiply_add(-0.5, x_abs, 0.5); + double u = fputil::sub_multiply(0.5, 0.5, x_abs); double sqrt_u = fputil::sqrt<double>(u); double neg2_sqrt_u = -2.0 * sqrt_u; diff --git a/libc/src/__support/math/asinpif16.h b/libc/src/__support/math/asinpif16.h index 7aefc35f82d01f..fcee574850c51f 100644 --- a/libc/src/__support/math/asinpif16.h +++ b/libc/src/__support/math/asinpif16.h @@ -124,9 +124,9 @@ LIBC_INLINE float16 asinpif16(float16 x) { // = 0.5 - 0.5 * x // = multiply_add(-0.5, x, 0.5) - double u = fputil::multiply_add(-0.5, x_abs, 0.5); + double u = fputil::sub_multiply(0.5, 0.5, x_abs); double asinpi_sqrt_u = asinpi_polyeval(fputil::sqrt<double>(u)); - double result = fputil::multiply_add(-2.0, asinpi_sqrt_u, 0.5); + double result = fputil::sub_multiply(0.5, 2.0, asinpi_sqrt_u); return fputil::cast<float16>(signed_result(result)); } diff --git a/libc/src/__support/math/atan.h b/libc/src/__support/math/atan.h index 8a11167a6268ef..27c412accb555a 100644 --- a/libc/src/__support/math/atan.h +++ b/libc/src/__support/math/atan.h @@ -79,7 +79,7 @@ LIBC_INLINE double atan(double x) { if (x == 0.0) return x; // |x| < 2^-26 - return fputil::multiply_add(-0x1.0p-54, x, x); + return fputil::sub_multiply(x, 0x1.0p-54, x); #endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS } @@ -153,7 +153,7 @@ LIBC_INLINE double atan(double x) { DoubleDouble den = fputil::exact_add(x_d, k); // numerator = 1 - k * |x| DoubleDouble num; - num.hi = fputil::multiply_add(-x_d, k, 1.0); + num.hi = fputil::sub_multiply(1.0, x_d, k); DoubleDouble prod = fputil::exact_mult(x_d, k); // Using Dekker's 2SUM algorithm to compute the lower part. num.lo = ((1.0 - num.hi) - prod.hi) - prod.lo; diff --git a/libc/src/__support/math/atan2f.h b/libc/src/__support/math/atan2f.h index ff2419e69c6aed..2721f165fc7315 100644 --- a/libc/src/__support/math/atan2f.h +++ b/libc/src/__support/math/atan2f.h @@ -127,8 +127,8 @@ LIBC_INLINE float atan2f_double_double(double num_d, double den_d, double q_d, // atan(n/d) - atan(idx/16) = atan((n/d - idx/16) / (1 + (n/d) * (idx/16))) // = atan((n - d*(idx/16)) / (d + n*idx/16)) k_d *= 0x1.0p-4; - num_r = fputil::multiply_add(k_d, -den_d, num_d); // Exact - den_r = fputil::multiply_add(k_d, num_d, den_d); // Exact + num_r = fputil::sub_multiply(num_d, k_d, den_d); // Exact + den_r = fputil::multiply_add(k_d, num_d, den_d); // Exact q.hi = num_r / den_r; } else { // For 0 < n/d < 1/16, we just need to calculate the lower part of their @@ -138,13 +138,13 @@ LIBC_INLINE float atan2f_double_double(double num_d, double den_d, double q_d, den_r = den_d; } #ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE - q.lo = fputil::multiply_add(q.hi, -den_r, num_r) / den_r; + q.lo = fputil::sub_multiply(num_r, q.hi, den_r) / den_r; #else // Compute `(num_r - q.hi * den_r) / den_r` accurately without FMA // instructions. fputil::DoubleDouble q_hi_dd = split_d(q.hi); - double t1 = fputil::multiply_add(q_hi_dd.hi, -den_r, num_r); // Exact - double t2 = fputil::multiply_add(q_hi_dd.lo, -den_r, t1); + double t1 = fputil::sub_multiply(num_r, q_hi_dd.hi, den_r); // Exact + double t2 = fputil::sub_multiply(t1, q_hi_dd.lo, den_r); q.lo = t2 / den_r; #endif // LIBC_TARGET_CPU_HAS_FMA_DOUBLE @@ -314,7 +314,7 @@ LIBC_INLINE float atan2f(float y, float x) { double p = atan_eval_no_table(num_d, den_d, k_d * 0x1.0p-4); r = final_sign * (p + (const_term.hi + ATAN_K_OVER_16[idx])); #else - q_d = fputil::multiply_add(k_d, -0x1.0p-4, q_d); + q_d = fputil::sub_multiply(q_d, k_d, 0x1.0p-4); double p = atan_eval(q_d, idx); r = final_sign * diff --git a/libc/src/__support/math/atan2f128.h b/libc/src/__support/math/atan2f128.h index 5df9071acc51bc..f17e8d113a3eda 100644 --- a/libc/src/__support/math/atan2f128.h +++ b/libc/src/__support/math/atan2f128.h @@ -187,7 +187,7 @@ LIBC_INLINE Float128 atan2f128(Float128 y, Float128 x) { // atan(n/d) - atan(k) = atan((n/d - k/64) / (1 + (n/d) * (k/64))) // = atan((n - d * k/64)) / (d + n * k/64)) // num_f128 = n - d * k/64 - DFloat128 num_f128 = fputil::multiply_add(den, -k_f128, num); + DFloat128 num_f128 = fputil::sub_multiply(num, den, k_f128); // den_f128 = d + n * k/64 DFloat128 den_f128 = fputil::multiply_add(num, k_f128, den); diff --git a/libc/src/__support/math/atan2f16.h b/libc/src/__support/math/atan2f16.h index 0621ab4fd05477..c7570bd544e2d0 100644 --- a/libc/src/__support/math/atan2f16.h +++ b/libc/src/__support/math/atan2f16.h @@ -91,7 +91,7 @@ LIBC_INLINE float16 atan2f16(float16 y, float16 x) { double k_d = fputil::nearest_integer(q_d * 0x1.0p4); int idx = static_cast<int>(k_d); - q_d = fputil::multiply_add(k_d, -0x1.0p-4, q_d); + q_d = fputil::sub_multiply(q_d, k_d, 0x1.0p-4); double p = atan_eval(q_d, static_cast<unsigned>(idx)); double r = final_sign * diff --git a/libc/src/__support/math/atan2f_float.h b/libc/src/__support/math/atan2f_float.h index 744783e855cef6..58cd97878fa5b2 100644 --- a/libc/src/__support/math/atan2f_float.h +++ b/libc/src/__support/math/atan2f_float.h @@ -67,7 +67,7 @@ LIBC_INLINE constexpr FloatFloat atan_eval(const FloatFloat &x) { // c0 ~ - x_hi^2 / 3 float c0 = -0x1.555556p-2f * x_hi_sq; // c1 ~ x_lo * (1 - x_hi^2) - float c1 = fputil::multiply_add(x_hi_sq, -x.lo, x.lo); + float c1 = fputil::sub_multiply(x.lo, x_hi_sq, x.lo); // p.lo ~ - x_hi^3 / 3 + x_lo * (1 - x_hi*2) p.lo = fputil::multiply_add(x.hi, c0, c1); return p; diff --git a/libc/src/__support/math/atan_utils.h b/libc/src/__support/math/atan_utils.h index 60d25e7d7374f9..ff857402dfa9dd 100644 --- a/libc/src/__support/math/atan_utils.h +++ b/libc/src/__support/math/atan_utils.h @@ -115,11 +115,11 @@ LIBC_INLINE_VAR constexpr DoubleDouble ATAN_I[65] = { p.hi = x.hi; double x_hi_sq = x.hi * x.hi; // c0 ~ x_hi^2 * 1/5 - 1/3 - double c0 = fputil::multiply_add(x_hi_sq, 0x1.999999999999ap-3, - -0x1.5555555555555p-2); + double c0 = + fputil::multiply_sub(x_hi_sq, 0x1.999999999999ap-3, 0x1.5555555555555p-2); // c1 ~ x_hi^2 * 1/9 - 1/7 - double c1 = fputil::multiply_add(x_hi_sq, 0x1.c71c71c71c71cp-4, - -0x1.2492492492492p-3); + double c1 = + fputil::multiply_sub(x_hi_sq, 0x1.c71c71c71c71cp-4, 0x1.2492492492492p-3); // x_hi^3 double x_hi_3 = x_hi_sq * x.hi; // x_hi^4 @@ -340,7 +340,7 @@ LIBC_INLINE double atan_eval(double x, unsigned i) { // atan(q) ~ q - q^3/3 + q^5/5 - q^7/7 + q^9/9 LIBC_INLINE double atan_eval_no_table(double num, double den, double k_over_16) { - double num_r = fputil::multiply_add(den, -k_over_16, num); + double num_r = fputil::sub_multiply(num, den, k_over_16); double den_r = fputil::multiply_add(num, k_over_16, den); double q = num_r / den_r; diff --git a/libc/src/__support/math/atanbf16.h b/libc/src/__support/math/atanbf16.h index 796420a6cf4b62..a1e4605269072d 100644 --- a/libc/src/__support/math/atanbf16.h +++ b/libc/src/__support/math/atanbf16.h @@ -69,7 +69,7 @@ LIBC_INLINE bfloat16 atanbf16(bfloat16 x) { if (LIBC_UNLIKELY(x_abs <= 0x3db8)) { // Keep the small correction normal even for the smallest bfloat16 input. double xd = xf; - return fputil::cast<bfloat16>(fputil::multiply_add(xd, -0x1p-9, xd)); + return fputil::cast<bfloat16>(fputil::sub_multiply(xd, xd, 0x1p-9)); } float x_sq = xf * xf; diff --git a/libc/src/__support/math/atanf.h b/libc/src/__support/math/atanf.h index 4f185650322517..fa3d7d2ea62bfd 100644 --- a/libc/src/__support/math/atanf.h +++ b/libc/src/__support/math/atanf.h @@ -65,9 +65,9 @@ LIBC_INLINE float atanf(float x) { // x <= 2^-12; if (LIBC_UNLIKELY(x_abs < 0x3980'0000)) { #if defined(LIBC_TARGET_CPU_HAS_FMA_FLOAT) - return fputil::multiply_add(x, -0x1.0p-25f, x); + return fputil::sub_multiply(x, x, 0x1.0p-25f); #else - return static_cast<float>(fputil::multiply_add(x_d, -0x1.0p-25, x_d)); + return static_cast<float>(fputil::sub_multiply(x_d, x_d, 0x1.0p-25)); #endif // LIBC_TARGET_CPU_HAS_FMA_FLOAT } // Use Taylor polynomial: @@ -101,7 +101,7 @@ LIBC_INLINE float atanf(float x) { // double precision. x_d = 1.0 / static_cast<double>(x_bits.get_val()); double k_d = fputil::nearest_integer(x_d * 0x1.0p4); - x_d = fputil::multiply_add(k_d, -0x1.0p-4, x_d); + x_d = fputil::sub_multiply(x_d, k_d, 0x1.0p-4); idx = static_cast<int>(k_d); final_sign = FINAL_SIGN[sign.is_pos()]; // Adjust constant term of the polynomial by +- pi/2. @@ -116,7 +116,7 @@ LIBC_INLINE float atanf(float x) { // Perform range reduction in single precision. float x_f = x_bits.get_val(); float k_f = fputil::nearest_integer(x_f * 0x1.0p4f); - x_f = fputil::multiply_add(k_f, -0x1.0p-4f, x_f); + x_f = fputil::sub_multiply(x_f, k_f, 0x1.0p-4f); x_d = static_cast<double>(x_f); idx = static_cast<int>(k_f); final_sign = FINAL_SIGN[sign.is_neg()]; diff --git a/libc/src/__support/math/atanf16.h b/libc/src/__support/math/atanf16.h index 01eb8770591ebd..92c9a4de581172 100644 --- a/libc/src/__support/math/atanf16.h +++ b/libc/src/__support/math/atanf16.h @@ -107,7 +107,7 @@ LIBC_INLINE constexpr float16 atanf16(float16 x) { -0x1.e65492p-5f, 0x1.8c0c36p-6f, -0x1.32316ep-8f); return fputil::cast<float16>(sign * - fputil::multiply_add(x_inv, -interm, PI_2)); + fputil::sub_multiply(PI_2, x_inv, interm)); } } // namespace math diff --git a/libc/src/__support/math/atanf_float.h b/libc/src/__support/math/atanf_float.h index 56db2ad2955e61..f3a10380a5256c 100644 --- a/libc/src/__support/math/atanf_float.h +++ b/libc/src/__support/math/atanf_float.h @@ -70,12 +70,12 @@ LIBC_INLINE float atanf_eval(float u, float k_over_16) { // (k/16)^2 float c2 = k_over_16 * k_over_16; // -(k/16)^3 - float c3 = fputil::multiply_add(-k_over_16, c2, k_over_16); + float c3 = fputil::sub_multiply(k_over_16, k_over_16, c2); float u2 = u * u; // (k^2/256 - 1/3) + u * (k/16 - (k/16)^3) float a0 = fputil::multiply_add(c3, u, c2 - 0x1.555556p-2f); // -k/16 + u*(k^2/256 - 1/3) + u^2 * (k/16 - (k/16)^3) - float a1 = fputil::multiply_add(u, a0, -k_over_16); + float a1 = fputil::multiply_sub(u, a0, k_over_16); // u - u^2 * k/16 + u^3 * ((k^2/256 - 1/3) + u^4 * (k/16 - (k/16)^3)) return fputil::multiply_add(u2, a1, u); } @@ -115,7 +115,7 @@ LIBC_INLINE float atanf(float x) { // atan(x) = sign(x) * pi/2 - atan(1/x) // ~ sign(x) * pi/2 - 1/x if (LIBC_UNLIKELY(x_abs >= 0x4200'0000)) - return fputil::multiply_add(sign, PI_OVER_2.hi, -1.0f / x); + return fputil::multiply_sub(sign, PI_OVER_2.hi, 1.0f / x); // x <= 2^-11: // atan(x) ~ x return x; diff --git a/libc/src/__support/math/atanpif16.h b/libc/src/__support/math/atanpif16.h index e0eaff6678fc58..faac2b62fb1cb5 100644 --- a/libc/src/__support/math/atanpif16.h +++ b/libc/src/__support/math/atanpif16.h @@ -151,7 +151,7 @@ LIBC_INLINE float16 atanpif16(float16 x) { double x_recip_sq = x_recip * x_recip; double sqrt_term = fputil::sqrt<double>(1.0 + x_recip_sq); double x_prime = x_recip / (1.0 + sqrt_term); - result = fputil::multiply_add(-2.0, atanpi_eval(x_prime), 0.5); + result = fputil::sub_multiply(0.5, 2.0, atanpi_eval(x_prime)); } else { // direct evaluation since 1/|x| <= 0.5 result = 0.5 - atanpi_eval(x_recip); diff --git a/libc/src/__support/math/cbrt.h b/libc/src/__support/math/cbrt.h index c6631e6f67e2cb..0e12f887d4ac19 100644 --- a/libc/src/__support/math/cbrt.h +++ b/libc/src/__support/math/cbrt.h @@ -61,7 +61,7 @@ LIBC_INLINE double initial_approximation(double x) { #ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE LIBC_INLINE double get_error(const DoubleDouble &x_3, const DoubleDouble &a_sq) { - return fputil::multiply_add(x_3.hi, a_sq.hi, -1.0) + + return fputil::multiply_sub(x_3.hi, a_sq.hi, 1.0) + fputil::multiply_add(x_3.lo, a_sq.hi, x_3.hi * a_sq.lo); } #else @@ -288,7 +288,7 @@ LIBC_INLINE double cbrt(double x) { DoubleDouble r2 = fputil::quick_mult(a, x2); double r2_upper = r2.hi + fputil::multiply_add(a, 0x1.0p-102, r2.lo); - double r2_lower = r2.hi + fputil::multiply_add(a, -0x1.0p-102, r2.lo); + double r2_lower = r2.hi + fputil::sub_multiply(r2.lo, a, 0x1.0p-102); // Ziv's accuracy test. if (LIBC_LIKELY(r2_upper == r2_lower)) diff --git a/libc/src/__support/math/cosf16.h b/libc/src/__support/math/cosf16.h index f7485a074ac639..2965ffa1f8581d 100644 --- a/libc/src/__support/math/cosf16.h +++ b/libc/src/__support/math/cosf16.h @@ -94,7 +94,7 @@ LIBC_INLINE constexpr float16 cosf16(float16 x) { // = cos_k * (cos_y - 1 + 1) - sin_k * sin_y // = cos_k * cosm1_y - sin_k * sin_y + cos_k return fputil::cast<float16>(fputil::multiply_add( - cos_k, cosm1_y, fputil::multiply_add(-sin_k, sin_y, cos_k))); + cos_k, cosm1_y, fputil::sub_multiply(cos_k, sin_k, sin_y))); } } // namespace math diff --git a/libc/src/__support/math/cosf_double_eval.h b/libc/src/__support/math/cosf_double_eval.h index aa7132df937869..d44e795e92b195 100644 --- a/libc/src/__support/math/cosf_double_eval.h +++ b/libc/src/__support/math/cosf_double_eval.h @@ -115,10 +115,10 @@ LIBC_INLINE float cosf(float x) { // double for intermediate results as it is more efficient than using an // emulated version of FMA. #if defined(LIBC_TARGET_CPU_HAS_FMA_FLOAT) - return fputil::multiply_add(xbits.get_val(), -0x1.0p-25f, 1.0f); + return fputil::sub_multiply(1.0f, xbits.get_val(), 0x1.0p-25f); #else // !LIBC_TARGET_CPU_HAS_FMA_FLOAT double xd = static_cast<double>(xbits.get_val()); - return static_cast<float>(fputil::multiply_add(xd, -0x1.0p-25, 1.0)); + return static_cast<float>(fputil::sub_multiply(1.0, xd, 0x1.0p-25)); #endif // LIBC_TARGET_CPU_HAS_FMA_FLOAT } @@ -150,8 +150,8 @@ LIBC_INLINE float cosf(float x) { sincosf_eval(xd, x_abs, sin_k, cos_k, sin_y, cosm1_y); - return static_cast<float>(fputil::multiply_add( - sin_y, -sin_k, fputil::multiply_add(cosm1_y, cos_k, cos_k))); + return static_cast<float>(fputil::sub_multiply( + fputil::multiply_add(cosm1_y, cos_k, cos_k), sin_y, sin_k)); } } // namespace double_eval diff --git a/libc/src/__support/math/cospif.h b/libc/src/__support/math/cospif.h index 49100feba32308..7f9c81b22f8555 100644 --- a/libc/src/__support/math/cospif.h +++ b/libc/src/__support/math/cospif.h @@ -56,9 +56,9 @@ LIBC_INLINE float cospif(float x) { if (LIBC_UNLIKELY(x_abs < 0x38A2'F984U)) { #if defined(LIBC_TARGET_CPU_HAS_FMA_FLOAT) - return fputil::multiply_add(xbits.get_val(), -0x1.0p-25f, 1.0f); + return fputil::sub_multiply(1.0f, xbits.get_val(), 0x1.0p-25f); #else - return static_cast<float>(fputil::multiply_add(xd, -0x1.0p-25, 1.0)); + return static_cast<float>(fputil::sub_multiply(1.0, xd, 0x1.0p-25)); #endif // LIBC_TARGET_CPU_HAS_FMA_FLOAT } @@ -99,8 +99,8 @@ LIBC_INLINE float cospif(float x) { return 0.0f; } - return static_cast<float>(fputil::multiply_add( - sin_y, -sin_k, fputil::multiply_add(cosm1_y, cos_k, cos_k))); + return static_cast<float>(fputil::sub_multiply( + fputil::multiply_add(cosm1_y, cos_k, cos_k), sin_y, sin_k)); } } // namespace math diff --git a/libc/src/__support/math/cospif16.h b/libc/src/__support/math/cospif16.h index 8cdbe108a5bbae..02bc38569c83d6 100644 --- a/libc/src/__support/math/cospif16.h +++ b/libc/src/__support/math/cospif16.h @@ -95,7 +95,7 @@ LIBC_INLINE constexpr float16 cospif16(float16 x) { // Since, cosm1_y = cos_y - 1, therefore: // cos(x * pi) = cos_k(cosm1_y) + cos_k - sin_k * sin_y return fputil::cast<float16>(fputil::multiply_add( - cos_k, cosm1_y, fputil::multiply_add(-sin_k, sin_y, cos_k))); + cos_k, cosm1_y, fputil::sub_multiply(cos_k, sin_k, sin_y))); } } // namespace math diff --git a/libc/src/__support/math/exp10f16_utils.h b/libc/src/__support/math/exp10f16_utils.h index a0fca6399a78fa..87d81d060f2eda 100644 --- a/libc/src/__support/math/exp10f16_utils.h +++ b/libc/src/__support/math/exp10f16_utils.h @@ -40,7 +40,7 @@ LIBC_INLINE ExpRangeReduction exp10_range_reduction(float16 x) { unsigned x_hi = static_cast<unsigned>(x_hi_mid) >> 3; unsigned x_mid = static_cast<unsigned>(x_hi_mid) & 0x7; // lo = x - (hi + mid) = round(x * 2^3 * log2(10)) * log10(2) * (-2^(-3)) + x - float lo = fputil::multiply_add(kf, LOG10F_2 * -0x1.0p-3f, xf); + float lo = fputil::sub_multiply(xf, kf, LOG10F_2 * 0x1.0p-3f); uint32_t exp2_hi_mid_bits = EXP2_MID_BITS[x_mid] + diff --git a/libc/src/__support/math/exp10f_float_eval.h b/libc/src/__support/math/exp10f_float_eval.h index 700397a85bd2b9..045ea008868c24 100644 --- a/libc/src/__support/math/exp10f_float_eval.h +++ b/libc/src/__support/math/exp10f_float_eval.h @@ -105,7 +105,7 @@ LIBC_INLINE float exp10f(float x) { float kf = fputil::nearest_integer(x * LOG2_10.hi); int k = static_cast<int>(kf); - float u_hi = fputil::multiply_add(x, LOG2_10.hi, -kf); + float u_hi = fputil::multiply_sub(x, LOG2_10.hi, kf); float u = fputil::multiply_add(x, LOG2_10.lo, u_hi); #else // !LIBC_TARGET_CPU_HAS_FMA_FLOAT // Cody-Waite reduction for non-FMA targets: @@ -121,8 +121,8 @@ LIBC_INLINE float exp10f(float x) { float kf = fputil::nearest_integer(x * LOG2_10); int k = static_cast<int>(kf); - float v_hi = fputil::multiply_add(-kf, LOG10_2_HI, x); - float v = fputil::multiply_add(-kf, LOG10_2_LO, v_hi); + float v_hi = fputil::sub_multiply(x, kf, LOG10_2_HI); + float v = fputil::sub_multiply(v_hi, kf, LOG10_2_LO); // Convert reduced argument to base-2: u = v * log2(10) float u = v * LOG2_10; diff --git a/libc/src/__support/math/exp10m1f.h b/libc/src/__support/math/exp10m1f.h index 4039b708cc09f2..a4604565f4db59 100644 --- a/libc/src/__support/math/exp10m1f.h +++ b/libc/src/__support/math/exp10m1f.h @@ -244,7 +244,7 @@ LIBC_INLINE float exp10m1f(float x) { double exp10_lo = fputil::polyeval(lo_sq, c0, c1, c2); // 10^x - 1 = 2^(mid + hi) * 10^lo - 1 // ~ mh * exp10_lo - 1 - return static_cast<float>(fputil::multiply_add(exp10_lo, rr.mh, -1.0)); + return static_cast<float>(fputil::multiply_sub(exp10_lo, rr.mh, 1.0)); } } // namespace math diff --git a/libc/src/__support/math/exp10m1f16.h b/libc/src/__support/math/exp10m1f16.h index 5d31ca6da90e4e..e14fb233cb9659 100644 --- a/libc/src/__support/math/exp10m1f16.h +++ b/libc/src/__support/math/exp10m1f16.h @@ -172,7 +172,7 @@ LIBC_INLINE constexpr float16 exp10m1f16(float16 x) { auto [exp2_hi_mid, exp10_lo] = exp10_range_reduction(x); // exp10m1(x) = exp2((hi + mid) * log2(lo)) * exp10(lo) - 1 return fputil::cast<float16>( - fputil::multiply_add(exp2_hi_mid, exp10_lo, -1.0f)); + fputil::multiply_sub(exp2_hi_mid, exp10_lo, 1.0f)); } } // namespace math diff --git a/libc/src/__support/math/exp2_double_eval.h b/libc/src/__support/math/exp2_double_eval.h index a462e2038e96aa..12eca4774c5b1b 100644 --- a/libc/src/__support/math/exp2_double_eval.h +++ b/libc/src/__support/math/exp2_double_eval.h @@ -82,7 +82,7 @@ LIBC_ALWAYS_INLINE double exp2(double x) { int hi = static_cast<int32_t>(k_bits >> 19) >> (13 + EXP_N_BITS); unsigned idx = static_cast<unsigned>(k_bits >> 32) & EXP_MASK; - double dx = fputil::multiply_add(kd, -EXP_SCALE_INV, x); // exact + double dx = fputil::sub_multiply(x, kd, EXP_SCALE_INV); // exact #ifdef LIBC_MATH_HAS_SKIP_ACCURATE_PASS double u = dx * LOG_2_DD.hi; diff --git a/libc/src/__support/math/exp2bf16.h b/libc/src/__support/math/exp2bf16.h index 1dcd05b82f3e1d..2990a261ebc2dd 100644 --- a/libc/src/__support/math/exp2bf16.h +++ b/libc/src/__support/math/exp2bf16.h @@ -144,7 +144,7 @@ LIBC_INLINE bfloat16 exp2bf16(bfloat16 x) { // lo = x - (hi + mid) = x - k / 2^n // = k * (-2^(-n)) + x - float lo = fputil::multiply_add(k_float, -0.0625f, x_float); + float lo = fputil::sub_multiply(x_float, k_float, 0.0625f); float exp2_mid = exp2bf16_internal::EXP2_MID[j]; diff --git a/libc/src/__support/math/exp2f_double_eval.h b/libc/src/__support/math/exp2f_double_eval.h index b69dbf65ef18d7..85efd0bec0cb7f 100644 --- a/libc/src/__support/math/exp2f_double_eval.h +++ b/libc/src/__support/math/exp2f_double_eval.h @@ -54,7 +54,7 @@ LIBC_INLINE float exp2f(float x) { #endif // LIBC_TARGET_CPU_HAS_NEAREST_INT // dx = lo = x - (hi + mid) = x - kf * 2^(-5) - double dx = fputil::multiply_add(-0x1.0p-5f, kf, x); + double dx = fputil::sub_multiply(x, 0x1.0p-5f, kf); // hi = floor(kf * 2^(-4)) // exp_hi = shift hi to the exponent field of double precision. diff --git a/libc/src/__support/math/exp2f_float_eval.h b/libc/src/__support/math/exp2f_float_eval.h index fdd8167a098661..f32a65cc5c605f 100644 --- a/libc/src/__support/math/exp2f_float_eval.h +++ b/libc/src/__support/math/exp2f_float_eval.h @@ -68,7 +68,7 @@ LIBC_INLINE float exp2f(float x) { float kf = fputil::nearest_integer(x); int k = static_cast<int>(kf); - float u = fputil::multiply_add(kf, -1.0f, x); + float u = fputil::sub_multiply(x, kf, 1.0f); return exp2f_eval(u, k); } diff --git a/libc/src/__support/math/exp2m1f.h b/libc/src/__support/math/exp2m1f.h index 3b5cfb05918f2f..bed44e36066833 100644 --- a/libc/src/__support/math/exp2m1f.h +++ b/libc/src/__support/math/exp2m1f.h @@ -154,7 +154,7 @@ LIBC_INLINE float exp2m1f(float x) { #endif // LIBC_TARGET_CPU_HAS_NEAREST_INT // lo = x - (hi + mid) = x - kf * 2^(-5) - double lo = fputil::multiply_add(-0x1.0p-5f, kf, x); + double lo = fputil::sub_multiply(x, 0x1.0p-5f, kf); // hi = floor(kf * 2^(-4)) // exp2_hi = shift hi to the exponent field of double precision. @@ -181,7 +181,7 @@ LIBC_INLINE float exp2m1f(float x) { // = 2^(hi + mid) * 2^lo - 1 // ~ mh * (1 + lo * P(lo)) - 1 // = mh * exp2_lo - 1 - return static_cast<float>(fputil::multiply_add(exp2_lo, mh, -1.0)); + return static_cast<float>(fputil::multiply_sub(exp2_lo, mh, 1.0)); } } // namespace math diff --git a/libc/src/__support/math/exp2m1f16.h b/libc/src/__support/math/exp2m1f16.h index 09f349e15bb3a1..61adb79790b13f 100644 --- a/libc/src/__support/math/exp2m1f16.h +++ b/libc/src/__support/math/exp2m1f16.h @@ -174,7 +174,7 @@ LIBC_INLINE constexpr float16 exp2m1f16(float16 x) { auto [exp2_hi_mid, exp2_lo] = exp2_range_reduction(x); // exp2m1(x) = exp2(hi + mid) * exp2(lo) - 1 return fputil::cast<float16>( - fputil::multiply_add(exp2_hi_mid, exp2_lo, -1.0f)); + fputil::multiply_sub(exp2_hi_mid, exp2_lo, 1.0f)); } } // namespace math diff --git a/libc/src/__support/math/expbf16.h b/libc/src/__support/math/expbf16.h index 2fdb5c3192583c..c186b9beea7776 100644 --- a/libc/src/__support/math/expbf16.h +++ b/libc/src/__support/math/expbf16.h @@ -174,7 +174,7 @@ LIBC_INLINE bfloat16 expbf16(bfloat16 x) { int x_hi = x_hi_mid >> 5; int x_mid = x_hi_mid & 0b11111; // lo = x - (hi + mid) = round(x * 4) / (-4) + x - float lo = fputil::multiply_add(kf, -0.25f, xf); + float lo = fputil::sub_multiply(xf, kf, 0.25f); float exp_hi = expbf16_internal::EXP_HI[x_hi + 12]; float exp_mid = expbf16_internal::EXP_MID[x_mid]; diff --git a/libc/src/__support/math/expf16_utils.h b/libc/src/__support/math/expf16_utils.h index de7add4ecf7317..15441b091f31d9 100644 --- a/libc/src/__support/math/expf16_utils.h +++ b/libc/src/__support/math/expf16_utils.h @@ -68,7 +68,7 @@ struct ExpRangeReduction { int x_hi = x_hi_mid >> 3; int x_mid = x_hi_mid & 0x7; // lo = x - (hi + mid) = round(x * 2^3) * (-2^(-3)) + x - float lo = fputil::multiply_add(kf, -0x1.0p-3f, xf); + float lo = fputil::sub_multiply(xf, kf, 0x1.0p-3f); float exp_hi = EXP_HI[x_hi + 18]; float exp_mid = EXP_MID[x_mid]; diff --git a/libc/src/__support/math/expf_double_eval.h b/libc/src/__support/math/expf_double_eval.h index 2c5c947edbe191..4404cb3d4f507a 100644 --- a/libc/src/__support/math/expf_double_eval.h +++ b/libc/src/__support/math/expf_double_eval.h @@ -88,7 +88,7 @@ LIBC_INLINE float expf(float x) { // x_hi = (hi + mid) * 2^7 = round(x * 2^7). float kf = fputil::nearest_integer(x * 0x1.0p7f); // Subtract (hi + mid) from x to get lo. - double xd = static_cast<double>(fputil::multiply_add(kf, -0x1.0p-7f, x)); + double xd = static_cast<double>(fputil::sub_multiply(x, kf, 0x1.0p-7f)); int x_hi = static_cast<int>(kf); x_hi += 104 << 7; // hi = x_hi >> 7 diff --git a/libc/src/__support/math/expf_float_eval.h b/libc/src/__support/math/expf_float_eval.h index 02cd0444958790..dceba0929e43b1 100644 --- a/libc/src/__support/math/expf_float_eval.h +++ b/libc/src/__support/math/expf_float_eval.h @@ -82,7 +82,7 @@ LIBC_INLINE float expf(float x) { float kf = fputil::nearest_integer(x * LOG2_E.hi); int k = static_cast<int>(kf); - float u_hi = fputil::multiply_add(x, LOG2_E.hi, -kf); + float u_hi = fputil::multiply_sub(x, LOG2_E.hi, kf); float u = fputil::multiply_add(x, LOG2_E.lo, u_hi); #else // !LIBC_TARGET_CPU_HAS_FMA_FLOAT // Cody-Waite reduction for non-FMA targets: @@ -98,8 +98,8 @@ LIBC_INLINE float expf(float x) { float kf = fputil::nearest_integer(x * LOG2_E); int k = static_cast<int>(kf); - float v_hi = fputil::multiply_add(-kf, LOG_2_HI, x); - float v = fputil::multiply_add(-kf, LOG_2_LO, v_hi); + float v_hi = fputil::sub_multiply(x, kf, LOG_2_HI); + float v = fputil::sub_multiply(v_hi, kf, LOG_2_LO); // Convert reduced argument to base-2: u = v * log2(e) float u = v * LOG2_E; @@ -161,7 +161,7 @@ LIBC_INLINE float expf(float x) { float kf = fputil::nearest_integer(x_safe * LOG2_E); int k = static_cast<int>(kf); - float u_hi = fputil::multiply_add(-kf, LOG_2_HI, x_safe); + float u_hi = fputil::sub_multiply(x_safe, kf, LOG_2_HI); fputil::FloatFloat u_lo = fputil::quick_mult(-kf, LOG_2_LO); fputil::FloatFloat u = fputil::exact_add(u_hi, u_lo.hi); diff --git a/libc/src/__support/math/expm1f.h b/libc/src/__support/math/expm1f.h index 8505d7e72740b5..ec58e74de9bb4b 100644 --- a/libc/src/__support/math/expm1f.h +++ b/libc/src/__support/math/expm1f.h @@ -159,7 +159,7 @@ LIBC_INLINE float expm1f(float x) { float kf = fputil::nearest_integer(x * 0x1.0p7f); int x_hi = static_cast<int>(kf); // Subtract (hi + mid) from x to get lo. - double xd = static_cast<double>(fputil::multiply_add(kf, -0x1.0p-7f, x)); + double xd = static_cast<double>(fputil::sub_multiply(x, kf, 0x1.0p-7f)); x_hi += 104 << 7; // hi = x_hi >> 7 double exp_hi = EXP_M1[x_hi >> 7]; @@ -174,7 +174,7 @@ LIBC_INLINE float expm1f(float x) { double exp_lo = fputil::polyeval(xd, 0x1.0p0, 0x1.ffffffffff777p-1, 0x1.000000000071cp-1, 0x1.555566668e5e7p-3, 0x1.55555555ef243p-5); - return static_cast<float>(fputil::multiply_add(exp_hi_mid, exp_lo, -1.0)); + return static_cast<float>(fputil::multiply_sub(exp_hi_mid, exp_lo, 1.0)); } } // namespace math diff --git a/libc/src/__support/math/expm1f16.h b/libc/src/__support/math/expm1f16.h index ccc73c88b7f9e8..3c3d49ee990fd5 100644 --- a/libc/src/__support/math/expm1f16.h +++ b/libc/src/__support/math/expm1f16.h @@ -132,7 +132,7 @@ LIBC_INLINE constexpr float16 expm1f16(float16 x) { // exp(x) = exp(hi + mid) * exp(lo) auto [exp_hi_mid, exp_lo] = exp_range_reduction(x); // expm1(x) = exp(hi + mid) * exp(lo) - 1 - return fputil::cast<float16>(fputil::multiply_add(exp_hi_mid, exp_lo, -1.0f)); + return fputil::cast<float16>(fputil::multiply_sub(exp_hi_mid, exp_lo, 1.0f)); } } // namespace math diff --git a/libc/src/__support/math/expxf16_utils.h b/libc/src/__support/math/expxf16_utils.h index 33f66a93df6317..b9852db6b097c3 100644 --- a/libc/src/__support/math/expxf16_utils.h +++ b/libc/src/__support/math/expxf16_utils.h @@ -50,7 +50,7 @@ LIBC_INLINE ExpRangeReduction exp2_range_reduction(float16 x) { unsigned x_hi = static_cast<unsigned>(x_hi_mid) >> 3; unsigned x_mid = static_cast<unsigned>(x_hi_mid) & 0x7; // lo = x - (hi + mid) = round(x * 2^3) * (-2^(-3)) + x - float lo = fputil::multiply_add(kf, -0x1.0p-3f, xf); + float lo = fputil::sub_multiply(xf, kf, 0x1.0p-3f); uint32_t exp2_hi_mid_bits = EXP2_MID_BITS[x_mid] + @@ -154,7 +154,7 @@ LIBC_INLINE constexpr float16 eval_sinh_or_cosh(float16 x) { float exp2_hi_mid_diff = exp2_hi_mid_p - exp2_hi_mid_m; // lo = x - (hi + mid) = round(x * log2(e) * 2^5) * log(2) * (-2^(-5)) + x - float lo = fputil::multiply_add(kf, LOGF_2 * -0x1.0p-5f, xf); + float lo = fputil::sub_multiply(xf, kf, LOGF_2 * 0x1.0p-5f); float lo_sq = lo * lo; // Degree-3 minimax polynomial generated by Sollya with the following diff --git a/libc/src/__support/math/hypot.h b/libc/src/__support/math/hypot.h index 56516de0c80e89..1417420f9c257f 100644 --- a/libc/src/__support/math/hypot.h +++ b/libc/src/__support/math/hypot.h @@ -274,7 +274,7 @@ LIBC_INLINE double hypot(double x, double y) { // Ziv's rounding test. double upper = r_hi + fputil::multiply_add(r_hi, ERR, r_lo); - double lower = r_hi + fputil::multiply_add(r_hi, -ERR, r_lo); + double lower = r_hi + fputil::sub_multiply(r_lo, r_hi, ERR); if (LIBC_LIKELY(upper == lower)) { return upper * scale_back; diff --git a/libc/src/__support/math/hypotf.h b/libc/src/__support/math/hypotf.h index 5d877db190acef..2a9d0012b35939 100644 --- a/libc/src/__support/math/hypotf.h +++ b/libc/src/__support/math/hypotf.h @@ -87,7 +87,7 @@ LIBC_INLINE float hypotf(float x, float y) { double a = y_a_smaller ? x_sq : y_sq; double b = y_a_smaller ? y_sq : x_sq; - double e = b - fputil::multiply_add(r_d, r_d, -a); + double e = b - fputil::multiply_sub(r_d, r_d, a); if (e == 0.0) return r_f; @@ -98,7 +98,7 @@ LIBC_INLINE float hypotf(float x, float y) { // We use FastTwoSum algorithm to compute those errors and then combine. #ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE double sum_sq_lo = (b - (sum_sq - a)); - double err = sum_sq_lo - fputil::multiply_add(r, r, -sum_sq); + double err = sum_sq_lo - fputil::multiply_sub(r, r, sum_sq); #else fputil::DoubleDouble r_sq = fputil::exact_mult(r, r); double sum_sq_lo = (b - (sum_sq - a)); diff --git a/libc/src/__support/math/hypotf16.h b/libc/src/__support/math/hypotf16.h index f93500977f0e7f..6acc7b3cc2d3fd 100644 --- a/libc/src/__support/math/hypotf16.h +++ b/libc/src/__support/math/hypotf16.h @@ -80,7 +80,7 @@ LIBC_INLINE float16 hypotf16(float16 x, float16 y) { // Perform rounding correction. float sum_sq_lo = fputil::multiply_add(bf, bf, a_sq - sum_sq); - float err = sum_sq_lo - fputil::multiply_add(r_d, r_d, -sum_sq); + float err = sum_sq_lo - fputil::multiply_sub(r_d, r_d, sum_sq); if (err > 0) { r_u |= 1; diff --git a/libc/src/__support/math/lgammaf.h b/libc/src/__support/math/lgammaf.h index 9d772916704205..486db3f699f5f5 100644 --- a/libc/src/__support/math/lgammaf.h +++ b/libc/src/__support/math/lgammaf.h @@ -126,7 +126,7 @@ LIBC_INLINE float lgammaf(float x) { constexpr double EULER_GAMMA = 0x1.2788cfc6fb619p-1; double sign_corr = xbits.is_neg() ? EULER_GAMMA : -EULER_GAMMA; return fputil::cast<float>( - fputil::multiply_add(sign_corr, abs_xd, -math::log(abs_xd))); + fputil::multiply_sub(sign_corr, abs_xd, math::log(abs_xd))); } if (x_abs < 0x3f290000u) { @@ -173,7 +173,7 @@ LIBC_INLINE float lgammaf(float x) { double s1625 = fputil::multiply_add(d8, p2425, r1623); double poly_g = fputil::multiply_add(d16, s1625, s015); // lgamma(x) = x*g(x) - log(-x) with single rounding via FMA. - lgamma_val = fputil::multiply_add(xd, poly_g, -math::log(abs_xd)); + lgamma_val = fputil::multiply_sub(xd, poly_g, math::log(abs_xd)); } else { // x = 0x1.f8a754p-9f if (LIBC_UNLIKELY(xbits.uintval() == 0x3b7c53aau)) @@ -209,7 +209,7 @@ LIBC_INLINE float lgammaf(float x) { double s015 = fputil::multiply_add(d8, r815, r07); double poly_h = fputil::multiply_add(d16, p1617, s015); // poly_val - log(abs_xd) with single rounding via FMA. - lgamma_val = fputil::multiply_add(abs_xd, poly_h, -math::log(abs_xd)); + lgamma_val = fputil::multiply_sub(abs_xd, poly_h, math::log(abs_xd)); } } else if (x_abs < 0x3f800000u) { if (xbits.is_neg()) { @@ -218,7 +218,7 @@ LIBC_INLINE float lgammaf(float x) { // exact (Sterbenz); x(x+1) = (t-1)(t-2) is the M2 prefactor. double w = xd * (xd + 1.0); double poly = lgamma_m2_poly(xd + 0.5); - lgamma_val = fputil::multiply_add(w, poly, -lg_ln(-w)); + lgamma_val = fputil::multiply_sub(w, poly, lg_ln(-w)); } else { // M1: t in [0.66015625, 1.0). lgamma(t) = (t-1) * P_M1(d), d = t - MID. // Degree-14 monomial fit of lgamma(t)/(t-1), max error 2^-51.3. @@ -254,7 +254,7 @@ LIBC_INLINE float lgammaf(float x) { // are exact (Sterbenz); (x+1)(x+2) = (t-1)(t-2) is the M2 prefactor. double u = (xd + 1.0) * (xd + 2.0); double poly = lgamma_m2_poly(xd + 1.5); - lgamma_val = fputil::multiply_add(u, poly, -lg_ln(xd * u)); + lgamma_val = fputil::multiply_sub(u, poly, lg_ln(xd * u)); } else { // M2: t in [1.0, 2.0). lgamma(t) = (t-1)*(t-2) * P_M2(t - 1.5). double d = abs_xd - 0x1.8p+0; @@ -376,7 +376,7 @@ LIBC_INLINE float lgammaf(float x) { double pb = (xd + 2.0) * (xd + 3.0); double prod = (pa * pb) * (xd + 4.0); double aprod = prod < 0.0 ? -prod : prod; - lgamma_val = fputil::multiply_add(xd + 3.0, poly, -lg_ln(aprod)); + lgamma_val = fputil::multiply_sub(xd + 3.0, poly, lg_ln(aprod)); } } } else { diff --git a/libc/src/__support/math/log.h b/libc/src/__support/math/log.h index 0ccf347400217b..085bdd9560c7b0 100644 --- a/libc/src/__support/math/log.h +++ b/libc/src/__support/math/log.h @@ -809,7 +809,7 @@ LIBC_INLINE LIBC_CONSTEXPR double log(double x) { // Perform exact range reduction #ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE - u = fputil::multiply_add(r, m, -1.0); // exact + u = fputil::multiply_sub(r, m, 1.0); // exact #else uint64_t c_m = x_m & 0x3FFF'E000'0000'0000ULL; double c = FPBits_t(c_m).get_val(); diff --git a/libc/src/__support/math/log10.h b/libc/src/__support/math/log10.h index db6d345f98b71f..e79fe4cb91364a 100644 --- a/libc/src/__support/math/log10.h +++ b/libc/src/__support/math/log10.h @@ -810,7 +810,7 @@ LIBC_INLINE double log10(double x) { // Perform exact range reduction #ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE - double u = fputil::multiply_add(r, m, -1.0); // exact + double u = fputil::multiply_sub(r, m, 1.0); // exact #else uint64_t c_m = x_m & 0x3FFF'E000'0000'0000ULL; double c = FPBits_t(c_m).get_val(); diff --git a/libc/src/__support/math/log10f16.h b/libc/src/__support/math/log10f16.h index 424cf5f39a0872..1ba920f307f971 100644 --- a/libc/src/__support/math/log10f16.h +++ b/libc/src/__support/math/log10f16.h @@ -163,7 +163,7 @@ LIBC_INLINE float16 log10f16(float16 x) { x_bits.set_biased_exponent(FPBits::EXP_BIAS); float mant_f = x_bits.get_val(); // v = 1.mant * 1/f - 1 = d/f - float v = fputil::multiply_add(mant_f, ONE_OVER_F_F[f], -1.0f); + float v = fputil::multiply_sub(mant_f, ONE_OVER_F_F[f], 1.0f); // Degree-3 minimax polynomial generated by Sollya with the following // commands: diff --git a/libc/src/__support/math/log10f_double_eval.h b/libc/src/__support/math/log10f_double_eval.h index b2695f33dca7eb..278ac1d6ddb61c 100644 --- a/libc/src/__support/math/log10f_double_eval.h +++ b/libc/src/__support/math/log10f_double_eval.h @@ -219,10 +219,10 @@ LIBC_INLINE float log10f(float x) { float u = xbits.get_val(); #ifdef LIBC_TARGET_CPU_HAS_FMA_FLOAT double v = - static_cast<double>(fputil::multiply_add(u, R[index], -1.0f)); // Exact. + static_cast<double>(fputil::multiply_sub(u, R[index], 1.0f)); // Exact. #else - double v = fputil::multiply_add(static_cast<double>(u), - static_cast<double>(R[index]), -1.0); // Exact + double v = fputil::multiply_sub(static_cast<double>(u), + static_cast<double>(R[index]), 1.0); // Exact #endif // LIBC_TARGET_CPU_HAS_FMA_FLOAT double v2 = v * v; // Exact diff --git a/libc/src/__support/math/log10p1bf16.h b/libc/src/__support/math/log10p1bf16.h index cf8aef01a28fd2..0af29eb2f15204 100644 --- a/libc/src/__support/math/log10p1bf16.h +++ b/libc/src/__support/math/log10p1bf16.h @@ -155,7 +155,7 @@ LIBC_INLINE bfloat16 log10p1bf16(bfloat16 x) { float mant_f = y_bits.get_val(); int f = y_bits.get_mantissa() >> (FPBitsFloat::FRACTION_LEN - 5); - float v = fputil::multiply_add(mant_f, ONE_OVER_F_F[f], -1.0f); + float v = fputil::multiply_sub(mant_f, ONE_OVER_F_F[f], 1.0f); // Degree-3 minimax polynomial generated by Sollya with the following // commands: // > display = hexadecimal; diff --git a/libc/src/__support/math/log10p1f16.h b/libc/src/__support/math/log10p1f16.h index 6184a5795741b1..8add8fb1e6a69e 100644 --- a/libc/src/__support/math/log10p1f16.h +++ b/libc/src/__support/math/log10p1f16.h @@ -185,7 +185,7 @@ LIBC_INLINE float16 log10p1f16(float16 x) { int f = y_bits.get_mantissa() >> (FPBitsFloat::FRACTION_LEN - 5); // v = 1.mant * 1/f - 1 = d/f - float v = fputil::multiply_add(mant_f, ONE_OVER_F_F[f], -1.0f); + float v = fputil::multiply_sub(mant_f, ONE_OVER_F_F[f], 1.0f); // Degree-3 minimax polynomial generated by Sollya with the following // commands: diff --git a/libc/src/__support/math/log1p.h b/libc/src/__support/math/log1p.h index 7dd7c66802c837..f1d3aeff8f3556 100644 --- a/libc/src/__support/math/log1p.h +++ b/libc/src/__support/math/log1p.h @@ -1024,7 +1024,7 @@ LIBC_INLINE double log1p(double x) { // Perform exact range reduction #ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE - double v_hi = fputil::multiply_add(r, m_dd.hi, -1.0); // Exact. + double v_hi = fputil::multiply_sub(r, m_dd.hi, 1.0); // Exact. #else // c = 1 + idx * 2^-7. double c = FPBits_t((static_cast<uint64_t>(idx) << (FRACTION_LEN - 7)) + diff --git a/libc/src/__support/math/log2.h b/libc/src/__support/math/log2.h index 36e82e8f1e8dc3..5ceff7f88b846c 100644 --- a/libc/src/__support/math/log2.h +++ b/libc/src/__support/math/log2.h @@ -921,7 +921,7 @@ LIBC_INLINE double log2(double x) { // Perform exact range reduction #ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE - double u = fputil::multiply_add(r, m, -1.0); // exact + double u = fputil::multiply_sub(r, m, 1.0); // exact #else uint64_t c_m = x_m & 0x3FFF'E000'0000'0000ULL; double c = FPBits_t(c_m).get_val(); diff --git a/libc/src/__support/math/log2f16.h b/libc/src/__support/math/log2f16.h index 4a738b5d5b8d45..18e78082fb5648 100644 --- a/libc/src/__support/math/log2f16.h +++ b/libc/src/__support/math/log2f16.h @@ -151,7 +151,7 @@ LIBC_INLINE float16 log2f16(float16 x) { x_bits.set_biased_exponent(FPBits::EXP_BIAS); float mant_f = x_bits.get_val(); // v = 1.mant * 1/f - 1 = d/f - float v = fputil::multiply_add(mant_f, ONE_OVER_F_F[f], -1.0f); + float v = fputil::multiply_sub(mant_f, ONE_OVER_F_F[f], 1.0f); // Degree-3 minimax polynomial generated by Sollya with the following // commands: diff --git a/libc/src/__support/math/log2f_double_eval.h b/libc/src/__support/math/log2f_double_eval.h index 91c6bd6ae87edb..0d7d8b6712d391 100644 --- a/libc/src/__support/math/log2f_double_eval.h +++ b/libc/src/__support/math/log2f_double_eval.h @@ -105,10 +105,10 @@ LIBC_INLINE float log2f(float x) { float u = xbits.get_val(); #ifdef LIBC_TARGET_CPU_HAS_FMA_FLOAT double v = - static_cast<double>(fputil::multiply_add(u, R[index], -1.0f)); // Exact. + static_cast<double>(fputil::multiply_sub(u, R[index], 1.0f)); // Exact. #else double v = - fputil::multiply_add(static_cast<double>(u), RD[index], -1.0); // Exact + fputil::multiply_sub(static_cast<double>(u), RD[index], 1.0); // Exact #endif // LIBC_TARGET_CPU_HAS_FMA_FLOAT double extra_factor = static_cast<double>(m) + LOG2_R[index]; diff --git a/libc/src/__support/math/log2p1f16.h b/libc/src/__support/math/log2p1f16.h index c60c94dee24741..7e884aa0be89ff 100644 --- a/libc/src/__support/math/log2p1f16.h +++ b/libc/src/__support/math/log2p1f16.h @@ -185,7 +185,7 @@ LIBC_INLINE float16 log2p1f16(float16 x) { int f = y_bits.get_mantissa() >> (FPBitsFloat::FRACTION_LEN - 5); // v = 1.mant * 1/f - 1 = d/f - float v = fputil::multiply_add(mant_f, ONE_OVER_F_F[f], -1.0f); + float v = fputil::multiply_sub(mant_f, ONE_OVER_F_F[f], 1.0f); // Degree-3 minimax polynomial generated by Sollya with the following // commands: diff --git a/libc/src/__support/math/logf16.h b/libc/src/__support/math/logf16.h index 8915f60e972a85..6a4218d5c68961 100644 --- a/libc/src/__support/math/logf16.h +++ b/libc/src/__support/math/logf16.h @@ -157,7 +157,7 @@ LIBC_INLINE float16 logf16(float16 x) { x_bits.set_biased_exponent(FPBits::EXP_BIAS); float mant_f = x_bits.get_val(); // v = 1.mant * 1/f - 1 = d/f - float v = fputil::multiply_add(mant_f, ONE_OVER_F_F[f], -1.0f); + float v = fputil::multiply_sub(mant_f, ONE_OVER_F_F[f], 1.0f); // Degree-3 minimax polynomial generated by Sollya with the following // commands: diff --git a/libc/src/__support/math/logf_double_eval.h b/libc/src/__support/math/logf_double_eval.h index 1116aecab747d3..e1cf04bcc32446 100644 --- a/libc/src/__support/math/logf_double_eval.h +++ b/libc/src/__support/math/logf_double_eval.h @@ -172,9 +172,9 @@ LIBC_INLINE float logf(float x) { float u = xbits.get_val(); double v = 0.0; #ifdef LIBC_TARGET_CPU_HAS_FMA_FLOAT - v = static_cast<double>(fputil::multiply_add(u, R[index], -1.0f)); // Exact. + v = static_cast<double>(fputil::multiply_sub(u, R[index], 1.0f)); // Exact. #else - v = fputil::multiply_add(static_cast<double>(u), RD[index], -1.0); // Exact + v = fputil::multiply_sub(static_cast<double>(u), RD[index], 1.0); // Exact #endif // LIBC_TARGET_CPU_HAS_FMA_FLOAT // Degree-5 polynomial approximation of log generated by Sollya with: diff --git a/libc/src/__support/math/pow.h b/libc/src/__support/math/pow.h index 143d58d2bb551a..2ecd9e02091baf 100644 --- a/libc/src/__support/math/pow.h +++ b/libc/src/__support/math/pow.h @@ -218,7 +218,7 @@ LIBC_INLINE double pow(double x, double y) { // Perform exact range reduction. #ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE - double dx = fputil::multiply_add(RD[idx_x], m_x.get_val(), -1.0); // Exact + double dx = fputil::multiply_sub(RD[idx_x], m_x.get_val(), 1.0); // Exact #else double c = FPBits(m_x.uintval() & 0x3fff'e000'0000'0000).get_val(); double dx = diff --git a/libc/src/__support/math/pow_fast.h b/libc/src/__support/math/pow_fast.h index 2ef040c76f75b1..f4a121487f876f 100644 --- a/libc/src/__support/math/pow_fast.h +++ b/libc/src/__support/math/pow_fast.h @@ -111,7 +111,7 @@ LIBC_INLINE double pow(double x, double y) { // Perform exact range reduction. #ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE - double dx = fputil::multiply_add(RD[idx_x], m_x.get_val(), -1.0); // Exact + double dx = fputil::multiply_sub(RD[idx_x], m_x.get_val(), 1.0); // Exact #else double c = FPBits(m_x.uintval() & 0x3fff'e000'0000'0000).get_val(); double dx = diff --git a/libc/src/__support/math/powf_double_eval.h b/libc/src/__support/math/powf_double_eval.h index 7c8b8c57cb965d..cd4951e2fbf75a 100644 --- a/libc/src/__support/math/powf_double_eval.h +++ b/libc/src/__support/math/powf_double_eval.h @@ -48,9 +48,9 @@ LIBC_INLINE double powf_accurate(float x, float y, double e_x, uint64_t sign) { int idx_x = static_cast<int>(x_mant >> (FloatBits::FRACTION_LEN - 7)); // Append hidden bit. float m_x = cpp::bit_cast<float>(x_mant | 0x3f80'0000); - double dx = fputil::multiply_add( + double dx = fputil::multiply_sub( static_cast<double>(m_x), - static_cast<double>(common_constants_internal::R[idx_x]), -1.0); + static_cast<double>(common_constants_internal::R[idx_x]), 1.0); // Degree-11 minimax polynomial for log2(1 + x)/x on [-2^-8, 2^-7] in // double-double, generated by Sollya with: @@ -339,7 +339,7 @@ LIBC_INLINE float powf(float x, float y) { // Since r has <= 7 bits and m_x has 24 bits, m_x * r - 1.0 is exact in // double. double dx = - fputil::multiply_add(m_x, powf_internal::R_32_D[idx_x], -1.0); // Exact + fputil::multiply_sub(m_x, powf_internal::R_32_D[idx_x], 1.0); // Exact // Degree-6 minimax polynomial approximation for log2(1 + dx)/dx on // [-2^-6, 2^-5]. Generated by Sollya with: @@ -378,7 +378,7 @@ LIBC_INLINE float powf(float x, float y) { // y * log2(x) = k + f, with k integer and |f| <= 0.5. double kd = fputil::nearest_integer(z); int64_t k = static_cast<int64_t>(kd); - double f = fputil::multiply_add(y_d, s, -kd); + double f = fputil::multiply_sub(y_d, s, kd); // Degree-7 polynomial approximation P(f) ~ (2^f - 1)/f on [-0.5, 0.5] // Generated by Sollya with: diff --git a/libc/src/__support/math/powf_float_eval.h b/libc/src/__support/math/powf_float_eval.h index d19effd17d0f63..49d15201f14a6c 100644 --- a/libc/src/__support/math/powf_float_eval.h +++ b/libc/src/__support/math/powf_float_eval.h @@ -147,13 +147,13 @@ LIBC_INLINE float powf(float x, float y) { // The computation is exact: m_x = (1 + dx) / r. float r = powf_internal::R_16[idx_x]; #if defined(LIBC_TARGET_CPU_HAS_FMA_FLOAT) - float dx = fputil::multiply_add(r, m_x.get_val(), -1.0f); // Exact + float dx = fputil::multiply_sub(r, m_x.get_val(), 1.0f); // Exact #else // !LIBC_TARGET_CPU_HAS_FMA_FLOAT // Without FMA, split m_x = c + cd where c retains the high 14 bits // to perform exact multiplication: r * c - 1.0f and r * cd. float c = FPBits(m_x.uintval() & 0x3fff'e000U).get_val(); float cd = m_x.get_val() - c; - float dx_hi = fputil::multiply_add(r, c, -1.0f); + float dx_hi = fputil::multiply_sub(r, c, 1.0f); float dx = fputil::multiply_add(r, cd, dx_hi); // Exact #endif // LIBC_TARGET_CPU_HAS_FMA_FLOAT diff --git a/libc/src/__support/math/rsqrtf16.h b/libc/src/__support/math/rsqrtf16.h index 9722975aa15bd6..69dedb49f8f279 100644 --- a/libc/src/__support/math/rsqrtf16.h +++ b/libc/src/__support/math/rsqrtf16.h @@ -304,7 +304,7 @@ LIBC_INLINE constexpr float16 rsqrtf16(float16 x) { if (LIBC_UNLIKELY(half_mantissa == 0x011F)) { result = fputil::multiply_add(result, 0x1.0p-21f, result); } else if (LIBC_UNLIKELY(half_mantissa == 0x0313)) { - result = fputil::multiply_add(result, -0x1.0p-21f, result); + result = fputil::sub_multiply(result, result, 0x1.0p-21f); } return fputil::cast<float16>(result); diff --git a/libc/src/__support/math/sin.h b/libc/src/__support/math/sin.h index 4ae04ae64a423d..cd3111ad21ad9c 100644 --- a/libc/src/__support/math/sin.h +++ b/libc/src/__support/math/sin.h @@ -97,7 +97,7 @@ LIBC_INLINE double sin(double x) { return x + x; // Make sure it works with FTZ/DAZ. #ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE - return fputil::multiply_add(x, -0x1.0p-54, x); + return fputil::sub_multiply(x, x, 0x1.0p-54); #else if (LIBC_UNLIKELY(x_e < 4)) { #ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY @@ -108,7 +108,7 @@ LIBC_INLINE double sin(double x) { return FPBits(xbits.uintval() - 1).get_val(); #endif // !LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY } - return fputil::multiply_add(x, -0x1.0p-54, x); + return fputil::sub_multiply(x, x, 0x1.0p-54); #endif // LIBC_TARGET_CPU_HAS_FMA_DOUBLE } // No range reduction needed. @@ -136,7 +136,7 @@ LIBC_INLINE double sin(double x) { // Overall errors <= ulp(x^3) + |x| * 2^-68. double err = fputil::multiply_add(x_sq, 0x1.0p-52, 0x1.0p-68); double r_lo_u = fputil::multiply_add(x, err, r_lo); - double r_lo_l = fputil::multiply_add(-x, err, r_lo); + double r_lo_l = fputil::sub_multiply(r_lo, x, err); double r_upper = x + r_lo_u; double r_lower = x + r_lo_l; diff --git a/libc/src/__support/math/sincos.h b/libc/src/__support/math/sincos.h index 4d8cf96dc45115..99be10fc156962 100644 --- a/libc/src/__support/math/sincos.h +++ b/libc/src/__support/math/sincos.h @@ -61,8 +61,8 @@ LIBC_INLINE void sincos(double x, double *sin_x, double *cos_x) { // For |x| < 2^-27, max(|sin(x) - x|, |cos(x) - 1|) < ulp(x)/2. #ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE - *sin_x = fputil::multiply_add(x, -0x1.0p-54, x); - *cos_x = fputil::multiply_add(x, -x, 1.0); + *sin_x = fputil::sub_multiply(x, x, 0x1.0p-54); + *cos_x = fputil::sub_multiply(1.0, x, x); #else *cos_x = fputil::round_result_slightly_down(1.0); @@ -75,7 +75,7 @@ LIBC_INLINE void sincos(double x, double *sin_x, double *cos_x) { *sin_x = FPBits(xbits.uintval() - 1).get_val(); #endif // !LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY } - *sin_x = fputil::multiply_add(x, -0x1.0p-54, x); + *sin_x = fputil::sub_multiply(x, x, 0x1.0p-54); #endif // LIBC_TARGET_CPU_HAS_FMA_DOUBLE return; } diff --git a/libc/src/__support/math/sincos_eval.h b/libc/src/__support/math/sincos_eval.h index 4a3dd972861cfb..e33684db08e487 100644 --- a/libc/src/__support/math/sincos_eval.h +++ b/libc/src/__support/math/sincos_eval.h @@ -39,13 +39,13 @@ LIBC_INLINE double sincos_eval(const DoubleDouble &u, DoubleDouble &sin_u, // + u_lo (1 + u_hi^2 * (-1/2 + u_hi^2 / 24)) double u_hi_sq = u.hi * u.hi; // Error < ulp(u_hi^2) < 2^(-6 - 52) = 2^-58. // p1 ~ 1/120 + u_hi^2 / 5040. - double p1 = fputil::multiply_add(u_hi_sq, -0x1.a01a01a01a01ap-13, - 0x1.1111111111111p-7); + double p1 = fputil::sub_multiply(0x1.1111111111111p-7, u_hi_sq, + 0x1.a01a01a01a01ap-13); // q1 ~ -1/2 + u_hi^2 / 24. - double q1 = fputil::multiply_add(u_hi_sq, 0x1.5555555555555p-5, -0x1.0p-1); + double q1 = fputil::multiply_sub(u_hi_sq, 0x1.5555555555555p-5, 0x1.0p-1); double u_hi_3 = u_hi_sq * u.hi; // p2 ~ -1/6 + u_hi^2 (1/120 - u_hi^2 * 1/5040) - double p2 = fputil::multiply_add(u_hi_sq, p1, -0x1.5555555555555p-3); + double p2 = fputil::multiply_sub(u_hi_sq, p1, 0x1.5555555555555p-3); // q2 ~ 1 + u_hi^2 (-1/2 + u_hi^2 / 24) double q2 = fputil::multiply_add(u_hi_sq, q1, 1.0); double sin_lo = fputil::multiply_add(u_hi_3, p2, u.lo * q2); @@ -78,10 +78,10 @@ LIBC_INLINE double sincos_eval(const DoubleDouble &u, DoubleDouble &sin_u, #endif // LIBC_TARGET_CPU_HAS_FMA_DOUBLE // r1 ~ -1/720 + u_hi^2 / 40320 - double r1 = fputil::multiply_add(u_hi_sq, 0x1.a01a01a01a01ap-16, - -0x1.6c16c16c16c17p-10); + double r1 = fputil::multiply_sub(u_hi_sq, 0x1.a01a01a01a01ap-16, + 0x1.6c16c16c16c17p-10); // s1 ~ -1 + u_hi^2 / 6 - double s1 = fputil::multiply_add(u_hi_sq, 0x1.5555555555555p-3, -1.0); + double s1 = fputil::multiply_sub(u_hi_sq, 0x1.5555555555555p-3, 1.0); double u_hi_4 = u_hi_sq * u_hi_sq; double u_hi_u_lo = u.hi * u.lo; // r2 ~ 1/24 + u_hi^2 (-1/720 + u_hi^2 / 40320) diff --git a/libc/src/__support/math/sincosf.h b/libc/src/__support/math/sincosf.h index b5f700011bfd4c..b21b6603e94e78 100644 --- a/libc/src/__support/math/sincosf.h +++ b/libc/src/__support/math/sincosf.h @@ -142,12 +142,12 @@ LIBC_INLINE void sincosf(float x, float *sinp, float *cosp) { // double for intermediate results as it is more efficient than using an // emulated version of FMA. #if defined(LIBC_TARGET_CPU_HAS_FMA_FLOAT) - *sinp = fputil::multiply_add(x, -0x1.0p-25f, x); - *cosp = fputil::multiply_add(FPBits(x_abs).get_val(), -0x1.0p-25f, 1.0f); + *sinp = fputil::sub_multiply(x, x, 0x1.0p-25f); + *cosp = fputil::sub_multiply(1.0f, FPBits(x_abs).get_val(), 0x1.0p-25f); #else - *sinp = static_cast<float>(fputil::multiply_add(xd, -0x1.0p-25, xd)); - *cosp = static_cast<float>(fputil::multiply_add( - static_cast<double>(FPBits(x_abs).get_val()), -0x1.0p-25, 1.0)); + *sinp = static_cast<float>(fputil::sub_multiply(xd, xd, 0x1.0p-25)); + *cosp = static_cast<float>(fputil::sub_multiply( + 1.0, static_cast<double>(FPBits(x_abs).get_val()), 0x1.0p-25)); #endif // LIBC_TARGET_CPU_HAS_FMA_FLOAT return; } @@ -221,8 +221,8 @@ LIBC_INLINE void sincosf(float x, float *sinp, float *cosp) { *sinp = static_cast<float>(fputil::multiply_add( sin_y, cos_k, fputil::multiply_add(cosm1_y, sin_k, sin_k))); - *cosp = static_cast<float>(fputil::multiply_add( - sin_y, -sin_k, fputil::multiply_add(cosm1_y, cos_k, cos_k))); + *cosp = static_cast<float>(fputil::sub_multiply( + fputil::multiply_add(cosm1_y, cos_k, cos_k), sin_y, sin_k)); } } // namespace math diff --git a/libc/src/__support/math/sincosf16_utils.h b/libc/src/__support/math/sincosf16_utils.h index cd91eaf711df29..46c74a78c18bf8 100644 --- a/libc/src/__support/math/sincosf16_utils.h +++ b/libc/src/__support/math/sincosf16_utils.h @@ -44,7 +44,7 @@ LIBC_INLINE_VAR constexpr float SIN_K_PI_OVER_32[64] = { LIBC_INLINE int32_t range_reduction_sincospif16(float x, float &y) { float kf = fputil::nearest_integer(x * 32); - y = fputil::multiply_add(x, 32.0f, -kf); + y = fputil::multiply_sub(x, 32.0f, kf); return static_cast<int32_t>(kf); } diff --git a/libc/src/__support/math/sincosf_float_eval.h b/libc/src/__support/math/sincosf_float_eval.h index d834f3d7cf23f1..4ca48b83f4d32a 100644 --- a/libc/src/__support/math/sincosf_float_eval.h +++ b/libc/src/__support/math/sincosf_float_eval.h @@ -197,9 +197,9 @@ template <bool IS_SIN> LIBC_INLINE float sincosf_eval(float x) { // 0x1.aa8...p-29 // p1 = y^2 * 1/120 - 1/6 - float p1 = fputil::multiply_add(y_sq, 0x1.111112p-7f, -0x1.555556p-3f); + float p1 = fputil::multiply_sub(y_sq, 0x1.111112p-7f, 0x1.555556p-3f); // q1 = y^2 * coeff(Q, 4) + coeff(Q, 2) - float q1 = fputil::multiply_add(y_sq, 0x1.54b8bep-5f, -0x1.ffffc4p-2f); + float q1 = fputil::multiply_sub(y_sq, 0x1.54b8bep-5f, 0x1.ffffc4p-2f); float y3 = y_sq * y; // c1 ~ cos(y) float c1 = fputil::multiply_add(y_sq, q1, 1.0f); @@ -211,7 +211,7 @@ template <bool IS_SIN> LIBC_INLINE float sincosf_eval(float x) { return fputil::multiply_add(sin_k, c1, cos_k * s1); } else { // cos(x) = cos(k * pi/8) * cos(y) - sin(k * pi/8) * sin(y). - return fputil::multiply_add(cos_k, c1, -sin_k * s1); + return fputil::multiply_sub(cos_k, c1, sin_k * s1); } } diff --git a/libc/src/__support/math/sincosf_utils.h b/libc/src/__support/math/sincosf_utils.h index 8fbc8384f9e325..88ee707c2d8232 100644 --- a/libc/src/__support/math/sincosf_utils.h +++ b/libc/src/__support/math/sincosf_utils.h @@ -119,7 +119,7 @@ LIBC_INLINE void sincosf_eval(double xd, uint32_t x_abs, double &sin_k, // => pi * x = (k + y) * pi / 32 LIBC_INLINE int64_t range_reduction_sincospi(double x, double &y) { double kd = fputil::nearest_integer(x * 32); - y = fputil::multiply_add(x, 32.0, -kd); + y = fputil::multiply_sub(x, 32.0, kd); return static_cast<int64_t>(kd); } diff --git a/libc/src/__support/math/sinf_double_eval.h b/libc/src/__support/math/sinf_double_eval.h index 4d498109221955..0ba1769b3f0248 100644 --- a/libc/src/__support/math/sinf_double_eval.h +++ b/libc/src/__support/math/sinf_double_eval.h @@ -113,9 +113,9 @@ LIBC_INLINE float sinf(float x) { // double for intermediate results as it is more efficient than using an // emulated version of FMA. #if defined(LIBC_TARGET_CPU_HAS_FMA_FLOAT) - return fputil::multiply_add(x, -0x1.0p-25f, x); + return fputil::sub_multiply(x, x, 0x1.0p-25f); #else - return static_cast<float>(fputil::multiply_add(xd, -0x1.0p-25, xd)); + return static_cast<float>(fputil::sub_multiply(xd, xd, 0x1.0p-25)); #endif // LIBC_TARGET_CPU_HAS_FMA_FLOAT } diff --git a/libc/src/__support/math/sinhcosh_utils.h b/libc/src/__support/math/sinhcosh_utils.h index 6ca377241d0fb8..33b7258c143fa5 100644 --- a/libc/src/__support/math/sinhcosh_utils.h +++ b/libc/src/__support/math/sinhcosh_utils.h @@ -352,7 +352,7 @@ LIBC_MATH_SMALL_TABLES_INLINE double exp_pm_eval(double x, bool is_sinh) { // even_lo ~ mp_lo + (s.hi * u^2) * P_even(u^2) // poly_lo ~ even_lo + d.hi * odd_lo double mp_lo = fputil::multiply_add(m_p.lo, t1, m_p.lo); - double mm_lo = fputil::multiply_add(-m_m.lo, t1, m_m.lo); + double mm_lo = fputil::sub_multiply(m_m.lo, m_m.lo, t1); double s_u2 = s.hi * u2; double even_lo = fputil::multiply_add(s_u2, p_even, mp_lo); double poly_lo = fputil::multiply_add(d.hi, odd_lo, even_lo); diff --git a/libc/src/__support/math/sinhcoshbf16_utils.h b/libc/src/__support/math/sinhcoshbf16_utils.h index 8e9a687a0579bb..32a1bcc8e0c1df 100644 --- a/libc/src/__support/math/sinhcoshbf16_utils.h +++ b/libc/src/__support/math/sinhcoshbf16_utils.h @@ -114,7 +114,7 @@ template <bool IsSinh> LIBC_INLINE float eval_sinh_or_cosh(float xf) { float exp2_hi_mid_diff = exp2_hi_mid_p - exp2_hi_mid_m; // lo = x - (hi + mid) = round(x * log2(e) * 2^5) * log(2) * (-2^(-5)) + x - float lo = fputil::multiply_add(kf, LOGF_2 * -0x1.0p-5f, xf); + float lo = fputil::sub_multiply(xf, kf, LOGF_2 * 0x1.0p-5f); float lo_sq = lo * lo; // Degree-3 minimax polynomial generated by Sollya with the following @@ -156,7 +156,7 @@ LIBC_INLINE float exp_half(float xf) { float exp2_hi_mid_p = fputil::FPBits<float>(exp2_hi_mid_bits_p).get_val(); // lo = x - (hi + mid) = round(x * log2(e) * 2^5) * log(2) * (-2^(-5)) + x - float lo = fputil::multiply_add(kf, LOGF_2 * -0x1.0p-5f, xf); + float lo = fputil::sub_multiply(xf, kf, LOGF_2 * 0x1.0p-5f); float lo_sq = lo * lo; constexpr cpp::array<float, 4> COEFFS = {0x1p+0f, 0x1p+0f, 0x1.0004p-1f, diff --git a/libc/src/__support/math/tanbf16.h b/libc/src/__support/math/tanbf16.h index 3dae149a4a643f..ee02b51d268653 100644 --- a/libc/src/__support/math/tanbf16.h +++ b/libc/src/__support/math/tanbf16.h @@ -72,8 +72,8 @@ LIBC_INLINE bfloat16 tanbf16(bfloat16 x) { return fputil::cast<bfloat16>( fputil::multiply_add(sin_y, cos_k, fputil::multiply_add(cosm1_y, sin_k, sin_k)) / - fputil::multiply_add(sin_y, -sin_k, - fputil::multiply_add(cosm1_y, cos_k, cos_k))); + fputil::sub_multiply(fputil::multiply_add(cosm1_y, cos_k, cos_k), sin_y, + sin_k)); } } // namespace math diff --git a/libc/src/__support/math/tanf16.h b/libc/src/__support/math/tanf16.h index 761990f73a90b2..6acd09348e0d5d 100644 --- a/libc/src/__support/math/tanf16.h +++ b/libc/src/__support/math/tanf16.h @@ -127,7 +127,7 @@ LIBC_INLINE float16 tanf16(float16 x) { using fputil::multiply_add; return fputil::cast<float16>( multiply_add(sin_y, cos_k, multiply_add(cosm1_y, sin_k, sin_k)) / - multiply_add(sin_y, -sin_k, multiply_add(cosm1_y, cos_k, cos_k))); + fputil::sub_multiply(multiply_add(cosm1_y, cos_k, cos_k), sin_y, sin_k)); } } // namespace math diff --git a/libc/src/__support/math/tanf_double_eval.h b/libc/src/__support/math/tanf_double_eval.h index e5ef67ed0d157c..7f7ae0c65802e9 100644 --- a/libc/src/__support/math/tanf_double_eval.h +++ b/libc/src/__support/math/tanf_double_eval.h @@ -161,7 +161,7 @@ LIBC_INLINE float tanf(float x) { using fputil::multiply_add; return static_cast<float>( multiply_add(sin_y, cos_k, multiply_add(cosm1_y, sin_k, sin_k)) / - multiply_add(sin_y, -sin_k, multiply_add(cosm1_y, cos_k, cos_k))); + fputil::sub_multiply(multiply_add(cosm1_y, cos_k, cos_k), sin_y, sin_k)); } } // namespace double_eval diff --git a/libc/src/__support/math/tanf_float_eval.h b/libc/src/__support/math/tanf_float_eval.h index 27f49c8e35bb6d..bcb215f636ef5e 100644 --- a/libc/src/__support/math/tanf_float_eval.h +++ b/libc/src/__support/math/tanf_float_eval.h @@ -95,7 +95,7 @@ LIBC_INLINE float tanf(float x) { // exactly 0 respectively, so the remaining term is computed with a small // relative error. float num = fputil::multiply_add(cos_k, t, sin_k); - float den = fputil::multiply_add(-sin_k, t, cos_k); + float den = fputil::sub_multiply(cos_k, sin_k, t); return num / den; } diff --git a/libc/src/__support/math/tanh_double_eval.h b/libc/src/__support/math/tanh_double_eval.h index 3c3b015030e568..7a17048f3d774f 100644 --- a/libc/src/__support/math/tanh_double_eval.h +++ b/libc/src/__support/math/tanh_double_eval.h @@ -338,8 +338,8 @@ LIBC_MATH_SMALL_TABLES_INLINE double tanh_eval(double x) { double y_hi = 1.0 / d.hi; double q_hi = two_m_hi * y_hi; #ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE - double rem_hi = fputil::multiply_add(-q_hi, d.hi, two_m_hi); - double rem_lo = fputil::multiply_add(-q_hi, d_lo, two_m_lo); + double rem_hi = fputil::sub_multiply(two_m_hi, q_hi, d.hi); + double rem_lo = fputil::sub_multiply(two_m_lo, q_hi, d_lo); #else // Split q_hi into 26 + 27 bits and d.hi into 27 + 26 bits via bit-masks so // that q_hi_hi * d_hi_hi (53 bits) and q_hi_hi * d_hi_lo (52 bits) are exact @@ -427,11 +427,11 @@ LIBC_INLINE double tanh_small_eval(double x, uint64_t x_abs) { if (LIBC_UNLIKELY(x_abs == 0)) return x; #ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE - double res = fputil::multiply_add(x, -0x1.0p-55, x); + double res = fputil::sub_multiply(x, x, 0x1.0p-55); #else // Avoid spurious FE_UNDERFLOW on x * -2^-55 when 2^-1022 < |x| < 2^-967, // and preserve directed rounding for subnormal |x| <= 2^-1022: - double x_scaled = fputil::multiply_add(x, 0x1.0p55, -x); + double x_scaled = fputil::multiply_sub(x, 0x1.0p55, x); double res = x_scaled * 0x1.0p-55; #endif // LIBC_TARGET_CPU_HAS_FMA_DOUBLE if (LIBC_UNLIKELY(x_abs <= 0x0010'0000'0000'0000ULL)) { @@ -485,7 +485,7 @@ LIBC_INLINE double tanh_small_eval(double x, uint64_t x_abs) { // The error of y_hi + y_lo is bounded by |y_hi| * 0x1.8p-54, and the rounding // error of r.lo + y_lo is bounded by |x| * 2^-103. double x_err = x * 0x1.0p-103; - double err = fputil::multiply_add(y_hi, -0x1.8p-54, x_err); + double err = fputil::sub_multiply(x_err, y_hi, 0x1.8p-54); // Low tail bounds lo_{u,l} ~ r.lo + (y_lo +/- err): double y_lo_u = y_lo + err; diff --git a/libc/src/__support/math/tanhf.h b/libc/src/__support/math/tanhf.h index 28d1c60a6912d1..9735750b2081b1 100644 --- a/libc/src/__support/math/tanhf.h +++ b/libc/src/__support/math/tanhf.h @@ -86,7 +86,7 @@ LIBC_INLINE float tanhf(float x) { #else const double half_way = xbits.is_neg() ? 0.5 : -0.5; - mk = static_cast<int>(fputil::multiply_add(xd, -LOG2_E_EXP2_6, half_way)); + mk = static_cast<int>(fputil::sub_multiply(half_way, xd, LOG2_E_EXP2_6)); k = static_cast<double>(-mk); #endif // LIBC_TARGET_CPU_HAS_NEAREST_INT // -hi = floor(-k * 2^(-MID_BITS)) diff --git a/libc/src/__support/math/tanhf16.h b/libc/src/__support/math/tanhf16.h index 768ec6845fe946..e483d2a57189e5 100644 --- a/libc/src/__support/math/tanhf16.h +++ b/libc/src/__support/math/tanhf16.h @@ -149,7 +149,7 @@ LIBC_INLINE float16 tanhf16(float16 x) { unsigned x_mid = static_cast<unsigned>(x_hi_mid) & 0x1f; // lo = x - (hi + mid) // = round(x * log2(e) * 2 * 2^5) * log(2) * 0.5 * (-2^(-5)) + x - float lo = fputil::multiply_add(kf, LOGF_2 * 0.5f * -0x1.0p-5f, xf); + float lo = fputil::sub_multiply(xf, kf, LOGF_2 * 0.5f * 0x1.0p-5f); uint32_t exp2_hi_mid_bits = EXP2_MID_5_BITS[x_mid] + diff --git a/libc/src/__support/math/tanpif.h b/libc/src/__support/math/tanpif.h index 722189e7c57c3a..efbe87ad57984c 100644 --- a/libc/src/__support/math/tanpif.h +++ b/libc/src/__support/math/tanpif.h @@ -76,8 +76,8 @@ LIBC_INLINE float tanpif(float x) { // Odd multiples of 1/4 have exact results of +1 or -1. double xd = static_cast<double>(x); if (LIBC_UNLIKELY(fputil::is_odd_integer(4.0 * xd))) - return fputil::is_odd_integer(fputil::multiply_add(2.0, xd, -0.5)) ? -1.0f - : 1.0f; + return fputil::is_odd_integer(fputil::multiply_sub(2.0, xd, 0.5)) ? -1.0f + : 1.0f; #ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS // Handle exceptional values. @@ -119,7 +119,7 @@ LIBC_INLINE float tanpif(float x) { using fputil::multiply_add; return fputil::cast<float>( multiply_add(sin_y, cos_k, multiply_add(cosm1_y, sin_k, sin_k)) / - multiply_add(sin_y, -sin_k, multiply_add(cosm1_y, cos_k, cos_k))); + fputil::sub_multiply(multiply_add(cosm1_y, cos_k, cos_k), sin_y, sin_k)); } } // namespace math diff --git a/libc/src/__support/math/tanpif16.h b/libc/src/__support/math/tanpif16.h index db317ee9d3fbd3..5fb1e204c4cb47 100644 --- a/libc/src/__support/math/tanpif16.h +++ b/libc/src/__support/math/tanpif16.h @@ -68,7 +68,7 @@ LIBC_INLINE float16 tanpif16(float16 x) { // Odd multiples of 1/4 have exact results of +1 or -1. float xf = x; if (LIBC_UNLIKELY(fputil::is_odd_integer(4.0f * xf))) - return fputil::is_odd_integer(fputil::multiply_add(2.0f, xf, -0.5f)) + return fputil::is_odd_integer(fputil::multiply_sub(2.0f, xf, 0.5f)) ? -1.0f16 : 1.0f16; @@ -129,7 +129,7 @@ LIBC_INLINE float16 tanpif16(float16 x) { using fputil::multiply_add; return fputil::cast<float16>( multiply_add(sin_y, cos_k, multiply_add(cosm1_y, sin_k, sin_k)) / - multiply_add(sin_y, -sin_k, multiply_add(cosm1_y, cos_k, cos_k))); + fputil::sub_multiply(multiply_add(cosm1_y, cos_k, cos_k), sin_y, sin_k)); } } // namespace math _______________________________________________ llvm-branch-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
