summaryrefslogtreecommitdiffstats
path: root/third_party/rust/libm/src/math/rint.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
commit26a029d407be480d791972afb5975cf62c9360a6 (patch)
treef435a8308119effd964b339f76abb83a57c29483 /third_party/rust/libm/src/math/rint.rs
parentInitial commit. (diff)
downloadfirefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz
firefox-26a029d407be480d791972afb5975cf62c9360a6.zip
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/libm/src/math/rint.rs')
-rw-r--r--third_party/rust/libm/src/math/rint.rs48
1 files changed, 48 insertions, 0 deletions
diff --git a/third_party/rust/libm/src/math/rint.rs b/third_party/rust/libm/src/math/rint.rs
new file mode 100644
index 0000000000..0c6025c1f7
--- /dev/null
+++ b/third_party/rust/libm/src/math/rint.rs
@@ -0,0 +1,48 @@
+#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
+pub fn rint(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
+ }
+ }
+}
+
+// PowerPC tests are failing on LLVM 13: https://github.com/rust-lang/rust/issues/88520
+#[cfg(not(target_arch = "powerpc64"))]
+#[cfg(test)]
+mod tests {
+ use super::rint;
+
+ #[test]
+ fn negative_zero() {
+ assert_eq!(rint(-0.0_f64).to_bits(), (-0.0_f64).to_bits());
+ }
+
+ #[test]
+ fn sanity_check() {
+ assert_eq!(rint(-1.0), -1.0);
+ assert_eq!(rint(2.8), 3.0);
+ assert_eq!(rint(-0.5), -0.0);
+ assert_eq!(rint(0.5), 0.0);
+ assert_eq!(rint(-1.5), -2.0);
+ assert_eq!(rint(1.5), 2.0);
+ }
+}