|
| 1 | +//===-- Half-precision tanpif 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/tanpif16.h" |
| 10 | +#include "hdr/errno_macros.h" |
| 11 | +#include "hdr/fenv_macros.h" |
| 12 | +#include "sincosf16_utils.h" |
| 13 | +#include "src/__support/FPUtil/FEnvImpl.h" |
| 14 | +#include "src/__support/FPUtil/FPBits.h" |
| 15 | +#include "src/__support/FPUtil/cast.h" |
| 16 | +#include "src/__support/FPUtil/except_value_utils.h" |
| 17 | +#include "src/__support/FPUtil/multiply_add.h" |
| 18 | +#include "src/__support/macros/optimization.h" |
| 19 | + |
| 20 | +namespace LIBC_NAMESPACE_DECL { |
| 21 | + |
| 22 | +constexpr size_t N_EXCEPTS = 21; |
| 23 | + |
| 24 | +constexpr fputil::ExceptValues<float16, N_EXCEPTS> TANF16_EXCEPTS{{ |
| 25 | + // (input, RZ output, RU offset, RD offset, RN offset) |
| 26 | + {0x07f2, 0x0e3d, 1, 0, 0}, {0x086a, 0x0eee, 1, 0, 1}, |
| 27 | + {0x08db, 0x0fa0, 1, 0, 0}, {0x094c, 0x1029, 1, 0, 0}, |
| 28 | + {0x0b10, 0x118c, 1, 0, 0}, {0x1ce0, 0x23a8, 1, 0, 1}, |
| 29 | + {0x1235, 0x18e0, 1, 0, 0}, {0x2579, 0x2c4e, 1, 0, 0}, |
| 30 | + {0x28b2, 0x2f68, 1, 0, 1}, {0x2a43, 0x30f4, 1, 0, 1}, |
| 31 | + {0x31b7, 0x3907, 1, 0, 0}, {0x329d, 0x3a12, 1, 0, 1}, |
| 32 | + {0x34f1, 0x3dd7, 1, 0, 0}, {0x3658, 0x41ee, 1, 0, 0}, |
| 33 | + {0x38d4, 0xc1ee, 0, 1, 0}, {0x3d96, 0x41ee, 1, 0, 0}, |
| 34 | + {0x3e6a, 0xc1ee, 0, 1, 0}, {0x40cb, 0x41ee, 1, 0, 0}, |
| 35 | + {0x4135, 0xc1ee, 0, 1, 0}, {0x42cb, 0x41ee, 1, 0, 0}, |
| 36 | + {0x4335, 0xc1ee, 0, 1, 0}, |
| 37 | +}}; |
| 38 | + |
| 39 | +LLVM_LIBC_FUNCTION(float16, tanpif16, (float16 x)) { |
| 40 | + using FPBits = typename fputil::FPBits<float16>; |
| 41 | + FPBits xbits(x); |
| 42 | + |
| 43 | + uint16_t x_u = xbits.uintval(); |
| 44 | + uint16_t x_abs = x_u & 0x7fff; |
| 45 | + |
| 46 | + // Handle exceptional values |
| 47 | + if (LIBC_UNLIKELY(x_abs <= 0x4335)) { |
| 48 | + if (LIBC_UNLIKELY(x_abs == 0U)) |
| 49 | + return x; |
| 50 | + |
| 51 | + bool x_sign = x_u >> 15; |
| 52 | + if (auto r = TANF16_EXCEPTS.lookup_odd(x_abs, x_sign); |
| 53 | + LIBC_UNLIKELY(r.has_value())) |
| 54 | + return r.value(); |
| 55 | + } |
| 56 | + |
| 57 | + // Numbers greater or equal to 2^10 are integers, or infinity, or NaN |
| 58 | + if (LIBC_UNLIKELY(x_abs >= 0x6400)) { |
| 59 | + // Check for NaN or infinity values |
| 60 | + if (LIBC_UNLIKELY(x_abs >= 0x7c00)) { |
| 61 | + if (x_abs == 0x7c00) { |
| 62 | + fputil::set_errno_if_required(EDOM); |
| 63 | + fputil::raise_except_if_required(FE_INVALID); |
| 64 | + } |
| 65 | + |
| 66 | + return x + FPBits::quiet_nan().get_val(); |
| 67 | + } |
| 68 | + |
| 69 | + return FPBits::zero(xbits.sign()).get_val(); |
| 70 | + } |
| 71 | + // Range reduction: |
| 72 | + // For |x| > 1/32, we perform range reduction as follows: |
| 73 | + // Find k and y such that: |
| 74 | + // x = (k + y) * 1/32 |
| 75 | + // k is an integer |
| 76 | + // |y| < 0.5 |
| 77 | + // |
| 78 | + // This is done by performing: |
| 79 | + // k = round(x * 32) |
| 80 | + // y = x * 32 - k |
| 81 | + // |
| 82 | + // Once k and y are computed, we then deduce the answer by tthe formula: |
| 83 | + // tan(x) = sin(x) / cos(x) |
| 84 | + // = (sin_y * cos_k + cos_y * sin_k) / (cos_y * cos_k - sin_y * sin_k) |
| 85 | + float xf = x; |
| 86 | + float sin_k, cos_k, sin_y, cosm1_y; |
| 87 | + sincospif16_eval(xf, sin_k, cos_k, sin_y, cosm1_y); |
| 88 | + |
| 89 | + if (LIBC_UNLIKELY(sin_y == 0 && cos_k == 0)) { |
| 90 | + fputil::set_errno_if_required(EDOM); |
| 91 | + fputil::raise_except_if_required(FE_DIVBYZERO); |
| 92 | + |
| 93 | + int16_t x_mp5_u = static_cast<int16_t>(x - 0.5); |
| 94 | + return ((x_mp5_u & 0x1) ? -1 : 1) * FPBits::inf().get_val(); |
| 95 | + } |
| 96 | + |
| 97 | + using fputil::multiply_add; |
| 98 | + return fputil::cast<float16>( |
| 99 | + multiply_add(sin_y, cos_k, multiply_add(cosm1_y, sin_k, sin_k)) / |
| 100 | + multiply_add(sin_y, -sin_k, multiply_add(cosm1_y, cos_k, cos_k))); |
| 101 | +} |
| 102 | + |
| 103 | +} // namespace LIBC_NAMESPACE_DECL |
0 commit comments