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/multiply_add.h"
16#include "src/__support/FPUtil/polyeval.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 // sinpi(x) underflows for 0 < |x| <= 0x1.45f3p-128.
63 if (LIBC_UNLIKELY(x_abs <= 0x0028'be60U))
64 fputil::set_errno_if_required(ERANGE);
65
66 // For very small values we can approximate sinpi(x) with x * pi
67 // An exhaustive test shows that this is accurate for |x| < 9.546391 ×
68 // 10-8
69 double xdpi = xd * 0x1.921fb54442d18p1;
70 return static_cast<float>(xdpi);
71 }
72
73 // |x| < 1/16.
74 double xsq = xd * xd;
75
76 // Degree-9 polynomial approximation:
77 // sinpi(x) ~ x + a_3 x^3 + a_5 x^5 + a_7 x^7 + a_9 x^9
78 // = x (1 + a_3 x^2 + ... + a_9 x^8)
79 // = x * P(x^2)
80 // generated by Sollya with the following commands:
81 // > display = hexadecimal;
82 // > Q = fpminimax(sin(pi * x)/x, [|0, 2, 4, 6, 8|], [|D...|], [0, 1/16]);
83 double result = fputil::polyeval(
84 x: xsq, a0: 0x1.921fb54442d18p1, a: -0x1.4abbce625bbf2p2, a: 0x1.466bc675e116ap1,
85 a: -0x1.32d2c0b62d41cp-1, a: 0x1.501ec4497cb7dp-4);
86 return static_cast<float>(xd * result);
87 }
88
89 // Numbers greater or equal to 2^23 are always integers or NaN
90 if (LIBC_UNLIKELY(x_abs >= 0x4B00'0000)) {
91
92 // check for NaN values
93 if (LIBC_UNLIKELY(x_abs >= 0x7f80'0000U)) {
94 if (xbits.is_signaling_nan()) {
95 fputil::raise_except_if_required(FE_INVALID);
96 return FPBits::quiet_nan().get_val();
97 }
98
99 if (x_abs == 0x7f80'0000U) {
100 fputil::set_errno_if_required(EDOM);
101 fputil::raise_except_if_required(FE_INVALID);
102 }
103
104 return x + FPBits::quiet_nan().get_val();
105 }
106
107 return FPBits::zero(sign: xbits.sign()).get_val();
108 }
109
110 // Combine the results with the sine of sum formula:
111 // sin(x * pi) = sin((k + y)*pi/32)
112 // = sin(y*pi/32) * cos(k*pi/32) + cos(y*pi/32) * sin(k*pi/32)
113 // = sin_y * cos_k + (1 + cosm1_y) * sin_k
114 // = sin_y * cos_k + (cosm1_y * sin_k + sin_k)
115 double sin_k = 0, cos_k = 0, sin_y = 0, cosm1_y = 0;
116 sincospif_eval(xd, sin_k, cos_k, sin_y, cosm1_y);
117
118 if (LIBC_UNLIKELY(sin_y == 0 && sin_k == 0))
119 return FPBits::zero(sign: xbits.sign()).get_val();
120
121 return static_cast<float>(fputil::multiply_add(
122 x: sin_y, y: cos_k, z: fputil::multiply_add(x: cosm1_y, y: sin_k, z: sin_k)));
123}
124
125} // namespace math
126} // namespace LIBC_NAMESPACE_DECL
127
128#endif // LLVM_LIBC_SRC___SUPPORT_MATH_SINPIF_H
129