brintos

brintos / llvm-project-archived public Read only

0
0
Text · 6.7 KiB · 48ba71a Raw
175 lines · c
1//===-- Implementation header for cosf --------------------------*- C++ -*-===//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#ifndef LIBC_SRC___SUPPORT_MATH_COSF_H10#define LIBC_SRC___SUPPORT_MATH_COSF_H11 12#include "src/__support/FPUtil/FEnvImpl.h"13#include "src/__support/FPUtil/FPBits.h"14#include "src/__support/FPUtil/except_value_utils.h"15#include "src/__support/FPUtil/multiply_add.h"16#include "src/__support/macros/config.h"17#include "src/__support/macros/optimization.h"            // LIBC_UNLIKELY18#include "src/__support/macros/properties/cpu_features.h" // LIBC_TARGET_CPU_HAS_FMA19 20#if defined(LIBC_MATH_HAS_SKIP_ACCURATE_PASS) &&                               \21    defined(LIBC_MATH_HAS_INTERMEDIATE_COMP_IN_FLOAT) &&                       \22    defined(LIBC_TARGET_CPU_HAS_FMA_FLOAT)23 24#include "sincosf_float_eval.h"25 26namespace LIBC_NAMESPACE_DECL {27namespace math {28 29LIBC_INLINE static constexpr float cosf(float x) {30  return sincosf_float_eval::sincosf_eval</*IS_SIN*/ false>(x);31}32 33} // namespace math34} // namespace LIBC_NAMESPACE_DECL35 36#else // !LIBC_MATH_HAS_INTERMEDIATE_COMP_IN_FLOAT37 38#include "sincosf_utils.h"39 40namespace LIBC_NAMESPACE_DECL {41 42namespace math {43 44LIBC_INLINE static constexpr float cosf(float x) {45 46#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS47  // Exceptional cases for cosf.48  constexpr size_t N_EXCEPTS = 6;49 50  constexpr fputil::ExceptValues<float, N_EXCEPTS> COSF_EXCEPTS{{51      // (inputs, RZ output, RU offset, RD offset, RN offset)52      // x = 0x1.64a032p43, cos(x) = 0x1.9d4ba4p-1 (RZ)53      {0x55325019, 0x3f4ea5d2, 1, 0, 0},54      // x = 0x1.4555p51, cos(x) = 0x1.115d7cp-1 (RZ)55      {0x5922aa80, 0x3f08aebe, 1, 0, 1},56      // x = 0x1.48a858p54, cos(x) = 0x1.f48148p-2 (RZ)57      {0x5aa4542c, 0x3efa40a4, 1, 0, 0},58      // x = 0x1.3170fp63, cos(x) = 0x1.fe2976p-1 (RZ)59      {0x5f18b878, 0x3f7f14bb, 1, 0, 0},60      // x = 0x1.2b9622p67, cos(x) = 0x1.f0285cp-1 (RZ)61      {0x6115cb11, 0x3f78142e, 1, 0, 1},62      // x = 0x1.ddebdep120, cos(x) = 0x1.114438p-1 (RZ)63      {0x7beef5ef, 0x3f08a21c, 1, 0, 0},64  }};65#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS66 67  using FPBits = typename fputil::FPBits<float>;68 69  FPBits xbits(x);70  xbits.set_sign(Sign::POS);71 72  uint32_t x_abs = xbits.uintval();73 74  // Range reduction:75  // For |x| > pi/16, we perform range reduction as follows:76  // Find k and y such that:77  //   x = (k + y) * pi/3278  //   k is an integer79  //   |y| < 0.580  // For small range (|x| < 2^45 when FMA instructions are available, 2^2281  // otherwise), this is done by performing:82  //   k = round(x * 32/pi)83  //   y = x * 32/pi - k84  // For large range, we will omit all the higher parts of 16/pi such that the85  // least significant bits of their full products with x are larger than 63,86  // since cos((k + y + 64*i) * pi/32) = cos(x + i * 2pi) = cos(x).87  //88  // When FMA instructions are not available, we store the digits of 32/pi in89  // chunks of 28-bit precision.  This will make sure that the products:90  //   x * THIRTYTWO_OVER_PI_28[i] are all exact.91  // When FMA instructions are available, we simply store the digits of 32/pi in92  // chunks of doubles (53-bit of precision).93  // So when multiplying by the largest values of single precision, the94  // resulting output should be correct up to 2^(-208 + 128) ~ 2^-80.  By the95  // worst-case analysis of range reduction, |y| >= 2^-38, so this should give96  // us more than 40 bits of accuracy. For the worst-case estimation of range97  // reduction, see for instances:98  //   Elementary Functions by J-M. Muller, Chapter 11,99  //   Handbook of Floating-Point Arithmetic by J-M. Muller et. al.,100  //   Chapter 10.2.101  //102  // Once k and y are computed, we then deduce the answer by the cosine of sum103  // formula:104  //   cos(x) = cos((k + y)*pi/32)105  //          = cos(y*pi/32) * cos(k*pi/32) - sin(y*pi/32) * sin(k*pi/32)106  // The values of sin(k*pi/32) and cos(k*pi/32) for k = 0..63 are precomputed107  // and stored using a vector of 32 doubles. Sin(y*pi/32) and cos(y*pi/32) are108  // computed using degree-7 and degree-6 minimax polynomials generated by109  // Sollya respectively.110 111#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS112  // |x| < 0x1.0p-12f113  if (LIBC_UNLIKELY(x_abs < 0x3980'0000U)) {114    // When |x| < 2^-12, the relative error of the approximation cos(x) ~ 1115    // is:116    //   |cos(x) - 1| < |x^2 / 2| = 2^-25 < epsilon(1)/2.117    // So the correctly rounded values of cos(x) are:118    //   = 1 - eps(x) if rounding mode = FE_TOWARDZERO or FE_DOWWARD,119    //   = 1 otherwise.120    // To simplify the rounding decision and make it more efficient and to121    // prevent compiler to perform constant folding, we use122    //   fma(x, -2^-25, 1) instead.123    // Note: to use the formula 1 - 2^-25*x to decide the correct rounding, we124    // do need fma(x, -2^-25, 1) to prevent underflow caused by -2^-25*x when125    // |x| < 2^-125. For targets without FMA instructions, we simply use126    // double for intermediate results as it is more efficient than using an127    // emulated version of FMA.128#if defined(LIBC_TARGET_CPU_HAS_FMA_FLOAT)129    return fputil::multiply_add(xbits.get_val(), -0x1.0p-25f, 1.0f);130#else // !LIBC_TARGET_CPU_HAS_FMA_FLOAT131    double xd = static_cast<double>(xbits.get_val());132    return static_cast<float>(fputil::multiply_add(xd, -0x1.0p-25, 1.0));133#endif // LIBC_TARGET_CPU_HAS_FMA_FLOAT134  }135 136  if (auto r = COSF_EXCEPTS.lookup(x_abs); LIBC_UNLIKELY(r.has_value()))137    return r.value();138#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS139 140  // x is inf or nan.141  if (LIBC_UNLIKELY(x_abs >= 0x7f80'0000U)) {142    if (xbits.is_signaling_nan()) {143      fputil::raise_except_if_required(FE_INVALID);144      return FPBits::quiet_nan().get_val();145    }146 147    if (x_abs == 0x7f80'0000U) {148      fputil::set_errno_if_required(EDOM);149      fputil::raise_except_if_required(FE_INVALID);150    }151    return x + FPBits::quiet_nan().get_val();152  }153 154  double xd = static_cast<double>(xbits.get_val());155  // Combine the results with the sine of sum formula:156  //   cos(x) = cos((k + y)*pi/32)157  //          = cos(y*pi/32) * cos(k*pi/32) - sin(y*pi/32) * sin(k*pi/32)158  //          = cosm1_y * cos_k + sin_y * sin_k159  //          = (cosm1_y * cos_k + cos_k) + sin_y * sin_k160  double sin_k = 0, cos_k = 0, sin_y = 0, cosm1_y = 0;161 162  sincosf_eval(xd, x_abs, sin_k, cos_k, sin_y, cosm1_y);163 164  return static_cast<float>(fputil::multiply_add(165      sin_y, -sin_k, fputil::multiply_add(cosm1_y, cos_k, cos_k)));166}167 168} // namespace math169 170} // namespace LIBC_NAMESPACE_DECL171 172#endif // LIBC_SRC___SUPPORT_MATH_COSF_H173 174#endif // LIBC_MATH_HAS_INTERMEDIATE_COMP_IN_FLOAT175