Skip to content

Commit 4856da1

Browse files
committed
fix bit shifting error
2 parents 7db2487 + f9f234f commit 4856da1

File tree

10 files changed

+905
-19
lines changed

10 files changed

+905
-19
lines changed

src/lib.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ pub trait F32Ext: private::Sealed {
7070

7171
fn exp(self) -> Self;
7272

73-
#[cfg(todo)]
7473
fn exp2(self) -> Self;
7574

7675
fn ln(self) -> Self;
@@ -81,7 +80,6 @@ pub trait F32Ext: private::Sealed {
8180

8281
fn log10(self) -> Self;
8382

84-
#[cfg(todo)]
8583
fn cbrt(self) -> Self;
8684

8785
fn hypot(self, other: Self) -> Self;
@@ -215,7 +213,6 @@ impl F32Ext for f32 {
215213
expf(self)
216214
}
217215

218-
#[cfg(todo)]
219216
#[inline]
220217
fn exp2(self) -> Self {
221218
exp2f(self)
@@ -241,7 +238,6 @@ impl F32Ext for f32 {
241238
log10f(self)
242239
}
243240

244-
#[cfg(todo)]
245241
#[inline]
246242
fn cbrt(self) -> Self {
247243
cbrtf(self)
@@ -389,7 +385,6 @@ pub trait F64Ext: private::Sealed {
389385

390386
fn exp(self) -> Self;
391387

392-
#[cfg(todo)]
393388
fn exp2(self) -> Self;
394389

395390
fn ln(self) -> Self;
@@ -400,7 +395,6 @@ pub trait F64Ext: private::Sealed {
400395

401396
fn log10(self) -> Self;
402397

403-
#[cfg(todo)]
404398
fn cbrt(self) -> Self;
405399

406400
fn hypot(self, other: Self) -> Self;
@@ -417,7 +411,6 @@ pub trait F64Ext: private::Sealed {
417411
#[cfg(todo)]
418412
fn asin(self) -> Self;
419413

420-
#[cfg(todo)]
421414
fn acos(self) -> Self;
422415

423416
#[cfg(todo)]
@@ -534,7 +527,6 @@ impl F64Ext for f64 {
534527
exp(self)
535528
}
536529

537-
#[cfg(todo)]
538530
#[inline]
539531
fn exp2(self) -> Self {
540532
exp2(self)
@@ -560,7 +552,6 @@ impl F64Ext for f64 {
560552
log10(self)
561553
}
562554

563-
#[cfg(todo)]
564555
#[inline]
565556
fn cbrt(self) -> Self {
566557
cbrt(self)
@@ -595,7 +586,6 @@ impl F64Ext for f64 {
595586
asin(self)
596587
}
597588

598-
#[cfg(todo)]
599589
#[inline]
600590
fn acos(self) -> Self {
601591
acos(self)

src/math/acos.rs

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/* origin: FreeBSD /usr/src/lib/msun/src/e_acos.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+
/* acos(x)
13+
* Method :
14+
* acos(x) = pi/2 - asin(x)
15+
* acos(-x) = pi/2 + asin(x)
16+
* For |x|<=0.5
17+
* acos(x) = pi/2 - (x + x*x^2*R(x^2)) (see asin.c)
18+
* For x>0.5
19+
* acos(x) = pi/2 - (pi/2 - 2asin(sqrt((1-x)/2)))
20+
* = 2asin(sqrt((1-x)/2))
21+
* = 2s + 2s*z*R(z) ...z=(1-x)/2, s=sqrt(z)
22+
* = 2f + (2c + 2s*z*R(z))
23+
* where f=hi part of s, and c = (z-f*f)/(s+f) is the correction term
24+
* for f so that f+c ~ sqrt(z).
25+
* For x<-0.5
26+
* acos(x) = pi - 2asin(sqrt((1-|x|)/2))
27+
* = pi - 0.5*(s+s*z*R(z)), where z=(1-|x|)/2,s=sqrt(z)
28+
*
29+
* Special cases:
30+
* if x is NaN, return x itself;
31+
* if |x|>1, return NaN with invalid signal.
32+
*
33+
* Function needed: sqrt
34+
*/
35+
36+
use super::sqrt;
37+
38+
const PIO2_HI: f64 = 1.57079632679489655800e+00; /* 0x3FF921FB, 0x54442D18 */
39+
const PIO2_LO: f64 = 6.12323399573676603587e-17; /* 0x3C91A626, 0x33145C07 */
40+
const PS0: f64 = 1.66666666666666657415e-01; /* 0x3FC55555, 0x55555555 */
41+
const PS1: f64 = -3.25565818622400915405e-01; /* 0xBFD4D612, 0x03EB6F7D */
42+
const PS2: f64 = 2.01212532134862925881e-01; /* 0x3FC9C155, 0x0E884455 */
43+
const PS3: f64 = -4.00555345006794114027e-02; /* 0xBFA48228, 0xB5688F3B */
44+
const PS4: f64 = 7.91534994289814532176e-04; /* 0x3F49EFE0, 0x7501B288 */
45+
const PS5: f64 = 3.47933107596021167570e-05; /* 0x3F023DE1, 0x0DFDF709 */
46+
const QS1: f64 = -2.40339491173441421878e+00; /* 0xC0033A27, 0x1C8A2D4B */
47+
const QS2: f64 = 2.02094576023350569471e+00; /* 0x40002AE5, 0x9C598AC8 */
48+
const QS3: f64 = -6.88283971605453293030e-01; /* 0xBFE6066C, 0x1B8D0159 */
49+
const QS4: f64 = 7.70381505559019352791e-02; /* 0x3FB3B8C5, 0xB12E9282 */
50+
51+
#[inline]
52+
fn r(z: f64) -> f64 {
53+
let p: f64 = z * (PS0 + z * (PS1 + z * (PS2 + z * (PS3 + z * (PS4 + z * PS5)))));
54+
let q: f64 = 1.0 + z * (QS1 + z * (QS2 + z * (QS3 + z * QS4)));
55+
return p / q;
56+
}
57+
58+
#[inline]
59+
pub fn acos(x: f64) -> f64 {
60+
let x1p_120f = f64::from_bits(0x3870000000000000); // 0x1p-120 === 2 ^ -120
61+
let z: f64;
62+
let w: f64;
63+
let s: f64;
64+
let c: f64;
65+
let df: f64;
66+
let hx: u32;
67+
let ix: u32;
68+
69+
hx = (x.to_bits() >> 32) as u32;
70+
ix = hx & 0x7fffffff;
71+
/* |x| >= 1 or nan */
72+
if ix >= 0x3ff00000 {
73+
let lx: u32 = x.to_bits() as u32;
74+
75+
if (ix - 0x3ff00000 | lx) == 0 {
76+
/* acos(1)=0, acos(-1)=pi */
77+
if (hx >> 31) != 0 {
78+
return 2. * PIO2_HI + x1p_120f;
79+
}
80+
return 0.;
81+
}
82+
return 0. / (x - x);
83+
}
84+
/* |x| < 0.5 */
85+
if ix < 0x3fe00000 {
86+
if ix <= 0x3c600000 {
87+
/* |x| < 2**-57 */
88+
return PIO2_HI + x1p_120f;
89+
}
90+
return PIO2_HI - (x - (PIO2_LO - x * r(x * x)));
91+
}
92+
/* x < -0.5 */
93+
if (hx >> 31) != 0 {
94+
z = (1.0 + x) * 0.5;
95+
s = sqrt(z);
96+
w = r(z) * s - PIO2_LO;
97+
return 2. * (PIO2_HI - (s + w));
98+
}
99+
/* x > 0.5 */
100+
z = (1.0 - x) * 0.5;
101+
s = sqrt(z);
102+
// Set the low 4 bytes to zero
103+
df = f64::from_bits(s.to_bits() & 0xff_ff_ff_ff_00_00_00_00);
104+
105+
c = (z - df * df) / (s + df);
106+
w = r(z) * s + c;
107+
return 2. * (df + w);
108+
}

src/math/cbrt.rs

Lines changed: 110 additions & 0 deletions
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

Lines changed: 72 additions & 0 deletions
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+
}

0 commit comments

Comments
 (0)