pxfm/
sinc.rs

1/*
2 * // Copyright (c) Radzivon Bartoshyk 7/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::double_double::DoubleDouble;
31use crate::dyadic_float::DyadicFloat128;
32use crate::sin::{get_sin_k_rational, range_reduction_small, sincos_eval};
33use crate::sin_table::SIN_K_PI_OVER_128;
34use crate::sincos_dyadic::{range_reduction_small_f128, sincos_eval_dyadic};
35use crate::sincos_reduce::LargeArgumentReduction;
36
37#[cold]
38#[inline(never)]
39fn sinc_refine(argument_reduction: &mut LargeArgumentReduction, x: f64, x_e: u64, k: u64) -> f64 {
40    const EXP_BIAS: u64 = (1u64 << (11 - 1u64)) - 1u64;
41    let u_f128 = if x_e < EXP_BIAS + 16 {
42        range_reduction_small_f128(x)
43    } else {
44        argument_reduction.accurate()
45    };
46
47    let sin_cos = sincos_eval_dyadic(&u_f128);
48
49    // cos(k * pi/128) = sin(k * pi/128 + pi/2) = sin((k + 64) * pi/128).
50    let sin_k_f128 = get_sin_k_rational(k);
51    let cos_k_f128 = get_sin_k_rational(k.wrapping_add(64));
52
53    // sin(x) = sin(k * pi/128 + u)
54    //        = sin(u) * cos(k*pi/128) + cos(u) * sin(k*pi/128)
55    let r = (sin_k_f128 * sin_cos.v_cos) + (cos_k_f128 * sin_cos.v_sin);
56
57    let reciprocal = DyadicFloat128::accurate_reciprocal(x);
58    (r * reciprocal).fast_as_f64()
59}
60
61/// Computes sinc(x)
62///
63/// Max ULP 0.5
64pub fn f_sinc(x: f64) -> f64 {
65    if !x.is_finite() {
66        return f64::NAN;
67    }
68
69    let x_abs = f64::from_bits(x.to_bits() & 0x7fff_ffff_ffff_ffff);
70    if x_abs.to_bits() == 0 {
71        return 1.0;
72    }
73
74    let x_e = (x.to_bits() >> 52) & 0x7ff;
75    const E_BIAS: u64 = (1u64 << (11 - 1u64)) - 1u64;
76
77    let y: DoubleDouble;
78    let k;
79
80    let mut argument_reduction = LargeArgumentReduction::default();
81
82    // |x| < 2^32 (with FMA) or |x| < 2^23 (w/o FMA)
83    if x_e < E_BIAS + 16 {
84        // |x| < 2^-26
85        if x_e < E_BIAS - 26 {
86            // Signed zeros.
87            if x == 0.0 {
88                return x;
89            }
90
91            // For |x| < 2^-26, sinc(x) ~ 1 - x^2/6
92            const M_ONE_OVER_6: f64 = f64::from_bits(0xbfc5555555555555);
93            return f_fmla(x, x * M_ONE_OVER_6, 1.);
94        }
95
96        // // Small range reduction.
97        (y, k) = range_reduction_small(x);
98    } else {
99        // Inf or NaN
100        if x_e > 2 * E_BIAS {
101            // sin(+-Inf) = NaN
102            return x + f64::NAN;
103        }
104
105        // Large range reduction.
106        (k, y) = argument_reduction.reduce(x);
107    }
108
109    let r_sincos = sincos_eval(y);
110
111    // Fast look up version, but needs 256-entry table.
112    // cos(k * pi/128) = sin(k * pi/128 + pi/2) = sin((k + 64) * pi/128).
113    let sk = SIN_K_PI_OVER_128[(k & 255) as usize];
114    let ck = SIN_K_PI_OVER_128[((k.wrapping_add(64)) & 255) as usize];
115
116    let sin_k = DoubleDouble::from_bit_pair(sk);
117    let cos_k = DoubleDouble::from_bit_pair(ck);
118
119    let sin_k_cos_y = DoubleDouble::quick_mult(r_sincos.v_cos, sin_k);
120    let cos_k_sin_y = DoubleDouble::quick_mult(r_sincos.v_sin, cos_k);
121
122    // sin_k_cos_y is always >> cos_k_sin_y
123    let mut rr = DoubleDouble::from_exact_add(sin_k_cos_y.hi, cos_k_sin_y.hi);
124    rr.lo += sin_k_cos_y.lo + cos_k_sin_y.lo;
125
126    rr = DoubleDouble::from_exact_add(rr.hi, rr.lo);
127    rr = DoubleDouble::div_dd_f64(rr, x);
128
129    let rlp = rr.lo + r_sincos.err;
130    let rlm = rr.lo - r_sincos.err;
131
132    let r_upper = rr.hi + rlp; // (rr.lo + ERR);
133    let r_lower = rr.hi + rlm; // (rr.lo - ERR);
134
135    // Ziv's accuracy test
136    if r_upper == r_lower {
137        return r_upper;
138    }
139    sinc_refine(&mut argument_reduction, x, x_e, k)
140}
141
142#[cfg(test)]
143mod tests {
144    use super::*;
145    #[test]
146    fn test_sinc() {
147        assert_eq!(f_sinc(0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004764135737289025), 1.);
148        assert_eq!(f_sinc(0.1), 0.9983341664682815);
149        assert_eq!(f_sinc(0.9), 0.870363232919426);
150        assert_eq!(f_sinc(-0.1), 0.9983341664682815);
151        assert_eq!(f_sinc(-0.9), 0.870363232919426);
152        assert!(f_sinc(f64::INFINITY).is_nan());
153        assert!(f_sinc(f64::NEG_INFINITY).is_nan());
154        assert!(f_sinc(f64::NAN).is_nan());
155    }
156}