brintos

brintos / llvm-project-archived public Read only

0
0
Text · 7.2 KiB · c362628 Raw
183 lines · cpp
1//===-- Single-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/sinf.h"10#include "src/__support/FPUtil/BasicOperations.h"11#include "src/__support/FPUtil/FEnvImpl.h"12#include "src/__support/FPUtil/FPBits.h"13#include "src/__support/FPUtil/PolyEval.h"14#include "src/__support/FPUtil/multiply_add.h"15#include "src/__support/FPUtil/rounding_mode.h"16#include "src/__support/common.h"17#include "src/__support/macros/config.h"18#include "src/__support/macros/optimization.h"            // LIBC_UNLIKELY19#include "src/__support/macros/properties/cpu_features.h" // LIBC_TARGET_CPU_HAS_FMA20 21#if defined(LIBC_MATH_HAS_SKIP_ACCURATE_PASS) &&                               \22    defined(LIBC_MATH_HAS_INTERMEDIATE_COMP_IN_FLOAT) &&                       \23    defined(LIBC_TARGET_CPU_HAS_FMA_FLOAT)24 25#include "src/__support/math/sincosf_float_eval.h"26 27namespace LIBC_NAMESPACE_DECL {28 29LLVM_LIBC_FUNCTION(float, sinf, (float x)) {30  return math::sincosf_float_eval::sincosf_eval</*IS_SIN*/ true>(x);31}32 33} // namespace LIBC_NAMESPACE_DECL34 35#else // !LIBC_MATH_HAS_INTERMEDIATE_COMP_IN_FLOAT36 37#include "src/__support/math/sincosf_utils.h"38 39#ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE40#include "src/__support/math/range_reduction_fma.h"41#else // !LIBC_TARGET_CPU_HAS_FMA_DOUBLE42#include "src/__support/math/range_reduction.h"43#endif // LIBC_TARGET_CPU_HAS_FMA_DOUBLE44 45namespace LIBC_NAMESPACE_DECL {46 47LLVM_LIBC_FUNCTION(float, sinf, (float x)) {48  using FPBits = typename fputil::FPBits<float>;49  FPBits xbits(x);50 51  uint32_t x_u = xbits.uintval();52  uint32_t x_abs = x_u & 0x7fff'ffffU;53  double xd = static_cast<double>(x);54 55  // Range reduction:56  // For |x| > pi/32, we perform range reduction as follows:57  // Find k and y such that:58  //   x = (k + y) * pi/3259  //   k is an integer60  //   |y| < 0.561  // For small range (|x| < 2^45 when FMA instructions are available, 2^2262  // otherwise), this is done by performing:63  //   k = round(x * 32/pi)64  //   y = x * 32/pi - k65  // For large range, we will omit all the higher parts of 32/pi such that the66  // least significant bits of their full products with x are larger than 63,67  // since sin((k + y + 64*i) * pi/32) = sin(x + i * 2pi) = sin(x).68  //69  // When FMA instructions are not available, we store the digits of 32/pi in70  // chunks of 28-bit precision.  This will make sure that the products:71  //   x * THIRTYTWO_OVER_PI_28[i] are all exact.72  // When FMA instructions are available, we simply store the digits of 32/pi in73  // chunks of doubles (53-bit of precision).74  // So when multiplying by the largest values of single precision, the75  // resulting output should be correct up to 2^(-208 + 128) ~ 2^-80.  By the76  // worst-case analysis of range reduction, |y| >= 2^-38, so this should give77  // us more than 40 bits of accuracy. For the worst-case estimation of range78  // reduction, see for instances:79  //   Elementary Functions by J-M. Muller, Chapter 11,80  //   Handbook of Floating-Point Arithmetic by J-M. Muller et. al.,81  //   Chapter 10.2.82  //83  // Once k and y are computed, we then deduce the answer by the sine of sum84  // formula:85  //   sin(x) = sin((k + y)*pi/32)86  //          = sin(y*pi/32) * cos(k*pi/32) + cos(y*pi/32) * sin(k*pi/32)87  // The values of sin(k*pi/32) and cos(k*pi/32) for k = 0..31 are precomputed88  // and stored using a vector of 32 doubles. Sin(y*pi/32) and cos(y*pi/32) are89  // computed using degree-7 and degree-6 minimax polynomials generated by90  // Sollya respectively.91 92  // |x| <= pi/1693  if (LIBC_UNLIKELY(x_abs <= 0x3e49'0fdbU)) {94 95    // |x| < 0x1.d12ed2p-12f96    if (LIBC_UNLIKELY(x_abs < 0x39e8'9769U)) {97      if (LIBC_UNLIKELY(x_abs == 0U)) {98        // For signed zeros.99        return x;100      }101      // When |x| < 2^-12, the relative error of the approximation sin(x) ~ x102      // is:103      //   |sin(x) - x| / |sin(x)| < |x^3| / (6|x|)104      //                           = x^2 / 6105      //                           < 2^-25106      //                           < epsilon(1)/2.107      // So the correctly rounded values of sin(x) are:108      //   = x - sign(x)*eps(x) if rounding mode = FE_TOWARDZERO,109      //                        or (rounding mode = FE_UPWARD and x is110      //                        negative),111      //   = x otherwise.112      // To simplify the rounding decision and make it more efficient, we use113      //   fma(x, -2^-25, x) instead.114      // An exhaustive test shows that this formula work correctly for all115      // rounding modes up to |x| < 0x1.c555dep-11f.116      // Note: to use the formula x - 2^-25*x to decide the correct rounding, we117      // do need fma(x, -2^-25, x) to prevent underflow caused by -2^-25*x when118      // |x| < 2^-125. For targets without FMA instructions, we simply use119      // double for intermediate results as it is more efficient than using an120      // emulated version of FMA.121#if defined(LIBC_TARGET_CPU_HAS_FMA_FLOAT)122      return fputil::multiply_add(x, -0x1.0p-25f, x);123#else124      return static_cast<float>(fputil::multiply_add(xd, -0x1.0p-25, xd));125#endif // LIBC_TARGET_CPU_HAS_FMA_FLOAT126    }127 128    // |x| < pi/16.129    double xsq = xd * xd;130 131    // Degree-9 polynomial approximation:132    //   sin(x) ~ x + a_3 x^3 + a_5 x^5 + a_7 x^7 + a_9 x^9133    //          = x (1 + a_3 x^2 + ... + a_9 x^8)134    //          = x * P(x^2)135    // generated by Sollya with the following commands:136    // > display = hexadecimal;137    // > Q = fpminimax(sin(x)/x, [|0, 2, 4, 6, 8|], [|1, D...|], [0, pi/16]);138    double result =139        fputil::polyeval(xsq, 1.0, -0x1.55555555554c6p-3, 0x1.1111111085e65p-7,140                         -0x1.a019f70fb4d4fp-13, 0x1.718d179815e74p-19);141    return static_cast<float>(xd * result);142  }143 144#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS145  if (LIBC_UNLIKELY(x_abs == 0x4619'9998U)) { // x = 0x1.33333p13146    float r = -0x1.63f4bap-2f;147    int rounding = fputil::quick_get_round();148    if ((rounding == FE_DOWNWARD && xbits.is_pos()) ||149        (rounding == FE_UPWARD && xbits.is_neg()))150      r = -0x1.63f4bcp-2f;151    return xbits.is_neg() ? -r : r;152  }153#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS154 155  if (LIBC_UNLIKELY(x_abs >= 0x7f80'0000U)) {156    if (xbits.is_signaling_nan()) {157      fputil::raise_except_if_required(FE_INVALID);158      return FPBits::quiet_nan().get_val();159    }160 161    if (x_abs == 0x7f80'0000U) {162      fputil::set_errno_if_required(EDOM);163      fputil::raise_except_if_required(FE_INVALID);164    }165    return x + FPBits::quiet_nan().get_val();166  }167 168  // Combine the results with the sine of sum formula:169  //   sin(x) = sin((k + y)*pi/32)170  //          = sin(y*pi/32) * cos(k*pi/32) + cos(y*pi/32) * sin(k*pi/32)171  //          = sin_y * cos_k + (1 + cosm1_y) * sin_k172  //          = sin_y * cos_k + (cosm1_y * sin_k + sin_k)173  double sin_k, cos_k, sin_y, cosm1_y;174 175  sincosf_eval(xd, x_abs, sin_k, cos_k, sin_y, cosm1_y);176 177  return static_cast<float>(fputil::multiply_add(178      sin_y, cos_k, fputil::multiply_add(cosm1_y, sin_k, sin_k)));179}180 181} // namespace LIBC_NAMESPACE_DECL182#endif // LIBC_MATH_HAS_INTERMEDIATE_COMP_IN_FLOAT183