Skip to content

Commit bb95301

Browse files
committed
rebase
2 parents 95b0a91 + 97fe0d8 commit bb95301

File tree

17 files changed

+840
-4
lines changed

17 files changed

+840
-4
lines changed

src/lib.rs

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

92-
#[cfg(todo)]
9392
fn cos(self) -> Self;
9493

9594
#[cfg(todo)]
@@ -259,7 +258,6 @@ impl F32Ext for f32 {
259258
sinf(self)
260259
}
261260

262-
#[cfg(todo)]
263261
#[inline]
264262
fn cos(self) -> Self {
265263
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/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, [email protected].
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];

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/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 */

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 */

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 */

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 */

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, [email protected].
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]). */

0 commit comments

Comments
 (0)