pxfm/sin_cosf/
sincpif.rs

1/*
2 * // Copyright (c) Radzivon Bartoshyk 9/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, is_integerf};
30use crate::polyeval::f_polyeval5;
31use crate::sin_cosf::sincosf_eval::sincospif_eval;
32
33/// Computes sin(PI\*x)/(PI\*x)
34///
35/// Produces normalized sinc.
36///
37/// ulp 0.5
38pub fn f_sincpif(x: f32) -> f32 {
39    let x_abs = x.to_bits() & 0x7fff_ffffu32;
40    let xd = x as f64;
41
42    if x_abs <= 0x3d80_0000u32 {
43        // |x| <= 1/16
44        if x_abs < 0x3580_2126u32 {
45            // |x| < 0.0000009546391
46            if x_abs == 0u32 {
47                // Signed zeros.
48                return 1.;
49            }
50
51            // Small values approximated with Taylor poly
52            // sincpi(x) ~ 1 - x^2*Pi^2/6 + O(x^4)
53            #[cfg(any(
54                all(
55                    any(target_arch = "x86", target_arch = "x86_64"),
56                    target_feature = "fma"
57                ),
58                all(target_arch = "aarch64", target_feature = "neon")
59            ))]
60            {
61                use crate::common::f_fmlaf;
62                const M_SQR_PI_OVER_6: f32 = f32::from_bits(0xbfd28d33);
63                return f_fmlaf(x, M_SQR_PI_OVER_6 * x, 1.);
64            }
65            #[cfg(not(any(
66                all(
67                    any(target_arch = "x86", target_arch = "x86_64"),
68                    target_feature = "fma"
69                ),
70                all(target_arch = "aarch64", target_feature = "neon")
71            )))]
72            {
73                const M_SQR_PI_OVER_6: f64 = f64::from_bits(0xbffa51a6625307d3);
74                let x2 = xd * xd;
75                let p = f_fmla(x2, M_SQR_PI_OVER_6, 1.);
76                return p as f32;
77            }
78        }
79
80        let xsqr = xd * xd;
81
82        // Generated by Sollya:
83        // d = [0, 1/16];
84        // f_sincpif = sin(y*pi)/(y*pi);
85        // Q = fpminimax(f_sincpif, [|0, 2, 4, 6, 8|], [|D...|], d, relative, floating);
86        // See ./notes/sincpif.sollya
87        let p = f_polyeval5(
88            xsqr,
89            f64::from_bits(0x3ff0000000000000),
90            f64::from_bits(0xbffa51a662530723),
91            f64::from_bits(0x3fe9f9cb401e8e85),
92            f64::from_bits(0xbfc86a8da89c9234),
93            f64::from_bits(0x3f9ac0a16798157e),
94        );
95        return p as f32;
96    }
97
98    // Numbers greater or equal to 2^23 are always integers or NaN
99    // integers are always 0
100    if x_abs >= 0x4b00_0000u32 || is_integerf(x) {
101        if x_abs >= 0x7f80_0000u32 {
102            return x + f32::NAN;
103        }
104        return if x.is_sign_negative() { -0. } else { 0. };
105    }
106
107    const PI: f64 = f64::from_bits(0x400921fb54442d18);
108    let rs = sincospif_eval(xd);
109    let sf = f_fmla(rs.sin_y, rs.cos_k, f_fmla(rs.cosm1_y, rs.sin_k, rs.sin_k));
110    (sf / (PI * xd)) as f32
111}
112
113#[cfg(test)]
114mod tests {
115    use super::*;
116
117    #[test]
118    fn test_sincpif_eval() {
119        assert_eq!(f_sincpif(1.0), 0.);
120        assert_eq!(f_sincpif(2.0), 0.);
121        assert_eq!(f_sincpif(3.0), 0.);
122        assert_eq!(f_sincpif(0.0543242), 0.99515265);
123        assert_eq!(f_sincpif(0.002134242), 0.9999925);
124        assert_eq!(f_sincpif(0.00000005421321), 1.0);
125        assert!(f_sincpif(f32::INFINITY).is_nan());
126        assert!(f_sincpif(f32::NEG_INFINITY).is_nan());
127        assert!(f_sincpif(f32::NAN).is_nan());
128    }
129}