xref: /llvm-project/libc/src/math/generic/sinf16.cpp (revision 6a865b6d3d44e53adc9342fb2d80a604f5459707)
1cd04653cSwldfngrs //===-- Half-precision sin(x) function ------------------------------------===//
2cd04653cSwldfngrs //
3cd04653cSwldfngrs // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4cd04653cSwldfngrs // See https://llvm.org/LICENSE.txt for license information.
5cd04653cSwldfngrs // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6cd04653cSwldfngrs //
7cd04653cSwldfngrs //===----------------------------------------------------------------------===//
8cd04653cSwldfngrs 
9cd04653cSwldfngrs #include "src/math/sinf16.h"
10cd04653cSwldfngrs #include "hdr/errno_macros.h"
11cd04653cSwldfngrs #include "hdr/fenv_macros.h"
12cd04653cSwldfngrs #include "sincosf16_utils.h"
13cd04653cSwldfngrs #include "src/__support/FPUtil/FEnvImpl.h"
14cd04653cSwldfngrs #include "src/__support/FPUtil/FPBits.h"
15cd04653cSwldfngrs #include "src/__support/FPUtil/cast.h"
16cd04653cSwldfngrs #include "src/__support/FPUtil/except_value_utils.h"
17cd04653cSwldfngrs #include "src/__support/FPUtil/multiply_add.h"
18cd04653cSwldfngrs #include "src/__support/macros/optimization.h"
19cd04653cSwldfngrs 
20cd04653cSwldfngrs namespace LIBC_NAMESPACE_DECL {
21cd04653cSwldfngrs 
22cd04653cSwldfngrs constexpr size_t N_EXCEPTS = 4;
23cd04653cSwldfngrs 
24cd04653cSwldfngrs constexpr fputil::ExceptValues<float16, N_EXCEPTS> SINF16_EXCEPTS{{
25cd04653cSwldfngrs     // (input, RZ output, RU offset, RD offset, RN offset)
26cd04653cSwldfngrs     {0x2b45, 0x2b43, 1, 0, 1},
27cd04653cSwldfngrs     {0x585c, 0x3ba3, 1, 0, 1},
28cd04653cSwldfngrs     {0x5cb0, 0xbbff, 0, 1, 0},
29cd04653cSwldfngrs     {0x51f5, 0xb80f, 0, 1, 0},
30cd04653cSwldfngrs }};
31cd04653cSwldfngrs 
32cd04653cSwldfngrs LLVM_LIBC_FUNCTION(float16, sinf16, (float16 x)) {
33cd04653cSwldfngrs   using FPBits = fputil::FPBits<float16>;
34cd04653cSwldfngrs   FPBits xbits(x);
35cd04653cSwldfngrs 
36cd04653cSwldfngrs   uint16_t x_u = xbits.uintval();
37cd04653cSwldfngrs   uint16_t x_abs = x_u & 0x7fff;
38cd04653cSwldfngrs   float xf = x;
39cd04653cSwldfngrs 
40cd04653cSwldfngrs   // Range reduction:
41cd04653cSwldfngrs   // For |x| > pi/32, we perform range reduction as follows:
42cd04653cSwldfngrs   // Find k and y such that:
43cd04653cSwldfngrs   //   x = (k + y) * pi/32
44cd04653cSwldfngrs   //   k is an integer, |y| < 0.5
45cd04653cSwldfngrs   //
46cd04653cSwldfngrs   // This is done by performing:
47cd04653cSwldfngrs   //   k = round(x * 32/pi)
48cd04653cSwldfngrs   //   y = x * 32/pi - k
49cd04653cSwldfngrs   //
50cd04653cSwldfngrs   // Once k and y are computed, we then deduce the answer by the sine of sum
51cd04653cSwldfngrs   // formula:
52cd04653cSwldfngrs   //   sin(x) = sin((k + y) * pi/32)
53cd04653cSwldfngrs   //   	      = sin(k * pi/32) * cos(y * pi/32) +
54cd04653cSwldfngrs   //   	        sin(y * pi/32) * cos(k * pi/32)
55cd04653cSwldfngrs 
56cd04653cSwldfngrs   // Handle exceptional values
57cd04653cSwldfngrs   bool x_sign = x_u >> 15;
58cd04653cSwldfngrs   if (auto r = SINF16_EXCEPTS.lookup_odd(x_abs, x_sign);
59cd04653cSwldfngrs       LIBC_UNLIKELY(r.has_value()))
60cd04653cSwldfngrs     return r.value();
61cd04653cSwldfngrs 
62cd04653cSwldfngrs   int rounding = fputil::quick_get_round();
63cd04653cSwldfngrs 
64cd04653cSwldfngrs   // Exhaustive tests show that for |x| <= 0x1.f4p-11, 1ULP rounding errors
65cd04653cSwldfngrs   // occur. To fix this, the following apply:
66cd04653cSwldfngrs   if (LIBC_UNLIKELY(x_abs <= 0x13d0)) {
67cd04653cSwldfngrs     // sin(+/-0) = +/-0
68cd04653cSwldfngrs     if (LIBC_UNLIKELY(x_abs == 0U))
69cd04653cSwldfngrs       return x;
70cd04653cSwldfngrs 
71cd04653cSwldfngrs     // When x > 0, and rounding upward, sin(x) == x.
72cd04653cSwldfngrs     // When x < 0, and rounding downward, sin(x) == x.
73cd04653cSwldfngrs     if ((rounding == FE_UPWARD && xbits.is_pos()) ||
74cd04653cSwldfngrs         (rounding == FE_DOWNWARD && xbits.is_neg()))
75cd04653cSwldfngrs       return x;
76cd04653cSwldfngrs 
77cd04653cSwldfngrs     // When x < 0, and rounding upward, sin(x) == (x - 1ULP)
78cd04653cSwldfngrs     if (rounding == FE_UPWARD && xbits.is_neg()) {
79cd04653cSwldfngrs       x_u--;
80cd04653cSwldfngrs       return FPBits(x_u).get_val();
81cd04653cSwldfngrs     }
82cd04653cSwldfngrs   }
83cd04653cSwldfngrs 
84cd04653cSwldfngrs   if (xbits.is_inf_or_nan()) {
85cd04653cSwldfngrs     if (xbits.is_inf()) {
86cd04653cSwldfngrs       fputil::set_errno_if_required(EDOM);
87cd04653cSwldfngrs       fputil::raise_except_if_required(FE_INVALID);
88cd04653cSwldfngrs     }
89cd04653cSwldfngrs 
90cd04653cSwldfngrs     return x + FPBits::quiet_nan().get_val();
91cd04653cSwldfngrs   }
92cd04653cSwldfngrs 
93cd04653cSwldfngrs   float sin_k, cos_k, sin_y, cosm1_y;
94cd04653cSwldfngrs   sincosf16_eval(xf, sin_k, cos_k, sin_y, cosm1_y);
95cd04653cSwldfngrs 
96cd04653cSwldfngrs   if (LIBC_UNLIKELY(sin_y == 0 && sin_k == 0))
97cd04653cSwldfngrs     return FPBits::zero(xbits.sign()).get_val();
98cd04653cSwldfngrs 
99*6a865b6dSwldfngrs   // Since, cosm1_y = cos_y - 1, therefore:
100cd04653cSwldfngrs   //   sin(x) = cos_k * sin_y + sin_k + (cosm1_y * sin_k)
101cd04653cSwldfngrs   return fputil::cast<float16>(fputil::multiply_add(
102cd04653cSwldfngrs       sin_y, cos_k, fputil::multiply_add(cosm1_y, sin_k, sin_k)));
103cd04653cSwldfngrs }
104cd04653cSwldfngrs 
105cd04653cSwldfngrs } // namespace LIBC_NAMESPACE_DECL
106