Skip to content

Commit 4b6e9c8

Browse files
authored
Merge branch 'master' into separate-import
2 parents 82b2a42 + 17583ae commit 4b6e9c8

17 files changed

+810
-5
lines changed

libm/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)

libm/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+
}

libm/src/math/expf.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/* origin: FreeBSD /usr/src/lib/msun/src/e_expf.c */
2+
/*
3+
* Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
4+
*/
5+
/*
6+
* ====================================================
7+
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
8+
*
9+
* Developed at SunPro, a Sun Microsystems, Inc. business.
10+
* Permission to use, copy, modify, and distribute this
11+
* software is freely granted, provided that this notice
12+
* is preserved.
13+
* ====================================================
14+
*/
15+
116
use super::scalbnf;
217

318
const HALF: [f32; 2] = [0.5, -0.5];

libm/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+
}

libm/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+
}

libm/src/math/log10.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
/* origin: FreeBSD /usr/src/lib/msun/src/e_log10.c */
2+
/*
3+
* ====================================================
4+
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5+
*
6+
* Developed at SunSoft, a Sun Microsystems, Inc. business.
7+
* Permission to use, copy, modify, and distribute this
8+
* software is freely granted, provided that this notice
9+
* is preserved.
10+
* ====================================================
11+
*/
12+
/*
13+
* Return the base 10 logarithm of x. See log.c for most comments.
14+
*
15+
* Reduce x to 2^k (1+f) and calculate r = log(1+f) - f + f*f/2
16+
* as in log.c, then combine and scale in extra precision:
17+
* log10(x) = (f - f*f/2 + r)/log(10) + k*log10(2)
18+
*/
19+
120
use core::f64;
221

322
const IVLN10HI: f64 = 4.34294481878168880939e-01; /* 0x3fdbcb7b, 0x15200000 */

libm/src/math/log10f.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/* origin: FreeBSD /usr/src/lib/msun/src/e_log10f.c */
2+
/*
3+
* ====================================================
4+
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5+
*
6+
* Developed at SunPro, a Sun Microsystems, Inc. business.
7+
* Permission to use, copy, modify, and distribute this
8+
* software is freely granted, provided that this notice
9+
* is preserved.
10+
* ====================================================
11+
*/
12+
/*
13+
* See comments in log10.c.
14+
*/
15+
116
use core::f32;
217

318
const IVLN10HI: f32 = 4.3432617188e-01; /* 0x3ede6000 */

libm/src/math/log2.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
/* origin: FreeBSD /usr/src/lib/msun/src/e_log2.c */
2+
/*
3+
* ====================================================
4+
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5+
*
6+
* Developed at SunSoft, a Sun Microsystems, Inc. business.
7+
* Permission to use, copy, modify, and distribute this
8+
* software is freely granted, provided that this notice
9+
* is preserved.
10+
* ====================================================
11+
*/
12+
/*
13+
* Return the base 2 logarithm of x. See log.c for most comments.
14+
*
15+
* Reduce x to 2^k (1+f) and calculate r = log(1+f) - f + f*f/2
16+
* as in log.c, then combine and scale in extra precision:
17+
* log2(x) = (f - f*f/2 + r)/log(2) + k
18+
*/
19+
120
use core::f64;
221

322
const IVLN2HI: f64 = 1.44269504072144627571e+00; /* 0x3ff71547, 0x65200000 */

libm/src/math/log2f.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/* origin: FreeBSD /usr/src/lib/msun/src/e_log2f.c */
2+
/*
3+
* ====================================================
4+
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5+
*
6+
* Developed at SunPro, a Sun Microsystems, Inc. business.
7+
* Permission to use, copy, modify, and distribute this
8+
* software is freely granted, provided that this notice
9+
* is preserved.
10+
* ====================================================
11+
*/
12+
/*
13+
* See comments in log2.c.
14+
*/
15+
116
use core::f32;
217

318
const IVLN2HI: f32 = 1.4428710938e+00; /* 0x3fb8b000 */

libm/src/math/logf.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/* origin: FreeBSD /usr/src/lib/msun/src/e_logf.c */
2+
/*
3+
* Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
4+
*/
5+
/*
6+
* ====================================================
7+
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
8+
*
9+
* Developed at SunPro, a Sun Microsystems, Inc. business.
10+
* Permission to use, copy, modify, and distribute this
11+
* software is freely granted, provided that this notice
12+
* is preserved.
13+
* ====================================================
14+
*/
15+
116
const LN2_HI: f32 = 6.9313812256e-01; /* 0x3f317180 */
217
const LN2_LO: f32 = 9.0580006145e-06; /* 0x3717f7d1 */
318
/* |(log(1+s)-log(1-s))/s - Lg(s)| < 2**-34.24 (~[-4.95e-11, 4.97e-11]). */

libm/src/math/mod.rs

Lines changed: 9 additions & 2 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,10 +34,9 @@ mod sqrtf;
3334
mod trunc;
3435
mod truncf;
3536

36-
//mod service;
37-
3837
// Use separated imports instead of {}-grouped imports for easier merging.
3938
pub use self::ceilf::ceilf;
39+
pub use self::cosf::cosf;
4040
pub use self::expf::expf;
4141
pub use self::fabs::fabs;
4242
pub use self::fabsf::fabsf;
@@ -63,6 +63,13 @@ pub use self::sqrtf::sqrtf;
6363
pub use self::trunc::trunc;
6464
pub use self::truncf::truncf;
6565

66+
mod k_cosf;
67+
mod k_sinf;
68+
mod rem_pio2_large;
69+
mod rem_pio2f;
70+
71+
use self::{k_cosf::k_cosf, k_sinf::k_sinf, rem_pio2_large::rem_pio2_large, rem_pio2f::rem_pio2f};
72+
6673
fn isnanf(x: f32) -> bool {
6774
x.to_bits() & 0x7fffffff > 0x7f800000
6875
}

libm/src/math/powf.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/* origin: FreeBSD /usr/src/lib/msun/src/e_powf.c */
2+
/*
3+
* Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
4+
*/
5+
/*
6+
* ====================================================
7+
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
8+
*
9+
* Developed at SunPro, a Sun Microsystems, Inc. business.
10+
* Permission to use, copy, modify, and distribute this
11+
* software is freely granted, provided that this notice
12+
* is preserved.
13+
* ====================================================
14+
*/
15+
116
use super::{fabsf, scalbnf, sqrtf};
217

318
const BP: [f32; 2] = [1.0, 1.5];

0 commit comments

Comments
 (0)