-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The `scalbn` functions are similar enough that they can easily be made generic. Do so and `f16` and `f128` versions.
- Loading branch information
Showing
15 changed files
with
112 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -95,6 +95,8 @@ round | |
roundf | ||
scalbn | ||
scalbnf | ||
scalbnf128 | ||
scalbnf16 | ||
sin | ||
sincos | ||
sincosf | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
mod copysign; | ||
mod fabs; | ||
mod scalbn; | ||
|
||
pub use copysign::copysign; | ||
pub use fabs::fabs; | ||
pub use scalbn::scalbn; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
use super::super::{CastFrom, CastInto, Float, IntTy, MinInt}; | ||
|
||
pub fn scalbn<F: Float>(mut x: F, mut n: i32) -> F | ||
where | ||
u32: CastInto<F::Int>, | ||
F::Int: CastFrom<i32>, | ||
F::Int: CastFrom<u32>, | ||
{ | ||
// Bits including the implicit bit | ||
let sig_total_bits = F::SIG_BITS + 1; | ||
|
||
// Maximum and minimum values when biased | ||
let exp_max: i32 = F::EXP_BIAS as i32; | ||
let exp_min = -(exp_max - 1); | ||
|
||
// 2 ^ Emax, where Emax is the maximum biased exponent value (1023 for f64) | ||
let f_exp_max = F::from_bits(F::Int::cast_from(F::EXP_BIAS << 1) << F::SIG_BITS); | ||
// 2 ^ Emin, where Emin is the minimum biased exponent value (-1022 for f64) | ||
let f_exp_min = F::from_bits(IntTy::<F>::ONE << F::SIG_BITS); | ||
// 2 ^ sig_total_bits, representation of what can be accounted for with subnormals | ||
let f_exp_subnorm = F::from_bits((F::EXP_BIAS + sig_total_bits).cast() << F::SIG_BITS); | ||
|
||
if n > exp_max { | ||
x *= f_exp_max; | ||
n -= exp_max; | ||
if n > exp_max { | ||
x *= f_exp_max; | ||
n -= exp_max; | ||
if n > exp_max { | ||
n = exp_max; | ||
} | ||
} | ||
} else if n < exp_min { | ||
let mul = f_exp_min * f_exp_subnorm; | ||
let add = (exp_max - 1) - sig_total_bits as i32; | ||
|
||
x *= mul; | ||
n += add; | ||
if n < exp_min { | ||
x *= mul; | ||
n += add; | ||
if n < exp_min { | ||
n = exp_min; | ||
} | ||
} | ||
} | ||
|
||
x * F::from_bits(F::Int::cast_from(F::EXP_BIAS as i32 + n) << F::SIG_BITS) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,4 @@ | ||
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)] | ||
pub fn scalbn(x: f64, mut n: i32) -> f64 { | ||
let x1p1023 = f64::from_bits(0x7fe0000000000000); // 0x1p1023 === 2 ^ 1023 | ||
let x1p53 = f64::from_bits(0x4340000000000000); // 0x1p53 === 2 ^ 53 | ||
let x1p_1022 = f64::from_bits(0x0010000000000000); // 0x1p-1022 === 2 ^ (-1022) | ||
|
||
let mut y = x; | ||
|
||
if n > 1023 { | ||
y *= x1p1023; | ||
n -= 1023; | ||
if n > 1023 { | ||
y *= x1p1023; | ||
n -= 1023; | ||
if n > 1023 { | ||
n = 1023; | ||
} | ||
} | ||
} else if n < -1022 { | ||
/* make sure final n < -53 to avoid double | ||
rounding in the subnormal range */ | ||
y *= x1p_1022 * x1p53; | ||
n += 1022 - 53; | ||
if n < -1022 { | ||
y *= x1p_1022 * x1p53; | ||
n += 1022 - 53; | ||
if n < -1022 { | ||
n = -1022; | ||
} | ||
} | ||
} | ||
y * f64::from_bits(((0x3ff + n) as u64) << 52) | ||
pub fn scalbn(x: f64, n: i32) -> f64 { | ||
super::generic::scalbn(x, n) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,4 @@ | ||
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)] | ||
pub fn scalbnf(mut x: f32, mut n: i32) -> f32 { | ||
let x1p127 = f32::from_bits(0x7f000000); // 0x1p127f === 2 ^ 127 | ||
let x1p_126 = f32::from_bits(0x800000); // 0x1p-126f === 2 ^ -126 | ||
let x1p24 = f32::from_bits(0x4b800000); // 0x1p24f === 2 ^ 24 | ||
|
||
if n > 127 { | ||
x *= x1p127; | ||
n -= 127; | ||
if n > 127 { | ||
x *= x1p127; | ||
n -= 127; | ||
if n > 127 { | ||
n = 127; | ||
} | ||
} | ||
} else if n < -126 { | ||
x *= x1p_126 * x1p24; | ||
n += 126 - 24; | ||
if n < -126 { | ||
x *= x1p_126 * x1p24; | ||
n += 126 - 24; | ||
if n < -126 { | ||
n = -126; | ||
} | ||
} | ||
} | ||
x * f32::from_bits(((0x7f + n) as u32) << 23) | ||
pub fn scalbnf(x: f32, n: i32) -> f32 { | ||
super::generic::scalbn(x, n) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)] | ||
pub fn scalbnf128(x: f128, n: i32) -> f128 { | ||
super::generic::scalbn(x, n) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)] | ||
pub fn scalbnf16(x: f16, n: i32) -> f16 { | ||
super::generic::scalbn(x, n) | ||
} |