-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
593764b
commit 3027113
Showing
4 changed files
with
107 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Source: musl libm rint | ||
// (equivalent to roundeven when rounding mode is default, | ||
// which Rust assumes) | ||
|
||
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)] | ||
pub fn roundeven(x: f64) -> f64 { | ||
let one_over_e = 1.0 / f64::EPSILON; | ||
let as_u64: u64 = x.to_bits(); | ||
let exponent: u64 = as_u64 >> 52 & 0x7ff; | ||
let is_positive = (as_u64 >> 63) == 0; | ||
if exponent >= 0x3ff + 52 { | ||
x | ||
} else { | ||
let ans = if is_positive { | ||
x + one_over_e - one_over_e | ||
} else { | ||
x - one_over_e + one_over_e | ||
}; | ||
|
||
if ans == 0.0 { | ||
if is_positive { | ||
0.0 | ||
} else { | ||
-0.0 | ||
} | ||
} else { | ||
ans | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::roundeven; | ||
|
||
#[test] | ||
fn negative_zero() { | ||
assert_eq!(roundeven(-0.0_f64).to_bits(), (-0.0_f64).to_bits()); | ||
} | ||
|
||
#[test] | ||
fn sanity_check() { | ||
assert_eq!(roundeven(-1.0), -1.0); | ||
assert_eq!(roundeven(2.8), 3.0); | ||
assert_eq!(roundeven(-0.5), -0.0); | ||
assert_eq!(roundeven(0.5), 0.0); | ||
assert_eq!(roundeven(-1.5), -2.0); | ||
assert_eq!(roundeven(1.5), 2.0); | ||
} | ||
} |
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,50 @@ | ||
// Source: musl libm rintf | ||
// (equivalent to roundevenf when rounding mode is default, | ||
// which Rust assumes) | ||
|
||
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)] | ||
pub fn roundevenf(x: f32) -> f32 { | ||
let one_over_e = 1.0 / f32::EPSILON; | ||
let as_u32: u32 = x.to_bits(); | ||
let exponent: u32 = as_u32 >> 23 & 0xff; | ||
let is_positive = (as_u32 >> 31) == 0; | ||
if exponent >= 0x7f + 23 { | ||
x | ||
} else { | ||
let ans = if is_positive { | ||
x + one_over_e - one_over_e | ||
} else { | ||
x - one_over_e + one_over_e | ||
}; | ||
|
||
if ans == 0.0 { | ||
if is_positive { | ||
0.0 | ||
} else { | ||
-0.0 | ||
} | ||
} else { | ||
ans | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::roundevenf; | ||
|
||
#[test] | ||
fn negative_zero() { | ||
assert_eq!(roundevenf(-0.0_f32).to_bits(), (-0.0_f32).to_bits()); | ||
} | ||
|
||
#[test] | ||
fn sanity_check() { | ||
assert_eq!(roundevenf(-1.0), -1.0); | ||
assert_eq!(roundevenf(2.8), 3.0); | ||
assert_eq!(roundevenf(-0.5), -0.0); | ||
assert_eq!(roundevenf(0.5), 0.0); | ||
assert_eq!(roundevenf(-1.5), -2.0); | ||
assert_eq!(roundevenf(1.5), 2.0); | ||
} | ||
} |