Coverage Report

Created: 2025-12-11 07:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/pxfm-0.1.27/src/sinmx.rs
Line
Count
Source
1
/*
2
 * // Copyright (c) Radzivon Bartoshyk 8/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::common::f_fmla;
30
use crate::double_double::DoubleDouble;
31
use crate::polyeval::f_estrin_polyeval5;
32
use crate::sin::{range_reduction_small, sincos_eval};
33
use crate::sin_helper::sincos_eval_dd;
34
use crate::sin_table::SIN_K_PI_OVER_128;
35
use crate::sincos_reduce::LargeArgumentReduction;
36
37
#[cold]
38
#[inline(never)]
39
0
fn sinmx_accurate(y: DoubleDouble, sin_k: DoubleDouble, cos_k: DoubleDouble, x: f64) -> f64 {
40
0
    let r_sincos = sincos_eval_dd(y);
41
42
    // k is an integer and -pi / 256 <= y <= pi / 256.
43
    // Then sin(x) = sin((k * pi/128 + y)
44
    //             = sin(y) * cos(k*pi/128) + cos(y) * sin(k*pi/128)
45
46
0
    let sin_k_cos_y = DoubleDouble::quick_mult(r_sincos.v_cos, sin_k);
47
0
    let cos_k_sin_y = DoubleDouble::quick_mult(r_sincos.v_sin, cos_k);
48
49
0
    let mut rr = DoubleDouble::full_dd_add(sin_k_cos_y, cos_k_sin_y);
50
0
    rr = DoubleDouble::full_add_f64(rr, -x);
51
0
    rr.to_f64()
52
0
}
53
54
#[cold]
55
0
fn sinmx_near_zero_hard(x: f64) -> f64 {
56
    const C: [(u64, u64); 8] = [
57
        (0xb37137ef120d4bbd, 0xb6db8d4e2aa9f813),
58
        (0xbc6555555554e720, 0xbfc5555555555555),
59
        (0x3c01110fff8e3ea0, 0x3f81111111111111),
60
        (0xbb6314569388b856, 0xbf2a01a01a01a01a),
61
        (0xbb61f946e615f3cd, 0x3ec71de3a556c723),
62
        (0x3a8998bc94bd3bf0, 0xbe5ae64567f2d4df),
63
        (0xba702e73490290eb, 0x3de61245e54b6747),
64
        (0xba0182df5b1ffd4c, 0xbd6ae4894bb27213),
65
    ];
66
0
    let x2 = DoubleDouble::from_exact_mult(x, x);
67
0
    let mut p = DoubleDouble::mul_add(
68
0
        x2,
69
0
        DoubleDouble::from_bit_pair(C[7]),
70
0
        DoubleDouble::from_bit_pair(C[6]),
71
    );
72
0
    p = DoubleDouble::mul_add(x2, p, DoubleDouble::from_bit_pair(C[5]));
73
0
    p = DoubleDouble::mul_add(x2, p, DoubleDouble::from_bit_pair(C[4]));
74
0
    p = DoubleDouble::mul_add(x2, p, DoubleDouble::from_bit_pair(C[3]));
75
0
    p = DoubleDouble::mul_add(x2, p, DoubleDouble::from_bit_pair(C[2]));
76
0
    p = DoubleDouble::mul_add(x2, p, DoubleDouble::from_bit_pair(C[1]));
77
0
    p = DoubleDouble::mul_add(x2, p, DoubleDouble::from_bit_pair(C[0]));
78
0
    p = DoubleDouble::quick_mult_f64(p, x);
79
0
    p.to_f64()
80
0
}
81
82
/// Computes sin(x) - x
83
///
84
/// ULP 0.5
85
0
pub fn f_sinmx(x: f64) -> f64 {
86
0
    let x_e = (x.to_bits() >> 52) & 0x7ff;
87
    const E_BIAS: u64 = (1u64 << (11 - 1u64)) - 1u64;
88
89
    let y: DoubleDouble;
90
    let k;
91
92
0
    let mut argument_reduction = LargeArgumentReduction::default();
93
94
    // |x| < 2^32 (with FMA) or |x| < 2^23 (w/o FMA)
95
0
    if x_e < E_BIAS + 16 {
96
0
        if x_e < E_BIAS - 6 {
97
            // |x| < 2^-6
98
0
            if x_e < E_BIAS - 32 {
99
                // |x| < 2^-32
100
                // Signed zeros.
101
0
                if x == 0.0 {
102
0
                    return x;
103
0
                }
104
105
                // For |x| < 2^-32, taylor series sin(x) - x ~ -x^3/6
106
0
                let x2 = x * x;
107
0
                let c = f_fmla(
108
0
                    x2,
109
0
                    f64::from_bits(0x3f81111111111111),
110
0
                    f64::from_bits(0xbfc5555555555555),
111
0
                ) * x2;
112
0
                return c * x;
113
0
            }
114
115
            // Generated by Sollya:
116
            // d = [2^-26, pi/16];
117
            // f_sinmx = (sin(x) - x)/x;
118
            // Q = fpminimax(f_sinmx, [|0, 2, 4, 6, 8, 10, 12|], [|127, 127, D...|], d);
119
0
            let x2 = DoubleDouble::from_exact_mult(x, x);
120
0
            let p = f_estrin_polyeval5(
121
0
                x2.hi,
122
0
                f64::from_bits(0x3f81111111111111),
123
0
                f64::from_bits(0xbf2a01a01a019d2f),
124
0
                f64::from_bits(0x3ec71de3a5269512),
125
0
                f64::from_bits(0xbe5ae642b76ba0f5),
126
0
                f64::from_bits(0x3de6035da3c7eaed),
127
            );
128
0
            let mut c = DoubleDouble::mul_f64_add(
129
0
                x2,
130
0
                p,
131
0
                DoubleDouble::from_bit_pair((0xbc655542976eb2af, 0xbfc5555555555555)),
132
            );
133
0
            c = DoubleDouble::mul_add(
134
0
                x2,
135
0
                c,
136
0
                DoubleDouble::from_bit_pair((0x34b215c35dc9e9be, 0xb832bde584573661)),
137
0
            );
138
0
            c = DoubleDouble::quick_mult_f64(c, x);
139
0
            let err = f_fmla(
140
0
                x2.hi,
141
0
                f64::from_bits(0x3cc0000000000000), // 2^-51
142
0
                f64::from_bits(0x3bc0000000000000), // 2^-67
143
            );
144
0
            let ub = c.hi + (c.lo + err);
145
0
            let lb = c.hi + (c.lo - err);
146
0
            if ub == lb {
147
0
                return c.to_f64();
148
0
            }
149
0
            return sinmx_near_zero_hard(x);
150
0
        }
151
        // // Small range reduction.
152
0
        (y, k) = range_reduction_small(x);
153
    } else {
154
        // Inf or NaN
155
0
        if x_e > 2 * E_BIAS {
156
            // sin(+-Inf) = NaN
157
0
            return x + f64::NAN;
158
0
        }
159
160
        // Large range reduction.
161
0
        (k, y) = argument_reduction.reduce(x);
162
    }
163
164
0
    let r_sincos = sincos_eval(y);
165
166
    // cos(k * pi/128) = sin(k * pi/128 + pi/2) = sin((k + 64) * pi/128).
167
0
    let sk = SIN_K_PI_OVER_128[(k & 255) as usize];
168
0
    let ck = SIN_K_PI_OVER_128[((k.wrapping_add(64)) & 255) as usize];
169
170
0
    let sin_k = DoubleDouble::from_bit_pair(sk);
171
0
    let cos_k = DoubleDouble::from_bit_pair(ck);
172
173
0
    let sin_k_cos_y = DoubleDouble::quick_mult(r_sincos.v_cos, sin_k);
174
0
    let cos_k_sin_y = DoubleDouble::quick_mult(r_sincos.v_sin, cos_k);
175
176
    // sin_k_cos_y is always >> cos_k_sin_y
177
0
    let mut rr = DoubleDouble::from_exact_add(sin_k_cos_y.hi, cos_k_sin_y.hi);
178
0
    rr.lo += sin_k_cos_y.lo + cos_k_sin_y.lo;
179
180
0
    rr = DoubleDouble::from_exact_add(rr.hi, rr.lo);
181
0
    rr = DoubleDouble::full_add_f64(rr, -x);
182
183
0
    let rlp = rr.lo + r_sincos.err;
184
0
    let rlm = rr.lo - r_sincos.err;
185
186
0
    let r_upper = rr.hi + rlp; // (rr.lo + ERR);
187
0
    let r_lower = rr.hi + rlm; // (rr.lo - ERR);
188
189
    // Ziv's accuracy test
190
0
    if r_upper == r_lower {
191
0
        return rr.to_f64();
192
0
    }
193
194
0
    sinmx_accurate(y, sin_k, cos_k, x)
195
0
}
196
197
#[cfg(test)]
198
mod tests {
199
    use super::*;
200
201
    #[test]
202
    fn f_sinf_test() {
203
        assert_eq!(f_sinmx(0.0), 0.0);
204
        assert_eq!(f_sinmx(1.0), -0.1585290151921035);
205
        assert_eq!(f_sinmx(0.3), -0.0044797933386604245);
206
        assert_eq!(f_sinmx(-1.0), 0.1585290151921035);
207
        assert_eq!(f_sinmx(-0.3), 0.0044797933386604245);
208
        assert_eq!(f_sinmx(std::f64::consts::PI / 2.), -0.5707963267948966);
209
        assert!(f_sinmx(f64::INFINITY).is_nan());
210
        assert!(f_sinmx(f64::NEG_INFINITY).is_nan());
211
    }
212
}