pxfm/sin_cosf/sinmxf.rs
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 */
29use crate::common::f_fmla;
30use crate::polyeval::f_polyeval5;
31use crate::sin_cosf::sincosf_eval::sincosf_eval;
32
33/// Computes sin(x) - x
34pub fn f_sinmxf(x: f32) -> f32 {
35 let x_abs = x.to_bits() & 0x7fff_ffffu32;
36 let xd = x as f64;
37 let ax: u32 = x.to_bits().wrapping_shl(1);
38 if ax == 0 || ax >= 0xffu32 << 24 {
39 if x_abs == 0u32 {
40 // For signed zeros.
41 return x;
42 }
43 return f32::NAN; // x == inf or x == NaN
44 }
45
46 // |x| <= pi/16
47 if x_abs <= 0x3e49_0fdbu32 {
48 // |x| < 0.000443633
49 if x_abs < 0x39e8_9769u32 {
50 // sin(x) - x ~ -x^3/6 + x^5/120 + O(x^7)
51 let dx = x as f64;
52 let x2 = dx * dx;
53 let c = f_fmla(
54 x2,
55 f64::from_bits(0x3f81111111111111),
56 f64::from_bits(0xbfc5555555555555),
57 ) * x2;
58 return (c * dx) as f32;
59 }
60
61 let xsqr = xd * xd;
62
63 // Generated by Sollya:
64 // d = [2^-10, pi/16];
65 // f_sinmx = (sin(x) - x)/x;
66 // Q = fpminimax(f_sinmx, [|0, 2, 4, 6, 8|], [|D...|], d);
67 // See ./notes/sinmxf.sollya
68 let p = f_polyeval5(
69 xsqr,
70 f64::from_bits(0xbb8f6a339382f07f),
71 f64::from_bits(0xbfc5555555555545),
72 f64::from_bits(0x3f811111110ddf5c),
73 f64::from_bits(0xbf2a019fb330c620),
74 f64::from_bits(0x3ec719bc2a614884),
75 );
76 return (xd * p) as f32;
77 }
78
79 // Formula:
80 // sin(x) = sin((k + y)*pi/32)
81 // = sin(y*pi/32) * cos(k*pi/32) + cos(y*pi/32) * sin(k*pi/32)
82 // The values of sin(k*pi/32) and cos(k*pi/32) for k = 0..31 are precomputed
83 // and stored using a vector of 32 doubles. Sin(y*pi/32) and cos(y*pi/32) are
84 // computed using degree-7 and degree-6 minimax polynomials generated by
85 // Sollya respectively.
86
87 let rs = sincosf_eval(xd, x_abs);
88 let sinx = f_fmla(rs.sin_y, rs.cos_k, f_fmla(rs.cosm1_y, rs.sin_k, rs.sin_k));
89 (sinx - xd) as f32
90}
91
92#[cfg(test)]
93mod tests {
94 use super::*;
95
96 #[test]
97 fn f_sinf_test() {
98 assert_eq!(f_sinmxf(0.00016344387), -0.00000000000072770376);
99 assert_eq!(f_sinmxf(0.0), 0.0);
100 assert_eq!(f_sinmxf(-0.0), 0.0);
101 assert_eq!(f_sinmxf(1.0), -0.15852901);
102 assert_eq!(f_sinmxf(0.3), -0.004479794);
103 assert_eq!(f_sinmxf(-1.0), 0.15852901);
104 assert_eq!(f_sinmxf(-0.3), 0.004479794);
105 assert_eq!(f_sinmxf(std::f32::consts::PI / 2.), -0.5707964);
106 assert!(f_sinmxf(f32::INFINITY).is_nan());
107 assert!(f_sinmxf(f32::NEG_INFINITY).is_nan());
108 assert!(f_sinmxf(f32::NAN).is_nan());
109 }
110}