Skip to content
This repository was archived by the owner on Apr 28, 2025. It is now read-only.

Commit a4e0202

Browse files
committed
Ported several remaining math functions from musl
Please note that these aren't tested yet.
1 parent 8e857c7 commit a4e0202

34 files changed

+4228
-0
lines changed

src/math/acosh.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use super::{log, log1p, sqrt};
2+
3+
const LN2: f64 = 0.693147180559945309417232121458176568; /* 0x3fe62e42, 0xfefa39ef*/
4+
5+
/* acosh(x) = log(x + sqrt(x*x-1)) */
6+
pub fn acosh(x: f64) -> f64 {
7+
let u = x.to_bits();
8+
let e = ((u >> 52) as usize) & 0x7ff;
9+
10+
/* x < 1 domain error is handled in the called functions */
11+
12+
if e < 0x3ff + 1 {
13+
/* |x| < 2, up to 2ulp error in [1,1.125] */
14+
return log1p(x-1.0+sqrt((x-1.0)*(x-1.0)+2.0*(x-1.0)));
15+
}
16+
if e < 0x3ff + 26 {
17+
/* |x| < 0x1p26 */
18+
return log(2.0*x-1.0/(x+sqrt(x*x-1.0)));
19+
}
20+
/* |x| >= 0x1p26 or nan */
21+
return log(x) + LN2;
22+
}

src/math/acoshf.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use super::{log1pf, logf, sqrtf};
2+
3+
const LN2: f32 = 0.693147180559945309417232121458176568;
4+
5+
/* acosh(x) = log(x + sqrt(x*x-1)) */
6+
pub fn acoshf(x: f32) -> f32 {
7+
let u = x.to_bits();
8+
let a = u & 0x7fffffff;
9+
10+
if a < 0x3f800000+(1<<23) {
11+
/* |x| < 2, invalid if x < 1 or nan */
12+
/* up to 2ulp error in [1,1.125] */
13+
return log1pf(x-1.0 + sqrtf((x-1.0)*(x-1.0)+2.0*(x-1.0)));
14+
}
15+
if a < 0x3f800000+(12<<23) {
16+
/* |x| < 0x1p12 */
17+
return logf(2.0*x - 1.0/(x+sqrtf(x*x-1.0)));
18+
}
19+
/* x >= 0x1p12 */
20+
return logf(x) + LN2;
21+
}

src/math/asinef.rs

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/* @(#)z_asinef.c 1.0 98/08/13 */
2+
/******************************************************************
3+
* The following routines are coded directly from the algorithms
4+
* and coefficients given in "Software Manual for the Elementary
5+
* Functions" by William J. Cody, Jr. and William Waite, Prentice
6+
* Hall, 1980.
7+
******************************************************************/
8+
/******************************************************************
9+
* Arcsine
10+
*
11+
* Input:
12+
* x - floating point value
13+
* acosine - indicates acos calculation
14+
*
15+
* Output:
16+
* Arcsine of x.
17+
*
18+
* Description:
19+
* This routine calculates arcsine / arccosine.
20+
*
21+
*****************************************************************/
22+
23+
use super::{fabsf, sqrtf};
24+
25+
const P: [f32; 2] = [ 0.933935835, -0.504400557 ];
26+
const Q: [f32; 2] = [ 0.560363004e+1, -0.554846723e+1 ];
27+
const A: [f32; 2] = [ 0.0, 0.785398163 ];
28+
const B: [f32; 2] = [ 1.570796326, 0.785398163 ];
29+
const Z_ROOTEPS_F: f32 = 1.7263349182589107e-4;
30+
31+
pub fn asinef(x: f32, acosine: usize) -> f32
32+
{
33+
let flag: usize;
34+
let i: usize;
35+
let mut branch: bool = false;
36+
let g: f32;
37+
let mut res: f32 = 0.0;
38+
let mut y: f32;
39+
40+
/* Check for special values. */
41+
//i = numtestf (x);
42+
if x.is_nan() || x.is_infinite() {
43+
force_eval!(x);
44+
return x;
45+
}
46+
47+
y = fabsf(x);
48+
flag = acosine;
49+
50+
if y > 0.5 {
51+
i = 1 - flag;
52+
53+
/* Check for range error. */
54+
if y > 1.0 {
55+
return 0.0 / 0.0;
56+
}
57+
58+
g = (1.0 - y) / 2.0;
59+
y = -2.0 * sqrtf(g);
60+
branch = true;
61+
} else {
62+
i = flag;
63+
if y < Z_ROOTEPS_F {
64+
res = y;
65+
g = 0.0; // pleasing the uninitialized variable
66+
} else {
67+
g = y * y;
68+
}
69+
}
70+
71+
if y >= Z_ROOTEPS_F || branch {
72+
/* Calculate the Taylor series. */
73+
let p = (P[1] * g + P[0]) * g;
74+
let q = (g + Q[1]) * g + Q[0];
75+
let r = p / q;
76+
77+
res = y + y * r;
78+
}
79+
80+
/* Calculate asine or acose. */
81+
if flag == 0 {
82+
res = (A[i] + res) + A[i];
83+
if x < 0.0 {
84+
res = -res;
85+
}
86+
} else {
87+
if x < 0.0 {
88+
res = (B[i] + res) + B[i];
89+
} else {
90+
res = (A[i] - res) + A[i];
91+
}
92+
}
93+
94+
return res;
95+
}

