1//===-- Implementation header for tanpif16 ----------------------*- 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-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_TANPIF16_H
10#define LLVM_LIBC_SRC___SUPPORT_MATH_TANPIF16_H
11
12#include "include/llvm-libc-macros/float16-macros.h"
13
14#ifdef LIBC_TYPES_HAS_FLOAT16
15
16#include "hdr/errno_macros.h"
17#include "hdr/fenv_macros.h"
18#include "sincosf16_utils.h"
19#include "src/__support/FPUtil/FEnvImpl.h"
20#include "src/__support/FPUtil/FPBits.h"
21#include "src/__support/FPUtil/cast.h"
22#include "src/__support/FPUtil/except_value_utils.h"
23#include "src/__support/FPUtil/is_integer.h"
24#include "src/__support/FPUtil/multiply_add.h"
25#include "src/__support/macros/optimization.h"
26
27namespace LIBC_NAMESPACE_DECL {
28namespace math {
29
30LIBC_INLINE float16 tanpif16(float16 x) {
31 using namespace sincosf16_internal;
32 using FPBits = typename fputil::FPBits<float16>;
33 FPBits xbits(x);
34
35 uint16_t x_u = xbits.uintval();
36 uint16_t x_abs = x_u & 0x7fff;
37
38 // Handle NaN and Inf.
39 if (LIBC_UNLIKELY(x_abs >= 0x7c00)) {
40 if (xbits.is_signaling_nan()) {
41 fputil::raise_except_if_required(FE_INVALID);
42 return FPBits::quiet_nan().get_val();
43 }
44 if (x_abs == 0x7c00) {
45 fputil::set_errno_if_required(EDOM);
46 fputil::raise_except_if_required(FE_INVALID);
47 }
48
49 return x + FPBits::quiet_nan().get_val();
50 }
51
52 // tanpi(x) underflows for 0 < |x| <= 0x1.45p-16.
53 if (LIBC_UNLIKELY(x_abs <= 0x0145U)) {
54 // Handle signed zeros.
55 if (x_abs == 0U)
56 return x;
57 fputil::set_errno_if_required(ERANGE);
58 fputil::raise_underflow_except_if_required<float>();
59 }
60
61 if (LIBC_UNLIKELY(fputil::is_integer(x))) {
62 // Preserve the input sign for even integers and flip it for odd integers.
63 Sign sign =
64 fputil::is_odd_integer(x) ? xbits.sign().negate() : xbits.sign();
65 return FPBits::zero(sign).get_val();
66 }
67
68 // Odd multiples of 1/4 have exact results of +1 or -1.
69 float xf = x;
70 if (LIBC_UNLIKELY(fputil::is_odd_integer(4.0f * xf)))
71 return fputil::is_odd_integer(x: fputil::multiply_sub(x: 2.0f, y: xf, z: 0.5f))
72 ? -1.0f16
73 : 1.0f16;
74
75#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
76 // Handle exceptional values.
77 if (LIBC_UNLIKELY(x_abs <= 0x4335)) {
78 constexpr size_t N_EXCEPTS = 21;
79
80 constexpr fputil::ExceptValues<float16, N_EXCEPTS> TANPIF16_EXCEPTS{.values: {
81 // (input, RZ output, RU offset, RD offset, RN offset)
82 {.input: 0x07f2, .rnd_towardzero_result: 0x0e3d, .rnd_upward_offset: 1, .rnd_downward_offset: 0, .rnd_tonearest_offset: 0}, {.input: 0x086a, .rnd_towardzero_result: 0x0eee, .rnd_upward_offset: 1, .rnd_downward_offset: 0, .rnd_tonearest_offset: 1},
83 {.input: 0x08db, .rnd_towardzero_result: 0x0fa0, .rnd_upward_offset: 1, .rnd_downward_offset: 0, .rnd_tonearest_offset: 0}, {.input: 0x094c, .rnd_towardzero_result: 0x1029, .rnd_upward_offset: 1, .rnd_downward_offset: 0, .rnd_tonearest_offset: 0},
84 {.input: 0x0b10, .rnd_towardzero_result: 0x118c, .rnd_upward_offset: 1, .rnd_downward_offset: 0, .rnd_tonearest_offset: 0}, {.input: 0x1ce0, .rnd_towardzero_result: 0x23a8, .rnd_upward_offset: 1, .rnd_downward_offset: 0, .rnd_tonearest_offset: 1},
85 {.input: 0x1235, .rnd_towardzero_result: 0x18e0, .rnd_upward_offset: 1, .rnd_downward_offset: 0, .rnd_tonearest_offset: 0}, {.input: 0x2579, .rnd_towardzero_result: 0x2c4e, .rnd_upward_offset: 1, .rnd_downward_offset: 0, .rnd_tonearest_offset: 0},
86 {.input: 0x28b2, .rnd_towardzero_result: 0x2f68, .rnd_upward_offset: 1, .rnd_downward_offset: 0, .rnd_tonearest_offset: 1}, {.input: 0x2a43, .rnd_towardzero_result: 0x30f4, .rnd_upward_offset: 1, .rnd_downward_offset: 0, .rnd_tonearest_offset: 1},
87 {.input: 0x31b7, .rnd_towardzero_result: 0x3907, .rnd_upward_offset: 1, .rnd_downward_offset: 0, .rnd_tonearest_offset: 0}, {.input: 0x329d, .rnd_towardzero_result: 0x3a12, .rnd_upward_offset: 1, .rnd_downward_offset: 0, .rnd_tonearest_offset: 1},
88 {.input: 0x34f1, .rnd_towardzero_result: 0x3dd7, .rnd_upward_offset: 1, .rnd_downward_offset: 0, .rnd_tonearest_offset: 0}, {.input: 0x3658, .rnd_towardzero_result: 0x41ee, .rnd_upward_offset: 1, .rnd_downward_offset: 0, .rnd_tonearest_offset: 0},
89 {.input: 0x38d4, .rnd_towardzero_result: 0xc1ee, .rnd_upward_offset: 0, .rnd_downward_offset: 1, .rnd_tonearest_offset: 0}, {.input: 0x3d96, .rnd_towardzero_result: 0x41ee, .rnd_upward_offset: 1, .rnd_downward_offset: 0, .rnd_tonearest_offset: 0},
90 {.input: 0x3e6a, .rnd_towardzero_result: 0xc1ee, .rnd_upward_offset: 0, .rnd_downward_offset: 1, .rnd_tonearest_offset: 0}, {.input: 0x40cb, .rnd_towardzero_result: 0x41ee, .rnd_upward_offset: 1, .rnd_downward_offset: 0, .rnd_tonearest_offset: 0},
91 {.input: 0x4135, .rnd_towardzero_result: 0xc1ee, .rnd_upward_offset: 0, .rnd_downward_offset: 1, .rnd_tonearest_offset: 0}, {.input: 0x42cb, .rnd_towardzero_result: 0x41ee, .rnd_upward_offset: 1, .rnd_downward_offset: 0, .rnd_tonearest_offset: 0},
92 {.input: 0x4335, .rnd_towardzero_result: 0xc1ee, .rnd_upward_offset: 0, .rnd_downward_offset: 1, .rnd_tonearest_offset: 0},
93 }};
94
95 bool x_sign = x_u >> 15;
96
97 if (auto r = TANPIF16_EXCEPTS.lookup_odd(x_abs, sign: x_sign);
98 LIBC_UNLIKELY(r.has_value()))
99 return r.value();
100 }
101#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
102
103 // Range reduction:
104 // All remaining inputs are finite nonintegers, so |x| < 2^10.
105 // For |x| > 1/32, we perform range reduction as follows:
106 // Find k and y such that:
107 // x = (k + y) * 1/32
108 // k is an integer
109 // |y| < 0.5
110 //
111 // This is done by performing:
112 // k = round(x * 32)
113 // y = x * 32 - k
114 //
115 // Once k and y are computed, we then deduce the answer by the formula:
116 // tan(x) = sin(x) / cos(x)
117 // = (sin_y * cos_k + cos_y * sin_k) / (cos_y * cos_k - sin_y * sin_k)
118 float sin_k = 0, cos_k = 0, sin_y = 0, cosm1_y = 0;
119 sincospif16_eval(xf, sin_k, cos_k, sin_y, cosm1_y);
120
121 if (LIBC_UNLIKELY(sin_y == 0 && cos_k == 0)) {
122 fputil::set_errno_if_required(ERANGE);
123 fputil::raise_except_if_required(FE_DIVBYZERO);
124
125 int16_t x_mp5_u = static_cast<int16_t>(x - 0.5);
126 return ((x_mp5_u & 0x1) ? -1 : 1) * FPBits::inf().get_val();
127 }
128
129 using fputil::multiply_add;
130 return fputil::cast<float16>(
131 x: multiply_add(x: sin_y, y: cos_k, z: multiply_add(x: cosm1_y, y: sin_k, z: sin_k)) /
132 fputil::sub_multiply(x: multiply_add(x: cosm1_y, y: cos_k, z: cos_k), y: sin_y, z: sin_k));
133}
134
135} // namespace math
136} // namespace LIBC_NAMESPACE_DECL
137
138#endif // LIBC_TYPES_HAS_FLOAT16
139
140#endif // LLVM_LIBC_SRC___SUPPORT_MATH_TANPIF16_H
141