| |
| |
| |
| |
| |
| |
| |
|
|
| #include "src/math/cos.h" |
| #include "hdr/errno_macros.h" |
| #include "src/__support/FPUtil/FEnvImpl.h" |
| #include "src/__support/FPUtil/FPBits.h" |
| #include "src/__support/FPUtil/double_double.h" |
| #include "src/__support/FPUtil/dyadic_float.h" |
| #include "src/__support/FPUtil/except_value_utils.h" |
| #include "src/__support/common.h" |
| #include "src/__support/macros/config.h" |
| #include "src/__support/macros/optimization.h" |
| #include "src/__support/macros/properties/cpu_features.h" |
| #include "src/math/generic/range_reduction_double_common.h" |
| #include "src/math/generic/sincos_eval.h" |
|
|
| #ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE |
| #include "range_reduction_double_fma.h" |
| #else |
| #include "range_reduction_double_nofma.h" |
| #endif |
|
|
| namespace LIBC_NAMESPACE_DECL { |
|
|
| using DoubleDouble = fputil::DoubleDouble; |
| using Float128 = typename fputil::DyadicFloat<128>; |
|
|
| LLVM_LIBC_FUNCTION(double, cos, (double x)) { |
| using FPBits = typename fputil::FPBits<double>; |
| FPBits xbits(x); |
|
|
| uint16_t x_e = xbits.get_biased_exponent(); |
|
|
| DoubleDouble y; |
| unsigned k; |
| LargeRangeReduction range_reduction_large{}; |
|
|
| |
| if (LIBC_LIKELY(x_e < FPBits::EXP_BIAS + FAST_PASS_EXPONENT)) { |
| |
| if (LIBC_UNLIKELY(x_e < FPBits::EXP_BIAS - 7)) { |
| |
| if (LIBC_UNLIKELY(x_e < FPBits::EXP_BIAS - 27)) { |
| |
| if (LIBC_UNLIKELY(x == 0.0)) |
| return 1.0; |
|
|
| |
| return fputil::round_result_slightly_down(1.0); |
| } |
| |
| k = 0; |
| y.lo = 0.0; |
| y.hi = x; |
| } else { |
| |
| k = range_reduction_small(x, y); |
| } |
| } else { |
| |
| if (LIBC_UNLIKELY(x_e > 2 * FPBits::EXP_BIAS)) { |
| if (xbits.is_signaling_nan()) { |
| fputil::raise_except_if_required(FE_INVALID); |
| return FPBits::quiet_nan().get_val(); |
| } |
| |
| if (xbits.get_mantissa() == 0) { |
| fputil::set_errno_if_required(EDOM); |
| fputil::raise_except_if_required(FE_INVALID); |
| } |
| return x + FPBits::quiet_nan().get_val(); |
| } |
|
|
| |
| k = range_reduction_large.fast(x, y); |
| } |
|
|
| DoubleDouble sin_y, cos_y; |
|
|
| [[maybe_unused]] double err = generic::sincos_eval(y, sin_y, cos_y); |
|
|
| |
| #ifdef LIBC_MATH_HAS_SMALL_TABLES |
| |
| auto get_idx_dd = [](unsigned kk) -> DoubleDouble { |
| unsigned idx = (kk & 64) ? 64 - (kk & 63) : (kk & 63); |
| DoubleDouble ans = SIN_K_PI_OVER_128[idx]; |
| if (kk & 128) { |
| ans.hi = -ans.hi; |
| ans.lo = -ans.lo; |
| } |
| return ans; |
| }; |
| DoubleDouble msin_k = get_idx_dd(k + 128); |
| DoubleDouble cos_k = get_idx_dd(k + 64); |
| #else |
| |
| |
| |
| DoubleDouble msin_k = SIN_K_PI_OVER_128[(k + 128) & 255]; |
| DoubleDouble cos_k = SIN_K_PI_OVER_128[(k + 64) & 255]; |
| #endif |
|
|
| |
| |
| |
| |
| DoubleDouble cos_k_cos_y = fputil::quick_mult(cos_y, cos_k); |
| DoubleDouble msin_k_sin_y = fputil::quick_mult(sin_y, msin_k); |
|
|
| DoubleDouble rr = fputil::exact_add<false>(cos_k_cos_y.hi, msin_k_sin_y.hi); |
| rr.lo += msin_k_sin_y.lo + cos_k_cos_y.lo; |
|
|
| #ifdef LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
| return rr.hi + rr.lo; |
| #else |
|
|
| double rlp = rr.lo + err; |
| double rlm = rr.lo - err; |
|
|
| double r_upper = rr.hi + rlp; |
| double r_lower = rr.hi + rlm; |
|
|
| |
| if (LIBC_LIKELY(r_upper == r_lower)) |
| return r_upper; |
|
|
| Float128 u_f128, sin_u, cos_u; |
| if (LIBC_LIKELY(x_e < FPBits::EXP_BIAS + FAST_PASS_EXPONENT)) |
| u_f128 = range_reduction_small_f128(x); |
| else |
| u_f128 = range_reduction_large.accurate(); |
|
|
| generic::sincos_eval(u_f128, sin_u, cos_u); |
|
|
| auto get_sin_k = [](unsigned kk) -> Float128 { |
| unsigned idx = (kk & 64) ? 64 - (kk & 63) : (kk & 63); |
| Float128 ans = SIN_K_PI_OVER_128_F128[idx]; |
| if (kk & 128) |
| ans.sign = Sign::NEG; |
| return ans; |
| }; |
|
|
| |
| |
| Float128 msin_k_f128 = get_sin_k(k + 128); |
| Float128 cos_k_f128 = get_sin_k(k + 64); |
|
|
| |
| |
| Float128 r = fputil::quick_add(fputil::quick_mul(cos_k_f128, cos_u), |
| fputil::quick_mul(msin_k_f128, sin_u)); |
|
|
| |
| |
|
|
| return static_cast<double>(r); |
| #endif |
| } |
|
|
| } |
|
|