src/math/asinh.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use super::{log, log1p, sqrt};
2+
3+
const LN2: f64 = 0.693147180559945309417232121458176568; /* 0x3fe62e42, 0xfefa39ef*/
4+
5+
/* asinh(x) = sign(x)*log(|x|+sqrt(x*x+1)) ~= x - x^3/6 + o(x^5) */
6+
pub fn asinh(mut x: f64) -> f64 {
7+
let mut u = x.to_bits();
8+
let e = ((u >> 52) as usize) & 0x7ff;
9+
let sign = (u >> 63) != 0;
10+
11+
/* |x| */
12+
u &= (!0) >> 1;
13+
x = f64::from_bits(u);
14+
15+
if e >= 0x3ff + 26 {
16+
/* |x| >= 0x1p26 or inf or nan */
17+
x = log(x) + LN2;
18+
} else if e >= 0x3ff + 1 {
19+
/* |x| >= 2 */
20+
x = log(2.0*x + 1.0/(sqrt(x*x+1.0)+x));
21+
} else if e >= 0x3ff - 26 {
22+
/* |x| >= 0x1p-26, up to 1.6ulp error in [0.125,0.5] */
23+
x = log1p(x + x*x/(sqrt(x*x+1.0)+1.0));
24+
} else {
25+
/* |x| < 0x1p-26, raise inexact if x != 0 */
26+
let x1p120 = f64::from_bits(0x4770000000000000);
27+
force_eval!(x + x1p120);
28+
}
29+
30+
if sign {
31+
-x
32+
} else {
33+
x
34+
}
35+
}

src/math/asinhf.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use super::{logf, log1pf, sqrtf};
2+
3+
const LN2: f32 = 0.693147180559945309417232121458176568;
4+
5+
/* asinh(x) = sign(x)*log(|x|+sqrt(x*x+1)) ~= x - x^3/6 + o(x^5) */
6+
pub fn asinhf(mut x: f32) -> f32 {
7+
let u = x.to_bits();
8+
let i = u & 0x7fffffff;
9+
let sign = (u >> 31) != 0;
10+
11+
/* |x| */
12+
x = f32::from_bits(i);
13+
14+
if i >= 0x3f800000 + (12<<23) {
15+
/* |x| >= 0x1p12 or inf or nan */
16+
x = logf(x) + LN2;
17+
} else if i >= 0x3f800000 + (1<<23) {
18+
/* |x| >= 2 */
19+
x = logf(2.0*x + 1.0/(sqrtf(x*x+1.0)+x));
20+
} else if i >= 0x3f800000 - (12<<23) {
21+
/* |x| >= 0x1p-12, up to 1.6ulp error in [0.125,0.5] */
22+
x = log1pf(x + x*x/(sqrtf(x*x+1.0)+1.0));
23+
} else {
24+
/* |x| < 0x1p-12, raise inexact if x!=0 */
25+
let x1p120 = f32::from_bits(0x7b800000);
26+
force_eval!(x + x1p120);
27+
}
28+
29+
if sign {
30+
-x
31+
} else {
32+
x
33+
}
34+
}

src/math/atanh.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
use super::{log1p};
2+
3+
/* atanh(x) = log((1+x)/(1-x))/2 = log1p(2x/(1-x))/2 ~= x + x^3/3 + o(x^5) */
4+
pub fn atanh(mut x: f64) -> f64 {
5+
let mut u = x.to_bits();
6+
let e = ((u >> 52) as usize) & 0x7ff;
7+
let sign = (u >> 63) != 0;
8+
9+
/* |x| */
10+
u &= 0x7fffffff;
11+
x = f64::from_bits(u);
12+
13+
if e < 0x3ff - 1 {
14+
if e < 0x3ff - 32 {
15+
/* handle underflow */
16+
if e == 0 {
17+
force_eval!(x as f32);
18+
}
19+
} else {
20+
/* |x| < 0.5, up to 1.7ulp error */
21+
x = 0.5*log1p(2.0*x + 2.0*x*x/(1.0-x));
22+
}
23+
} else {
24+
/* avoid overflow */
25+
x = 0.5*log1p(2.0*(x/(1.0-x)));
26+
}
27+
28+
if sign {
29+
-x
30+
} else {
31+
x
32+
}
33+
}

src/math/atanhf.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use super::{log1pf};
2+
3+
/* atanh(x) = log((1+x)/(1-x))/2 = log1p(2x/(1-x))/2 ~= x + x^3/3 + o(x^5) */
4+
pub fn atanhf(mut x: f32) -> f32 {
5+
let mut u = x.to_bits();
6+
let sign = (u >> 31) != 0;
7+
8+
/* |x| */
9+
u &= 0x7fffffff;
10+
x = f32::from_bits(u);
11+
12+
if u < 0x3f800000 - (1<<23) {
13+
if u < 0x3f800000 - (32<<23) {
14+
/* handle underflow */
15+
if u < (1<<23) {
16+
force_eval!((x*x) as f32);
17+
}
18+
} else {
19+
/* |x| < 0.5, up to 1.7ulp error */
20+
x = 0.5*log1pf(2.0*x + 2.0*x*x/(1.0-x));
21+
}
22+
} else {
23+
/* avoid overflow */
24+
x = 0.5*log1pf(2.0*(x/(1.0-x)));
25+
}
26+
27+
if sign {
28+
-x
29+
} else {
30+
x
31+
}
32+
}

src/math/copysign.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
pub fn copysign(x: f64, y: f64) -> f64 {
2+
let mut ux = x.to_bits();
3+
let uy = y.to_bits();
4+
ux &= (!0) >> 1;
5+
ux |= uy & (1<<63);
6+
f64::from_bits(ux)
7+
}

src/math/copysignf.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
pub fn copysignf(x: f32, y: f32) -> f32 {
2+
let mut ux = x.to_bits();
3+
let uy = y.to_bits();
4+
ux &= 0x7fffffff;
5+
ux |= uy & 0x80000000;
6+
f32::from_bits(ux)
7+
}

0 commit comments

Comments
 (0)