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    let x_e = (x.to_bits() >> 52) & 0x7ff;
66    const E_BIAS: u64 = (1u64 << (11 - 1u64)) - 1u64;
67
68    let y: DoubleDouble;
69    let k;
70
71    let mut argument_reduction = LargeArgumentReduction::default();
72
73    // |x| < 2^32 (with FMA) or |x| < 2^23 (w/o FMA)
74    if x_e < E_BIAS + 16 {
75        // |x| < 2^-26
76        if x_e < E_BIAS - 26 {
77            // Signed zeros.
78            if x == 0.0 {
79                return 1.0;
80            }
81
82            // For |x| < 2^-26, sinc(x) ~ 1 - x^2/6
83            const M_ONE_OVER_6: f64 = f64::from_bits(0xbfc5555555555555);
84            return f_fmla(x, x * M_ONE_OVER_6, 1.);
85        }
86
87        // // Small range reduction.
88        (y, k) = range_reduction_small(x);
89    } else {
90        // Inf or NaN
91        if x_e > 2 * E_BIAS {
92            // sin(+-Inf) = NaN
93            return x + f64::NAN;
94        }
95
96        // Large range reduction.
97        (k, y) = argument_reduction.reduce(x);
98    }
99
100    let r_sincos = sincos_eval(y);
101
102    // Fast look up version, but needs 256-entry table.
103    // cos(k * pi/128) = sin(k * pi/128 + pi/2) = sin((k + 64) * pi/128).
104    let sk = SIN_K_PI_OVER_128[(k & 255) as usize];
105    let ck = SIN_K_PI_OVER_128[((k.wrapping_add(64)) & 255) as usize];
106
107    let sin_k = DoubleDouble::from_bit_pair(sk);
108    let cos_k = DoubleDouble::from_bit_pair(ck);
109
110    let sin_k_cos_y = DoubleDouble::quick_mult(r_sincos.v_cos, sin_k);
111    let cos_k_sin_y = DoubleDouble::quick_mult(r_sincos.v_sin, cos_k);
112
113    // sin_k_cos_y is always >> cos_k_sin_y
114    let mut rr = DoubleDouble::from_exact_add(sin_k_cos_y.hi, cos_k_sin_y.hi);
115    rr.lo += sin_k_cos_y.lo + cos_k_sin_y.lo;
116
117    rr = DoubleDouble::from_exact_add(rr.hi, rr.lo);
118    rr = DoubleDouble::div_dd_f64(rr, x);
119
120    let rlp = rr.lo + r_sincos.err;
121    let rlm = rr.lo - r_sincos.err;
122
123    let r_upper = rr.hi + rlp; // (rr.lo + ERR);
124    let r_lower = rr.hi + rlm; // (rr.lo - ERR);
125
126    // Ziv's accuracy test
127    if r_upper == r_lower {
128        return r_upper;
129    }
130    sinc_refine(&mut argument_reduction, x, x_e, k)
131}
132
133#[cfg(test)]
134mod tests {
135    use super::*;
136    #[test]
137    fn test_sinc() {
138        assert_eq!(f_sinc(0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004764135737289025), 1.);
139        assert_eq!(f_sinc(0.1), 0.9983341664682815);
140        assert_eq!(f_sinc(0.9), 0.870363232919426);
141        assert_eq!(f_sinc(-0.1), 0.9983341664682815);
142        assert_eq!(f_sinc(-0.9), 0.870363232919426);
143        assert!(f_sinc(f64::INFINITY).is_nan());
144        assert!(f_sinc(f64::NEG_INFINITY).is_nan());
145        assert!(f_sinc(f64::NAN).is_nan());
146    }
147}