blob: 792d405b1bb9ea68b4be5ea3bc2f1aab8e7188df [file] [log] [blame]
wldfngrsf7bb1292024-11-08 15:56:31 +01001//===-- 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
20namespace LIBC_NAMESPACE_DECL {
21
lntued5781482025-03-11 17:13:46 -040022#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
wldfngrsf7bb1292024-11-08 15:56:31 +010023constexpr size_t N_EXCEPTS = 21;
24
wldfngrscd046532024-12-03 21:08:46 +010025constexpr fputil::ExceptValues<float16, N_EXCEPTS> TANPIF16_EXCEPTS{{
wldfngrsf7bb1292024-11-08 15:56:31 +010026 // (input, RZ output, RU offset, RD offset, RN offset)
27 {0x07f2, 0x0e3d, 1, 0, 0}, {0x086a, 0x0eee, 1, 0, 1},
28 {0x08db, 0x0fa0, 1, 0, 0}, {0x094c, 0x1029, 1, 0, 0},
29 {0x0b10, 0x118c, 1, 0, 0}, {0x1ce0, 0x23a8, 1, 0, 1},
30 {0x1235, 0x18e0, 1, 0, 0}, {0x2579, 0x2c4e, 1, 0, 0},
31 {0x28b2, 0x2f68, 1, 0, 1}, {0x2a43, 0x30f4, 1, 0, 1},
32 {0x31b7, 0x3907, 1, 0, 0}, {0x329d, 0x3a12, 1, 0, 1},
33 {0x34f1, 0x3dd7, 1, 0, 0}, {0x3658, 0x41ee, 1, 0, 0},
34 {0x38d4, 0xc1ee, 0, 1, 0}, {0x3d96, 0x41ee, 1, 0, 0},
35 {0x3e6a, 0xc1ee, 0, 1, 0}, {0x40cb, 0x41ee, 1, 0, 0},
36 {0x4135, 0xc1ee, 0, 1, 0}, {0x42cb, 0x41ee, 1, 0, 0},
37 {0x4335, 0xc1ee, 0, 1, 0},
38}};
lntued5781482025-03-11 17:13:46 -040039#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
wldfngrsf7bb1292024-11-08 15:56:31 +010040
41LLVM_LIBC_FUNCTION(float16, tanpif16, (float16 x)) {
42 using FPBits = typename fputil::FPBits<float16>;
43 FPBits xbits(x);
44
45 uint16_t x_u = xbits.uintval();
46 uint16_t x_abs = x_u & 0x7fff;
47
48 // Handle exceptional values
49 if (LIBC_UNLIKELY(x_abs <= 0x4335)) {
50 if (LIBC_UNLIKELY(x_abs == 0U))
51 return x;
52
lntued5781482025-03-11 17:13:46 -040053#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
wldfngrsf7bb1292024-11-08 15:56:31 +010054 bool x_sign = x_u >> 15;
lntued5781482025-03-11 17:13:46 -040055
wldfngrscd046532024-12-03 21:08:46 +010056 if (auto r = TANPIF16_EXCEPTS.lookup_odd(x_abs, x_sign);
wldfngrsf7bb1292024-11-08 15:56:31 +010057 LIBC_UNLIKELY(r.has_value()))
58 return r.value();
lntued5781482025-03-11 17:13:46 -040059#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
wldfngrsf7bb1292024-11-08 15:56:31 +010060 }
61
62 // Numbers greater or equal to 2^10 are integers, or infinity, or NaN
63 if (LIBC_UNLIKELY(x_abs >= 0x6400)) {
64 // Check for NaN or infinity values
65 if (LIBC_UNLIKELY(x_abs >= 0x7c00)) {
wldfngrsfdf20942025-04-08 14:23:38 +010066 if (xbits.is_signaling_nan()) {
67 fputil::raise_except_if_required(FE_INVALID);
68 return FPBits::quiet_nan().get_val();
69 }
70 // is inf
wldfngrsf7bb1292024-11-08 15:56:31 +010071 if (x_abs == 0x7c00) {
72 fputil::set_errno_if_required(EDOM);
73 fputil::raise_except_if_required(FE_INVALID);
74 }
75
76 return x + FPBits::quiet_nan().get_val();
77 }
78
79 return FPBits::zero(xbits.sign()).get_val();
80 }
81 // Range reduction:
82 // For |x| > 1/32, we perform range reduction as follows:
83 // Find k and y such that:
84 // x = (k + y) * 1/32
85 // k is an integer
86 // |y| < 0.5
87 //
88 // This is done by performing:
89 // k = round(x * 32)
90 // y = x * 32 - k
91 //
wldfngrsecf4f952025-01-13 05:46:53 +010092 // Once k and y are computed, we then deduce the answer by the formula:
wldfngrsf7bb1292024-11-08 15:56:31 +010093 // tan(x) = sin(x) / cos(x)
94 // = (sin_y * cos_k + cos_y * sin_k) / (cos_y * cos_k - sin_y * sin_k)
95 float xf = x;
96 float sin_k, cos_k, sin_y, cosm1_y;
97 sincospif16_eval(xf, sin_k, cos_k, sin_y, cosm1_y);
98
99 if (LIBC_UNLIKELY(sin_y == 0 && cos_k == 0)) {
100 fputil::set_errno_if_required(EDOM);
101 fputil::raise_except_if_required(FE_DIVBYZERO);
102
103 int16_t x_mp5_u = static_cast<int16_t>(x - 0.5);
104 return ((x_mp5_u & 0x1) ? -1 : 1) * FPBits::inf().get_val();
105 }
106
107 using fputil::multiply_add;
108 return fputil::cast<float16>(
109 multiply_add(sin_y, cos_k, multiply_add(cosm1_y, sin_k, sin_k)) /
110 multiply_add(sin_y, -sin_k, multiply_add(cosm1_y, cos_k, cos_k)));
111}
112
113} // namespace LIBC_NAMESPACE_DECL