pxfm/sin_cosf/cospif.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::{is_integerf, is_odd_integerf};
30use crate::polyeval::f_polyeval5;
31use crate::sin_cosf::argument_reduction_pi::ArgumentReducerPi;
32use crate::sin_cosf::sincosf_eval::{cospif_eval, sinpif_eval};
33
34/// Computes cos(PI*x)
35///
36/// Max ULP 0.5
37#[inline]
38pub fn f_cospif(x: f32) -> f32 {
39 let x_abs = x.to_bits() & 0x7fff_ffffu32;
40 let x = f32::from_bits(x_abs);
41 let xd = x as f64;
42
43 // |x| <= 1/16
44 if x_abs <= 0x3d80_0000u32 {
45 // |x| < 0.00000009546391
46 if x_abs < 0x38a2_f984u32 {
47 #[cfg(any(
48 all(
49 any(target_arch = "x86", target_arch = "x86_64"),
50 target_feature = "fma"
51 ),
52 all(target_arch = "aarch64", target_feature = "neon")
53 ))]
54 {
55 use crate::common::f_fmlaf;
56 return f_fmlaf(x, f32::from_bits(0xb3000000), 1.);
57 }
58 #[cfg(not(any(
59 all(
60 any(target_arch = "x86", target_arch = "x86_64"),
61 target_feature = "fma"
62 ),
63 all(target_arch = "aarch64", target_feature = "neon")
64 )))]
65 {
66 use crate::common::f_fmla;
67 return f_fmla(xd, f64::from_bits(0xbe60000000000000), 1.) as f32;
68 }
69 }
70
71 // Cos(x*PI)
72 // Generated poly by Sollya:
73 // d = [0, 1/16];
74 // f_cos = cos(y*pi);
75 // Q = fpminimax(f_cos, [|0, 2, 4, 6, 8|], [|D...|], d, relative, floating);
76 //
77 // See ./notes/cospif.sollya
78
79 let x2 = xd * xd;
80 let p = f_polyeval5(
81 x2,
82 f64::from_bits(0x3ff0000000000000),
83 f64::from_bits(0xc013bd3cc9be43f7),
84 f64::from_bits(0x40103c1f08091fe0),
85 f64::from_bits(0xbff55d3ba3d94835),
86 f64::from_bits(0x3fce173c2a00e74e),
87 );
88 return p as f32;
89 }
90
91 // Numbers greater or equal to 2^23 are always integers or NaN
92 if x_abs >= 0x4b00_0000u32 || is_integerf(x) {
93 if x_abs >= 0x7f80_0000u32 {
94 return x + f32::NAN;
95 }
96 if x_abs < 0x4b80_0000u32 {
97 static CF: [f32; 2] = [1., -1.];
98 return CF[is_odd_integerf(x) as usize];
99 }
100 return 1.;
101 }
102
103 // We're computing cos(y) after argument reduction then return valid value
104 // based on quadrant
105 let reducer = ArgumentReducerPi { x: x as f64 };
106 let (y, k) = reducer.reduce_0p25();
107 // Decide based on quadrant what kernel function to use
108 (match k & 3 {
109 0 => cospif_eval(y),
110 1 => sinpif_eval(-y),
111 2 => -cospif_eval(y),
112 _ => sinpif_eval(y),
113 }) as f32
114}
115
116#[cfg(test)]
117mod tests {
118 use super::*;
119
120 #[test]
121 fn test_f_cospif() {
122 assert_eq!(f_cospif(1.), -1.);
123 assert_eq!(f_cospif(-3.5), 0.0);
124 assert_eq!(f_cospif(3.), -1.);
125 assert_eq!(f_cospif(-3.), -1.);
126 assert_eq!(f_cospif(2.), 1.);
127 assert_eq!(f_cospif(-2.), 1.);
128 assert_eq!(f_cospif(115.30706), -0.5696978);
129 assert!(f_cospif(f32::INFINITY).is_nan());
130 assert!(f_cospif(f32::NAN).is_nan());
131 assert!(f_cospif(f32::NEG_INFINITY).is_nan());
132 }
133}