brintos

brintos / llvm-project-archived public Read only

0
0
Text · 4.0 KiB · 20323a8 Raw
125 lines · cpp
1//===-- Half-precision tan(x) 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-exception.6//7//===----------------------------------------------------------------------===//8 9#include "src/math/tanf16.h"10#include "hdr/errno_macros.h"11#include "hdr/fenv_macros.h"12#include "src/__support/FPUtil/FEnvImpl.h"13#include "src/__support/FPUtil/FPBits.h"14#include "src/__support/FPUtil/cast.h"15#include "src/__support/FPUtil/except_value_utils.h"16#include "src/__support/FPUtil/multiply_add.h"17#include "src/__support/macros/optimization.h"18#include "src/__support/math/sincosf16_utils.h"19 20namespace LIBC_NAMESPACE_DECL {21 22#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS23constexpr size_t N_EXCEPTS = 9;24 25constexpr fputil::ExceptValues<float16, N_EXCEPTS> TANF16_EXCEPTS{{26    // (input, RZ output, RU offset, RD offset, RN offset)27    {0x2894, 0x2894, 1, 0, 1},28    {0x3091, 0x3099, 1, 0, 0},29    {0x3098, 0x30a0, 1, 0, 0},30    {0x55ed, 0x3911, 1, 0, 0},31    {0x607b, 0xc638, 0, 1, 1},32    {0x674e, 0x3b7d, 1, 0, 0},33    {0x6807, 0x4014, 1, 0, 1},34    {0x6f4d, 0xbe19, 0, 1, 1},35    {0x7330, 0xcb62, 0, 1, 0},36}};37#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS38 39LLVM_LIBC_FUNCTION(float16, tanf16, (float16 x)) {40  using namespace sincosf16_internal;41  using FPBits = fputil::FPBits<float16>;42  FPBits xbits(x);43 44  uint16_t x_u = xbits.uintval();45  uint16_t x_abs = x_u & 0x7fff;46  float xf = x;47 48#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS49  bool x_sign = x_u >> 15;50  // Handle exceptional values51  if (auto r = TANF16_EXCEPTS.lookup_odd(x_abs, x_sign);52      LIBC_UNLIKELY(r.has_value()))53    return r.value();54#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS55 56  // |x| <= 0x1.d1p-557  if (LIBC_UNLIKELY(x_abs <= 0x2b44)) {58    // |x| <= 0x1.398p-1159    if (LIBC_UNLIKELY(x_abs <= 0x10e6)) {60      // tan(+/-0) = +/-061      if (LIBC_UNLIKELY(x_abs == 0))62        return x;63 64      int rounding = fputil::quick_get_round();65 66      // Exhaustive tests show that, when:67      // x > 0, and rounding upward or68      // x < 0, and rounding downward then,69      // tan(x) = x * 2^-11 + x70      if ((xbits.is_pos() && rounding == FE_UPWARD) ||71          (xbits.is_neg() && rounding == FE_DOWNWARD))72        return fputil::cast<float16>(fputil::multiply_add(xf, 0x1.0p-11f, xf));73      return x;74    }75 76    float xsq = xf * xf;77 78    // Degree-6 minimax odd polynomial of tan(x) generated by Sollya with:79    // > P = fpminimax(tan(x)/x, [|0, 2, 4, 6|], [|1, SG...|], [0, pi/32]);80    float result = fputil::polyeval(xsq, 0x1p0f, 0x1.555556p-2f, 0x1.110ee4p-3f,81                                    0x1.be80f6p-5f);82 83    return fputil::cast<float16>(xf * result);84  }85 86  // tan(+/-inf) = NaN, and tan(NaN) = NaN87  if (LIBC_UNLIKELY(x_abs >= 0x7c00)) {88    if (xbits.is_signaling_nan()) {89      fputil::raise_except_if_required(FE_INVALID);90      return FPBits::quiet_nan().get_val();91    }92    // x = +/-inf93    if (x_abs == 0x7c00) {94      fputil::set_errno_if_required(EDOM);95      fputil::raise_except_if_required(FE_INVALID);96    }97 98    return x + FPBits::quiet_nan().get_val();99  }100 101  // Range reduction:102  // For |x| > pi/32, we perform range reduction as follows:103  // Find k and y such that:104  //   x = (k + y) * pi/32;105  //   k is an integer, |y| < 0.5106  //107  // This is done by performing:108  //   k = round(x * 32/pi)109  //   y = x * 32/pi - k110  //111  // Once k and y are computed, we then deduce the answer by the formula:112  // tan(x) = sin(x) / cos(x)113  // 	    = (sin_y * cos_k + cos_y * sin_k) / (cos_y * cos_k - sin_y * sin_k)114  float sin_k, cos_k, sin_y, cosm1_y;115  sincosf16_eval(xf, sin_k, cos_k, sin_y, cosm1_y);116 117  // Note that, cosm1_y = cos_y - 1:118  using fputil::multiply_add;119  return fputil::cast<float16>(120      multiply_add(sin_y, cos_k, multiply_add(cosm1_y, sin_k, sin_k)) /121      multiply_add(sin_y, -sin_k, multiply_add(cosm1_y, cos_k, cos_k)));122}123 124} // namespace LIBC_NAMESPACE_DECL125