brintos

brintos / llvm-project-archived public Read only

0
0
Text · 4.8 KiB · cf29c76 Raw
148 lines · c
1//===-- Implementation header for acospif16 ---------------------*- 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 LLVM_LIBC_SRC___SUPPORT_MATH_ACOSPIF16_H10#define LLVM_LIBC_SRC___SUPPORT_MATH_ACOSPIF16_H11 12#include "include/llvm-libc-macros/float16-macros.h"13 14#ifdef LIBC_TYPES_HAS_FLOAT1615 16#include "src/__support/FPUtil/FEnvImpl.h"17#include "src/__support/FPUtil/FPBits.h"18#include "src/__support/FPUtil/PolyEval.h"19#include "src/__support/FPUtil/cast.h"20#include "src/__support/FPUtil/multiply_add.h"21#include "src/__support/FPUtil/sqrt.h"22#include "src/__support/macros/optimization.h"23 24namespace LIBC_NAMESPACE_DECL {25 26namespace math {27 28LIBC_INLINE static constexpr float16 acospif16(float16 x) {29  using FPBits = fputil::FPBits<float16>;30  FPBits xbits(x);31 32  uint16_t x_u = xbits.uintval();33  uint16_t x_abs = x_u & 0x7fff;34  uint16_t x_sign = x_u >> 15;35 36  // |x| > 0x1p0, |x| > 1, or x is NaN.37  if (LIBC_UNLIKELY(x_abs > 0x3c00)) {38    // acospif16(NaN) = NaN39    if (xbits.is_nan()) {40      if (xbits.is_signaling_nan()) {41        fputil::raise_except_if_required(FE_INVALID);42        return FPBits::quiet_nan().get_val();43      }44 45      return x;46    }47 48    // 1 < |x| <= +inf49    fputil::raise_except_if_required(FE_INVALID);50    fputil::set_errno_if_required(EDOM);51 52    return FPBits::quiet_nan().get_val();53  }54 55  // |x| == 0x1p0, x is 1 or -156  // if x is (-)1, return 157  // if x is (+)1, return 058  if (LIBC_UNLIKELY(x_abs == 0x3c00))59    return fputil::cast<float16>(x_sign ? 1.0f : 0.0f);60 61  float xf = x;62  float xsq = xf * xf;63 64  // Degree-6 minimax polynomial coefficients of asin(x) generated by Sollya65  // with: > P = fpminimax(asin(x)/(pi * x), [|0, 2, 4, 6, 8|], [|SG...|], [0,66  // 0.5]);67  constexpr float POLY_COEFFS[5] = {0x1.45f308p-2f, 0x1.b2900cp-5f,68                                    0x1.897e36p-6f, 0x1.9efafcp-7f,69                                    0x1.06d884p-6f};70  // |x| <= 0x1p-1, |x| <= 0.571  if (x_abs <= 0x3800) {72    // if x is 0, return 0.573    if (LIBC_UNLIKELY(x_abs == 0))74      return fputil::cast<float16>(0.5f);75 76    // Note that: acos(x) = pi/2 + asin(-x) = pi/2 - asin(x), then77    //            acospi(x) = 0.5 - asin(x)/pi78    float interm =79        fputil::polyeval(xsq, POLY_COEFFS[0], POLY_COEFFS[1], POLY_COEFFS[2],80                         POLY_COEFFS[3], POLY_COEFFS[4]);81 82    return fputil::cast<float16>(fputil::multiply_add(-xf, interm, 0.5f));83  }84 85  // When |x| > 0.5, assume that 0.5 < |x| <= 186  //87  // Step-by-step range-reduction proof:88  // 1:  Let y = asin(x), such that, x = sin(y)89  // 2:  From complimentary angle identity:90  //       x = sin(y) = cos(pi/2 - y)91  // 3:  Let z = pi/2 - y, such that x = cos(z)92  // 4:  From double angle formula; cos(2A) = 1 - 2 * sin^2(A):93  //       z = 2A, z/2 = A94  //       cos(z) = 1 - 2 * sin^2(z/2)95  // 5:  Make sin(z/2) subject of the formula:96  //       sin(z/2) = sqrt((1 - cos(z))/2)97  // 6:  Recall [3]; x = cos(z). Therefore:98  //       sin(z/2) = sqrt((1 - x)/2)99  // 7:  Let u = (1 - x)/2100  // 8:  Therefore:101  //       asin(sqrt(u)) = z/2102  //       2 * asin(sqrt(u)) = z103  // 9:  Recall [3]; z = pi/2 - y. Therefore:104  //       y = pi/2 - z105  //       y = pi/2 - 2 * asin(sqrt(u))106  // 10: Recall [1], y = asin(x). Therefore:107  //       asin(x) = pi/2 - 2 * asin(sqrt(u))108  // 11: Recall that: acos(x) = pi/2 + asin(-x) = pi/2 - asin(x)109  //     Therefore:110  //       acos(x) = pi/2 - (pi/2 - 2 * asin(sqrt(u)))111  //       acos(x) = 2 * asin(sqrt(u))112  //       acospi(x) = 2 * (asin(sqrt(u)) / pi)113  //114  // THE RANGE REDUCTION, HOW?115  // 12: Recall [7], u = (1 - x)/2116  // 13: Since 0.5 < x <= 1, therefore:117  //       0 <= u <= 0.25 and 0 <= sqrt(u) <= 0.5118  //119  // Hence, we can reuse the same [0, 0.5] domain polynomial approximation for120  // Step [11] as `sqrt(u)` is in range.121  // When -1 < x <= -0.5, the identity:122  //       acos(x) = pi - acos(-x)123  //       acospi(x) = 1 - acos(-x)/pi124  // allows us to compute for the negative x value (lhs)125  // with a positive x value instead (rhs).126 127  float xf_abs = (xf < 0 ? -xf : xf);128  float u = fputil::multiply_add(-0.5f, xf_abs, 0.5f);129  float sqrt_u = fputil::sqrt<float>(u);130 131  float asin_sqrt_u =132      sqrt_u * fputil::polyeval(u, POLY_COEFFS[0], POLY_COEFFS[1],133                                POLY_COEFFS[2], POLY_COEFFS[3], POLY_COEFFS[4]);134 135  // Same as acos(x), but devided the expression with pi136  return fputil::cast<float16>(137      x_sign ? fputil::multiply_add(-2.0f, asin_sqrt_u, 1.0f)138             : 2.0f * asin_sqrt_u);139}140 141} // namespace math142 143} // namespace LIBC_NAMESPACE_DECL144 145#endif // LIBC_TYPES_HAS_FLOAT16146 147#endif // LLVM_LIBC_SRC___SUPPORT_MATH_ACOSPIF16_H148