| 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 |
*/ |
| 29 |
use crate::common::f_fmla; |
| 30 |
use crate::polyeval::{f_polyeval3, f_polyeval5}; |
| 31 |
use crate::sin_cosf::sincosf_eval::sincosf_eval; |
| 32 |
|
| 33 |
#[inline(always)] |
| 34 |
fn cscf_gen_impl(x: f32) -> f32 { |
| 35 |
let x_abs = x.to_bits() & 0x7fff_ffffu32; |
| 36 |
let xd = x as f64; |
| 37 |
|
| 38 |
// |x| <= pi/16 |
| 39 |
if x_abs <= 0x3e49_0fdbu32 { |
| 40 |
// |x| < 0.000443633 |
| 41 |
if x_abs < 0x39e8_9769u32 { |
| 42 |
if x_abs == 0u32 { |
| 43 |
// For signed zeros. |
| 44 |
return if x.is_sign_negative() { |
| 45 |
f32::NEG_INFINITY |
| 46 |
} else { |
| 47 |
f32::INFINITY |
| 48 |
}; |
| 49 |
} |
| 50 |
let dx = x as f64; |
| 51 |
let c_term = 1. / dx; |
| 52 |
let x2 = dx * dx; |
| 53 |
// Maclaurin series |
| 54 |
// 1/x + x/6 + (7 x^3)/360 + (31 x^5)/15120 + O(x^7) |
| 55 |
let p = f_polyeval3( |
| 56 |
x2, |
| 57 |
f64::from_bits(0x3fc5555555555555), |
| 58 |
f64::from_bits(0x3f93e93e93e93e94), |
| 59 |
f64::from_bits(0x3f60b2463814bc5f), |
| 60 |
); |
| 61 |
return f_fmla(dx, p, c_term) as f32; |
| 62 |
} |
| 63 |
|
| 64 |
let xsqr = xd * xd; |
| 65 |
|
| 66 |
/* |
| 67 |
Generated by Sollya: |
| 68 |
f = 1 / sin(x) - 1/x; |
| 69 |
|
| 70 |
d = [0.000443633; pi/16]; |
| 71 |
pf = fpminimax(f, [|1, 3, 5, 7, 9|], [|D...|], d, relative, floating); |
| 72 |
|
| 73 |
See ./notes/cscf.sollya |
| 74 |
*/ |
| 75 |
|
| 76 |
let p = f_polyeval5( |
| 77 |
xsqr, |
| 78 |
f64::from_bits(0x3fc5555555555562), |
| 79 |
f64::from_bits(0x3f93e93e93e730a3), |
| 80 |
f64::from_bits(0x3f60cbb77382ae6f), |
| 81 |
f64::from_bits(0x3f2b85bfd4188934), |
| 82 |
f64::from_bits(0x3ef697a32ebe822d), |
| 83 |
); |
| 84 |
return f_fmla(xd, p, 1. / xd) as f32; |
| 85 |
} |
| 86 |
|
| 87 |
if x_abs >= 0x7f80_0000u32 { |
| 88 |
return x + f32::NAN; |
| 89 |
} |
| 90 |
|
| 91 |
// Formula: |
| 92 |
// sin(x) = sin((k + y)*pi/32) |
| 93 |
// = sin(y*pi/32) * cos(k*pi/32) + cos(y*pi/32) * sin(k*pi/32) |
| 94 |
// The values of sin(k*pi/32) and cos(k*pi/32) for k = 0..31 are precomputed |
| 95 |
// and stored using a vector of 32 doubles. Sin(y*pi/32) and cos(y*pi/32) are |
| 96 |
// computed using degree-7 and degree-6 minimax polynomials generated by |
| 97 |
// Sollya respectively. |
| 98 |
|
| 99 |
let rs = sincosf_eval(xd, x_abs); |
| 100 |
(1. / f_fmla(rs.sin_y, rs.cos_k, f_fmla(rs.cosm1_y, rs.sin_k, rs.sin_k))) as f32 |
| 101 |
} |
| 102 |
|
| 103 |
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 104 |
#[target_feature(enable = "avx", enable = "fma")] |
| 105 |
unsafe fn cscf_fma_impl(x: f32) -> f32 { |
| 106 |
let x_abs = x.to_bits() & 0x7fff_ffffu32; |
| 107 |
let xd = x as f64; |
| 108 |
|
| 109 |
// |x| <= pi/16 |
| 110 |
if x_abs <= 0x3e49_0fdbu32 { |
| 111 |
// |x| < 0.000443633 |
| 112 |
if x_abs < 0x39e8_9769u32 { |
| 113 |
if x_abs == 0u32 { |
| 114 |
// For signed zeros. |
| 115 |
return if x.is_sign_negative() { |
| 116 |
f32::NEG_INFINITY |
| 117 |
} else { |
| 118 |
f32::INFINITY |
| 119 |
}; |
| 120 |
} |
| 121 |
let dx = x as f64; |
| 122 |
let c_term = 1. / dx; |
| 123 |
let x2 = dx * dx; |
| 124 |
// Maclaurin series |
| 125 |
// 1/x + x/6 + (7 x^3)/360 + (31 x^5)/15120 + O(x^7) |
| 126 |
use crate::polyeval::d_polyeval3; |
| 127 |
let p = d_polyeval3( |
| 128 |
x2, |
| 129 |
f64::from_bits(0x3fc5555555555555), |
| 130 |
f64::from_bits(0x3f93e93e93e93e94), |
| 131 |
f64::from_bits(0x3f60b2463814bc5f), |
| 132 |
); |
| 133 |
return f64::mul_add(dx, p, c_term) as f32; |
| 134 |
} |
| 135 |
|
| 136 |
let xsqr = xd * xd; |
| 137 |
|
| 138 |
/* |
| 139 |
Generated by Sollya: |
| 140 |
f = 1 / sin(x) - 1/x; |
| 141 |
|
| 142 |
d = [0.000443633; pi/16]; |
| 143 |
pf = fpminimax(f, [|1, 3, 5, 7, 9|], [|D...|], d, relative, floating); |
| 144 |
|
| 145 |
See ./notes/cscf.sollya |
| 146 |
*/ |
| 147 |
use crate::polyeval::d_polyeval5; |
| 148 |
let p = d_polyeval5( |
| 149 |
xsqr, |
| 150 |
f64::from_bits(0x3fc5555555555562), |
| 151 |
f64::from_bits(0x3f93e93e93e730a3), |
| 152 |
f64::from_bits(0x3f60cbb77382ae6f), |
| 153 |
f64::from_bits(0x3f2b85bfd4188934), |
| 154 |
f64::from_bits(0x3ef697a32ebe822d), |
| 155 |
); |
| 156 |
return f64::mul_add(xd, p, 1. / xd) as f32; |
| 157 |
} |
| 158 |
|
| 159 |
if x_abs >= 0x7f80_0000u32 { |
| 160 |
return x + f32::NAN; |
| 161 |
} |
| 162 |
|
| 163 |
// Formula: |
| 164 |
// sin(x) = sin((k + y)*pi/32) |
| 165 |
// = sin(y*pi/32) * cos(k*pi/32) + cos(y*pi/32) * sin(k*pi/32) |
| 166 |
// The values of sin(k*pi/32) and cos(k*pi/32) for k = 0..31 are precomputed |
| 167 |
// and stored using a vector of 32 doubles. Sin(y*pi/32) and cos(y*pi/32) are |
| 168 |
// computed using degree-7 and degree-6 minimax polynomials generated by |
| 169 |
// Sollya respectively. |
| 170 |
use crate::sin_cosf::sincosf_eval::sincosf_eval_fma; |
| 171 |
let rs = sincosf_eval_fma(xd, x_abs); |
| 172 |
(1. / f64::mul_add( |
| 173 |
rs.sin_y, |
| 174 |
rs.cos_k, |
| 175 |
f64::mul_add(rs.cosm1_y, rs.sin_k, rs.sin_k), |
| 176 |
)) as f32 |
| 177 |
} |
| 178 |
|
| 179 |
/// Cosecant ( 1 / sin(x) ) |
| 180 |
/// |
| 181 |
/// ULP 0.5 |
| 182 |
#[inline] |
| 183 |
pub fn f_cscf(x: f32) -> f32 { |
| 184 |
#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))] |
| 185 |
{ |
| 186 |
cscf_gen_impl(x) |
| 187 |
} |
| 188 |
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))] |
| 189 |
{ |
| 190 |
use std::sync::OnceLock; |
| 191 |
static EXECUTOR: OnceLock<unsafe fn(f32) -> f32> = OnceLock::new(); |
| 192 |
let q = EXECUTOR.get_or_init(|| { |
| 193 |
if std::arch::is_x86_feature_detected!("avx") |
| 194 |
&& std::arch::is_x86_feature_detected!("fma") |
| 195 |
{ |
| 196 |
cscf_fma_impl |
| 197 |
} else { |
| 198 |
cscf_gen_impl |
| 199 |
} |
| 200 |
}); |
| 201 |
unsafe { q(x) } |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
#[cfg(test)] |
| 206 |
mod tests { |
| 207 |
use super::*; |
| 208 |
|
| 209 |
#[test] |
| 210 |
fn f_cscf_test() { |
| 211 |
assert_eq!(f_cscf(0.04915107), 20.353632); |
| 212 |
assert_eq!(f_cscf(0.5), 2.0858297); |
| 213 |
assert_eq!(f_cscf(0.07), 14.297387); |
| 214 |
assert_eq!(f_cscf(3.6171106e-5), 27646.375); |
| 215 |
assert_eq!(f_cscf(-5.535772e-10), -1806432800.0); |
| 216 |
assert_eq!(f_cscf(0.0), f32::INFINITY); |
| 217 |
assert_eq!(f_cscf(-0.0), f32::NEG_INFINITY); |
| 218 |
assert_eq!(f_cscf(-1.0854926e-19), -9.2124077e18); |
| 219 |
} |
| 220 |
} |