1//===-- Implementation header for sinpif ---------------------------* 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_SINPIF_H
10#define LLVM_LIBC_SRC___SUPPORT_MATH_SINPIF_H
11
12#include "sincosf_utils.h"
13#include "src/__support/FPUtil/FEnvImpl.h"
14#include "src/__support/FPUtil/FPBits.h"
15#include "src/__support/FPUtil/PolyEval.h"
16#include "src/__support/FPUtil/multiply_add.h"
17#include "src/__support/common.h"
18#include "src/__support/macros/config.h"
19#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
20
21namespace LIBC_NAMESPACE_DECL {
22namespace math {
23
24LIBC_INLINE float sinpif(float x) {
25 using namespace sincosf_utils_internal;
26 using FPBits = typename fputil::FPBits<float>;
27 FPBits xbits(x);
28
29 uint32_t x_u = xbits.uintval();
30 uint32_t x_abs = x_u & 0x7fff'ffffU;
31 double xd = static_cast<double>(x);
32
33 // Range reduction:
34 // For |x| > 1/32, we perform range reduction as follows:
35 // Find k and y such that:
36 // x = (k + y) * 1/32
37 // k is an integer
38 // |y| < 0.5
39 //
40 // This is done by performing:
41 // k = round(x * 32)
42 // y = x * 32 - k
43 //
44 // Once k and y are computed, we then deduce the answer by the sine of sum
45 // formula:
46 // sin(x * pi) = sin((k + y)*pi/32)
47 // = sin(y*pi/32) * cos(k*pi/32) + cos(y*pi/32) * sin(k*pi/32)
48 // The values of sin(k*pi/32) and cos(k*pi/32) for k = 0..31 are precomputed
49 // and stored using a vector of 32 doubles. Sin(y*pi/32) and cos(y*pi/32) are
50 // computed using degree-7 and degree-6 minimax polynomials generated by
51 // Sollya respectively.
52
53 // |x| <= 1/16
54 if (LIBC_UNLIKELY(x_abs <= 0x3d80'0000U)) {
55
56 if (LIBC_UNLIKELY(x_abs < 0x33CD'01D7U)) {
57 if (LIBC_UNLIKELY(x_abs == 0U)) {
58 // For signed zeros.
59 return x;
60 }
61
62 // For very small values we can approximate sinpi(x) with x * pi
63 // An exhaustive test shows that this is accurate for |x| < 9.546391 ×
64 // 10-8
65 double xdpi = xd * 0x1.921fb54442d18p1;
66 return static_cast<float>(xdpi);
67 }
68
69 // |x| < 1/16.
70 double xsq = xd * xd;
71
72 // Degree-9 polynomial approximation:
73 // sinpi(x) ~ x + a_3 x^3 + a_5 x^5 + a_7 x^7 + a_9 x^9
74 // = x (1 + a_3 x^2 + ... + a_9 x^8)
75 // = x * P(x^2)
76 // generated by Sollya with the following commands:
77 // > display = hexadecimal;
78 // > Q = fpminimax(sin(pi * x)/x, [|0, 2, 4, 6, 8|], [|D...|], [0, 1/16]);
79 double result = fputil::polyeval(
80 x: xsq, a0: 0x1.921fb54442d18p1, a: -0x1.4abbce625bbf2p2, a: 0x1.466bc675e116ap1,
81 a: -0x1.32d2c0b62d41cp-1, a: 0x1.501ec4497cb7dp-4);
82 return static_cast<float>(xd * result);
83 }
84
85 // Numbers greater or equal to 2^23 are always integers or NaN
86 if (LIBC_UNLIKELY(x_abs >= 0x4B00'0000)) {
87
88 // check for NaN values
89 if (LIBC_UNLIKELY(x_abs >= 0x7f80'0000U)) {
90 if (xbits.is_signaling_nan()) {
91 fputil::raise_except_if_required(FE_INVALID);
92 return FPBits::quiet_nan().get_val();
93 }
94
95 if (x_abs == 0x7f80'0000U) {
96 fputil::set_errno_if_required(EDOM);
97 fputil::raise_except_if_required(FE_INVALID);
98 }
99
100 return x + FPBits::quiet_nan().get_val();
101 }
102
103 return FPBits::zero(sign: xbits.sign()).get_val();
104 }
105
106 // Combine the results with the sine of sum formula:
107 // sin(x * pi) = sin((k + y)*pi/32)
108 // = sin(y*pi/32) * cos(k*pi/32) + cos(y*pi/32) * sin(k*pi/32)
109 // = sin_y * cos_k + (1 + cosm1_y) * sin_k
110 // = sin_y * cos_k + (cosm1_y * sin_k + sin_k)
111 double sin_k = 0, cos_k = 0, sin_y = 0, cosm1_y = 0;
112 sincospif_eval(xd, sin_k, cos_k, sin_y, cosm1_y);
113
114 if (LIBC_UNLIKELY(sin_y == 0 && sin_k == 0))
115 return FPBits::zero(sign: xbits.sign()).get_val();
116
117 return static_cast<float>(fputil::multiply_add(
118 x: sin_y, y: cos_k, z: fputil::multiply_add(x: cosm1_y, y: sin_k, z: sin_k)));
119}
120
121} // namespace math
122} // namespace LIBC_NAMESPACE_DECL
123
124#endif // LLVM_LIBC_SRC___SUPPORT_MATH_SINPIF_H
125