Skip to content

Commit 46d8a41

Browse files
bors[bot]burrbulljaparic
committed
64: add exp and log/ln functions for f32 r=japaric a=burrbull closes rust-lang#17 closes rust-lang#29 closes rust-lang#55 Co-authored-by: Andrey Zgarbul <[email protected]> Co-authored-by: Jorge Aparicio <[email protected]>
2 parents 6b0860a + be1d7b9 commit 46d8a41

File tree

8 files changed

+220
-34
lines changed

8 files changed

+220
-34
lines changed

src/lib.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,13 @@ pub trait F32Ext {
7171

7272
fn sqrt(self) -> Self;
7373

74-
#[cfg(todo)]
7574
fn exp(self) -> Self;
7675

7776
#[cfg(todo)]
7877
fn exp2(self) -> Self;
7978

80-
#[cfg(todo)]
8179
fn ln(self) -> Self;
8280

83-
#[cfg(todo)]
8481
fn log(self, base: Self) -> Self;
8582

8683
#[cfg(todo)]
@@ -220,7 +217,6 @@ impl F32Ext for f32 {
220217
sqrtf(self)
221218
}
222219

223-
#[cfg(todo)]
224220
#[inline]
225221
fn exp(self) -> Self {
226222
expf(self)
@@ -232,13 +228,11 @@ impl F32Ext for f32 {
232228
exp2f(self)
233229
}
234230

235-
#[cfg(todo)]
236231
#[inline]
237232
fn ln(self) -> Self {
238233
logf(self)
239234
}
240235

241-
#[cfg(todo)]
242236
#[inline]
243237
fn log(self, base: Self) -> Self {
244238
self.ln() / base.ln()
@@ -371,7 +365,6 @@ impl F32Ext for f32 {
371365
/// NOTE this meant to be a closed extension trait. The only stable way to use this trait is to
372366
/// import it to access its methods.
373367
pub trait F64Ext {
374-
#[cfg(todo)]
375368
fn floor(self) -> Self;
376369

377370
#[cfg(todo)]
@@ -485,7 +478,6 @@ pub trait F64Ext {
485478
}
486479

487480
impl F64Ext for f64 {
488-
#[cfg(todo)]
489481
#[inline]
490482
fn floor(self) -> Self {
491483
floor(self)

src/math/expf.rs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
use super::scalbnf;
2+
3+
const HALF : [f32; 2] = [0.5,-0.5];
4+
const LN2_HI : f32 = 6.9314575195e-01; /* 0x3f317200 */
5+
const LN2_LO : f32 = 1.4286067653e-06; /* 0x35bfbe8e */
6+
const INV_LN2 : f32 = 1.4426950216e+00; /* 0x3fb8aa3b */
7+
/*
8+
* Domain [-0.34568, 0.34568], range ~[-4.278e-9, 4.447e-9]:
9+
* |x*(exp(x)+1)/(exp(x)-1) - p(x)| < 2**-27.74
10+
*/
11+
const P1 : f32 = 1.6666625440e-1; /* 0xaaaa8f.0p-26 */
12+
const P2 : f32 = -2.7667332906e-3; /* -0xb55215.0p-32 */
13+
14+
#[inline]
15+
pub fn expf(mut x: f32) -> f32 {
16+
let x1p127 = f32::from_bits(0x7f000000); // 0x1p127f === 2 ^ 127
17+
let x1p_126 = f32::from_bits(0x800000); // 0x1p-126f === 2 ^ -126 /*original 0x1p-149f ??????????? */
18+
19+
let mut hx = x.to_bits();
20+
let sign = (hx >> 31) as i32; /* sign bit of x */
21+
let signb : bool = sign != 0;
22+
hx &= 0x7fffffff; /* high word of |x| */
23+
24+
/* special cases */
25+
if hx >= 0x42aeac50 { /* if |x| >= -87.33655f or NaN */
26+
if hx > 0x7f800000 {/* NaN */
27+
return x;
28+
}
29+
if (hx >= 0x42b17218) && (!signb) { /* x >= 88.722839f */
30+
/* overflow */
31+
x *= x1p127;
32+
return x;
33+
}
34+
if signb {
35+
/* underflow */
36+
force_eval!(-x1p_126/x);
37+
if hx >= 0x42cff1b5 { /* x <= -103.972084f */
38+
return 0.
39+
}
40+
}
41+
}
42+
43+
/* argument reduction */
44+
let k : i32;
45+
let hi : f32;
46+
let lo : f32;
47+
if hx > 0x3eb17218 { /* if |x| > 0.5 ln2 */
48+
if hx > 0x3f851592 { /* if |x| > 1.5 ln2 */
49+
k = (INV_LN2*x + HALF[sign as usize]) as i32;
50+
} else {
51+
k = 1 - sign - sign;
52+
}
53+
let kf = k as f32;
54+
hi = x - kf*LN2_HI; /* k*ln2hi is exact here */
55+
lo = kf*LN2_LO;
56+
x = hi - lo;
57+
} else if hx > 0x39000000 { /* |x| > 2**-14 */
58+
k = 0;
59+
hi = x;
60+
lo = 0.;
61+
} else {
62+
/* raise inexact */
63+
force_eval!(x1p127 + x);
64+
return 1. + x;
65+
}
66+
67+
/* x is now in primary range */
68+
let xx = x*x;
69+
let c = x - xx*(P1+xx*P2);
70+
let y = 1. + (x*c/(2.-c) - lo + hi);
71+
if k == 0 {
72+
y
73+
} else {
74+
scalbnf(y, k)
75+
}
76+
}

src/math/floor.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use core::f64;
2+
3+
const TOINT : f64 = 1. / f64::EPSILON;
4+
5+
#[inline]
6+
pub fn floor(x : f64) -> f64 {
7+
let ui = x.to_bits();
8+
let e = ((ui >> 52) & 0x7ff) as i32;
9+
10+
if (e >= 0x3ff+52) || (x == 0.) {
11+
return x;
12+
}
13+
/* y = int(x) - x, where int(x) is an integer neighbor of x */
14+
let y = if (ui >> 63) != 0 {
15+
x - TOINT + TOINT - x
16+
} else {
17+
x + TOINT - TOINT - x
18+
};
19+
/* special case because of non-nearest rounding modes */
20+
if e <= 0x3ff-1 {
21+
force_eval!(y);
22+
return if (ui >> 63) != 0 { -1. } else { 0. };
23+
}
24+
if y > 0. {
25+
x + y - 1.
26+
} else {
27+
x + y
28+
}
29+
}

src/math/logf.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
const LN2_HI : f32 = 6.9313812256e-01; /* 0x3f317180 */
2+
const LN2_LO : f32 = 9.0580006145e-06; /* 0x3717f7d1 */
3+
/* |(log(1+s)-log(1-s))/s - Lg(s)| < 2**-34.24 (~[-4.95e-11, 4.97e-11]). */
4+
const LG1 : f32 = 0.66666662693; /* 0xaaaaaa.0p-24*/
5+
const LG2 : f32 = 0.40000972152; /* 0xccce13.0p-25 */
6+
const LG3 : f32 = 0.28498786688; /* 0x91e9ee.0p-25 */
7+
const LG4 : f32 = 0.24279078841; /* 0xf89e26.0p-26 */
8+
9+
#[inline]
10+
pub fn logf(mut x: f32) -> f32 {
11+
let x1p25 = f32::from_bits(0x4c000000); // 0x1p25f === 2 ^ 25
12+
13+
let mut ix = x.to_bits();
14+
let mut k = 0i32;
15+
16+
if (ix < 0x00800000) || ((ix>>31) != 0) { /* x < 2**-126 */
17+
if ix<<1 == 0 {
18+
return -1./(x*x); /* log(+-0)=-inf */
19+
}
20+
if (ix>>31) != 0 {
21+
return (x-x)/0.; /* log(-#) = NaN */
22+
}
23+
/* subnormal number, scale up x */
24+
k -= 25;
25+
x *= x1p25;
26+
ix = x.to_bits();
27+
} else if ix >= 0x7f800000 {
28+
return x;
29+
} else if ix == 0x3f800000 {
30+
return 0.;
31+
}
32+
33+
/* reduce x into [sqrt(2)/2, sqrt(2)] */
34+
ix += 0x3f800000 - 0x3f3504f3;
35+
k += ((ix>>23) as i32) - 0x7f;
36+
ix = (ix & 0x007fffff) + 0x3f3504f3;
37+
x = f32::from_bits(ix);
38+
39+
let f = x - 1.;
40+
let s = f/(2. + f);
41+
let z = s*s;
42+
let w = z*z;
43+
let t1 = w*(LG2+w*LG4);
44+
let t2 = z*(LG1+w*LG3);
45+
let r = t2 + t1;
46+
let hfsq = 0.5*f*f;
47+
let dk = k as f32;
48+
s*(hfsq+r) + dk*LN2_LO - hfsq + f + dk*LN2_HI
49+
}

src/math/mod.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,28 @@ mod fabsf;
99
mod fmodf;
1010
mod powf;
1111
mod round;
12+
mod scalbn;
1213
mod scalbnf;
1314
mod sqrtf;
15+
mod logf;
16+
mod expf;
17+
mod floor;
1418

15-
pub use self::fabs::fabs;
16-
pub use self::fabsf::fabsf;
17-
pub use self::fmodf::fmodf;
18-
pub use self::powf::powf;
19-
pub use self::round::round;
20-
pub use self::scalbnf::scalbnf;
21-
pub use self::sqrtf::sqrtf;
19+
//mod service;
20+
21+
pub use self::{
22+
fabs::fabs,
23+
fabsf::fabsf,
24+
fmodf::fmodf,
25+
powf::powf,
26+
round::round,
27+
scalbn::scalbn,
28+
scalbnf::scalbnf,
29+
sqrtf::sqrtf,
30+
logf::logf,
31+
expf::expf,
32+
floor::floor,
33+
};
2234

2335
fn isnanf(x: f32) -> bool {
2436
x.to_bits() & 0x7fffffff > 0x7f800000

src/math/scalbn.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#[inline]
2+
pub fn scalbn(x : f64, mut n: i32) -> f64 {
3+
let x1p1023 = f64::from_bits(0x7fe0000000000000); // 0x1p1023 === 2 ^ 1023
4+
let x1p53 = f64::from_bits(0x4340000000000000); // 0x1p53 === 2 ^ 53
5+
let x1p_1022 = f64::from_bits(0x0010000000000000); // 0x1p-1022 === 2 ^ (-1022)
6+
7+
let mut y = x;
8+
9+
if n > 1023 {
10+
y *= x1p1023;
11+
n -= 1023;
12+
if n > 1023 {
13+
y *= x1p1023;
14+
n -= 1023;
15+
if n > 1023 {
16+
n = 1023;
17+
}
18+
}
19+
} else if n < -1022 {
20+
/* make sure final n < -53 to avoid double
21+
rounding in the subnormal range */
22+
y *= x1p_1022 * x1p53;
23+
n += 1022 - 53;
24+
if n < -1022 {
25+
y *= x1p_1022 * x1p53;
26+
n += 1022 - 53;
27+
if n < -1022 {
28+
n = -1022;
29+
}
30+
}
31+
}
32+
y*f64::from_bits(((0x3ff+n) as u64)<<52)
33+
}

src/math/scalbnf.rs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,29 @@
11
#[inline]
2-
pub fn scalbnf(mut x: f32, mut n: i32) -> f32 {
3-
let x1p127 = f32::from_bits(0x7f000000); // 0x1p127f === 2 ^ 127
4-
let x1p_126 = f32::from_bits(0x800000); // 0x1p-126f === 2 ^ -126
5-
let x1p24 = f32::from_bits(0x4b800000); // 0x1p24f === 2 ^ 24
6-
7-
let mut y: f32 = x;
8-
2+
pub fn scalbnf(mut x: f32, mut n : i32) -> f32 {
3+
let x1p127 = f32::from_bits(0x7f000000); // 0x1p127f === 2 ^ 127
4+
let x1p_126 = f32::from_bits(0x800000); // 0x1p-126f === 2 ^ -126
5+
let x1p24 = f32::from_bits(0x4b800000); // 0x1p24f === 2 ^ 24
6+
97
if n > 127 {
10-
y *= x1p127;
8+
x *= x1p127;
119
n -= 127;
1210
if n > 127 {
13-
y *= x1p127;
11+
x *= x1p127;
1412
n -= 127;
1513
if n > 127 {
1614
n = 127;
1715
}
1816
}
1917
} else if n < -126 {
20-
y *= x1p_126;
21-
y *= x1p24;
18+
x *= x1p_126 * x1p24;
2219
n += 126 - 24;
2320
if n < -126 {
24-
y *= x1p_126;
25-
y *= x1p24;
21+
x *= x1p_126 * x1p24;
2622
n += 126 - 24;
2723
if n < -126 {
2824
n = -126;
2925
}
3026
}
3127
}
32-
33-
x = y * f32::from_bits((0x7f + n as u32) << 23);
34-
x
28+
x * f32::from_bits(((0x7f+n) as u32)<<23)
3529
}

test-generator/src/main.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -570,10 +570,11 @@ f32_f32! {
570570
// cosf,
571571
// coshf,
572572
// exp2f,
573-
// expf,
573+
expf,
574574
// fdimf,
575575
// log10f,
576576
// log2f,
577+
logf,
577578
// roundf,
578579
// sinf,
579580
// sinhf,
@@ -613,7 +614,7 @@ f64_f64! {
613614
// exp,
614615
// exp2,
615616
// expm1,
616-
// floor,
617+
floor,
617618
// log,
618619
// log10,
619620
// log1p,
@@ -644,5 +645,5 @@ f64f64f64_f64! {
644645

645646
// With signature `fn(f64, i32) -> f64`
646647
f64i32_f64! {
647-
// scalbn,
648+
scalbn,
648649
}

0 commit comments

Comments
 (0)