Coverage Report

Created: 2026-01-09 07:43

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/pxfm-0.1.27/src/csc.rs
Line
Count
Source
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
 */
29
use crate::double_double::DoubleDouble;
30
use crate::sin::{get_sin_k_rational, range_reduction_small, sincos_eval};
31
use crate::sin_table::SIN_K_PI_OVER_128;
32
use crate::sincos_dyadic::{range_reduction_small_f128, sincos_eval_dyadic};
33
use crate::sincos_reduce::LargeArgumentReduction;
34
35
#[cold]
36
0
fn csc_accurate(x: f64, argument_reduction: &mut LargeArgumentReduction, x_e: u64, k: u64) -> f64 {
37
    const EXP_BIAS: u64 = (1u64 << (11 - 1u64)) - 1u64;
38
0
    let u_f128 = if x_e < EXP_BIAS + 16 {
39
0
        range_reduction_small_f128(x)
40
    } else {
41
0
        argument_reduction.accurate()
42
    };
43
44
0
    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
0
    let sin_k_f128 = get_sin_k_rational(k);
48
0
    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
0
    let r = (sin_k_f128 * sin_cos.v_cos) + (cos_k_f128 * sin_cos.v_sin);
53
0
    r.reciprocal().fast_as_f64()
54
0
}
55
56
/// Cosecant for double precision
57
///
58
/// ULP 0.5
59
0
pub fn f_csc(x: f64) -> f64 {
60
0
    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
0
    let mut argument_reduction = LargeArgumentReduction::default();
67
68
    // |x| < 2^32 (with FMA) or |x| < 2^23 (w/o FMA)
69
0
    if x_e < E_BIAS + 16 {
70
        // |x| < 2^-26
71
0
        if x_e < E_BIAS - 26 {
72
            // Signed zeros.
73
0
            if x == 0.0 {
74
0
                return if x.is_sign_negative() {
75
0
                    f64::NEG_INFINITY
76
                } else {
77
0
                    f64::INFINITY
78
                };
79
0
            }
80
81
0
            if x_e < E_BIAS - 52 {
82
0
                return 1. / x;
83
0
            }
84
85
            // For |x| < 2^-26, |sin(x) - x| < ulp(x)/2.
86
0
            let rcp = DoubleDouble::from_quick_recip(x);
87
0
            return DoubleDouble::f64_mul_f64_add(x, f64::from_bits(0x3fc5555555555555), rcp)
88
0
                .to_f64();
89
0
        }
90
91
        // // Small range reduction.
92
0
        (y, k) = range_reduction_small(x);
93
    } else {
94
        // Inf or NaN
95
0
        if x_e > 2 * E_BIAS {
96
            // sin(+-Inf) = NaN
97
0
            return x + f64::NAN;
98
0
        }
99
100
        // Large range reduction.
101
0
        (k, y) = argument_reduction.reduce(x);
102
    }
103
104
0
    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
0
    let sk = SIN_K_PI_OVER_128[(k & 255) as usize];
109
0
    let ck = SIN_K_PI_OVER_128[((k.wrapping_add(64)) & 255) as usize];
110
111
0
    let sin_k = DoubleDouble::from_bit_pair(sk);
112
0
    let cos_k = DoubleDouble::from_bit_pair(ck);
113
114
0
    let sin_k_cos_y = DoubleDouble::quick_mult(r_sincos.v_cos, sin_k);
115
0
    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
0
    let mut rr = DoubleDouble::from_exact_add(sin_k_cos_y.hi, cos_k_sin_y.hi);
119
0
    rr.lo += sin_k_cos_y.lo + cos_k_sin_y.lo;
120
121
0
    rr = DoubleDouble::from_exact_add(rr.hi, rr.lo);
122
0
    rr = rr.recip();
123
124
0
    let rlp = rr.lo + r_sincos.err;
125
0
    let rlm = rr.lo - r_sincos.err;
126
127
0
    let r_upper = rr.hi + rlp; // (rr.lo + ERR);
128
0
    let r_lower = rr.hi + rlm; // (rr.lo - ERR);
129
130
    // Ziv's accuracy test
131
0
    if r_upper == r_lower {
132
0
        return rr.to_f64();
133
0
    }
134
135
0
    csc_accurate(x, &mut argument_reduction, x_e, k)
136
0
}
137
138
#[cfg(test)]
139
mod 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
}