224 lines · cpp
1//===-- Double-precision sincos function ----------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#include "src/math/sincos.h"10#include "hdr/errno_macros.h"11#include "src/__support/FPUtil/FEnvImpl.h"12#include "src/__support/FPUtil/FPBits.h"13#include "src/__support/FPUtil/double_double.h"14#include "src/__support/FPUtil/dyadic_float.h"15#include "src/__support/FPUtil/except_value_utils.h"16#include "src/__support/FPUtil/multiply_add.h"17#include "src/__support/FPUtil/rounding_mode.h"18#include "src/__support/common.h"19#include "src/__support/macros/config.h"20#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY21#include "src/__support/macros/properties/cpu_features.h" // LIBC_TARGET_CPU_HAS_FMA22#include "src/__support/math/range_reduction_double_common.h"23#include "src/__support/math/sincos_eval.h"24 25#ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE26#include "src/__support/math/range_reduction_double_fma.h"27#else28#include "src/__support/math/range_reduction_double_nofma.h"29#endif // LIBC_TARGET_CPU_HAS_FMA_DOUBLE30 31namespace LIBC_NAMESPACE_DECL {32 33using DoubleDouble = fputil::DoubleDouble;34using Float128 = typename fputil::DyadicFloat<128>;35 36LLVM_LIBC_FUNCTION(void, sincos, (double x, double *sin_x, double *cos_x)) {37 using namespace math::range_reduction_double_internal;38 using FPBits = typename fputil::FPBits<double>;39 FPBits xbits(x);40 41 uint16_t x_e = xbits.get_biased_exponent();42 43 DoubleDouble y;44 unsigned k;45 LargeRangeReduction range_reduction_large{};46 47 // |x| < 2^1648 if (LIBC_LIKELY(x_e < FPBits::EXP_BIAS + FAST_PASS_EXPONENT)) {49 // |x| < 2^-750 if (LIBC_UNLIKELY(x_e < FPBits::EXP_BIAS - 7)) {51 // |x| < 2^-2752 if (LIBC_UNLIKELY(x_e < FPBits::EXP_BIAS - 27)) {53 // Signed zeros.54 if (LIBC_UNLIKELY(x == 0.0)) {55 *sin_x = x;56 *cos_x = 1.0;57 return;58 }59 60 // For |x| < 2^-27, max(|sin(x) - x|, |cos(x) - 1|) < ulp(x)/2.61#ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE62 *sin_x = fputil::multiply_add(x, -0x1.0p-54, x);63 *cos_x = fputil::multiply_add(x, -x, 1.0);64#else65 *cos_x = fputil::round_result_slightly_down(1.0);66 67 if (LIBC_UNLIKELY(x_e < 4)) {68 int rounding_mode = fputil::quick_get_round();69 if (rounding_mode == FE_TOWARDZERO ||70 (xbits.sign() == Sign::POS && rounding_mode == FE_DOWNWARD) ||71 (xbits.sign() == Sign::NEG && rounding_mode == FE_UPWARD))72 *sin_x = FPBits(xbits.uintval() - 1).get_val();73 }74 *sin_x = fputil::multiply_add(x, -0x1.0p-54, x);75#endif // LIBC_TARGET_CPU_HAS_FMA_DOUBLE76 return;77 }78 // No range reduction needed.79 k = 0;80 y.lo = 0.0;81 y.hi = x;82 } else {83 // Small range reduction.84 k = range_reduction_small(x, y);85 }86 } else {87 // Inf or NaN88 if (LIBC_UNLIKELY(x_e > 2 * FPBits::EXP_BIAS)) {89 if (xbits.is_signaling_nan()) {90 fputil::raise_except_if_required(FE_INVALID);91 *sin_x = *cos_x = FPBits::quiet_nan().get_val();92 return;93 }94 95 // sin(+-Inf) = NaN96 if (xbits.get_mantissa() == 0) {97 fputil::set_errno_if_required(EDOM);98 fputil::raise_except_if_required(FE_INVALID);99 }100 *sin_x = *cos_x = x + FPBits::quiet_nan().get_val();101 return;102 }103 104 // Large range reduction.105 k = range_reduction_large.fast(x, y);106 }107 108 DoubleDouble sin_y, cos_y;109 110 [[maybe_unused]] double err =111 math::sincos_eval_internal::sincos_eval(y, sin_y, cos_y);112 113 // Look up sin(k * pi/128) and cos(k * pi/128)114#ifdef LIBC_MATH_HAS_SMALL_TABLES115 // Memory saving versions. Use 65-entry table.116 auto get_idx_dd = [](unsigned kk) -> DoubleDouble {117 unsigned idx = (kk & 64) ? 64 - (kk & 63) : (kk & 63);118 DoubleDouble ans = SIN_K_PI_OVER_128[idx];119 if (kk & 128) {120 ans.hi = -ans.hi;121 ans.lo = -ans.lo;122 }123 return ans;124 };125 DoubleDouble sin_k = get_idx_dd(k);126 DoubleDouble cos_k = get_idx_dd(k + 64);127#else128 // Fast look up version, but needs 256-entry table.129 // cos(k * pi/128) = sin(k * pi/128 + pi/2) = sin((k + 64) * pi/128).130 DoubleDouble sin_k = SIN_K_PI_OVER_128[k & 255];131 DoubleDouble cos_k = SIN_K_PI_OVER_128[(k + 64) & 255];132#endif // LIBC_MATH_HAS_SMALL_TABLES133 134 DoubleDouble msin_k{-sin_k.lo, -sin_k.hi};135 136 // After range reduction, k = round(x * 128 / pi) and y = x - k * (pi / 128).137 // So k is an integer and -pi / 256 <= y <= pi / 256.138 // Then sin(x) = sin((k * pi/128 + y)139 // = sin(y) * cos(k*pi/128) + cos(y) * sin(k*pi/128)140 DoubleDouble sin_k_cos_y = fputil::quick_mult(cos_y, sin_k);141 DoubleDouble cos_k_sin_y = fputil::quick_mult(sin_y, cos_k);142 // cos(x) = cos((k * pi/128 + y)143 // = cos(y) * cos(k*pi/128) - sin(y) * sin(k*pi/128)144 DoubleDouble cos_k_cos_y = fputil::quick_mult(cos_y, cos_k);145 DoubleDouble msin_k_sin_y = fputil::quick_mult(sin_y, msin_k);146 147 DoubleDouble sin_dd =148 fputil::exact_add<false>(sin_k_cos_y.hi, cos_k_sin_y.hi);149 DoubleDouble cos_dd =150 fputil::exact_add<false>(cos_k_cos_y.hi, msin_k_sin_y.hi);151 sin_dd.lo += sin_k_cos_y.lo + cos_k_sin_y.lo;152 cos_dd.lo += msin_k_sin_y.lo + cos_k_cos_y.lo;153 154#ifdef LIBC_MATH_HAS_SKIP_ACCURATE_PASS155 *sin_x = sin_dd.hi + sin_dd.lo;156 *cos_x = cos_dd.hi + cos_dd.lo;157 return;158#else159 // Accurate test and pass for correctly rounded implementation.160 161 double sin_lp = sin_dd.lo + err;162 double sin_lm = sin_dd.lo - err;163 double cos_lp = cos_dd.lo + err;164 double cos_lm = cos_dd.lo - err;165 166 double sin_upper = sin_dd.hi + sin_lp;167 double sin_lower = sin_dd.hi + sin_lm;168 double cos_upper = cos_dd.hi + cos_lp;169 double cos_lower = cos_dd.hi + cos_lm;170 171 // Ziv's rounding test.172 if (LIBC_LIKELY(sin_upper == sin_lower && cos_upper == cos_lower)) {173 *sin_x = sin_upper;174 *cos_x = cos_upper;175 return;176 }177 178 Float128 u_f128, sin_u, cos_u;179 if (LIBC_LIKELY(x_e < FPBits::EXP_BIAS + FAST_PASS_EXPONENT))180 u_f128 = range_reduction_small_f128(x);181 else182 u_f128 = range_reduction_large.accurate();183 184 math::sincos_eval_internal::sincos_eval(u_f128, sin_u, cos_u);185 186 auto get_sin_k = [](unsigned kk) -> Float128 {187 unsigned idx = (kk & 64) ? 64 - (kk & 63) : (kk & 63);188 Float128 ans = SIN_K_PI_OVER_128_F128[idx];189 if (kk & 128)190 ans.sign = Sign::NEG;191 return ans;192 };193 194 // cos(k * pi/128) = sin(k * pi/128 + pi/2) = sin((k + 64) * pi/128).195 Float128 sin_k_f128 = get_sin_k(k);196 Float128 cos_k_f128 = get_sin_k(k + 64);197 Float128 msin_k_f128 = get_sin_k(k + 128);198 199 // TODO: Add assertion if Ziv's accuracy tests fail in debug mode.200 // https://github.com/llvm/llvm-project/issues/96452.201 202 if (sin_upper == sin_lower)203 *sin_x = sin_upper;204 else205 // sin(x) = sin((k * pi/128 + u)206 // = sin(u) * cos(k*pi/128) + cos(u) * sin(k*pi/128)207 *sin_x = static_cast<double>(208 fputil::quick_add(fputil::quick_mul(sin_k_f128, cos_u),209 fputil::quick_mul(cos_k_f128, sin_u)));210 211 if (cos_upper == cos_lower)212 *cos_x = cos_upper;213 else214 // cos(x) = cos((k * pi/128 + u)215 // = cos(u) * cos(k*pi/128) - sin(u) * sin(k*pi/128)216 *cos_x = static_cast<double>(217 fputil::quick_add(fputil::quick_mul(cos_k_f128, cos_u),218 fputil::quick_mul(msin_k_f128, sin_u)));219 220#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS221}222 223} // namespace LIBC_NAMESPACE_DECL224