Skip to content

Commit 5d17a76

Browse files
committed
add exp and log/ln functions for f32
1 parent 6b0860a commit 5d17a76

File tree

4 files changed

+129
-6
lines changed

4 files changed

+129
-6
lines changed

src/lib.rs

Lines changed: 0 additions & 6 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()

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() as i32;
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/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); // 0x1p24f === 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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ mod powf;
1111
mod round;
1212
mod scalbnf;
1313
mod sqrtf;
14+
mod logf;
15+
mod expf;
1416

1517
pub use self::fabs::fabs;
1618
pub use self::fabsf::fabsf;
@@ -19,6 +21,8 @@ pub use self::powf::powf;
1921
pub use self::round::round;
2022
pub use self::scalbnf::scalbnf;
2123
pub use self::sqrtf::sqrtf;
24+
pub use self::logf::logf;
25+
pub use self::expf::expf;
2226

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

0 commit comments

Comments
 (0)