Skip to content

Commit 857513d

Browse files
101: implement cbrt and cbrtf r=japaric a=erikdesjardins closes rust-lang#10, closes rust-lang#43 Co-authored-by: Erik <[email protected]>
2 parents a9e0c4f + 66f93d6 commit 857513d

File tree

5 files changed

+188
-6
lines changed

5 files changed

+188
-6
lines changed

src/lib.rs

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

8282
fn log10(self) -> Self;
8383

84-
#[cfg(todo)]
8584
fn cbrt(self) -> Self;
8685

8786
fn hypot(self, other: Self) -> Self;
@@ -241,7 +240,6 @@ impl F32Ext for f32 {
241240
log10f(self)
242241
}
243242

244-
#[cfg(todo)]
245243
#[inline]
246244
fn cbrt(self) -> Self {
247245
cbrtf(self)
@@ -400,7 +398,6 @@ pub trait F64Ext: private::Sealed {
400398

401399
fn log10(self) -> Self;
402400

403-
#[cfg(todo)]
404401
fn cbrt(self) -> Self;
405402

406403
fn hypot(self, other: Self) -> Self;
@@ -561,7 +558,6 @@ impl F64Ext for f64 {
561558
log10(self)
562559
}
563560

564-
#[cfg(todo)]
565561
#[inline]
566562
fn cbrt(self) -> Self {
567563
cbrt(self)

src/math/cbrt.rs

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/* origin: FreeBSD /usr/src/lib/msun/src/s_cbrt.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+
* Optimized by Bruce D. Evans.
13+
*/
14+
/* cbrt(x)
15+
* Return cube root of x
16+
*/
17+
18+
use core::f64;
19+
20+
const B1: u32 = 715094163; /* B1 = (1023-1023/3-0.03306235651)*2**20 */
21+
const B2: u32 = 696219795; /* B2 = (1023-1023/3-54/3-0.03306235651)*2**20 */
22+
23+
/* |1/cbrt(x) - p(x)| < 2**-23.5 (~[-7.93e-8, 7.929e-8]). */
24+
const P0: f64 = 1.87595182427177009643; /* 0x3ffe03e6, 0x0f61e692 */
25+
const P1: f64 = -1.88497979543377169875; /* 0xbffe28e0, 0x92f02420 */
26+
const P2: f64 = 1.621429720105354466140; /* 0x3ff9f160, 0x4a49d6c2 */
27+
const P3: f64 = -0.758397934778766047437; /* 0xbfe844cb, 0xbee751d9 */
28+
const P4: f64 = 0.145996192886612446982; /* 0x3fc2b000, 0xd4e4edd7 */
29+
30+
#[inline]
31+
pub fn cbrt(x: f64) -> f64 {
32+
let x1p54 = f64::from_bits(0x4350000000000000); // 0x1p54 === 2 ^ 54
33+
34+
let mut ui: u64 = x.to_bits();
35+
let mut r: f64;
36+
let s: f64;
37+
let mut t: f64;
38+
let w: f64;
39+
let mut hx: u32 = (ui >> 32) as u32 & 0x7fffffff;
40+
41+
if hx >= 0x7ff00000 {
42+
/* cbrt(NaN,INF) is itself */
43+
return x + x;
44+
}
45+
46+
/*
47+
* Rough cbrt to 5 bits:
48+
* cbrt(2**e*(1+m) ~= 2**(e/3)*(1+(e%3+m)/3)
49+
* where e is integral and >= 0, m is real and in [0, 1), and "/" and
50+
* "%" are integer division and modulus with rounding towards minus
51+
* infinity. The RHS is always >= the LHS and has a maximum relative
52+
* error of about 1 in 16. Adding a bias of -0.03306235651 to the
53+
* (e%3+m)/3 term reduces the error to about 1 in 32. With the IEEE
54+
* floating point representation, for finite positive normal values,
55+
* ordinary integer divison of the value in bits magically gives
56+
* almost exactly the RHS of the above provided we first subtract the
57+
* exponent bias (1023 for doubles) and later add it back. We do the
58+
* subtraction virtually to keep e >= 0 so that ordinary integer
59+
* division rounds towards minus infinity; this is also efficient.
60+
*/
61+
if hx < 0x00100000 {
62+
/* zero or subnormal? */
63+
ui = (x * x1p54).to_bits();
64+
hx = (ui >> 32) as u32 & 0x7fffffff;
65+
if hx == 0 {
66+
return x; /* cbrt(0) is itself */
67+
}
68+
hx = hx / 3 + B2;
69+
} else {
70+
hx = hx / 3 + B1;
71+
}
72+
ui &= 1 << 63;
73+
ui |= (hx as u64) << 32;
74+
t = f64::from_bits(ui);
75+
76+
/*
77+
* New cbrt to 23 bits:
78+
* cbrt(x) = t*cbrt(x/t**3) ~= t*P(t**3/x)
79+
* where P(r) is a polynomial of degree 4 that approximates 1/cbrt(r)
80+
* to within 2**-23.5 when |r - 1| < 1/10. The rough approximation
81+
* has produced t such than |t/cbrt(x) - 1| ~< 1/32, and cubing this
82+
* gives us bounds for r = t**3/x.
83+
*
84+
* Try to optimize for parallel evaluation as in __tanf.c.
85+
*/
86+
r = (t * t) * (t / x);
87+
t = t * ((P0 + r * (P1 + r * P2)) + ((r * r) * r) * (P3 + r * P4));
88+
89+
/*
90+
* Round t away from zero to 23 bits (sloppily except for ensuring that
91+
* the result is larger in magnitude than cbrt(x) but not much more than
92+
* 2 23-bit ulps larger). With rounding towards zero, the error bound
93+
* would be ~5/6 instead of ~4/6. With a maximum error of 2 23-bit ulps
94+
* in the rounded t, the infinite-precision error in the Newton
95+
* approximation barely affects third digit in the final error
96+
* 0.667; the error in the rounded t can be up to about 3 23-bit ulps
97+
* before the final error is larger than 0.667 ulps.
98+
*/
99+
ui = t.to_bits();
100+
ui = (ui + 0x80000000) & 0xffffffffc0000000;
101+
t = f64::from_bits(ui);
102+
103+
/* one step Newton iteration to 53 bits with error < 0.667 ulps */
104+
s = t * t; /* t*t is exact */
105+
r = x / s; /* error <= 0.5 ulps; |r| < |t| */
106+
w = t + t; /* t+t is exact */
107+
r = (r - t) / (w + r); /* r-t is exact; w+r ~= 3*t */
108+
t = t + t * r; /* error <= 0.5 + 0.5/3 + epsilon */
109+
t
110+
}

src/math/cbrtf.rs

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/* origin: FreeBSD /usr/src/lib/msun/src/s_cbrtf.c */
2+
/*
3+
* Conversion to float by Ian Lance Taylor, Cygnus Support, [email protected].
4+
* Debugged and optimized by Bruce D. Evans.
5+
*/
6+
/*
7+
* ====================================================
8+
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
9+
*
10+
* Developed at SunPro, a Sun Microsystems, Inc. business.
11+
* Permission to use, copy, modify, and distribute this
12+
* software is freely granted, provided that this notice
13+
* is preserved.
14+
* ====================================================
15+
*/
16+
/* cbrtf(x)
17+
* Return cube root of x
18+
*/
19+
20+
use core::f32;
21+
22+
const B1: u32 = 709958130; /* B1 = (127-127.0/3-0.03306235651)*2**23 */
23+
const B2: u32 = 642849266; /* B2 = (127-127.0/3-24/3-0.03306235651)*2**23 */
24+
25+
#[inline]
26+
pub fn cbrtf(x: f32) -> f32 {
27+
let x1p24 = f32::from_bits(0x4b800000); // 0x1p24f === 2 ^ 24
28+
29+
let mut r: f64;
30+
let mut t: f64;
31+
let mut ui: u32 = x.to_bits();
32+
let mut hx: u32 = ui & 0x7fffffff;
33+
34+
if hx >= 0x7f800000 {
35+
/* cbrt(NaN,INF) is itself */
36+
return x + x;
37+
}
38+
39+
/* rough cbrt to 5 bits */
40+
if hx < 0x00800000 {
41+
/* zero or subnormal? */
42+
if hx == 0 {
43+
return x; /* cbrt(+-0) is itself */
44+
}
45+
ui = (x * x1p24).to_bits();
46+
hx = ui & 0x7fffffff;
47+
hx = hx / 3 + B2;
48+
} else {
49+
hx = hx / 3 + B1;
50+
}
51+
ui &= 0x80000000;
52+
ui |= hx;
53+
54+
/*
55+
* First step Newton iteration (solving t*t-x/t == 0) to 16 bits. In
56+
* double precision so that its terms can be arranged for efficiency
57+
* without causing overflow or underflow.
58+
*/
59+
t = f32::from_bits(ui) as f64;
60+
r = t * t * t;
61+
t = t * (x as f64 + x as f64 + r) / (x as f64 + r + r);
62+
63+
/*
64+
* Second step Newton iteration to 47 bits. In double precision for
65+
* efficiency and accuracy.
66+
*/
67+
r = t * t * t;
68+
t = t * (x as f64 + x as f64 + r) / (x as f64 + r + r);
69+
70+
/* rounding to 24 bits is perfect in round-to-nearest mode */
71+
t as f32
72+
}

src/math/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ macro_rules! force_eval {
66
};
77
}
88

9+
mod cbrt;
10+
mod cbrtf;
911
mod ceil;
1012
mod ceilf;
1113
mod cosf;
@@ -39,6 +41,8 @@ mod trunc;
3941
mod truncf;
4042

4143
// Use separated imports instead of {}-grouped imports for easier merging.
44+
pub use self::cbrt::cbrt;
45+
pub use self::cbrtf::cbrtf;
4246
pub use self::ceil::ceil;
4347
pub use self::ceilf::ceilf;
4448
pub use self::cosf::cosf;

test-generator/src/main.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,7 @@ f32_f32! {
656656
truncf,
657657
// asinf,
658658
// atanf,
659-
// cbrtf,
659+
cbrtf,
660660
cosf,
661661
ceilf,
662662
// coshf,
@@ -699,7 +699,7 @@ f64_f64! {
699699
// acos,
700700
// asin,
701701
// atan,
702-
// cbrt,
702+
cbrt,
703703
ceil,
704704
// cos,
705705
// cosh,

0 commit comments

Comments
 (0)