/rust/registry/src/index.crates.io-1949cf8c6b5b557f/pxfm-0.1.30/src/acosf.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::common::f_fmla; |
30 | | use std::hint::black_box; |
31 | | |
32 | | #[inline(always)] |
33 | 0 | pub(crate) fn poly12<Q: Fn(f64, f64, f64) -> f64>(z: f64, c: [u64; 12], fma: &Q) -> f64 { |
34 | 0 | let z2 = z * z; |
35 | 0 | let z4 = z2 * z2; |
36 | 0 | let mut c0 = fma(z, f64::from_bits(c[1]), f64::from_bits(c[0])); |
37 | 0 | let c2 = fma(z, f64::from_bits(c[3]), f64::from_bits(c[2])); |
38 | 0 | let mut c4 = fma(z, f64::from_bits(c[5]), f64::from_bits(c[4])); |
39 | 0 | let c6 = fma(z, f64::from_bits(c[7]), f64::from_bits(c[6])); |
40 | 0 | let mut c8 = fma(z, f64::from_bits(c[9]), f64::from_bits(c[8])); |
41 | 0 | let c10 = fma(z, f64::from_bits(c[11]), f64::from_bits(c[10])); |
42 | 0 | c0 = fma(c2, z2, c0); |
43 | 0 | c4 = fma(c6, z2, c4); |
44 | 0 | c8 = fma(z2, c10, c8); |
45 | 0 | fma(z4, fma(z4, c8, c4), c0) |
46 | 0 | } Unexecuted instantiation: pxfm::acosf::poly12::<<f64>::mul_add> Unexecuted instantiation: pxfm::acosf::poly12::<pxfm::common::f_fmla> |
47 | | |
48 | | #[cold] |
49 | 0 | fn as_special(x: f32) -> f32 { |
50 | | const PIH: f32 = f64::from_bits(0x400921fb60000000) as f32; |
51 | | const PIL: f32 = -f64::from_bits(0x3e70000000000000) as f32; |
52 | 0 | let t = x.to_bits(); |
53 | 0 | if t == (0x7fu32 << 23) { |
54 | 0 | return 0.0; |
55 | 0 | } // x=1 |
56 | 0 | if t == (0x17fu32 << 23) { |
57 | 0 | return PIH + PIL; |
58 | 0 | } // x=-1 |
59 | 0 | let ax = t.wrapping_shl(1); |
60 | 0 | if ax > (0xffu32 << 24) { |
61 | 0 | return x + x; |
62 | 0 | } // nan |
63 | 0 | f32::NAN |
64 | 0 | } |
65 | | |
66 | | #[inline(always)] |
67 | 0 | fn acosf_gen_impl<Q: Fn(f64, f64, f64) -> f64>(x: f32, fma: Q) -> f32 { |
68 | | const PI2: f64 = f64::from_bits(0x3ff921fb54442d18); |
69 | | const O: [f64; 2] = [0., f64::from_bits(0x400921fb54442d18)]; |
70 | 0 | let xs = x as f64; |
71 | | let mut r: f64; |
72 | 0 | let t = x.to_bits(); |
73 | 0 | let ax = t.wrapping_shl(1); |
74 | 0 | if ax >= 0x7f << 24 { |
75 | 0 | return as_special(x); |
76 | 0 | } |
77 | 0 | if ax < 0x7ec2a1dcu32 { |
78 | | // |x| < 0.880141 |
79 | | const B: [u64; 16] = [ |
80 | | 0x3fefffffffd9ccb8, |
81 | | 0x3fc5555c94838007, |
82 | | 0x3fb32ded4b7c20fa, |
83 | | 0x3fa8566df703309e, |
84 | | 0xbf9980c959bec9a3, |
85 | | 0x3fe56fbb04998344, |
86 | | 0xc01403d8e4c49f52, |
87 | | 0x403b06c3e9f311ea, |
88 | | 0xc059ea97c4e2c21f, |
89 | | 0x407200b8261cc61b, |
90 | | 0xc082274c2799a5c7, |
91 | | 0x408a558a59cc19d3, |
92 | | 0xc08aca4b6a529ff0, |
93 | | 0x408228744703f813, |
94 | | 0xc06d7dbb0b322228, |
95 | | 0x4045c2018c0c0105, |
96 | | ]; |
97 | | /* avoid spurious underflow */ |
98 | 0 | if ax < 0x40000000u32 { |
99 | | // |x| < 2^-63 |
100 | 0 | return PI2 as f32; |
101 | 0 | } |
102 | 0 | let z = xs; |
103 | 0 | let z2 = z * z; |
104 | | |
105 | 0 | let w0 = fma(z2, f64::from_bits(B[1]), f64::from_bits(B[0])); |
106 | 0 | let w1 = fma(z2, f64::from_bits(B[3]), f64::from_bits(B[2])); |
107 | 0 | let w2 = fma(z2, f64::from_bits(B[5]), f64::from_bits(B[4])); |
108 | 0 | let w3 = fma(z2, f64::from_bits(B[7]), f64::from_bits(B[6])); |
109 | 0 | let w4 = fma(z2, f64::from_bits(B[9]), f64::from_bits(B[8])); |
110 | 0 | let w5 = fma(z2, f64::from_bits(B[11]), f64::from_bits(B[10])); |
111 | 0 | let w6 = fma(z2, f64::from_bits(B[13]), f64::from_bits(B[12])); |
112 | 0 | let w7 = fma(z2, f64::from_bits(B[15]), f64::from_bits(B[14])); |
113 | | |
114 | 0 | let z4 = z2 * z2; |
115 | 0 | let z8 = z4 * z4; |
116 | 0 | let z16 = z8 * z8; |
117 | | |
118 | 0 | r = z |
119 | 0 | * ((fma(z4, w1, w0) + z8 * fma(z4, w3, w2)) |
120 | 0 | + z16 * (fma(z4, w5, w4) + z8 * fma(z4, w7, w6))); |
121 | | |
122 | 0 | let ub = f64::from_bits(0x3ff921fb54574191) - r; |
123 | 0 | let lb = f64::from_bits(0x3ff921fb543118a0) - r; |
124 | | // Ziv's accuracy test |
125 | 0 | if ub == lb { |
126 | 0 | return ub as f32; |
127 | 0 | } |
128 | 0 | } |
129 | | // accurate path |
130 | 0 | if ax < (0x7eu32 << 24) { |
131 | | const C: [u64; 12] = [ |
132 | | 0x3fc555555555529c, |
133 | | 0x3fb333333337e0dd, |
134 | | 0x3fa6db6db3b4465e, |
135 | | 0x3f9f1c72e13ac306, |
136 | | 0x3f96e89cebe06bc4, |
137 | | 0x3f91c6dcf5289094, |
138 | | 0x3f8c6dbbcc7c6315, |
139 | | 0x3f88f8dc2615e996, |
140 | | 0x3f7a5833b7bf15e8, |
141 | | 0x3f943f44ace1665c, |
142 | | 0xbf90fb17df881c73, |
143 | | 0x3fa07520c026b2d6, |
144 | | ]; |
145 | 0 | if t == 0x328885a3u32 { |
146 | 0 | return black_box(f64::from_bits(0x3ff921fb60000000) as f32) |
147 | 0 | + black_box(f64::from_bits(0x3e60000000000000) as f32); |
148 | 0 | } |
149 | 0 | if t == 0x39826222u32 { |
150 | 0 | return black_box(f64::from_bits(0x3ff920f6a0000000) as f32) |
151 | 0 | + black_box(f64::from_bits(0x3e60000000000000) as f32); |
152 | 0 | } |
153 | 0 | let x2 = xs * xs; |
154 | 0 | r = fma(-(xs * x2), poly12(x2, C, &fma), PI2 - xs); |
155 | | } else { |
156 | | const C: [u64; 12] = [ |
157 | | 0x3ff6a09e667f3bcb, |
158 | | 0x3fbe2b7dddff2db9, |
159 | | 0x3f9b27247ab42dbc, |
160 | | 0x3f802995cc4e0744, |
161 | | 0x3f65ffb0276ec8ea, |
162 | | 0x3f5033885a928dec, |
163 | | 0x3f3911f2be23f8c7, |
164 | | 0x3f24c3c55d2437fd, |
165 | | 0x3f0af477e1d7b461, |
166 | | 0x3f0abd6bdff67dcb, |
167 | | 0xbef1717e86d0fa28, |
168 | | 0x3ef6ff526de46023, |
169 | | ]; |
170 | 0 | let bx = xs.abs(); |
171 | 0 | let z = 1.0 - bx; |
172 | 0 | let s = f64::copysign(z.sqrt(), xs); |
173 | 0 | r = fma(s, poly12(z, C, &fma), O[t.wrapping_shr(31) as usize]); |
174 | | } |
175 | 0 | r as f32 |
176 | 0 | } Unexecuted instantiation: pxfm::acosf::acosf_gen_impl::<<f64>::mul_add> Unexecuted instantiation: pxfm::acosf::acosf_gen_impl::<pxfm::common::f_fmla> |
177 | | |
178 | | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
179 | | #[target_feature(enable = "avx", enable = "fma")] |
180 | 0 | unsafe fn acosf_fma_impl(x: f32) -> f32 { |
181 | 0 | acosf_gen_impl(x, f64::mul_add) |
182 | 0 | } |
183 | | |
184 | | /// Compute acos |
185 | | /// |
186 | | /// Max found ULP 0.49999982 |
187 | | #[inline] |
188 | 0 | pub fn f_acosf(x: f32) -> f32 { |
189 | | #[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))] |
190 | | { |
191 | | acosf_gen_impl(x, f_fmla) |
192 | | } |
193 | | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
194 | | { |
195 | | use std::sync::OnceLock; |
196 | | static EXECUTOR: OnceLock<unsafe fn(f32) -> f32> = OnceLock::new(); |
197 | 0 | let q = EXECUTOR.get_or_init(|| { |
198 | 0 | if std::arch::is_x86_feature_detected!("avx") |
199 | 0 | && std::arch::is_x86_feature_detected!("fma") |
200 | | { |
201 | 0 | acosf_fma_impl |
202 | | } else { |
203 | 0 | fn def_acosf(x: f32) -> f32 { |
204 | 0 | acosf_gen_impl(x, f_fmla) |
205 | 0 | } |
206 | 0 | def_acosf |
207 | | } |
208 | 0 | }); |
209 | 0 | unsafe { q(x) } |
210 | | } |
211 | 0 | } |
212 | | |
213 | | #[cfg(test)] |
214 | | mod tests { |
215 | | use super::*; |
216 | | |
217 | | #[test] |
218 | | fn test_acosf() { |
219 | | assert_eq!(f_acosf(-0.5), 2.0943952); |
220 | | assert_eq!(f_acosf(0.5), std::f32::consts::FRAC_PI_3); |
221 | | assert!(f_acosf(7.).is_nan()); |
222 | | } |
223 | | } |