From 06aba076817ff1b01287e136e0aea8941baa93bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A1ssio=20Kirch?= Date: Sun, 15 Jul 2018 19:49:12 -0300 Subject: [PATCH 1/4] implement cosh --- library/compiler-builtins/libm/src/lib.rs | 2 -- .../compiler-builtins/libm/src/math/cosh.rs | 34 +++++++++++++++++++ .../libm/src/math/k_expo2.rs | 14 ++++++++ .../compiler-builtins/libm/src/math/mod.rs | 4 +++ 4 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 library/compiler-builtins/libm/src/math/cosh.rs create mode 100644 library/compiler-builtins/libm/src/math/k_expo2.rs diff --git a/library/compiler-builtins/libm/src/lib.rs b/library/compiler-builtins/libm/src/lib.rs index 45213a25f7ab..3a1785357bf6 100644 --- a/library/compiler-builtins/libm/src/lib.rs +++ b/library/compiler-builtins/libm/src/lib.rs @@ -407,7 +407,6 @@ pub trait F64Ext: private::Sealed { fn sinh(self) -> Self; - #[cfg(todo)] fn cosh(self) -> Self; #[cfg(todo)] @@ -589,7 +588,6 @@ impl F64Ext for f64 { sinh(self) } - #[cfg(todo)] #[inline] fn cosh(self) -> Self { cosh(self) diff --git a/library/compiler-builtins/libm/src/math/cosh.rs b/library/compiler-builtins/libm/src/math/cosh.rs new file mode 100644 index 000000000000..54c0a61dc058 --- /dev/null +++ b/library/compiler-builtins/libm/src/math/cosh.rs @@ -0,0 +1,34 @@ +use super::exp; +use super::expm1; +use super::k_expo2; + +#[inline] +pub fn cosh(mut x: f64) -> f64 { + /* |x| */ + let mut ix = x.to_bits(); + ix &= 0x7fffffffffffffff; + x = f64::from_bits(ix); + let w = ix >> 32; + let w = w as u32; + + /* |x| < log(2) */ + if w < 0x3fe62e42 { + if w < 0x3ff00000 - (26 << 20) { + let x1p120 = f64::from_bits(0x4770000000000000); + force_eval!(x + x1p120); + return 1.; + } + let t = expm1(x); // exponential minus 1 + return 1. + t * t / (2. * (1. + t)); + } + + /* |x| < log(DBL_MAX) */ + if w < 0x40862e42 { + let t = exp(x); + /* note: if x>log(0x1p26) then the 1/t is not needed */ + return 0.5 * (t + 1. / t); + } + + /* |x| > log(DBL_MAX) or nan */ + k_expo2(x) +} diff --git a/library/compiler-builtins/libm/src/math/k_expo2.rs b/library/compiler-builtins/libm/src/math/k_expo2.rs new file mode 100644 index 000000000000..7a4045dfc7b6 --- /dev/null +++ b/library/compiler-builtins/libm/src/math/k_expo2.rs @@ -0,0 +1,14 @@ +use super::exp; + +/* k is such that k*ln2 has minimal relative error and x - kln2 > log(FLT_MIN) */ +const K: i64 = 2043; + +/* expf(x)/2 for x >= log(FLT_MAX), slightly better than 0.5f*expf(x/2)*expf(x/2) */ +#[inline] +pub(crate) fn k_expo2(x: f64) -> f64 { + let k_ln2 = f64::from_bits(0x40962066151add8b); + /* note that k is odd and scale*scale overflows */ + let scale = f64::from_bits(((0x3ff + K / 2) as u64) << 20); + /* exp(x - k ln2) * 2**(k-1) */ + exp(x - k_ln2) * scale * scale +} diff --git a/library/compiler-builtins/libm/src/math/mod.rs b/library/compiler-builtins/libm/src/math/mod.rs index eda6b5b72931..3b3822711d65 100644 --- a/library/compiler-builtins/libm/src/math/mod.rs +++ b/library/compiler-builtins/libm/src/math/mod.rs @@ -19,6 +19,7 @@ mod ceil; mod ceilf; mod cos; mod cosf; +mod cosh; mod coshf; mod exp; mod exp2; @@ -75,6 +76,7 @@ pub use self::ceil::ceil; pub use self::ceilf::ceilf; pub use self::cos::cos; pub use self::cosf::cosf; +pub use self::cosh::cosh; pub use self::coshf::coshf; pub use self::exp::exp; pub use self::exp2::exp2; @@ -122,6 +124,7 @@ pub use self::truncf::truncf; mod expo2; mod k_cos; mod k_cosf; +mod k_expo2; mod k_expo2f; mod k_sin; mod k_sinf; @@ -135,6 +138,7 @@ mod rem_pio2f; use self::expo2::expo2; use self::k_cos::k_cos; use self::k_cosf::k_cosf; +use self::k_expo2::k_expo2; use self::k_expo2f::k_expo2f; use self::k_sin::k_sin; use self::k_sinf::k_sinf; From bbf1c5432a76f59ab1771bf92a4e46a5afe99693 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Sun, 15 Jul 2018 18:13:09 -0500 Subject: [PATCH 2/4] enable tests for cosh --- library/compiler-builtins/libm/test-generator/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/compiler-builtins/libm/test-generator/src/main.rs b/library/compiler-builtins/libm/test-generator/src/main.rs index 7caada4ef3e9..897f16d6c91d 100644 --- a/library/compiler-builtins/libm/test-generator/src/main.rs +++ b/library/compiler-builtins/libm/test-generator/src/main.rs @@ -703,7 +703,7 @@ f64_f64! { cbrt, ceil, cos, - // cosh, + cosh, exp, exp2, expm1, From 1e617876949964277328a2dde5914128f0fb9d33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A1ssio=20Kirch?= Date: Sun, 15 Jul 2018 21:35:08 -0300 Subject: [PATCH 3/4] unused cast removed --- library/compiler-builtins/libm/src/math/cosh.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/library/compiler-builtins/libm/src/math/cosh.rs b/library/compiler-builtins/libm/src/math/cosh.rs index 54c0a61dc058..f3f7fbfbeb16 100644 --- a/library/compiler-builtins/libm/src/math/cosh.rs +++ b/library/compiler-builtins/libm/src/math/cosh.rs @@ -9,7 +9,6 @@ pub fn cosh(mut x: f64) -> f64 { ix &= 0x7fffffffffffffff; x = f64::from_bits(ix); let w = ix >> 32; - let w = w as u32; /* |x| < log(2) */ if w < 0x3fe62e42 { From e8e11976299cb5e58a0b2bbdbd0fd9537f23afff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A1ssio=20Kirch?= Date: Sun, 15 Jul 2018 21:36:22 -0300 Subject: [PATCH 4/4] words insertion on k_expo2 --- library/compiler-builtins/libm/src/math/k_expo2.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/compiler-builtins/libm/src/math/k_expo2.rs b/library/compiler-builtins/libm/src/math/k_expo2.rs index 7a4045dfc7b6..e295c7a53462 100644 --- a/library/compiler-builtins/libm/src/math/k_expo2.rs +++ b/library/compiler-builtins/libm/src/math/k_expo2.rs @@ -1,14 +1,14 @@ use super::exp; /* k is such that k*ln2 has minimal relative error and x - kln2 > log(FLT_MIN) */ -const K: i64 = 2043; +const K: i32 = 2043; /* expf(x)/2 for x >= log(FLT_MAX), slightly better than 0.5f*expf(x/2)*expf(x/2) */ #[inline] pub(crate) fn k_expo2(x: f64) -> f64 { let k_ln2 = f64::from_bits(0x40962066151add8b); /* note that k is odd and scale*scale overflows */ - let scale = f64::from_bits(((0x3ff + K / 2) as u64) << 20); + let scale = f64::from_bits(((((0x3ff + K / 2) as u32) << 20) as u64) << 32); /* exp(x - k ln2) * 2**(k-1) */ exp(x - k_ln2) * scale * scale }