pxfm/acospif.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::asinpif::ASINCOSF_PI_TABLE;
30use crate::common::{dd_fmla, f_fmla};
31
32/// Computes acos(x)/PI
33///
34/// Max ULP 0.5
35#[inline]
36pub fn f_acospif(x: f32) -> f32 {
37 let ax = x.abs();
38 let az = ax as f64;
39 let z = x as f64;
40 let t: u32 = x.to_bits();
41 let e: i32 = ((t >> 23) & 0xff) as i32;
42 if e >= 127 {
43 if x == 1.0 {
44 return 0.0;
45 }
46 if x == -1.0 {
47 return 1.0;
48 }
49 if e == 0xff && (t.wrapping_shl(9)) != 0 {
50 return x + x;
51 } // nan
52 return f32::NAN;
53 }
54 let s: i32 = 146i32.wrapping_sub(e);
55 let mut i = 0i32;
56 if s < 32 {
57 i = (((t & 0x007fffff) | 1 << 23) >> s) as i32;
58 }
59 let c = ASINCOSF_PI_TABLE[i as usize & 15];
60 let z2 = z * z;
61 let z4 = z2 * z2;
62 if i == 0 {
63 let mut c0 = f_fmla(z2, f64::from_bits(c[1]), f64::from_bits(c[0]));
64 let c2 = f_fmla(z2, f64::from_bits(c[3]), f64::from_bits(c[2]));
65 let mut c4 = f_fmla(z2, f64::from_bits(c[5]), f64::from_bits(c[4]));
66 let c6 = f_fmla(z2, f64::from_bits(c[7]), f64::from_bits(c[6]));
67 c0 += c2 * z4;
68 c4 += c6 * z4;
69 /* For |x| <= 0x1.0fd288p-127, c0 += c4*(z4*z4) would raise a spurious
70 underflow exception, we use an FMA instead, where c4 * z4 does not
71 underflow. */
72 c0 = dd_fmla(c4 * z4, z4, c0);
73 f_fmla(-z, c0, 0.5) as f32
74 } else {
75 let f = (1. - az).sqrt();
76 let mut c0 = f_fmla(az, f64::from_bits(c[1]), f64::from_bits(c[0]));
77 let c2 = f_fmla(az, f64::from_bits(c[3]), f64::from_bits(c[2]));
78 let mut c4 = f_fmla(az, f64::from_bits(c[5]), f64::from_bits(c[4]));
79 let c6 = f_fmla(az, f64::from_bits(c[7]), f64::from_bits(c[6]));
80 c0 += c2 * z2;
81 c4 += c6 * z2;
82 c0 += c4 * z4;
83 static SIGN: [f64; 2] = [0., 1.];
84 let r = SIGN[(t >> 31) as usize] + c0 * f64::copysign(f, x as f64);
85 r as f32
86 }
87}
88
89#[cfg(test)]
90mod tests {
91 use super::*;
92 #[test]
93 fn test_acospif() {
94 assert_eq!(f_acospif(0.0), 0.5);
95 assert_eq!(f_acospif(0.5), 0.33333334);
96 assert_eq!(f_acospif(1.0), 0.0);
97 }
98}