pxfm/sin_cosf/
cosf.rs

1/*
2 * // Copyright (c) Radzivon Bartoshyk 8/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 crate::polyeval::f_polyeval5;
31use crate::sin_cosf::sincosf_eval::sincosf_eval;
32
33/// Computes cosine function
34///
35/// ULP 0.5
36///
37#[inline]
38pub fn f_cosf(x: f32) -> f32 {
39    let x_abs = x.to_bits() & 0x7fff_ffffu32;
40    let x = f32::from_bits(x_abs);
41    let xd = x as f64;
42
43    // |x| <= pi/16
44    if x_abs <= 0x3e49_0fdbu32 {
45        // |x| < 0.000244141
46        if x_abs < 0x3980_0000u32 {
47            #[cfg(any(
48                all(
49                    any(target_arch = "x86", target_arch = "x86_64"),
50                    target_feature = "fma"
51                ),
52                all(target_arch = "aarch64", target_feature = "neon")
53            ))]
54            {
55                use crate::common::f_fmlaf;
56                return f_fmlaf(x, f32::from_bits(0xb3000000), 1.);
57            }
58            #[cfg(not(any(
59                all(
60                    any(target_arch = "x86", target_arch = "x86_64"),
61                    target_feature = "fma"
62                ),
63                all(target_arch = "aarch64", target_feature = "neon")
64            )))]
65            {
66                return f_fmla(xd, f64::from_bits(0xbe60000000000000), 1.) as f32;
67            }
68        }
69
70        // Cosine
71        // Generated poly by Sollya:
72        // f_cos_16 = cos(x);
73        //
74        // Q = fpminimax(f_cos_16, [|0, 2, 4, 6, 8|], [|1, D...|], [0, pi/16]);
75        // See ./notes/cosf.sollya
76
77        let x2 = xd * xd;
78        let p = f_polyeval5(
79            x2,
80            f64::from_bits(0x3ff0000000000000),
81            f64::from_bits(0xbfdffffffffffcea),
82            f64::from_bits(0x3fa55555553d611a),
83            f64::from_bits(0xbf56c16b2e26561a),
84            f64::from_bits(0x3ef9faa67b9da80b),
85        );
86        return p as f32;
87    }
88
89    if x_abs >= 0x7f80_0000u32 {
90        return x + f32::NAN;
91    }
92
93    // Formula:
94    //   cos(x) = cos((k + y)*pi/32)
95    //          = cos(y*pi/32) * cos(k*pi/32) - sin(y*pi/32) * sin(k*pi/32)
96    // The values of sin(k*pi/32) and cos(k*pi/32) for k = 0..63 are precomputed
97    // and stored using a vector of 32 doubles. Sin(y*pi/32) and cos(y*pi/32) are
98    // computed using degree-7 and degree-6 minimax polynomials generated by
99    // Sollya respectively.
100    // Combine the results with the sine of sum formula:
101    //   cos(x) = cos((k + y)*pi/32)
102    //          = cos(y*pi/32) * cos(k*pi/32) - sin(y*pi/32) * sin(k*pi/32)
103    //          = cosm1_y * cos_k + sin_y * sin_k
104    //          = (cosm1_y * cos_k + cos_k) + sin_y * sin_k
105
106    let rs = sincosf_eval(xd, x_abs);
107    f_fmla(rs.sin_y, -rs.sin_k, f_fmla(rs.cosm1_y, rs.cos_k, rs.cos_k)) as f32
108}
109
110#[cfg(test)]
111mod tests {
112    use super::*;
113
114    #[test]
115    fn f_cosf_test() {
116        assert_eq!(f_cosf(0.0), 1.0);
117        assert_eq!(f_cosf(std::f32::consts::PI), -1f32);
118        assert_eq!(f_cosf(0.5), 0.87758255);
119        assert_eq!(f_cosf(0.7), 0.7648422);
120        assert_eq!(f_cosf(1.7), -0.12884454);
121        assert!(f_cosf(f32::INFINITY).is_nan());
122        assert!(f_cosf(f32::NEG_INFINITY).is_nan());
123        assert!(f_cosf(f32::NAN).is_nan());
124        assert_eq!(f_cosf(0.0002480338), 0.9999999692396206);
125    }
126}