pxfm/tangent/
tanpif.rs

1/*
2 * // Copyright (c) Radzivon Bartoshyk 6/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::sin_cosf::ArgumentReducerPi;
31use crate::tangent::evalf::tanpif_eval;
32
33#[inline(always)]
34fn tanpif_gen_impl(x: f32) -> f32 {
35    let ix = x.to_bits();
36    let e = ix & (0xff << 23);
37    if e > (150 << 23) {
38        // |x| > 2^23
39        if e == (0xff << 23) {
40            // x = nan or inf
41            if (ix.wrapping_shl(9)) == 0 {
42                // x = inf
43                return f32::NAN;
44            }
45            return x + x; // x = nan
46        }
47        return f32::copysign(0.0, x);
48    }
49
50    let argument_reduction = ArgumentReducerPi { x: x as f64 };
51
52    let (y, k) = argument_reduction.reduce();
53
54    if y == 0.0 {
55        let km = (k.abs() & 31) as i32; // k mod 32
56
57        match km {
58            0 => return 0.0f32.copysign(x),               // tanpi(n) = 0
59            16 => return f32::copysign(f32::INFINITY, x), // tanpi(n+0.5) = ±∞
60            8 => return f32::copysign(1.0, x),            // tanpi(n+0.25) = ±1
61            24 => return -f32::copysign(1.0, x),          // tanpi(n+0.75) = ∓1
62            _ => {}
63        }
64    }
65
66    let ax = ix & 0x7fff_ffff;
67    if ax < 0x38d1b717u32 {
68        // taylor series for tan(PI*x) where |x| < 0.0001
69        let dx = x as f64;
70        let dx_sqr = dx * dx;
71        // tan(PI*x) ~ PI*x + PI^3*x^3/3 + O(x^5)
72        let r = f_fmla(
73            dx_sqr,
74            f64::from_bits(0x4024abbce625be53),
75            f64::from_bits(0x400921fb54442d18),
76        );
77        return (r * dx) as f32;
78    }
79
80    // tanpif_eval returns:
81    // - rs.tan_y = tan(pi/32 * y)          -> tangent of the remainder
82    // - rs.tan_k = tan(pi/32 * k)          -> tan of the main angle multiple
83    let rs = tanpif_eval(y, k);
84
85    // Then computing tan through identities
86    // num = tan(k*pi/32) + tan(y*pi/32)
87    let num = rs.tan_y + rs.tan_k;
88    // den = 1 - tan(k*pi/32) * tan(y*pi/32)
89    let den = f_fmla(rs.tan_y, -rs.tan_k, 1.);
90    (num / den) as f32
91}
92
93#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
94#[target_feature(enable = "avx", enable = "fma")]
95unsafe fn tanpif_fma_impl(x: f32) -> f32 {
96    let ix = x.to_bits();
97    let e = ix & (0xff << 23);
98    if e > (150 << 23) {
99        // |x| > 2^23
100        if e == (0xff << 23) {
101            // x = nan or inf
102            if (ix.wrapping_shl(9)) == 0 {
103                // x = inf
104                return f32::NAN;
105            }
106            return x + x; // x = nan
107        }
108        return f32::copysign(0.0, x);
109    }
110
111    let argument_reduction = ArgumentReducerPi { x: x as f64 };
112
113    let (y, k) = argument_reduction.reduce_fma();
114
115    if y == 0.0 {
116        let km = (k.abs() & 31) as i32; // k mod 32
117
118        match km {
119            0 => return 0.0f32.copysign(x),               // tanpi(n) = 0
120            16 => return f32::copysign(f32::INFINITY, x), // tanpi(n+0.5) = ±∞
121            8 => return f32::copysign(1.0, x),            // tanpi(n+0.25) = ±1
122            24 => return -f32::copysign(1.0, x),          // tanpi(n+0.75) = ∓1
123            _ => {}
124        }
125    }
126
127    let ax = ix & 0x7fff_ffff;
128    if ax < 0x38d1b717u32 {
129        // taylor series for tan(PI*x) where |x| < 0.0001
130        let dx = x as f64;
131        let dx_sqr = dx * dx;
132        // tan(PI*x) ~ PI*x + PI^3*x^3/3 + O(x^5)
133        let r = f64::mul_add(
134            dx_sqr,
135            f64::from_bits(0x4024abbce625be53),
136            f64::from_bits(0x400921fb54442d18),
137        );
138        return (r * dx) as f32;
139    }
140
141    // tanpif_eval returns:
142    // - rs.tan_y = tan(pi/32 * y)          -> tangent of the remainder
143    // - rs.tan_k = tan(pi/32 * k)          -> tan of the main angle multiple
144    use crate::tangent::evalf::tanpif_eval_fma;
145    let rs = tanpif_eval_fma(y, k);
146
147    // Then computing tan through identities
148    // num = tan(k*pi/32) + tan(y*pi/32)
149    let num = rs.tan_y + rs.tan_k;
150    // den = 1 - tan(k*pi/32) * tan(y*pi/32)
151    let den = f64::mul_add(rs.tan_y, -rs.tan_k, 1.);
152    (num / den) as f32
153}
154
155/// Computes tan(PI*x)
156///
157/// Max found ULP 0.5
158#[inline]
159pub fn f_tanpif(x: f32) -> f32 {
160    #[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
161    {
162        tanpif_gen_impl(x)
163    }
164    #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
165    {
166        use std::sync::OnceLock;
167        static EXECUTOR: OnceLock<unsafe fn(f32) -> f32> = OnceLock::new();
168        let q = EXECUTOR.get_or_init(|| {
169            if std::arch::is_x86_feature_detected!("avx")
170                && std::arch::is_x86_feature_detected!("fma")
171            {
172                tanpif_fma_impl
173            } else {
174                tanpif_gen_impl
175            }
176        });
177        unsafe { q(x) }
178    }
179}
180
181#[cfg(test)]
182mod tests {
183    use super::*;
184
185    #[test]
186    fn test_tanpif() {
187        assert_eq!(f_tanpif(3.666738e-5), 0.00011519398);
188        assert_eq!(f_tanpif(1.0355987e-25), 3.2534293e-25);
189        assert_eq!(f_tanpif(5.5625), -5.0273395);
190        assert_eq!(f_tanpif(-29.75), 1.0);
191        assert_eq!(f_tanpif(-21.5625), 5.0273395);
192        assert_eq!(f_tanpif(-15.611655), 2.7329326);
193        assert_eq!(f_tanpif(115.30706), 1.4426143);
194        assert!(f_tanpif(f32::INFINITY).is_nan());
195        assert!(f_tanpif(f32::NAN).is_nan());
196    }
197}