| 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_polyeval4; |
| 31 |
use crate::sin_cosf::sincosf_eval::sincosf_eval; |
| 32 |
|
| 33 |
/// Computes cos(x) - 1 |
| 34 |
/// |
| 35 |
/// ULP 0.5 |
| 36 |
pub fn f_cosm1f(x: f32) -> f32 { |
| 37 |
let x_abs = x.to_bits() & 0x7fff_ffffu32; |
| 38 |
let x = f32::from_bits(x_abs); |
| 39 |
|
| 40 |
// |x| <= pi/16 |
| 41 |
if x_abs <= 0x3e49_0fdbu32 { |
| 42 |
let xd = x as f64; |
| 43 |
// |x| < 0.000244141 |
| 44 |
if x_abs < 0x3980_0000u32 { |
| 45 |
// Taylor expansion for small cos(x) - 1 ~ -x^2/2 + x^4/24 + O(x^6) |
| 46 |
let x_sqr = xd * xd; |
| 47 |
const R: f64 = 1. / 2.; |
| 48 |
return (-x_sqr * R) as f32; |
| 49 |
} |
| 50 |
|
| 51 |
// Cosine |
| 52 |
// Generated poly by Sollya: |
| 53 |
// Polynomial cos(x) - 1 = x^2 * P(x^2) |
| 54 |
// |
| 55 |
// d = [0.0000000000001, pi/16]; |
| 56 |
// f_cosm1 = cos(x) - 1; |
| 57 |
// Q = fpminimax(f_cosm1, [|2,4,6,8|], [|0, D...|], d); |
| 58 |
|
| 59 |
let x2 = xd * xd; |
| 60 |
let p = f_polyeval4( |
| 61 |
x2, |
| 62 |
f64::from_bits(0xbfe0000000000000), |
| 63 |
f64::from_bits(0x3fa55555554ed21a), |
| 64 |
f64::from_bits(0xbf56c16b981a61d0), |
| 65 |
f64::from_bits(0x3ef9fc205e761f45), |
| 66 |
); |
| 67 |
return (p * x2) as f32; |
| 68 |
} |
| 69 |
|
| 70 |
if x_abs >= 0x7f80_0000u32 { |
| 71 |
return x + f32::NAN; |
| 72 |
} |
| 73 |
|
| 74 |
// cos(x) - 1 = -2*sin^2(x/2) |
| 75 |
// Hence we're computing sin using formula: |
| 76 |
// sin(x) = sin((k + y)*pi/32) |
| 77 |
// = sin(y*pi/32) * cos(k*pi/32) + cos(y*pi/32) * sin(k*pi/32) |
| 78 |
// The values of sin(k*pi/32) and cos(k*pi/32) for k = 0..31 are precomputed |
| 79 |
// and stored using a vector of 32 doubles. Sin(y*pi/32) and cos(y*pi/32) are |
| 80 |
// computed using degree-7 and degree-6 minimax polynomials generated by |
| 81 |
// Sollya respectively. |
| 82 |
|
| 83 |
let x_abs = (x * 0.5).to_bits(); |
| 84 |
let xd = f32::from_bits(x_abs) as f64; |
| 85 |
|
| 86 |
let rs = sincosf_eval(xd, x_abs); |
| 87 |
let sin_x_over_2 = f_fmla(rs.sin_y, rs.cos_k, f_fmla(rs.cosm1_y, rs.sin_k, rs.sin_k)); |
| 88 |
let sin_sqr = sin_x_over_2 * sin_x_over_2; |
| 89 |
let cosm1x = -2. * sin_sqr; |
| 90 |
cosm1x as f32 |
| 91 |
} |
| 92 |
|
| 93 |
#[cfg(test)] |
| 94 |
mod tests { |
| 95 |
use super::*; |
| 96 |
|
| 97 |
#[test] |
| 98 |
fn f_cosm1f_test() { |
| 99 |
assert_eq!(f_cosm1f(0.00015928394), -1.2685687e-8); |
| 100 |
assert_eq!(f_cosm1f(0.0), 0.0); |
| 101 |
assert_eq!(f_cosm1f(std::f32::consts::PI), -2.); |
| 102 |
assert_eq!(f_cosm1f(0.5), -0.122417435); |
| 103 |
assert_eq!(f_cosm1f(0.7), -0.2351578); |
| 104 |
assert_eq!(f_cosm1f(1.7), -1.1288445); |
| 105 |
assert!(f_cosm1f(f32::INFINITY).is_nan()); |
| 106 |
assert!(f_cosm1f(f32::NEG_INFINITY).is_nan()); |
| 107 |
assert!(f_cosm1f(f32::NAN).is_nan()); |
| 108 |
assert_eq!(f_cosm1f(0.0002480338), -3.076038e-8); |
| 109 |
} |
| 110 |
} |