1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
/* origin: FreeBSD /usr/src/lib/msun/src/s_sinf.c */
/*
* Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
* Optimized by Bruce D. Evans.
*/
/*
* ====================================================
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
*
* Developed at SunPro, a Sun Microsystems, Inc. business.
* Permission to use, copy, modify, and distribute this
* software is freely granted, provided that this notice
* is preserved.
* ====================================================
*/
use super::{k_cosf, k_sinf, rem_pio2f};
/* Small multiples of pi/2 rounded to double precision. */
const PI_2: f32 = 0.5 * 3.1415926535897931160E+00;
const S1PIO2: f32 = 1.0 * PI_2; /* 0x3FF921FB, 0x54442D18 */
const S2PIO2: f32 = 2.0 * PI_2; /* 0x400921FB, 0x54442D18 */
const S3PIO2: f32 = 3.0 * PI_2; /* 0x4012D97C, 0x7F3321D2 */
const S4PIO2: f32 = 4.0 * PI_2; /* 0x401921FB, 0x54442D18 */
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
pub fn sincosf(x: f32) -> (f32, f32) {
let s: f32;
let c: f32;
let mut ix: u32;
let sign: bool;
ix = x.to_bits();
sign = (ix >> 31) != 0;
ix &= 0x7fffffff;
/* |x| ~<= pi/4 */
if ix <= 0x3f490fda {
/* |x| < 2**-12 */
if ix < 0x39800000 {
/* raise inexact if x!=0 and underflow if subnormal */
let x1p120 = f32::from_bits(0x7b800000); // 0x1p120 == 2^120
if ix < 0x00100000 {
force_eval!(x / x1p120);
} else {
force_eval!(x + x1p120);
}
return (x, 1.0);
}
return (k_sinf(x as f64), k_cosf(x as f64));
}
/* |x| ~<= 5*pi/4 */
if ix <= 0x407b53d1 {
if ix <= 0x4016cbe3 {
/* |x| ~<= 3pi/4 */
if sign {
s = -k_cosf((x + S1PIO2) as f64);
c = k_sinf((x + S1PIO2) as f64);
} else {
s = k_cosf((S1PIO2 - x) as f64);
c = k_sinf((S1PIO2 - x) as f64);
}
}
/* -sin(x+c) is not correct if x+c could be 0: -0 vs +0 */
else {
if sign {
s = -k_sinf((x + S2PIO2) as f64);
c = -k_cosf((x + S2PIO2) as f64);
} else {
s = -k_sinf((x - S2PIO2) as f64);
c = -k_cosf((x - S2PIO2) as f64);
}
}
return (s, c);
}
/* |x| ~<= 9*pi/4 */
if ix <= 0x40e231d5 {
if ix <= 0x40afeddf {
/* |x| ~<= 7*pi/4 */
if sign {
s = k_cosf((x + S3PIO2) as f64);
c = -k_sinf((x + S3PIO2) as f64);
} else {
s = -k_cosf((x - S3PIO2) as f64);
c = k_sinf((x - S3PIO2) as f64);
}
} else {
if sign {
s = k_sinf((x + S4PIO2) as f64);
c = k_cosf((x + S4PIO2) as f64);
} else {
s = k_sinf((x - S4PIO2) as f64);
c = k_cosf((x - S4PIO2) as f64);
}
}
return (s, c);
}
/* sin(Inf or NaN) is NaN */
if ix >= 0x7f800000 {
let rv = x - x;
return (rv, rv);
}
/* general argument reduction needed */
let (n, y) = rem_pio2f(x);
s = k_sinf(y);
c = k_cosf(y);
match n & 3 {
0 => (s, c),
1 => (c, -s),
2 => (-s, -c),
3 => (-c, s),
#[cfg(debug_assertions)]
_ => unreachable!(),
#[cfg(not(debug_assertions))]
_ => (0.0, 1.0),
}
}
// PowerPC tests are failing on LLVM 13: https://github.com/rust-lang/rust/issues/88520
#[cfg(not(target_arch = "powerpc64"))]
#[cfg(test)]
mod tests {
use super::sincosf;
use crate::_eqf;
#[test]
fn with_pi() {
let (s, c) = sincosf(core::f32::consts::PI);
_eqf(s.abs(), 0.0).unwrap();
_eqf(c, -1.0).unwrap();
}
#[test]
fn rotational_symmetry() {
use core::f32::consts::PI;
const N: usize = 24;
for n in 0..N {
let theta = 2. * PI * (n as f32) / (N as f32);
let (s, c) = sincosf(theta);
let (s_plus, c_plus) = sincosf(theta + 2. * PI);
let (s_minus, c_minus) = sincosf(theta - 2. * PI);
const TOLERANCE: f32 = 1e-6;
assert!(
(s - s_plus).abs() < TOLERANCE,
"|{} - {}| = {} >= {}",
s,
s_plus,
(s - s_plus).abs(),
TOLERANCE
);
assert!(
(s - s_minus).abs() < TOLERANCE,
"|{} - {}| = {} >= {}",
s,
s_minus,
(s - s_minus).abs(),
TOLERANCE
);
assert!(
(c - c_plus).abs() < TOLERANCE,
"|{} - {}| = {} >= {}",
c,
c_plus,
(c - c_plus).abs(),
TOLERANCE
);
assert!(
(c - c_minus).abs() < TOLERANCE,
"|{} - {}| = {} >= {}",
c,
c_minus,
(c - c_minus).abs(),
TOLERANCE
);
}
}
}