pxfm/sin_cosf/sincosf.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_estrin_polyeval5;
31use crate::sin_cosf::sincosf_eval::sincosf_eval;
32
33/// Sine and cosine
34///
35/// Max found ULP on working range 0.49999967
36#[inline]
37pub fn f_sincosf(x: f32) -> (f32, f32) {
38 let x_abs = x.to_bits() & 0x7fff_ffffu32;
39 let xd = x as f64;
40
41 // |x| <= pi/16
42 if x_abs <= 0x3e49_0fdbu32 {
43 // |x| < 0.000443633
44 if x_abs < 0x3980_0000u32 {
45 if x_abs == 0u32 {
46 // For signed zeros.
47 return (x, 1.0);
48 }
49 #[cfg(any(
50 all(
51 any(target_arch = "x86", target_arch = "x86_64"),
52 target_feature = "fma"
53 ),
54 all(target_arch = "aarch64", target_feature = "neon")
55 ))]
56 {
57 use crate::common::f_fmlaf;
58 let sf = f_fmlaf(x, f32::from_bits(0xb3000000), x);
59 let cf = f_fmlaf(f32::from_bits(x_abs), f32::from_bits(0xb3000000), 1.);
60 return (sf, cf);
61 }
62 #[cfg(not(any(
63 all(
64 any(target_arch = "x86", target_arch = "x86_64"),
65 target_feature = "fma"
66 ),
67 all(target_arch = "aarch64", target_feature = "neon")
68 )))]
69 {
70 let sf = f_fmla(xd, f64::from_bits(0xbe60000000000000), xd) as f32;
71 let cf = f_fmla(xd, f64::from_bits(0xbe60000000000000), 1.) as f32;
72 return (sf, cf);
73 }
74 }
75
76 let xsqr = xd * xd;
77
78 /*
79 Generated by Sollya:
80 f_sinpi_16 = sin(x)/x;
81 Q = fpminimax(f_sinpi_16, [|0, 2, 4, 6, 8|], [|1, D...|], [0, pi/16]);
82
83 See ./notes/sincosf.sollya
84 */
85 let p = f_estrin_polyeval5(
86 xsqr,
87 f64::from_bits(0x3ff0000000000000),
88 f64::from_bits(0xbfc55555555554c6),
89 f64::from_bits(0x3f81111111085e65),
90 f64::from_bits(0xbf2a019f70fb4d4f),
91 f64::from_bits(0x3ec718d179815e74),
92 );
93 let sf = (xd * p) as f32;
94
95 // Cosine
96 // Generated poly by Sollya:
97 // f_cos_16 = cos(x);
98 //
99 // Q = fpminimax(f_cos_16, [|0, 2, 4, 6, 8|], [|1, D...|], [0, pi/16]);
100 let cf = f_estrin_polyeval5(
101 xsqr,
102 f64::from_bits(0x3ff0000000000000),
103 f64::from_bits(0xbfdffffffffffcea),
104 f64::from_bits(0x3fa55555553d611a),
105 f64::from_bits(0xbf56c16b2e26561a),
106 f64::from_bits(0x3ef9faa67b9da80b),
107 );
108 return (sf, cf as f32);
109 }
110
111 if x_abs >= 0x7f80_0000u32 {
112 return (x + f32::NAN, x + f32::NAN);
113 }
114
115 // Formula:
116 // sin(x) = sin((k + y)*pi/32)
117 // = sin(y*pi/32) * cos(k*pi/32) + cos(y*pi/32) * sin(k*pi/32)
118 // The values of sin(k*pi/32) and cos(k*pi/32) for k = 0..31 are precomputed
119 // and stored using a vector of 32 doubles. Sin(y*pi/32) and cos(y*pi/32) are
120 // computed using degree-7 and degree-6 minimax polynomials generated by
121 // Sollya respectively.
122
123 let rs = sincosf_eval(xd, x_abs);
124 let sf = f_fmla(rs.sin_y, rs.cos_k, f_fmla(rs.cosm1_y, rs.sin_k, rs.sin_k)) as f32;
125 let cf = f_fmla(rs.sin_y, -rs.sin_k, f_fmla(rs.cosm1_y, rs.cos_k, rs.cos_k)) as f32;
126 (sf, cf)
127}
128
129#[cfg(test)]
130mod tests {
131 use super::*;
132
133 #[test]
134 fn f_sincosf_test() {
135 let sincos0 = f_sincosf(0.0);
136 assert!(sincos0.0 < 1e-8);
137 assert_eq!(sincos0.1, 1.0);
138 let sincos_pi = f_sincosf(std::f32::consts::PI);
139 assert!(sincos_pi.0 < 1e-8);
140 let sincos_pi_0_5 = f_sincosf(0.5);
141 assert_eq!(sincos_pi_0_5.0, 0.47942555);
142 assert_eq!(sincos_pi_0_5.1, 0.87758255);
143 let sincos_pi_n0_5 = f_sincosf(-0.5);
144 assert_eq!(sincos_pi_n0_5.0, -0.47942555);
145 assert_eq!(sincos_pi_n0_5.1, 0.87758255);
146 let v_z = f_sincosf(0.0002480338);
147 assert_eq!(v_z.1, 0.9999999692396206);
148 }
149
150 #[test]
151 fn f_sincosf_edge_test() {
152 let s0 = f_sincosf(f32::INFINITY);
153 assert!(s0.0.is_nan());
154 assert!(s0.1.is_nan());
155 let s1 = f_sincosf(f32::NEG_INFINITY);
156 assert!(s1.0.is_nan());
157 assert!(s1.1.is_nan());
158 let s2 = f_sincosf(f32::NAN);
159 assert!(s2.0.is_nan());
160 assert!(s2.1.is_nan());
161 }
162}