1 | //===-- Single-precision asin function ------------------------------------===// |
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 | #include "src/math/asinf.h" |
10 | #include "src/__support/FPUtil/FEnvImpl.h" |
11 | #include "src/__support/FPUtil/FPBits.h" |
12 | #include "src/__support/FPUtil/PolyEval.h" |
13 | #include "src/__support/FPUtil/except_value_utils.h" |
14 | #include "src/__support/FPUtil/multiply_add.h" |
15 | #include "src/__support/FPUtil/sqrt.h" |
16 | #include "src/__support/macros/config.h" |
17 | #include "src/__support/macros/optimization.h" // LIBC_UNLIKELY |
18 | #include "src/__support/macros/properties/cpu_features.h" // LIBC_TARGET_CPU_HAS_FMA |
19 | |
20 | #include "inv_trigf_utils.h" |
21 | |
22 | namespace LIBC_NAMESPACE_DECL { |
23 | |
24 | #ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
25 | static constexpr size_t N_EXCEPTS = 2; |
26 | |
27 | // Exceptional values when |x| <= 0.5 |
28 | static constexpr fputil::ExceptValues<float, N_EXCEPTS> ASINF_EXCEPTS_LO = {{ |
29 | // (inputs, RZ output, RU offset, RD offset, RN offset) |
30 | // x = 0x1.137f0cp-5, asinf(x) = 0x1.138c58p-5 (RZ) |
31 | {0x3d09bf86, 0x3d09c62c, 1, 0, 1}, |
32 | // x = 0x1.cbf43cp-4, asinf(x) = 0x1.cced1cp-4 (RZ) |
33 | {0x3de5fa1e, 0x3de6768e, 1, 0, 0}, |
34 | }}; |
35 | |
36 | // Exceptional values when 0.5 < |x| <= 1 |
37 | static constexpr fputil::ExceptValues<float, N_EXCEPTS> ASINF_EXCEPTS_HI = {{ |
38 | // (inputs, RZ output, RU offset, RD offset, RN offset) |
39 | // x = 0x1.107434p-1, asinf(x) = 0x1.1f4b64p-1 (RZ) |
40 | {0x3f083a1a, 0x3f0fa5b2, 1, 0, 0}, |
41 | // x = 0x1.ee836cp-1, asinf(x) = 0x1.4f0654p0 (RZ) |
42 | {0x3f7741b6, 0x3fa7832a, 1, 0, 0}, |
43 | }}; |
44 | #endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
45 | |
46 | LLVM_LIBC_FUNCTION(float, asinf, (float x)) { |
47 | using FPBits = typename fputil::FPBits<float>; |
48 | |
49 | FPBits xbits(x); |
50 | uint32_t x_uint = xbits.uintval(); |
51 | uint32_t x_abs = xbits.uintval() & 0x7fff'ffffU; |
52 | constexpr double SIGN[2] = {1.0, -1.0}; |
53 | uint32_t x_sign = x_uint >> 31; |
54 | |
55 | // |x| <= 0.5-ish |
56 | if (x_abs < 0x3f04'471dU) { |
57 | // |x| < 0x1.d12edp-12 |
58 | if (LIBC_UNLIKELY(x_abs < 0x39e8'9768U)) { |
59 | // When |x| < 2^-12, the relative error of the approximation asin(x) ~ x |
60 | // is: |
61 | // |asin(x) - x| / |asin(x)| < |x^3| / (6|x|) |
62 | // = x^2 / 6 |
63 | // < 2^-25 |
64 | // < epsilon(1)/2. |
65 | // So the correctly rounded values of asin(x) are: |
66 | // = x + sign(x)*eps(x) if rounding mode = FE_TOWARDZERO, |
67 | // or (rounding mode = FE_UPWARD and x is |
68 | // negative), |
69 | // = x otherwise. |
70 | // To simplify the rounding decision and make it more efficient, we use |
71 | // fma(x, 2^-25, x) instead. |
72 | // An exhaustive test shows that this formula work correctly for all |
73 | // rounding modes up to |x| < 0x1.d12edp-12. |
74 | // Note: to use the formula x + 2^-25*x to decide the correct rounding, we |
75 | // do need fma(x, 2^-25, x) to prevent underflow caused by 2^-25*x when |
76 | // |x| < 2^-125. For targets without FMA instructions, we simply use |
77 | // double for intermediate results as it is more efficient than using an |
78 | // emulated version of FMA. |
79 | #if defined(LIBC_TARGET_CPU_HAS_FMA_FLOAT) |
80 | return fputil::multiply_add(x, 0x1.0p-25f, x); |
81 | #else |
82 | double xd = static_cast<double>(x); |
83 | return static_cast<float>(fputil::multiply_add(xd, 0x1.0p-25, xd)); |
84 | #endif // LIBC_TARGET_CPU_HAS_FMA_FLOAT |
85 | } |
86 | |
87 | #ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
88 | // Check for exceptional values |
89 | if (auto r = ASINF_EXCEPTS_LO.lookup_odd(x_abs, x_sign); |
90 | LIBC_UNLIKELY(r.has_value())) |
91 | return r.value(); |
92 | #endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
93 | |
94 | // For |x| <= 0.5, we approximate asinf(x) by: |
95 | // asin(x) = x * P(x^2) |
96 | // Where P(X^2) = Q(X) is a degree-20 minimax even polynomial approximating |
97 | // asin(x)/x on [0, 0.5] generated by Sollya with: |
98 | // > Q = fpminimax(asin(x)/x, [|0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20|], |
99 | // [|1, D...|], [0, 0.5]); |
100 | // An exhaustive test shows that this approximation works well up to a |
101 | // little more than 0.5. |
102 | double xd = static_cast<double>(x); |
103 | double xsq = xd * xd; |
104 | double x3 = xd * xsq; |
105 | double r = asin_eval(xsq); |
106 | return static_cast<float>(fputil::multiply_add(x3, r, xd)); |
107 | } |
108 | |
109 | // |x| > 1, return NaNs. |
110 | if (LIBC_UNLIKELY(x_abs > 0x3f80'0000U)) { |
111 | if (xbits.is_signaling_nan()) { |
112 | fputil::raise_except_if_required(FE_INVALID); |
113 | return FPBits::quiet_nan().get_val(); |
114 | } |
115 | |
116 | if (x_abs <= 0x7f80'0000U) { |
117 | fputil::set_errno_if_required(EDOM); |
118 | fputil::raise_except_if_required(FE_INVALID); |
119 | } |
120 | |
121 | return FPBits::quiet_nan().get_val(); |
122 | } |
123 | |
124 | #ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
125 | // Check for exceptional values |
126 | if (auto r = ASINF_EXCEPTS_HI.lookup_odd(x_abs, x_sign); |
127 | LIBC_UNLIKELY(r.has_value())) |
128 | return r.value(); |
129 | #endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS |
130 | |
131 | // When |x| > 0.5, we perform range reduction as follow: |
132 | // |
133 | // Assume further that 0.5 < x <= 1, and let: |
134 | // y = asin(x) |
135 | // We will use the double angle formula: |
136 | // cos(2y) = 1 - 2 sin^2(y) |
137 | // and the complement angle identity: |
138 | // x = sin(y) = cos(pi/2 - y) |
139 | // = 1 - 2 sin^2 (pi/4 - y/2) |
140 | // So: |
141 | // sin(pi/4 - y/2) = sqrt( (1 - x)/2 ) |
142 | // And hence: |
143 | // pi/4 - y/2 = asin( sqrt( (1 - x)/2 ) ) |
144 | // Equivalently: |
145 | // asin(x) = y = pi/2 - 2 * asin( sqrt( (1 - x)/2 ) ) |
146 | // Let u = (1 - x)/2, then: |
147 | // asin(x) = pi/2 - 2 * asin( sqrt(u) ) |
148 | // Moreover, since 0.5 < x <= 1: |
149 | // 0 <= u < 1/4, and 0 <= sqrt(u) < 0.5, |
150 | // And hence we can reuse the same polynomial approximation of asin(x) when |
151 | // |x| <= 0.5: |
152 | // asin(x) ~ pi/2 - 2 * sqrt(u) * P(u), |
153 | |
154 | xbits.set_sign(Sign::POS); |
155 | double sign = SIGN[x_sign]; |
156 | double xd = static_cast<double>(xbits.get_val()); |
157 | double u = fputil::multiply_add(-0.5, xd, 0.5); |
158 | double c1 = sign * (-2 * fputil::sqrt<double>(u)); |
159 | double c2 = fputil::multiply_add(sign, M_MATH_PI_2, c1); |
160 | double c3 = c1 * u; |
161 | |
162 | double r = asin_eval(u); |
163 | return static_cast<float>(fputil::multiply_add(c3, r, c2)); |
164 | } |
165 | |
166 | } // namespace LIBC_NAMESPACE_DECL |
167 | |