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