pxfm/sin_cosf/
sinf.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/// Sine function
34///
35/// Max found ULP 0.5
36#[inline]
37pub fn f_sinf(x: f32) -> f32 {
38    let x_abs = x.to_bits() & 0x7fff_ffffu32;
39    let xd = x as f64;
40
41    // |x| <= pi/16
42    if x_abs <= 0x3e49_0fdbu32 {
43        // |x| < 0.000443633
44        if x_abs < 0x39e8_9769u32 {
45            if x_abs == 0u32 {
46                // For signed zeros.
47                return x;
48            }
49            #[cfg(any(
50                all(
51                    any(target_arch = "x86", target_arch = "x86_64"),
52                    target_feature = "fma"
53                ),
54                all(target_arch = "aarch64", target_feature = "neon")
55            ))]
56            {
57                use crate::common::f_fmlaf;
58                return f_fmlaf(x, f32::from_bits(0xb3000000), x);
59            }
60            #[cfg(not(any(
61                all(
62                    any(target_arch = "x86", target_arch = "x86_64"),
63                    target_feature = "fma"
64                ),
65                all(target_arch = "aarch64", target_feature = "neon")
66            )))]
67            {
68                return f_fmla(xd, f64::from_bits(0xbe60000000000000), xd) as f32;
69            }
70        }
71
72        let xsqr = xd * xd;
73
74        /*
75           Generated by Sollya:
76           f_sinpi_16 = sin(x)/x;
77           Q = fpminimax(f_sinpi_16, [|0, 2, 4, 6, 8|], [|1, D...|], [0, pi/16]);
78
79           See ./notes/sinf.sollya
80        */
81        let p = f_polyeval5(
82            xsqr,
83            f64::from_bits(0x3ff0000000000000),
84            f64::from_bits(0xbfc55555555554c6),
85            f64::from_bits(0x3f81111111085e65),
86            f64::from_bits(0xbf2a019f70fb4d4f),
87            f64::from_bits(0x3ec718d179815e74),
88        );
89        return (xd * p) as f32;
90    }
91
92    if x_abs >= 0x7f80_0000u32 {
93        return x + f32::NAN;
94    }
95
96    // Formula:
97    //   sin(x) = sin((k + y)*pi/32)
98    //          = sin(y*pi/32) * cos(k*pi/32) + cos(y*pi/32) * sin(k*pi/32)
99    // The values of sin(k*pi/32) and cos(k*pi/32) for k = 0..31 are precomputed
100    // and stored using a vector of 32 doubles. Sin(y*pi/32) and cos(y*pi/32) are
101    // computed using degree-7 and degree-6 minimax polynomials generated by
102    // Sollya respectively.
103
104    let rs = sincosf_eval(xd, x_abs);
105    f_fmla(rs.sin_y, rs.cos_k, f_fmla(rs.cosm1_y, rs.sin_k, rs.sin_k)) as f32
106}
107
108#[cfg(test)]
109mod tests {
110    use super::*;
111
112    #[test]
113    fn f_sinf_test() {
114        assert_eq!(f_sinf(0.0), 0.0);
115        assert_eq!(f_sinf(1.0), 0.84147096);
116        assert_eq!(f_sinf(0.3), 0.29552022);
117        assert_eq!(f_sinf(-1.0), -0.84147096);
118        assert_eq!(f_sinf(-0.3), -0.29552022);
119        assert_eq!(f_sinf(std::f32::consts::PI / 2.), 1.);
120        assert!(f_sinf(f32::INFINITY).is_nan());
121        assert!(f_sinf(f32::NEG_INFINITY).is_nan());
122        assert!((f_sinf(std::f32::consts::PI) - 0f32).abs() < 1e-6);
123        assert!((f_sinf(std::f32::consts::FRAC_PI_2) - 1f32).abs() < 1e-6);
124    }
125}