/rust/registry/src/index.crates.io-1949cf8c6b5b557f/pxfm-0.1.28/src/asinpi.rs
Line | Count | Source |
1 | | /* |
2 | | * // Copyright (c) Radzivon Bartoshyk 6/2025. All rights reserved. |
3 | | * // |
4 | | * // Redistribution and use in source and binary forms, with or without modification, |
5 | | * // are permitted provided that the following conditions are met: |
6 | | * // |
7 | | * // 1. Redistributions of source code must retain the above copyright notice, this |
8 | | * // list of conditions and the following disclaimer. |
9 | | * // |
10 | | * // 2. Redistributions in binary form must reproduce the above copyright notice, |
11 | | * // this list of conditions and the following disclaimer in the documentation |
12 | | * // and/or other materials provided with the distribution. |
13 | | * // |
14 | | * // 3. Neither the name of the copyright holder nor the names of its |
15 | | * // contributors may be used to endorse or promote products derived from |
16 | | * // this software without specific prior written permission. |
17 | | * // |
18 | | * // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
19 | | * // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
20 | | * // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
21 | | * // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
22 | | * // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
23 | | * // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
24 | | * // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
25 | | * // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
26 | | * // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
27 | | * // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | | */ |
29 | | use crate::acospi::INV_PI_DD; |
30 | | use crate::asin::asin_eval; |
31 | | use crate::asin_eval_dyadic::asin_eval_dyadic; |
32 | | use crate::common::{dd_fmla, dyad_fmla, f_fmla}; |
33 | | use crate::double_double::DoubleDouble; |
34 | | use crate::dyadic_float::{DyadicFloat128, DyadicSign}; |
35 | | use crate::rounding::CpuRound; |
36 | | |
37 | | /// Computes asin(x)/PI |
38 | | /// |
39 | | /// Max found ULP 0.5 |
40 | 0 | pub fn f_asinpi(x: f64) -> f64 { |
41 | 0 | let x_e = (x.to_bits() >> 52) & 0x7ff; |
42 | | const E_BIAS: u64 = (1u64 << (11 - 1u64)) - 1u64; |
43 | | |
44 | 0 | let x_abs = f64::from_bits(x.to_bits() & 0x7fff_ffff_ffff_ffff); |
45 | | |
46 | | // |x| < 0.5. |
47 | 0 | if x_e < E_BIAS - 1 { |
48 | | // |x| < 2^-26. |
49 | 0 | if x_e < E_BIAS - 26 { |
50 | | // When |x| < 2^-26, the relative error of the approximation asin(x) ~ x |
51 | | // is: |
52 | | // |asin(x) - x| / |asin(x)| < |x^3| / (6|x|) |
53 | | // = x^2 / 6 |
54 | | // < 2^-54 |
55 | | // < epsilon(1)/2. |
56 | | // = x otherwise. , |
57 | 0 | if x.abs() == 0. { |
58 | 0 | return x; |
59 | 0 | } |
60 | | |
61 | 0 | if x_e < E_BIAS - 56 { |
62 | 0 | if (x_abs.to_bits().wrapping_shl(12)) == 0x59af9a1194efe000u64 { |
63 | 0 | let e = (x.to_bits() >> 52) & 0x7ff; |
64 | 0 | let h = f64::from_bits(0x3c7b824198b94a89); |
65 | 0 | let l = f64::from_bits(0x391fffffffffffff); |
66 | 0 | let mut t = (if x > 0. { 1.0f64 } else { -1.0f64 }).to_bits(); |
67 | 0 | t = t.wrapping_sub(0x3c9u64.wrapping_sub(e).wrapping_shl(52)); |
68 | 0 | return f_fmla(l, f64::from_bits(t), h * f64::from_bits(t)); |
69 | 0 | } |
70 | | |
71 | 0 | let h = x * INV_PI_DD.hi; |
72 | 0 | let sx = x * f64::from_bits(0x4690000000000000); /* scale x */ |
73 | 0 | let mut l = dd_fmla(sx, INV_PI_DD.hi, -h * f64::from_bits(0x4690000000000000)); |
74 | 0 | l = dd_fmla(sx, INV_PI_DD.lo, l); |
75 | | /* scale back */ |
76 | 0 | let res = dyad_fmla(l, f64::from_bits(0x3950000000000000), h); |
77 | 0 | return res; |
78 | 0 | } |
79 | | |
80 | | /* We use the Sollya polynomial 0x1.45f306dc9c882a53f84eafa3ea4p-2 * x |
81 | | + 0x1.b2995e7b7b606p-5 * x^3, with relative error bounded by 2^-106.965 |
82 | | on [2^-53, 2^-26] */ |
83 | | const C1H: f64 = f64::from_bits(0x3fd45f306dc9c883); |
84 | | const C1L: f64 = f64::from_bits(0xbc76b01ec5417057); |
85 | | const C3: f64 = f64::from_bits(0x3fab2995e7b7b606); |
86 | 0 | let h = C1H; |
87 | 0 | let l = dd_fmla(C3, x * x, C1L); |
88 | | /* multiply h+l by x */ |
89 | 0 | let hh = h * x; |
90 | 0 | let mut ll = dd_fmla(h, x, -hh); |
91 | | /* hh+ll = h*x */ |
92 | 0 | ll = dd_fmla(l, x, ll); |
93 | 0 | return hh + ll; |
94 | 0 | } |
95 | | |
96 | 0 | let x_sq = DoubleDouble::from_exact_mult(x, x); |
97 | 0 | let err = x_abs * f64::from_bits(0x3cc0000000000000); |
98 | | // Polynomial approximation: |
99 | | // p ~ asin(x)/x |
100 | | |
101 | 0 | let (p, err) = asin_eval(x_sq, err); |
102 | | // asin(x) ~ x * (ASIN_COEFFS[idx][0] + p) |
103 | 0 | let mut r0 = DoubleDouble::from_exact_mult(x, p.hi); |
104 | 0 | let mut r_lo = f_fmla(x, p.lo, r0.lo); |
105 | | |
106 | 0 | r0 = DoubleDouble::mult(DoubleDouble::new(r_lo, r0.hi), INV_PI_DD); |
107 | 0 | r_lo = r0.lo; |
108 | | |
109 | 0 | let r_upper = r0.hi + (r_lo + err); |
110 | 0 | let r_lower = r0.hi + (r_lo - err); |
111 | | |
112 | 0 | if r_upper == r_lower { |
113 | 0 | return r_upper; |
114 | 0 | } |
115 | | |
116 | | // Ziv's accuracy test failed, perform 128-bit calculation. |
117 | | |
118 | | // Recalculate mod 1/64. |
119 | 0 | let idx = (x_sq.hi * f64::from_bits(0x4050000000000000)).cpu_round() as usize; |
120 | | |
121 | | // Get x^2 - idx/64 exactly. When FMA is available, double-double |
122 | | // multiplication will be correct for all rounding modes. Otherwise, we use |
123 | | // Float128 directly. |
124 | 0 | let x_f128 = DyadicFloat128::new_from_f64(x); |
125 | | |
126 | | let u: DyadicFloat128; |
127 | | #[cfg(any( |
128 | | all( |
129 | | any(target_arch = "x86", target_arch = "x86_64"), |
130 | | target_feature = "fma" |
131 | | ), |
132 | | target_arch = "aarch64" |
133 | | ))] |
134 | | { |
135 | | // u = x^2 - idx/64 |
136 | | let u_hi = DyadicFloat128::new_from_f64(f_fmla( |
137 | | idx as f64, |
138 | | f64::from_bits(0xbf90000000000000), |
139 | | x_sq.hi, |
140 | | )); |
141 | | u = u_hi.quick_add(&DyadicFloat128::new_from_f64(x_sq.lo)); |
142 | | } |
143 | | |
144 | | #[cfg(not(any( |
145 | | all( |
146 | | any(target_arch = "x86", target_arch = "x86_64"), |
147 | | target_feature = "fma" |
148 | | ), |
149 | | target_arch = "aarch64" |
150 | | )))] |
151 | 0 | { |
152 | 0 | let x_sq_f128 = x_f128.quick_mul(&x_f128); |
153 | 0 | u = x_sq_f128.quick_add(&DyadicFloat128::new_from_f64( |
154 | 0 | idx as f64 * (f64::from_bits(0xbf90000000000000)), |
155 | 0 | )); |
156 | 0 | } |
157 | | |
158 | 0 | let p_f128 = asin_eval_dyadic(u, idx); |
159 | 0 | let mut r = x_f128.quick_mul(&p_f128); |
160 | 0 | r = r.quick_mul(&crate::acospi::INV_PI_F128); |
161 | 0 | return r.fast_as_f64(); |
162 | 0 | } |
163 | | |
164 | | const PI_OVER_TWO: DoubleDouble = DoubleDouble::new( |
165 | | f64::from_bits(0x3c91a62633145c07), |
166 | | f64::from_bits(0x3ff921fb54442d18), |
167 | | ); |
168 | | |
169 | 0 | let x_sign = if x.is_sign_negative() { -1.0 } else { 1.0 }; |
170 | | |
171 | | // |x| >= 1 |
172 | 0 | if x_e >= E_BIAS { |
173 | | // x = +-1, asin(x) = +- pi/2 |
174 | 0 | if x_abs == 1.0 { |
175 | | // return +- pi/2 |
176 | 0 | return x * 0.5; // asinpi_specific |
177 | 0 | } |
178 | | // |x| > 1, return NaN. |
179 | 0 | if x.is_nan() { |
180 | 0 | return x; |
181 | 0 | } |
182 | 0 | return f64::NAN; |
183 | 0 | } |
184 | | |
185 | | // u = (1 - |x|)/2 |
186 | 0 | let u = f_fmla(x_abs, -0.5, 0.5); |
187 | | // v_hi + v_lo ~ sqrt(u). |
188 | | // Let: |
189 | | // h = u - v_hi^2 = (sqrt(u) - v_hi) * (sqrt(u) + v_hi) |
190 | | // Then: |
191 | | // sqrt(u) = v_hi + h / (sqrt(u) + v_hi) |
192 | | // ~ v_hi + h / (2 * v_hi) |
193 | | // So we can use: |
194 | | // v_lo = h / (2 * v_hi). |
195 | | // Then, |
196 | | // asin(x) ~ pi/2 - 2*(v_hi + v_lo) * P(u) |
197 | 0 | let v_hi = u.sqrt(); |
198 | | let h; |
199 | | #[cfg(any( |
200 | | all( |
201 | | any(target_arch = "x86", target_arch = "x86_64"), |
202 | | target_feature = "fma" |
203 | | ), |
204 | | target_arch = "aarch64" |
205 | | ))] |
206 | | { |
207 | | h = f_fmla(v_hi, -v_hi, u); |
208 | | } |
209 | | #[cfg(not(any( |
210 | | all( |
211 | | any(target_arch = "x86", target_arch = "x86_64"), |
212 | | target_feature = "fma" |
213 | | ), |
214 | | target_arch = "aarch64" |
215 | | )))] |
216 | 0 | { |
217 | 0 | let v_hi_sq = DoubleDouble::from_exact_mult(v_hi, v_hi); |
218 | 0 | h = (u - v_hi_sq.hi) - v_hi_sq.lo; |
219 | 0 | } |
220 | | // Scale v_lo and v_hi by 2 from the formula: |
221 | | // vh = v_hi * 2 |
222 | | // vl = 2*v_lo = h / v_hi. |
223 | 0 | let vh = v_hi * 2.0; |
224 | 0 | let vl = h / v_hi; |
225 | | |
226 | | // Polynomial approximation: |
227 | | // p ~ asin(sqrt(u))/sqrt(u) |
228 | 0 | let err = vh * f64::from_bits(0x3cc0000000000000); |
229 | | |
230 | 0 | let (p, err) = asin_eval(DoubleDouble::new(0.0, u), err); |
231 | | |
232 | | // Perform computations in double-double arithmetic: |
233 | | // asin(x) = pi/2 - (v_hi + v_lo) * (ASIN_COEFFS[idx][0] + p) |
234 | 0 | let r0 = DoubleDouble::quick_mult(DoubleDouble::new(vl, vh), p); |
235 | 0 | let mut r = DoubleDouble::from_exact_add(PI_OVER_TWO.hi, -r0.hi); |
236 | | |
237 | 0 | let mut r_lo = PI_OVER_TWO.lo - r0.lo + r.lo; |
238 | | |
239 | 0 | let p = DoubleDouble::mult(DoubleDouble::new(r_lo, r.hi), INV_PI_DD); |
240 | 0 | r_lo = p.lo; |
241 | 0 | r.hi = p.hi; |
242 | | |
243 | | let (r_upper, r_lower); |
244 | | |
245 | | #[cfg(any( |
246 | | all( |
247 | | any(target_arch = "x86", target_arch = "x86_64"), |
248 | | target_feature = "fma" |
249 | | ), |
250 | | target_arch = "aarch64" |
251 | | ))] |
252 | | { |
253 | | r_upper = f_fmla(r.hi, x_sign, f_fmla(r_lo, x_sign, err)); |
254 | | r_lower = f_fmla(r.hi, x_sign, f_fmla(r_lo, x_sign, -err)); |
255 | | } |
256 | | #[cfg(not(any( |
257 | | all( |
258 | | any(target_arch = "x86", target_arch = "x86_64"), |
259 | | target_feature = "fma" |
260 | | ), |
261 | | target_arch = "aarch64" |
262 | | )))] |
263 | 0 | { |
264 | 0 | let r_lo = r_lo * x_sign; |
265 | 0 | let r_hi = r.hi * x_sign; |
266 | 0 | r_upper = r_hi + (r_lo + err); |
267 | 0 | r_lower = r.hi + (r_lo - err); |
268 | 0 | } |
269 | | |
270 | 0 | if r_upper == r_lower { |
271 | 0 | return r_upper; |
272 | 0 | } |
273 | | |
274 | | // Ziv's accuracy test failed, we redo the computations in Float128. |
275 | | // Recalculate mod 1/64. |
276 | 0 | let idx = (u * f64::from_bits(0x4050000000000000)).cpu_round() as usize; |
277 | | |
278 | | // After the first step of Newton-Raphson approximating v = sqrt(u), we have |
279 | | // that: |
280 | | // sqrt(u) = v_hi + h / (sqrt(u) + v_hi) |
281 | | // v_lo = h / (2 * v_hi) |
282 | | // With error: |
283 | | // sqrt(u) - (v_hi + v_lo) = h * ( 1/(sqrt(u) + v_hi) - 1/(2*v_hi) ) |
284 | | // = -h^2 / (2*v * (sqrt(u) + v)^2). |
285 | | // Since: |
286 | | // (sqrt(u) + v_hi)^2 ~ (2sqrt(u))^2 = 4u, |
287 | | // we can add another correction term to (v_hi + v_lo) that is: |
288 | | // v_ll = -h^2 / (2*v_hi * 4u) |
289 | | // = -v_lo * (h / 4u) |
290 | | // = -vl * (h / 8u), |
291 | | // making the errors: |
292 | | // sqrt(u) - (v_hi + v_lo + v_ll) = O(h^3) |
293 | | // well beyond 128-bit precision needed. |
294 | | |
295 | | // Get the rounding error of vl = 2 * v_lo ~ h / vh |
296 | | // Get full product of vh * vl |
297 | | let vl_lo; |
298 | | #[cfg(any( |
299 | | all( |
300 | | any(target_arch = "x86", target_arch = "x86_64"), |
301 | | target_feature = "fma" |
302 | | ), |
303 | | target_arch = "aarch64" |
304 | | ))] |
305 | | { |
306 | | vl_lo = f_fmla(-v_hi, vl, h) / v_hi; |
307 | | } |
308 | | #[cfg(not(any( |
309 | | all( |
310 | | any(target_arch = "x86", target_arch = "x86_64"), |
311 | | target_feature = "fma" |
312 | | ), |
313 | | target_arch = "aarch64" |
314 | | )))] |
315 | 0 | { |
316 | 0 | let vh_vl = DoubleDouble::from_exact_mult(v_hi, vl); |
317 | 0 | vl_lo = ((h - vh_vl.hi) - vh_vl.lo) / v_hi; |
318 | 0 | } |
319 | | |
320 | | // vll = 2*v_ll = -vl * (h / (4u)). |
321 | 0 | let t = h * (-0.25) / u; |
322 | 0 | let vll = f_fmla(vl, t, vl_lo); |
323 | | // m_v = -(v_hi + v_lo + v_ll). |
324 | 0 | let mv0 = DyadicFloat128::new_from_f64(vl) + DyadicFloat128::new_from_f64(vll); |
325 | 0 | let mut m_v = DyadicFloat128::new_from_f64(vh) + mv0; |
326 | 0 | m_v.sign = DyadicSign::Neg; |
327 | | |
328 | | // Perform computations in Float128: |
329 | | // asin(x) = pi/2 - (v_hi + v_lo + vll) * P(u). |
330 | 0 | let y_f128 = |
331 | 0 | DyadicFloat128::new_from_f64(f_fmla(idx as f64, f64::from_bits(0xbf90000000000000), u)); |
332 | | |
333 | | const PI_OVER_TWO_F128: DyadicFloat128 = DyadicFloat128 { |
334 | | sign: DyadicSign::Pos, |
335 | | exponent: -127, |
336 | | mantissa: 0xc90fdaa2_2168c234_c4c6628b_80dc1cd1_u128, |
337 | | }; |
338 | | |
339 | 0 | let p_f128 = asin_eval_dyadic(y_f128, idx); |
340 | 0 | let r0_f128 = m_v * p_f128; |
341 | 0 | let mut r_f128 = PI_OVER_TWO_F128 + r0_f128; |
342 | | |
343 | 0 | if x.is_sign_negative() { |
344 | 0 | r_f128.sign = DyadicSign::Neg; |
345 | 0 | } |
346 | | |
347 | 0 | r_f128 = r_f128.quick_mul(&crate::acospi::INV_PI_F128); |
348 | | |
349 | 0 | r_f128.fast_as_f64() |
350 | 0 | } |
351 | | |
352 | | #[cfg(test)] |
353 | | mod tests { |
354 | | use super::*; |
355 | | |
356 | | #[test] |
357 | | fn f_asinpi_test() { |
358 | | assert_eq!( |
359 | | f_asinpi(-0.00000000032681723993732703), |
360 | | -0.00000000010402915844735117 |
361 | | ); |
362 | | assert_eq!(f_asinpi(0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000017801371778309684), 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005666352624669099); |
363 | | assert_eq!(f_asinpi(0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000026752519513526076), 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008515591441480124); |
364 | | assert_eq!(f_asinpi(-0.4), -0.13098988043445461); |
365 | | assert_eq!(f_asinpi(-0.8), -0.2951672353008666); |
366 | | assert_eq!(f_asinpi(0.4332432142124432), 0.14263088583055605); |
367 | | assert_eq!(f_asinpi(0.8543543534343434), 0.326047108714517); |
368 | | assert_eq!(f_asinpi(0.00323146509843243), 0.0010286090778797426); |
369 | | } |
370 | | } |