Coverage Report

Created: 2025-11-09 06:56

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/pxfm-0.1.25/src/acospif.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::asinpif::ASINCOSF_PI_TABLE;
30
use crate::common::{dd_fmla, f_fmla};
31
32
/// Computes acos(x)/PI
33
///
34
/// Max ULP 0.5
35
#[inline]
36
0
pub fn f_acospif(x: f32) -> f32 {
37
0
    let ax = x.abs();
38
0
    let az = ax as f64;
39
0
    let z = x as f64;
40
0
    let t: u32 = x.to_bits();
41
0
    let e: i32 = ((t >> 23) & 0xff) as i32;
42
0
    if e >= 127 {
43
0
        if x == 1.0 {
44
0
            return 0.0;
45
0
        }
46
0
        if x == -1.0 {
47
0
            return 1.0;
48
0
        }
49
0
        if e == 0xff && (t.wrapping_shl(9)) != 0 {
50
0
            return x + x;
51
0
        } // nan
52
0
        return f32::NAN;
53
0
    }
54
0
    let s: i32 = 146i32.wrapping_sub(e);
55
0
    let mut i = 0i32;
56
0
    if s < 32 {
57
0
        i = (((t & 0x007fffff) | 1 << 23) >> s) as i32;
58
0
    }
59
0
    let c = ASINCOSF_PI_TABLE[i as usize & 15];
60
0
    let z2 = z * z;
61
0
    let z4 = z2 * z2;
62
0
    if i == 0 {
63
0
        let mut c0 = f_fmla(z2, f64::from_bits(c[1]), f64::from_bits(c[0]));
64
0
        let c2 = f_fmla(z2, f64::from_bits(c[3]), f64::from_bits(c[2]));
65
0
        let mut c4 = f_fmla(z2, f64::from_bits(c[5]), f64::from_bits(c[4]));
66
0
        let c6 = f_fmla(z2, f64::from_bits(c[7]), f64::from_bits(c[6]));
67
0
        c0 += c2 * z4;
68
0
        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
0
        c0 = dd_fmla(c4 * z4, z4, c0);
73
0
        f_fmla(-z, c0, 0.5) as f32
74
    } else {
75
0
        let f = (1. - az).sqrt();
76
0
        let mut c0 = f_fmla(az, f64::from_bits(c[1]), f64::from_bits(c[0]));
77
0
        let c2 = f_fmla(az, f64::from_bits(c[3]), f64::from_bits(c[2]));
78
0
        let mut c4 = f_fmla(az, f64::from_bits(c[5]), f64::from_bits(c[4]));
79
0
        let c6 = f_fmla(az, f64::from_bits(c[7]), f64::from_bits(c[6]));
80
0
        c0 += c2 * z2;
81
0
        c4 += c6 * z2;
82
0
        c0 += c4 * z4;
83
        static SIGN: [f64; 2] = [0., 1.];
84
0
        let r = SIGN[(t >> 31) as usize] + c0 * f64::copysign(f, x as f64);
85
0
        r as f32
86
    }
87
0
}
88
89
#[cfg(test)]
90
mod 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
}