Skip to content

Commit 0a33394

Browse files
committed
Implement sinh
This also adds expo2 for the __expo2 function, and combine_words() to replace the INSERT_WORDS macro. Closes rust-lang#35
1 parent 4c0992b commit 0a33394

File tree

5 files changed

+71
-3
lines changed

5 files changed

+71
-3
lines changed

src/lib.rs

-2
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,6 @@ pub trait F64Ext: private::Sealed {
414414

415415
fn ln_1p(self) -> Self;
416416

417-
#[cfg(todo)]
418417
fn sinh(self) -> Self;
419418

420419
#[cfg(todo)]
@@ -595,7 +594,6 @@ impl F64Ext for f64 {
595594
log1p(self)
596595
}
597596

598-
#[cfg(todo)]
599597
#[inline]
600598
fn sinh(self) -> Self {
601599
sinh(self)

src/math/expo2.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use super::{combine_words, exp};
2+
3+
/* exp(x)/2 for x >= log(DBL_MAX), slightly better than 0.5*exp(x/2)*exp(x/2) */
4+
pub(crate) fn expo2(x: f64) -> f64 {
5+
/* k is such that k*ln2 has minimal relative error and x - kln2 > log(DBL_MIN) */
6+
const K: i32 = 2043;
7+
let kln2 = f64::from_bits(0x40962066151add8b);
8+
9+
/* note that k is odd and scale*scale overflows */
10+
let scale = combine_words(((0x3ff + K / 2) as u32) << 20, 0);
11+
/* exp(x - k ln2) * 2**(k-1) */
12+
return exp(x - kln2) * scale * scale;
13+
}

src/math/mod.rs

+9
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ mod scalbn;
5050
mod scalbnf;
5151
mod sin;
5252
mod sinf;
53+
mod sinh;
5354
mod sqrt;
5455
mod sqrtf;
5556
mod tanf;
@@ -100,13 +101,15 @@ pub use self::scalbn::scalbn;
100101
pub use self::scalbnf::scalbnf;
101102
pub use self::sin::sin;
102103
pub use self::sinf::sinf;
104+
pub use self::sinh::sinh;
103105
pub use self::sqrt::sqrt;
104106
pub use self::sqrtf::sqrtf;
105107
pub use self::tanf::tanf;
106108
pub use self::trunc::trunc;
107109
pub use self::truncf::truncf;
108110

109111
// Private modules
112+
mod expo2;
110113
mod k_cos;
111114
mod k_cosf;
112115
mod k_sin;
@@ -117,6 +120,7 @@ mod rem_pio2_large;
117120
mod rem_pio2f;
118121

119122
// Private re-imports
123+
use self::expo2::expo2;
120124
use self::k_cos::k_cos;
121125
use self::k_cosf::k_cosf;
122126
use self::k_sin::k_sin;
@@ -151,3 +155,8 @@ pub fn with_set_low_word(f: f64, lo: u32) -> f64 {
151155
tmp |= lo as u64;
152156
f64::from_bits(tmp)
153157
}
158+
159+
#[inline]
160+
fn combine_words(hi: u32, lo: u32) -> f64 {
161+
f64::from_bits((hi as u64) << 32 | lo as u64)
162+
}

src/math/sinh.rs

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
use super::{expm1, expo2};
2+
3+
// sinh(x) = (exp(x) - 1/exp(x))/2
4+
// = (exp(x)-1 + (exp(x)-1)/exp(x))/2
5+
// = x + x^3/6 + o(x^5)
6+
//
7+
pub fn sinh(x: f64) -> f64 {
8+
// union {double f; uint64_t i;} u = {.f = x};
9+
// uint32_t w;
10+
// double t, h, absx;
11+
12+
let mut uf: f64 = x;
13+
let mut ui: u64 = f64::to_bits(uf);
14+
let w: u32;
15+
let t: f64;
16+
let mut h: f64;
17+
let absx: f64;
18+
19+
h = 0.5;
20+
if ui >> 63 != 0 {
21+
h = -h;
22+
}
23+
/* |x| */
24+
ui &= !1 / 2;
25+
uf = f64::from_bits(ui);
26+
absx = uf;
27+
w = (ui >> 32) as u32;
28+
29+
/* |x| < log(DBL_MAX) */
30+
if w < 0x40862e42 {
31+
t = expm1(absx);
32+
if w < 0x3ff00000 {
33+
if w < 0x3ff00000 - (26 << 20) {
34+
/* note: inexact and underflow are raised by expm1 */
35+
/* note: this branch avoids spurious underflow */
36+
return x;
37+
}
38+
return h * (2.0 * t - t * t / (t + 1.0));
39+
}
40+
/* note: |x|>log(0x1p26)+eps could be just h*exp(x) */
41+
return h * (t + t / (t + 1.0));
42+
}
43+
44+
/* |x| > log(DBL_MAX) or nan */
45+
/* note: the result is stored to handle overflow */
46+
t = 2.0 * h * expo2(absx);
47+
return t;
48+
}

test-generator/src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,7 @@ f64_f64! {
714714
log2,
715715
round,
716716
sin,
717-
// sinh,
717+
sinh,
718718
sqrt,
719719
// tan,
720720
// tanh,

0 commit comments

Comments
 (0)