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