/rust/registry/src/index.crates.io-1949cf8c6b5b557f/pxfm-0.1.27/src/tangent/tanpif.rs
Line | Count | Source |
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 | | */ |
29 | | use crate::common::f_fmla; |
30 | | use crate::sin_cosf::ArgumentReducerPi; |
31 | | use crate::tangent::evalf::tanpif_eval; |
32 | | |
33 | | #[inline(always)] |
34 | 0 | fn tanpif_gen_impl(x: f32) -> f32 { |
35 | 0 | let ix = x.to_bits(); |
36 | 0 | let e = ix & (0xff << 23); |
37 | 0 | if e > (150 << 23) { |
38 | | // |x| > 2^23 |
39 | 0 | if e == (0xff << 23) { |
40 | | // x = nan or inf |
41 | 0 | if (ix.wrapping_shl(9)) == 0 { |
42 | | // x = inf |
43 | 0 | return f32::NAN; |
44 | 0 | } |
45 | 0 | return x + x; // x = nan |
46 | 0 | } |
47 | 0 | return f32::copysign(0.0, x); |
48 | 0 | } |
49 | | |
50 | 0 | let argument_reduction = ArgumentReducerPi { x: x as f64 }; |
51 | | |
52 | 0 | let (y, k) = argument_reduction.reduce(); |
53 | | |
54 | 0 | if y == 0.0 { |
55 | 0 | let km = (k.abs() & 31) as i32; // k mod 32 |
56 | | |
57 | 0 | match km { |
58 | 0 | 0 => return 0.0f32.copysign(x), // tanpi(n) = 0 |
59 | 0 | 16 => return f32::copysign(f32::INFINITY, x), // tanpi(n+0.5) = ±∞ |
60 | 0 | 8 => return f32::copysign(1.0, x), // tanpi(n+0.25) = ±1 |
61 | 0 | 24 => return -f32::copysign(1.0, x), // tanpi(n+0.75) = ∓1 |
62 | 0 | _ => {} |
63 | | } |
64 | 0 | } |
65 | | |
66 | 0 | let ax = ix & 0x7fff_ffff; |
67 | 0 | if ax < 0x38d1b717u32 { |
68 | | // taylor series for tan(PI*x) where |x| < 0.0001 |
69 | 0 | let dx = x as f64; |
70 | 0 | let dx_sqr = dx * dx; |
71 | | // tan(PI*x) ~ PI*x + PI^3*x^3/3 + O(x^5) |
72 | 0 | let r = f_fmla( |
73 | 0 | dx_sqr, |
74 | 0 | f64::from_bits(0x4024abbce625be53), |
75 | 0 | f64::from_bits(0x400921fb54442d18), |
76 | | ); |
77 | 0 | return (r * dx) as f32; |
78 | 0 | } |
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 | 0 | let rs = tanpif_eval(y, k); |
84 | | |
85 | | // Then computing tan through identities |
86 | | // num = tan(k*pi/32) + tan(y*pi/32) |
87 | 0 | let num = rs.tan_y + rs.tan_k; |
88 | | // den = 1 - tan(k*pi/32) * tan(y*pi/32) |
89 | 0 | let den = f_fmla(rs.tan_y, -rs.tan_k, 1.); |
90 | 0 | (num / den) as f32 |
91 | 0 | } |
92 | | |
93 | | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
94 | | #[target_feature(enable = "avx", enable = "fma")] |
95 | 0 | unsafe fn tanpif_fma_impl(x: f32) -> f32 { |
96 | 0 | let ix = x.to_bits(); |
97 | 0 | let e = ix & (0xff << 23); |
98 | 0 | if e > (150 << 23) { |
99 | | // |x| > 2^23 |
100 | 0 | if e == (0xff << 23) { |
101 | | // x = nan or inf |
102 | 0 | if (ix.wrapping_shl(9)) == 0 { |
103 | | // x = inf |
104 | 0 | return f32::NAN; |
105 | 0 | } |
106 | 0 | return x + x; // x = nan |
107 | 0 | } |
108 | 0 | return f32::copysign(0.0, x); |
109 | 0 | } |
110 | | |
111 | 0 | let argument_reduction = ArgumentReducerPi { x: x as f64 }; |
112 | | |
113 | 0 | let (y, k) = argument_reduction.reduce_fma(); |
114 | | |
115 | 0 | if y == 0.0 { |
116 | 0 | let km = (k.abs() & 31) as i32; // k mod 32 |
117 | | |
118 | 0 | match km { |
119 | 0 | 0 => return 0.0f32.copysign(x), // tanpi(n) = 0 |
120 | 0 | 16 => return f32::copysign(f32::INFINITY, x), // tanpi(n+0.5) = ±∞ |
121 | 0 | 8 => return f32::copysign(1.0, x), // tanpi(n+0.25) = ±1 |
122 | 0 | 24 => return -f32::copysign(1.0, x), // tanpi(n+0.75) = ∓1 |
123 | 0 | _ => {} |
124 | | } |
125 | 0 | } |
126 | | |
127 | 0 | let ax = ix & 0x7fff_ffff; |
128 | 0 | if ax < 0x38d1b717u32 { |
129 | | // taylor series for tan(PI*x) where |x| < 0.0001 |
130 | 0 | let dx = x as f64; |
131 | 0 | let dx_sqr = dx * dx; |
132 | | // tan(PI*x) ~ PI*x + PI^3*x^3/3 + O(x^5) |
133 | 0 | let r = f64::mul_add( |
134 | 0 | dx_sqr, |
135 | 0 | f64::from_bits(0x4024abbce625be53), |
136 | 0 | f64::from_bits(0x400921fb54442d18), |
137 | | ); |
138 | 0 | return (r * dx) as f32; |
139 | 0 | } |
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 | 0 | 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 | 0 | let num = rs.tan_y + rs.tan_k; |
150 | | // den = 1 - tan(k*pi/32) * tan(y*pi/32) |
151 | 0 | let den = f64::mul_add(rs.tan_y, -rs.tan_k, 1.); |
152 | 0 | (num / den) as f32 |
153 | 0 | } |
154 | | |
155 | | /// Computes tan(PI*x) |
156 | | /// |
157 | | /// Max found ULP 0.5 |
158 | | #[inline] |
159 | 0 | pub 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 | 0 | let q = EXECUTOR.get_or_init(|| { |
169 | 0 | if std::arch::is_x86_feature_detected!("avx") |
170 | 0 | && std::arch::is_x86_feature_detected!("fma") |
171 | | { |
172 | 0 | tanpif_fma_impl |
173 | | } else { |
174 | 0 | tanpif_gen_impl |
175 | | } |
176 | 0 | }); |
177 | 0 | unsafe { q(x) } |
178 | | } |
179 | 0 | } |
180 | | |
181 | | #[cfg(test)] |
182 | | mod 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 | | } |