pxfm/sin_cosf/secf.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_polyeval6;
31use crate::sin_cosf::sincosf_eval::sincosf_eval;
32
33/// Computes secant ( 1 / cos(x) )
34///
35/// Max found ULP 0.5
36#[inline]
37pub fn f_secf(x: f32) -> f32 {
38 let x_abs = x.to_bits() & 0x7fff_ffffu32;
39 let x = f32::from_bits(x_abs);
40 let xd = x as f64;
41
42 // |x| <= pi/16
43 if x_abs <= 0x3e49_0fdbu32 {
44 // |x| < 0.000244141
45 if x_abs < 0x3980_0000u32 {
46 // taylor series for sec(x) ~ 1 + x^2/2 + O(x^4)
47 // for such small interval just doing 2 first coefficients from taylor series
48 // FMA availability is mandatory to perform it in f32 without upcasting to f64.
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 return f_fmlaf(x, x * f32::from_bits(0x3f000000), 1.);
59 }
60 #[cfg(not(any(
61 all(
62 any(target_arch = "x86", target_arch = "x86_64"),
63 target_feature = "fma"
64 ),
65 all(target_arch = "aarch64", target_feature = "neon")
66 )))]
67 {
68 let x2 = xd * xd;
69 return f_fmla(x2, f64::from_bits(0x3fe0000000000000), 1.) as f32;
70 }
71 }
72
73 // Secant
74 // Generated poly by Sollya:
75 // f = 1 / cos(x);
76 // d = [0.000244141; pi/16];
77 // pf = fpminimax(f, [|0, 2, 4, 6, 8, 10|], [|1, D...|], d, relative, floating);
78 //
79 // See ./notes/secf.sollya
80
81 let x2 = xd * xd;
82 let p = f_polyeval6(
83 x2,
84 f64::from_bits(0x3ff0000000000000),
85 f64::from_bits(0x3fe000000001c0fb),
86 f64::from_bits(0x3fcaaaaaa0b8a71b),
87 f64::from_bits(0x3fb5b06437bc5a13),
88 f64::from_bits(0x3fa192a33a9fca4f),
89 f64::from_bits(0x3f8dde280c29af37),
90 );
91 return p as f32;
92 }
93
94 if x_abs >= 0x7f80_0000u32 {
95 return x + f32::NAN;
96 }
97
98 // Formula:
99 // cos(x) = cos((k + y)*pi/32)
100 // = cos(y*pi/32) * cos(k*pi/32) - sin(y*pi/32) * sin(k*pi/32)
101 // The values of sin(k*pi/32) and cos(k*pi/32) for k = 0..63 are precomputed
102 // and stored using a vector of 32 doubles. Sin(y*pi/32) and cos(y*pi/32) are
103 // computed using degree-7 and degree-6 minimax polynomials generated by
104 // Sollya respectively.
105 // Combine the results with the sine of sum formula:
106 // cos(x) = cos((k + y)*pi/32)
107 // = cos(y*pi/32) * cos(k*pi/32) - sin(y*pi/32) * sin(k*pi/32)
108 // = cosm1_y * cos_k + sin_y * sin_k
109 // = (cosm1_y * cos_k + cos_k) + sin_y * sin_k
110 // then sec(x) = 1/cos(x)
111
112 let rs = sincosf_eval(xd, x_abs);
113 (1. / f_fmla(rs.sin_y, -rs.sin_k, f_fmla(rs.cosm1_y, rs.cos_k, rs.cos_k))) as f32
114}
115
116#[cfg(test)]
117mod tests {
118 use super::*;
119
120 #[test]
121 fn test_f_secf() {
122 assert_eq!(f_secf(0.0), 1.0);
123 assert_eq!(f_secf(0.5), 1.139494);
124 assert_eq!(f_secf(-0.5), 1.139494);
125 assert_eq!(f_secf(1.5), 14.136833);
126 assert_eq!(f_secf(-1.5), 14.136833);
127 assert!(f_secf(f32::INFINITY).is_nan());
128 assert!(f_secf(f32::NEG_INFINITY).is_nan());
129 assert!(f_secf(f32::NAN).is_nan());
130 }
131}