1//===----------------------------------------------------------------------===//
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/// \file
10/// Double-precision implementation of sinf.
11///
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_LIBC_SRC___SUPPORT_MATH_SINF_DOUBLE_EVAL_H
15#define LLVM_LIBC_SRC___SUPPORT_MATH_SINF_DOUBLE_EVAL_H
16
17#include "src/__support/FPUtil/BasicOperations.h"
18#include "src/__support/FPUtil/FEnvImpl.h"
19#include "src/__support/FPUtil/FPBits.h"
20#include "src/__support/FPUtil/PolyEval.h"
21#include "src/__support/FPUtil/multiply_add.h"
22#include "src/__support/FPUtil/rounding_mode.h"
23#include "src/__support/macros/config.h"
24#include "src/__support/macros/optimization.h" // LIBC_UNLIKELY
25#include "src/__support/macros/properties/cpu_features.h" // LIBC_TARGET_CPU_HAS_FMA
26
27#include "src/__support/math/sincosf_utils.h"
28
29#ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE
30#include "src/__support/math/range_reduction_fma.h"
31#else // !LIBC_TARGET_CPU_HAS_FMA_DOUBLE
32#include "src/__support/math/range_reduction.h"
33#endif // LIBC_TARGET_CPU_HAS_FMA_DOUBLE
34
35namespace LIBC_NAMESPACE_DECL {
36
37namespace math {
38namespace double_eval {
39
40LIBC_INLINE float sinf(float x) {
41 using namespace sincosf_utils_internal;
42 using FPBits = typename fputil::FPBits<float>;
43 FPBits xbits(x);
44
45 uint32_t x_u = xbits.uintval();
46 uint32_t x_abs = x_u & 0x7fff'ffffU;
47 double xd = static_cast<double>(x);
48
49 // Range reduction:
50 // For |x| > pi/32, we perform range reduction as follows:
51 // Find k and y such that:
52 // x = (k + y) * pi/32
53 // k is an integer
54 // |y| < 0.5
55 // For small range (|x| < 2^45 when FMA instructions are available, 2^22
56 // otherwise), this is done by performing:
57 // k = round(x * 32/pi)
58 // y = x * 32/pi - k
59 // For large range, we will omit all the higher parts of 32/pi such that the
60 // least significant bits of their full products with x are larger than 63,
61 // since sin((k + y + 64*i) * pi/32) = sin(x + i * 2pi) = sin(x).
62 //
63 // When FMA instructions are not available, we store the digits of 32/pi in
64 // chunks of 28-bit precision. This will make sure that the products:
65 // x * THIRTYTWO_OVER_PI_28[i] are all exact.
66 // When FMA instructions are available, we simply store the digits of 32/pi in
67 // chunks of doubles (53-bit of precision).
68 // So when multiplying by the largest values of single precision, the
69 // resulting output should be correct up to 2^(-208 + 128) ~ 2^-80. By the
70 // worst-case analysis of range reduction, |y| >= 2^-38, so this should give
71 // us more than 40 bits of accuracy. For the worst-case estimation of range
72 // reduction, see for instances:
73 // Elementary Functions by J-M. Muller, Chapter 11,
74 // Handbook of Floating-Point Arithmetic by J-M. Muller et. al.,
75 // Chapter 10.2.
76 //
77 // Once k and y are computed, we then deduce the answer by the sine of sum
78 // formula:
79 // sin(x) = sin((k + y)*pi/32)
80 // = sin(y*pi/32) * cos(k*pi/32) + cos(y*pi/32) * sin(k*pi/32)
81 // The values of sin(k*pi/32) and cos(k*pi/32) for k = 0..31 are precomputed
82 // and stored using a vector of 32 doubles. Sin(y*pi/32) and cos(y*pi/32) are
83 // computed using degree-7 and degree-6 minimax polynomials generated by
84 // Sollya respectively.
85
86 // |x| <= pi/16
87 if (LIBC_UNLIKELY(x_abs <= 0x3e49'0fdbU)) {
88
89 // |x| < 0x1.d12ed2p-12f
90 if (LIBC_UNLIKELY(x_abs < 0x39e8'9769U)) {
91 if (LIBC_UNLIKELY(x_abs == 0U)) {
92 // For signed zeros.
93 return x;
94 }
95 // When |x| < 2^-12, the relative error of the approximation sin(x) ~ x
96 // is:
97 // |sin(x) - x| / |sin(x)| < |x^3| / (6|x|)
98 // = x^2 / 6
99 // < 2^-25
100 // < epsilon(1)/2.
101 // So the correctly rounded values of sin(x) are:
102 // = x - sign(x)*eps(x) if rounding mode = FE_TOWARDZERO,
103 // or (rounding mode = FE_UPWARD and x is
104 // negative),
105 // = x otherwise.
106 // To simplify the rounding decision and make it more efficient, we use
107 // fma(x, -2^-25, x) instead.
108 // An exhaustive test shows that this formula work correctly for all
109 // rounding modes up to |x| < 0x1.c555dep-11f.
110 // Note: to use the formula x - 2^-25*x to decide the correct rounding, we
111 // do need fma(x, -2^-25, x) to prevent underflow caused by -2^-25*x when
112 // |x| < 2^-125. For targets without FMA instructions, we simply use
113 // double for intermediate results as it is more efficient than using an
114 // emulated version of FMA.
115#if defined(LIBC_TARGET_CPU_HAS_FMA_FLOAT)
116 return fputil::multiply_add(x, -0x1.0p-25f, x);
117#else
118 return static_cast<float>(fputil::multiply_add(x: xd, y: -0x1.0p-25, z: xd));
119#endif // LIBC_TARGET_CPU_HAS_FMA_FLOAT
120 }
121
122 // |x| < pi/16.
123 double xsq = xd * xd;
124
125 // Degree-9 polynomial approximation:
126 // sin(x) ~ x + a_3 x^3 + a_5 x^5 + a_7 x^7 + a_9 x^9
127 // = x (1 + a_3 x^2 + ... + a_9 x^8)
128 // = x * P(x^2)
129 // generated by Sollya with the following commands:
130 // > display = hexadecimal;
131 // > Q = fpminimax(sin(x)/x, [|0, 2, 4, 6, 8|], [|1, D...|], [0, pi/16]);
132 double result =
133 fputil::polyeval(x: xsq, a0: 1.0, a: -0x1.55555555554c6p-3, a: 0x1.1111111085e65p-7,
134 a: -0x1.a019f70fb4d4fp-13, a: 0x1.718d179815e74p-19);
135 return static_cast<float>(xd * result);
136 }
137
138#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
139 if (LIBC_UNLIKELY(x_abs == 0x4619'9998U)) { // x = 0x1.33333p13
140 float r = -0x1.63f4bap-2f;
141#ifndef LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
142 int rounding = fputil::quick_get_round();
143 if ((rounding == FE_DOWNWARD && xbits.is_pos()) ||
144 (rounding == FE_UPWARD && xbits.is_neg()))
145 r = -0x1.63f4bcp-2f;
146#endif // !LIBC_MATH_HAS_ASSUME_ROUND_NEAREST_ONLY
147 return xbits.is_neg() ? -r : r;
148 }
149#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS
150
151 if (LIBC_UNLIKELY(x_abs >= 0x7f80'0000U)) {
152 if (xbits.is_signaling_nan()) {
153 fputil::raise_except_if_required(FE_INVALID);
154 return FPBits::quiet_nan().get_val();
155 }
156
157 if (x_abs == 0x7f80'0000U) {
158 fputil::set_errno_if_required(EDOM);
159 fputil::raise_except_if_required(FE_INVALID);
160 }
161 return x + FPBits::quiet_nan().get_val();
162 }
163
164 // Combine the results with the sine of sum formula:
165 // sin(x) = sin((k + y)*pi/32)
166 // = sin(y*pi/32) * cos(k*pi/32) + cos(y*pi/32) * sin(k*pi/32)
167 // = sin_y * cos_k + (1 + cosm1_y) * sin_k
168 // = sin_y * cos_k + (cosm1_y * sin_k + sin_k)
169 double sin_k, cos_k, sin_y, cosm1_y;
170
171 sincosf_eval(xd, x_abs, sin_k, cos_k, sin_y, cosm1_y);
172
173 return static_cast<float>(fputil::multiply_add(
174 x: sin_y, y: cos_k, z: fputil::multiply_add(x: cosm1_y, y: sin_k, z: sin_k)));
175}
176
177} // namespace double_eval
178} // namespace math
179
180} // namespace LIBC_NAMESPACE_DECL
181
182#endif // LLVM_LIBC_SRC___SUPPORT_MATH_SINF_DOUBLE_EVAL_H
183