Skip to content

Commit 15a5ee9

Browse files
81: implement log2 and log2f r=japaric a=erikdesjardins closes rust-lang#27, closes rust-lang#28 Co-authored-by: Erik <[email protected]>
2 parents 565b581 + 4671644 commit 15a5ee9

File tree

5 files changed

+164
-8
lines changed

5 files changed

+164
-8
lines changed

src/lib.rs

-4
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ pub trait F32Ext: private::Sealed {
7575

7676
fn log(self, base: Self) -> Self;
7777

78-
#[cfg(todo)]
7978
fn log2(self) -> Self;
8079

8180
#[cfg(todo)]
@@ -228,7 +227,6 @@ impl F32Ext for f32 {
228227
self.ln() / base.ln()
229228
}
230229

231-
#[cfg(todo)]
232230
#[inline]
233231
fn log2(self) -> Self {
234232
log2f(self)
@@ -399,7 +397,6 @@ pub trait F64Ext: private::Sealed {
399397
#[cfg(todo)]
400398
fn log(self, base: Self) -> Self;
401399

402-
#[cfg(todo)]
403400
fn log2(self) -> Self;
404401

405402
#[cfg(todo)]
@@ -557,7 +554,6 @@ impl F64Ext for f64 {
557554
self.ln() / base.ln()
558555
}
559556

560-
#[cfg(todo)]
561557
#[inline]
562558
fn log2(self) -> Self {
563559
log2(self)

src/math/log2.rs

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
use core::f64;
2+
3+
const IVLN2HI: f64 = 1.44269504072144627571e+00; /* 0x3ff71547, 0x65200000 */
4+
const IVLN2LO: f64 = 1.67517131648865118353e-10; /* 0x3de705fc, 0x2eefa200 */
5+
const LG1: f64 = 6.666666666666735130e-01; /* 3FE55555 55555593 */
6+
const LG2: f64 = 3.999999999940941908e-01; /* 3FD99999 9997FA04 */
7+
const LG3: f64 = 2.857142874366239149e-01; /* 3FD24924 94229359 */
8+
const LG4: f64 = 2.222219843214978396e-01; /* 3FCC71C5 1D8E78AF */
9+
const LG5: f64 = 1.818357216161805012e-01; /* 3FC74664 96CB03DE */
10+
const LG6: f64 = 1.531383769920937332e-01; /* 3FC39A09 D078C69F */
11+
const LG7: f64 = 1.479819860511658591e-01; /* 3FC2F112 DF3E5244 */
12+
13+
pub fn log2(mut x: f64) -> f64 {
14+
let x1p54 = f64::from_bits(0x4350000000000000); // 0x1p54 === 2 ^ 54
15+
16+
let mut ui: u64 = x.to_bits();
17+
let hfsq: f64;
18+
let f: f64;
19+
let s: f64;
20+
let z: f64;
21+
let r: f64;
22+
let mut w: f64;
23+
let t1: f64;
24+
let t2: f64;
25+
let y: f64;
26+
let mut hi: f64;
27+
let lo: f64;
28+
let mut val_hi: f64;
29+
let mut val_lo: f64;
30+
let mut hx: u32;
31+
let mut k: i32;
32+
33+
hx = (ui >> 32) as u32;
34+
k = 0;
35+
if hx < 0x00100000 || (hx >> 31) > 0 {
36+
if ui << 1 == 0 {
37+
return -1. / (x * x); /* log(+-0)=-inf */
38+
}
39+
if (hx >> 31) > 0 {
40+
return (x - x) / 0.0; /* log(-#) = NaN */
41+
}
42+
/* subnormal number, scale x up */
43+
k -= 54;
44+
x *= x1p54;
45+
ui = x.to_bits();
46+
hx = (ui >> 32) as u32;
47+
} else if hx >= 0x7ff00000 {
48+
return x;
49+
} else if hx == 0x3ff00000 && ui << 32 == 0 {
50+
return 0.;
51+
}
52+
53+
/* reduce x into [sqrt(2)/2, sqrt(2)] */
54+
hx += 0x3ff00000 - 0x3fe6a09e;
55+
k += (hx >> 20) as i32 - 0x3ff;
56+
hx = (hx & 0x000fffff) + 0x3fe6a09e;
57+
ui = (hx as u64) << 32 | (ui & 0xffffffff);
58+
x = f64::from_bits(ui);
59+
60+
f = x - 1.0;
61+
hfsq = 0.5 * f * f;
62+
s = f / (2.0 + f);
63+
z = s * s;
64+
w = z * z;
65+
t1 = w * (LG2 + w * (LG4 + w * LG6));
66+
t2 = z * (LG1 + w * (LG3 + w * (LG5 + w * LG7)));
67+
r = t2 + t1;
68+
69+
/* hi+lo = f - hfsq + s*(hfsq+R) ~ log(1+f) */
70+
hi = f - hfsq;
71+
ui = hi.to_bits();
72+
ui &= (-1i64 as u64) << 32;
73+
hi = f64::from_bits(ui);
74+
lo = f - hi - hfsq + s * (hfsq + r);
75+
76+
val_hi = hi * IVLN2HI;
77+
val_lo = (lo + hi) * IVLN2LO + lo * IVLN2HI;
78+
79+
/* spadd(val_hi, val_lo, y), except for not using double_t: */
80+
y = k.into();
81+
w = y + val_hi;
82+
val_lo += (y - w) + val_hi;
83+
val_hi = w;
84+
85+
return val_lo + val_hi;
86+
}

src/math/log2f.rs

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
use core::f32;
2+
3+
const IVLN2HI: f32 = 1.4428710938e+00; /* 0x3fb8b000 */
4+
const IVLN2LO: f32 = -1.7605285393e-04; /* 0xb9389ad4 */
5+
/* |(log(1+s)-log(1-s))/s - Lg(s)| < 2**-34.24 (~[-4.95e-11, 4.97e-11]). */
6+
const LG1: f32 = 0.66666662693; /* 0xaaaaaa.0p-24 */
7+
const LG2: f32 = 0.40000972152; /* 0xccce13.0p-25 */
8+
const LG3: f32 = 0.28498786688; /* 0x91e9ee.0p-25 */
9+
const LG4: f32 = 0.24279078841; /* 0xf89e26.0p-26 */
10+
11+
pub fn log2f(mut x: f32) -> f32 {
12+
let x1p25f = f32::from_bits(0x4c000000); // 0x1p25f === 2 ^ 25
13+
14+
let mut ui: u32 = x.to_bits();
15+
let hfsq: f32;
16+
let f: f32;
17+
let s: f32;
18+
let z: f32;
19+
let r: f32;
20+
let w: f32;
21+
let t1: f32;
22+
let t2: f32;
23+
let mut hi: f32;
24+
let lo: f32;
25+
let mut ix: u32;
26+
let mut k: i32;
27+
28+
ix = ui;
29+
k = 0;
30+
if ix < 0x00800000 || (ix >> 31) > 0 {
31+
/* x < 2**-126 */
32+
if ix << 1 == 0 {
33+
return -1. / (x * x); /* log(+-0)=-inf */
34+
}
35+
if (ix >> 31) > 0 {
36+
return (x - x) / 0.0; /* log(-#) = NaN */
37+
}
38+
/* subnormal number, scale up x */
39+
k -= 25;
40+
x *= x1p25f;
41+
ui = x.to_bits();
42+
ix = ui;
43+
} else if ix >= 0x7f800000 {
44+
return x;
45+
} else if ix == 0x3f800000 {
46+
return 0.;
47+
}
48+
49+
/* reduce x into [sqrt(2)/2, sqrt(2)] */
50+
ix += 0x3f800000 - 0x3f3504f3;
51+
k += (ix >> 23) as i32 - 0x7f;
52+
ix = (ix & 0x007fffff) + 0x3f3504f3;
53+
ui = ix;
54+
x = f32::from_bits(ui);
55+
56+
f = x - 1.0;
57+
s = f / (2.0 + f);
58+
z = s * s;
59+
w = z * z;
60+
t1 = w * (LG2 + w * LG4);
61+
t2 = z * (LG1 + w * LG3);
62+
r = t2 + t1;
63+
hfsq = 0.5 * f * f;
64+
65+
hi = f - hfsq;
66+
ui = hi.to_bits();
67+
ui &= 0xfffff000;
68+
hi = f32::from_bits(ui);
69+
lo = f - hi - hfsq + s * (hfsq + r);
70+
return (lo + hi) * IVLN2LO + lo * IVLN2HI + hi * IVLN2HI + k as f32;
71+
}

src/math/mod.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ mod floorf;
1515
mod fmodf;
1616
mod hypot;
1717
mod hypotf;
18+
mod log2;
19+
mod log2f;
1820
mod logf;
1921
mod powf;
2022
mod round;
@@ -30,8 +32,9 @@ mod truncf;
3032

3133
pub use self::{
3234
ceilf::ceilf, expf::expf, fabs::fabs, fabsf::fabsf, floor::floor, floorf::floorf, fmodf::fmodf,
33-
hypot::hypot, hypotf::hypotf, logf::logf, powf::powf, round::round, roundf::roundf,
34-
scalbn::scalbn, scalbnf::scalbnf, sqrt::sqrt, sqrtf::sqrtf, trunc::trunc, truncf::truncf,
35+
hypot::hypot, hypotf::hypotf, log2::log2, log2f::log2f, logf::logf, powf::powf, round::round,
36+
roundf::roundf, scalbn::scalbn, scalbnf::scalbnf, sqrt::sqrt, sqrtf::sqrtf, trunc::trunc,
37+
truncf::truncf,
3538
};
3639

3740
fn isnanf(x: f32) -> bool {

test-generator/src/main.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,7 @@ f32_f32! {
664664
expf,
665665
// fdimf,
666666
// log10f,
667-
// log2f,
667+
log2f,
668668
logf,
669669
roundf,
670670
// sinf,
@@ -709,7 +709,7 @@ f64_f64! {
709709
// log,
710710
// log10,
711711
// log1p,
712-
// log2,
712+
log2,
713713
round,
714714
// sin,
715715
// sinh,

0 commit comments

Comments
 (0)