brintos

brintos / llvm-project-archived public Read only

0
0
Text · 6.0 KiB · 1b6310f Raw
181 lines · cpp
1//===-- Double-precision sin 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/sin.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/multiply_add.h"16#include "src/__support/FPUtil/rounding_mode.h"17#include "src/__support/common.h"18#include "src/__support/macros/config.h"19#include "src/__support/macros/optimization.h"            // LIBC_UNLIKELY20#include "src/__support/macros/properties/cpu_features.h" // LIBC_TARGET_CPU_HAS_FMA21#include "src/__support/math/range_reduction_double_common.h"22#include "src/__support/math/sincos_eval.h"23 24#ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE25#include "src/__support/math/range_reduction_double_fma.h"26#else27#include "src/__support/math/range_reduction_double_nofma.h"28#endif // LIBC_TARGET_CPU_HAS_FMA_DOUBLE29 30namespace LIBC_NAMESPACE_DECL {31 32using DoubleDouble = fputil::DoubleDouble;33using Float128 = typename fputil::DyadicFloat<128>;34 35LLVM_LIBC_FUNCTION(double, sin, (double x)) {36  using namespace math::range_reduction_double_internal;37  using FPBits = typename fputil::FPBits<double>;38  FPBits xbits(x);39 40  uint16_t x_e = xbits.get_biased_exponent();41 42  DoubleDouble y;43  unsigned k;44  LargeRangeReduction range_reduction_large{};45 46  // |x| < 2^1647  if (LIBC_LIKELY(x_e < FPBits::EXP_BIAS + FAST_PASS_EXPONENT)) {48    // |x| < 2^-749    if (LIBC_UNLIKELY(x_e < FPBits::EXP_BIAS - 7)) {50      // |x| < 2^-26, |sin(x) - x| < ulp(x)/2.51      if (LIBC_UNLIKELY(x_e < FPBits::EXP_BIAS - 26)) {52        // Signed zeros.53        if (LIBC_UNLIKELY(x == 0.0))54          return x + x; // Make sure it works with FTZ/DAZ.55 56#ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE57        return fputil::multiply_add(x, -0x1.0p-54, x);58#else59        if (LIBC_UNLIKELY(x_e < 4)) {60          int rounding_mode = fputil::quick_get_round();61          if (rounding_mode == FE_TOWARDZERO ||62              (xbits.sign() == Sign::POS && rounding_mode == FE_DOWNWARD) ||63              (xbits.sign() == Sign::NEG && rounding_mode == FE_UPWARD))64            return FPBits(xbits.uintval() - 1).get_val();65        }66        return fputil::multiply_add(x, -0x1.0p-54, x);67#endif // LIBC_TARGET_CPU_HAS_FMA_DOUBLE68      }69      // No range reduction needed.70      k = 0;71      y.lo = 0.0;72      y.hi = x;73    } else {74      // Small range reduction.75      k = range_reduction_small(x, y);76    }77  } else {78    // Inf or NaN79    if (LIBC_UNLIKELY(x_e > 2 * FPBits::EXP_BIAS)) {80      // sin(+-Inf) = NaN81      if (xbits.is_signaling_nan()) {82        fputil::raise_except_if_required(FE_INVALID);83        return FPBits::quiet_nan().get_val();84      }85 86      if (xbits.get_mantissa() == 0) {87        fputil::set_errno_if_required(EDOM);88        fputil::raise_except_if_required(FE_INVALID);89      }90      return x + FPBits::quiet_nan().get_val();91    }92 93    // Large range reduction.94    k = range_reduction_large.fast(x, y);95  }96 97  DoubleDouble sin_y, cos_y;98 99  [[maybe_unused]] double err =100      math::sincos_eval_internal::sincos_eval(y, sin_y, cos_y);101 102  // Look up sin(k * pi/128) and cos(k * pi/128)103#ifdef LIBC_MATH_HAS_SMALL_TABLES104  // Memory saving versions.  Use 65-entry table.105  auto get_idx_dd = [](unsigned kk) -> DoubleDouble {106    unsigned idx = (kk & 64) ? 64 - (kk & 63) : (kk & 63);107    DoubleDouble ans = SIN_K_PI_OVER_128[idx];108    if (kk & 128) {109      ans.hi = -ans.hi;110      ans.lo = -ans.lo;111    }112    return ans;113  };114  DoubleDouble sin_k = get_idx_dd(k);115  DoubleDouble cos_k = get_idx_dd(k + 64);116#else117  // Fast look up version, but needs 256-entry table.118  // cos(k * pi/128) = sin(k * pi/128 + pi/2) = sin((k + 64) * pi/128).119  DoubleDouble sin_k = SIN_K_PI_OVER_128[k & 255];120  DoubleDouble cos_k = SIN_K_PI_OVER_128[(k + 64) & 255];121#endif122 123  // After range reduction, k = round(x * 128 / pi) and y = x - k * (pi / 128).124  // So k is an integer and -pi / 256 <= y <= pi / 256.125  // Then sin(x) = sin((k * pi/128 + y)126  //             = sin(y) * cos(k*pi/128) + cos(y) * sin(k*pi/128)127  DoubleDouble sin_k_cos_y = fputil::quick_mult(cos_y, sin_k);128  DoubleDouble cos_k_sin_y = fputil::quick_mult(sin_y, cos_k);129 130  DoubleDouble rr = fputil::exact_add<false>(sin_k_cos_y.hi, cos_k_sin_y.hi);131  rr.lo += sin_k_cos_y.lo + cos_k_sin_y.lo;132 133#ifdef LIBC_MATH_HAS_SKIP_ACCURATE_PASS134  return rr.hi + rr.lo;135#else136  // Accurate test and pass for correctly rounded implementation.137 138  double rlp = rr.lo + err;139  double rlm = rr.lo - err;140 141  double r_upper = rr.hi + rlp; // (rr.lo + ERR);142  double r_lower = rr.hi + rlm; // (rr.lo - ERR);143 144  // Ziv's rounding test.145  if (LIBC_LIKELY(r_upper == r_lower))146    return r_upper;147 148  Float128 u_f128, sin_u, cos_u;149  if (LIBC_LIKELY(x_e < FPBits::EXP_BIAS + FAST_PASS_EXPONENT))150    u_f128 = range_reduction_small_f128(x);151  else152    u_f128 = range_reduction_large.accurate();153 154  math::sincos_eval_internal::sincos_eval(u_f128, sin_u, cos_u);155 156  auto get_sin_k = [](unsigned kk) -> Float128 {157    unsigned idx = (kk & 64) ? 64 - (kk & 63) : (kk & 63);158    Float128 ans = SIN_K_PI_OVER_128_F128[idx];159    if (kk & 128)160      ans.sign = Sign::NEG;161    return ans;162  };163 164  // cos(k * pi/128) = sin(k * pi/128 + pi/2) = sin((k + 64) * pi/128).165  Float128 sin_k_f128 = get_sin_k(k);166  Float128 cos_k_f128 = get_sin_k(k + 64);167 168  // sin(x) = sin(k * pi/128 + u)169  //        = sin(u) * cos(k*pi/128) + cos(u) * sin(k*pi/128)170  Float128 r = fputil::quick_add(fputil::quick_mul(sin_k_f128, cos_u),171                                 fputil::quick_mul(cos_k_f128, sin_u));172 173  // TODO: Add assertion if Ziv's accuracy tests fail in debug mode.174  // https://github.com/llvm/llvm-project/issues/96452.175 176  return static_cast<double>(r);177#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS178}179 180} // namespace LIBC_NAMESPACE_DECL181