pxfm/
csc.rs

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