pxfm/sin_cosf/cscf.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_polyeval3, f_polyeval5};
31use crate::sin_cosf::sincosf_eval::sincosf_eval;
32
33/// Cosecant ( 1 / sin(x) )
34///
35/// ULP 0.5
36#[inline]
37pub fn f_cscf(x: 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 < 0x39e8_9769u32 {
45 if x_abs == 0u32 {
46 // For signed zeros.
47 return if x.is_sign_negative() {
48 f32::NEG_INFINITY
49 } else {
50 f32::INFINITY
51 };
52 }
53 let dx = x as f64;
54 let c_term = 1. / dx;
55 let x2 = dx * dx;
56 // Maclaurin series
57 // 1/x + x/6 + (7 x^3)/360 + (31 x^5)/15120 + O(x^7)
58 let p = f_polyeval3(
59 x2,
60 f64::from_bits(0x3fc5555555555555),
61 f64::from_bits(0x3f93e93e93e93e94),
62 f64::from_bits(0x3f60b2463814bc5f),
63 );
64 return f_fmla(dx, p, c_term) as f32;
65 }
66
67 let xsqr = xd * xd;
68
69 /*
70 Generated by Sollya:
71 f = 1 / sin(x) - 1/x;
72
73 d = [0.000443633; pi/16];
74 pf = fpminimax(f, [|1, 3, 5, 7, 9|], [|D...|], d, relative, floating);
75
76 See ./notes/cscf.sollya
77 */
78
79 let p = f_polyeval5(
80 xsqr,
81 f64::from_bits(0x3fc5555555555562),
82 f64::from_bits(0x3f93e93e93e730a3),
83 f64::from_bits(0x3f60cbb77382ae6f),
84 f64::from_bits(0x3f2b85bfd4188934),
85 f64::from_bits(0x3ef697a32ebe822d),
86 );
87 return f_fmla(xd, p, 1. / xd) as f32;
88 }
89
90 if x_abs >= 0x7f80_0000u32 {
91 return x + f32::NAN;
92 }
93
94 // Formula:
95 // sin(x) = sin((k + y)*pi/32)
96 // = sin(y*pi/32) * cos(k*pi/32) + cos(y*pi/32) * sin(k*pi/32)
97 // The values of sin(k*pi/32) and cos(k*pi/32) for k = 0..31 are precomputed
98 // and stored using a vector of 32 doubles. Sin(y*pi/32) and cos(y*pi/32) are
99 // computed using degree-7 and degree-6 minimax polynomials generated by
100 // Sollya respectively.
101
102 let rs = sincosf_eval(xd, x_abs);
103 (1. / f_fmla(rs.sin_y, rs.cos_k, f_fmla(rs.cosm1_y, rs.sin_k, rs.sin_k))) as f32
104}
105
106#[cfg(test)]
107mod tests {
108 use super::*;
109
110 #[test]
111 fn f_cscf_test() {
112 assert_eq!(f_cscf(0.04915107), 20.353632);
113 assert_eq!(f_cscf(0.5), 2.0858297);
114 assert_eq!(f_cscf(0.07), 14.297387);
115 assert_eq!(f_cscf(3.6171106e-5), 27646.375);
116 assert_eq!(f_cscf(-5.535772e-10), -1806432800.0);
117 assert_eq!(f_cscf(0.0), f32::INFINITY);
118 assert_eq!(f_cscf(-0.0), f32::NEG_INFINITY);
119 assert_eq!(f_cscf(-1.0854926e-19), -9.2124077e18);
120 }
121}