pxfm/
asinf.rs

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 */
29use crate::acosf::poly12;
30use crate::common::{dd_fmlaf, f_fmla};
31
32#[cold]
33fn as_special(x: f32) -> f32 {
34    let t = x.to_bits();
35    let ax = t.wrapping_shl(1);
36    if ax > (0xffu32 << 24) {
37        return x + x;
38    } // nan
39    f32::NAN
40}
41
42#[inline(always)]
43/// fma - fma
44/// dd_fma - mandatory fma fallback
45fn asinf_gen_impl<Q: Fn(f64, f64, f64) -> f64, F: Fn(f32, f32, f32) -> f32>(
46    x: f32,
47    fma: Q,
48    dd_fma: F,
49) -> f32 {
50    const PI2: f64 = f64::from_bits(0x3ff921fb54442d18);
51    let xs = x as f64;
52    let mut r;
53    let t = x.to_bits();
54    let ax = t.wrapping_shl(1);
55    if ax > 0x7f << 24 {
56        return as_special(x);
57    }
58    if ax < 0x7ec29000u32 {
59        // |x| < 1.49029
60        if ax < 115 << 24 {
61            // |x| < 0.000244141
62            return dd_fma(x, f64::from_bits(0x3e60000000000000) as f32, x);
63        }
64        const B: [u64; 16] = [
65            0x3ff0000000000005,
66            0x3fc55557aeca105d,
67            0x3fb3314ec3db7d12,
68            0x3fa775738a5a6f92,
69            0x3f75d5f7ce1c8538,
70            0x3fd605c6d58740f0,
71            0xc005728b732d73c6,
72            0x402f152170f151eb,
73            0xc04f962ea3ca992e,
74            0x40671971e17375a0,
75            0xc07860512b4ba230,
76            0x40826a3b8d4bdb14,
77            0xc0836f2ea5698b51,
78            0x407b3d722aebfa2e,
79            0xc066cf89703b1289,
80            0x4041518af6a65e2d,
81        ];
82        let z = xs;
83        let z2 = z * z;
84        let w0 = fma(z2, f64::from_bits(B[1]), f64::from_bits(B[0]));
85        let w1 = fma(z2, f64::from_bits(B[3]), f64::from_bits(B[2]));
86        let w2 = fma(z2, f64::from_bits(B[5]), f64::from_bits(B[4]));
87        let w3 = fma(z2, f64::from_bits(B[7]), f64::from_bits(B[6]));
88        let w4 = fma(z2, f64::from_bits(B[9]), f64::from_bits(B[8]));
89        let w5 = fma(z2, f64::from_bits(B[11]), f64::from_bits(B[10]));
90        let w6 = fma(z2, f64::from_bits(B[13]), f64::from_bits(B[12]));
91        let w7 = fma(z2, f64::from_bits(B[15]), f64::from_bits(B[14]));
92
93        let z4 = z2 * z2;
94        let z8 = z4 * z4;
95        let z16 = z8 * z8;
96
97        r = z
98            * ((fma(z4, w1, w0) + z8 * fma(z4, w3, w2))
99                + z16 * (fma(z4, w5, w4) + z8 * fma(z4, w7, w6)));
100        let ub = r;
101        let lb = r - z * f64::from_bits(0x3e0efa8eb0000000);
102        // Ziv's accuracy test
103        if ub == lb {
104            return ub as f32;
105        }
106    }
107    if ax < (0x7eu32 << 24) {
108        const C: [u64; 12] = [
109            0x3fc555555555529c,
110            0x3fb333333337e0dd,
111            0x3fa6db6db3b4465e,
112            0x3f9f1c72e13ac306,
113            0x3f96e89cebe06bc4,
114            0x3f91c6dcf5289094,
115            0x3f8c6dbbcc7c6315,
116            0x3f88f8dc2615e996,
117            0x3f7a5833b7bf15e8,
118            0x3f943f44ace1665c,
119            0xbf90fb17df881c73,
120            0x3fa07520c026b2d6,
121        ];
122        let z = xs;
123        let z2 = z * z;
124        let c0 = poly12(z2, C, &fma);
125        r = z + (z * z2) * c0;
126    } else {
127        if ax == 0x7e55688au32 {
128            return f32::copysign(f64::from_bits(0x3fe75b8a20000000) as f32, x)
129                + f32::copysign(f64::from_bits(0x3e50000000000000) as f32, x);
130        }
131        if ax == 0x7e107434u32 {
132            return f32::copysign(f64::from_bits(0x3fe1f4b640000000) as f32, x)
133                + f32::copysign(f64::from_bits(0x3e50000000000000) as f32, x);
134        }
135        let bx = xs.abs();
136        let z = 1.0 - bx;
137        let s = z.sqrt();
138        const C: [u64; 12] = [
139            0x3ff6a09e667f3bcb,
140            0x3fbe2b7dddff2db9,
141            0x3f9b27247ab42dbc,
142            0x3f802995cc4e0744,
143            0x3f65ffb0276ec8ea,
144            0x3f5033885a928dec,
145            0x3f3911f2be23f8c7,
146            0x3f24c3c55d2437fd,
147            0x3f0af477e1d7b461,
148            0x3f0abd6bdff67dcb,
149            0xbef1717e86d0fa28,
150            0x3ef6ff526de46023,
151        ];
152        r = PI2 - s * poly12(z, C, &fma);
153        r = f64::copysign(r, xs);
154    }
155    r as f32
156}
157
158#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
159#[target_feature(enable = "avx", enable = "fma")]
160unsafe fn asinf_fma_impl(x: f32) -> f32 {
161    asinf_gen_impl(x, f64::mul_add, f32::mul_add)
162}
163
164/// Computes asin
165///
166/// Max found ULP 0.49999928
167#[inline]
168pub fn f_asinf(x: f32) -> f32 {
169    #[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
170    {
171        asinf_gen_impl(x, f_fmla, dd_fmlaf)
172    }
173    #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
174    {
175        use std::sync::OnceLock;
176        static EXECUTOR: OnceLock<unsafe fn(f32) -> f32> = OnceLock::new();
177        let q = EXECUTOR.get_or_init(|| {
178            if std::arch::is_x86_feature_detected!("avx")
179                && std::arch::is_x86_feature_detected!("fma")
180            {
181                asinf_fma_impl
182            } else {
183                fn def_asinf(x: f32) -> f32 {
184                    asinf_gen_impl(x, f_fmla, dd_fmlaf)
185                }
186                def_asinf
187            }
188        });
189        unsafe { q(x) }
190    }
191}
192
193#[cfg(test)]
194mod tests {
195    use super::*;
196
197    #[test]
198    fn test_asinf() {
199        assert_eq!(f_asinf(-0.5), -std::f32::consts::FRAC_PI_6);
200        assert_eq!(f_asinf(0.5), std::f32::consts::FRAC_PI_6);
201        assert!(f_asinf(7.).is_nan());
202    }
203}