Skip to content

Commit 7b9a6c0

Browse files
bors[bot]burrbull
andcommitted
67: cosf with corrections from review r=japaric a=burrbull Co-authored-by: Andrey Zgarbul <[email protected]> Co-authored-by: Zgarbul Andrey <[email protected]>
2 parents f416baf + 01d7102 commit 7b9a6c0

File tree

8 files changed

+608
-9
lines changed

8 files changed

+608
-9
lines changed

src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ pub trait F32Ext: private::Sealed {
8787
#[cfg(todo)]
8888
fn sin(self) -> Self;
8989

90-
#[cfg(todo)]
9190
fn cos(self) -> Self;
9291

9392
#[cfg(todo)]
@@ -252,7 +251,6 @@ impl F32Ext for f32 {
252251
sinf(self)
253252
}
254253

255-
#[cfg(todo)]
256254
#[inline]
257255
fn cos(self) -> Self {
258256
cosf(self)

src/math/cosf.rs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
use super::{k_cosf, k_sinf, rem_pio2f};
2+
3+
use core::f64::consts::FRAC_PI_2;
4+
5+
/* Small multiples of pi/2 rounded to double precision. */
6+
const C1_PIO2: f64 = 1. * FRAC_PI_2; /* 0x3FF921FB, 0x54442D18 */
7+
const C2_PIO2: f64 = 2. * FRAC_PI_2; /* 0x400921FB, 0x54442D18 */
8+
const C3_PIO2: f64 = 3. * FRAC_PI_2; /* 0x4012D97C, 0x7F3321D2 */
9+
const C4_PIO2: f64 = 4. * FRAC_PI_2; /* 0x401921FB, 0x54442D18 */
10+
11+
#[inline]
12+
pub fn cosf(x: f32) -> f32 {
13+
let x64 = x as f64;
14+
15+
let x1p120 = f32::from_bits(0x7b800000); // 0x1p120f === 2 ^ 120
16+
17+
let mut ix = x.to_bits();
18+
let sign = (ix >> 31) != 0;
19+
ix &= 0x7fffffff;
20+
21+
if ix <= 0x3f490fda {
22+
/* |x| ~<= pi/4 */
23+
if ix < 0x39800000 {
24+
/* |x| < 2**-12 */
25+
/* raise inexact if x != 0 */
26+
force_eval!(x + x1p120);
27+
return 1.;
28+
}
29+
return k_cosf(x64);
30+
}
31+
if ix <= 0x407b53d1 {
32+
/* |x| ~<= 5*pi/4 */
33+
if ix > 0x4016cbe3 {
34+
/* |x| ~> 3*pi/4 */
35+
return -k_cosf(if sign { x64 + C2_PIO2 } else { x64 - C2_PIO2 });
36+
} else {
37+
if sign {
38+
return k_sinf(x64 + C1_PIO2);
39+
} else {
40+
return k_sinf(C1_PIO2 - x64);
41+
}
42+
}
43+
}
44+
if ix <= 0x40e231d5 {
45+
/* |x| ~<= 9*pi/4 */
46+
if ix > 0x40afeddf {
47+
/* |x| ~> 7*pi/4 */
48+
return k_cosf(if sign { x64 + C4_PIO2 } else { x64 - C4_PIO2 });
49+
} else {
50+
if sign {
51+
return k_sinf(-x64 - C3_PIO2);
52+
} else {
53+
return k_sinf(x64 - C3_PIO2);
54+
}
55+
}
56+
}
57+
58+
/* cos(Inf or NaN) is NaN */
59+
if ix >= 0x7f800000 {
60+
return x - x;
61+
}
62+
63+
/* general argument reduction needed */
64+
let (n, y) = rem_pio2f(x);
65+
match n & 3 {
66+
0 => k_cosf(y),
67+
1 => k_sinf(-y),
68+
2 => -k_cosf(y),
69+
_ => k_sinf(y),
70+
}
71+
}

src/math/k_cosf.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/* |cos(x) - c(x)| < 2**-34.1 (~[-5.37e-11, 5.295e-11]). */
2+
const C0: f64 = -0.499999997251031003120; /* -0x1ffffffd0c5e81.0p-54 */
3+
const C1: f64 = 0.0416666233237390631894; /* 0x155553e1053a42.0p-57 */
4+
const C2: f64 = -0.00138867637746099294692; /* -0x16c087e80f1e27.0p-62 */
5+
const C3: f64 = 0.0000243904487962774090654; /* 0x199342e0ee5069.0p-68 */
6+
7+
#[inline]
8+
pub(crate) fn k_cosf(x: f64) -> f32 {
9+
let z = x * x;
10+
let w = z * z;
11+
let r = C2 + z * C3;
12+
(((1.0 + z * C0) + w * C1) + (w * z) * r) as f32
13+
}

src/math/k_sinf.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/* |sin(x)/x - s(x)| < 2**-37.5 (~[-4.89e-12, 4.824e-12]). */
2+
const S1: f64 = -0.166666666416265235595; /* -0x15555554cbac77.0p-55 */
3+
const S2: f64 = 0.0083333293858894631756; /* 0x111110896efbb2.0p-59 */
4+
const S3: f64 = -0.000198393348360966317347; /* -0x1a00f9e2cae774.0p-65 */
5+
const S4: f64 = 0.0000027183114939898219064; /* 0x16cd878c3b46a7.0p-71 */
6+
7+
#[inline]
8+
pub(crate) fn k_sinf(x: f64) -> f32 {
9+
let z = x * x;
10+
let w = z * z;
11+
let r = S3 + z * S4;
12+
let s = z * x;
13+
((x + s * (S1 + z * S2)) + s * w * r) as f32
14+
}

src/math/mod.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ macro_rules! force_eval {
77
}
88

99
mod ceilf;
10+
mod cosf;
1011
mod expf;
1112
mod fabs;
1213
mod fabsf;
@@ -33,15 +34,21 @@ mod sqrtf;
3334
mod trunc;
3435
mod truncf;
3536

36-
//mod service;
37-
3837
pub use self::{
39-
ceilf::ceilf, expf::expf, fabs::fabs, fabsf::fabsf, floor::floor, floorf::floorf, fmodf::fmodf,
40-
hypot::hypot, hypotf::hypotf, log::log, log10::log10, log10f::log10f, log1p::log1p,
41-
log1pf::log1pf, log2::log2, log2f::log2f, logf::logf, powf::powf, round::round, roundf::roundf,
42-
scalbn::scalbn, scalbnf::scalbnf, sqrt::sqrt, sqrtf::sqrtf, trunc::trunc, truncf::truncf,
38+
ceilf::ceilf, cosf::cosf, expf::expf, fabs::fabs, fabsf::fabsf, floor::floor, floorf::floorf,
39+
fmodf::fmodf, hypot::hypot, hypotf::hypotf, log::log, log10::log10, log10f::log10f,
40+
log1p::log1p, log1pf::log1pf, log2::log2, log2f::log2f, logf::logf, powf::powf, round::round,
41+
roundf::roundf, scalbn::scalbn, scalbnf::scalbnf, sqrt::sqrt, sqrtf::sqrtf, trunc::trunc,
42+
truncf::truncf,
4343
};
4444

45+
mod k_cosf;
46+
mod k_sinf;
47+
mod rem_pio2_large;
48+
mod rem_pio2f;
49+
50+
use self::{k_cosf::k_cosf, k_sinf::k_sinf, rem_pio2_large::rem_pio2_large, rem_pio2f::rem_pio2f};
51+
4552
fn isnanf(x: f32) -> bool {
4653
x.to_bits() & 0x7fffffff > 0x7f800000
4754
}

0 commit comments

Comments
 (0)