about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--library/compiler-builtins/libm/src/math/generic/ceil.rs91
-rw-r--r--library/compiler-builtins/libm/src/math/generic/floor.rs77
-rw-r--r--library/compiler-builtins/libm/src/math/generic/trunc.rs89
3 files changed, 220 insertions, 37 deletions
diff --git a/library/compiler-builtins/libm/src/math/generic/ceil.rs b/library/compiler-builtins/libm/src/math/generic/ceil.rs
index 971a4d3d8c5..bf7e1d8e210 100644
--- a/library/compiler-builtins/libm/src/math/generic/ceil.rs
+++ b/library/compiler-builtins/libm/src/math/generic/ceil.rs
@@ -7,9 +7,14 @@
 //! performance seems to be better (based on icount) and it does not seem to experience rounding
 //! errors on i386.
 
+use super::super::support::{FpResult, Status};
 use super::super::{Float, Int, IntTy, MinInt};
 
 pub fn ceil<F: Float>(x: F) -> F {
+    ceil_status(x).val
+}
+
+pub fn ceil_status<F: Float>(x: F) -> FpResult<F> {
     let zero = IntTy::<F>::ZERO;
 
     let mut ix = x.to_bits();
@@ -17,20 +22,20 @@ pub fn ceil<F: Float>(x: F) -> F {
 
     // If the represented value has no fractional part, no truncation is needed.
     if e >= F::SIG_BITS as i32 {
-        return x;
+        return FpResult::ok(x);
     }
 
-    if e >= 0 {
+    let status;
+    let res = if e >= 0 {
         // |x| >= 1.0
-
         let m = F::SIG_MASK >> e.unsigned();
         if (ix & m) == zero {
             // Portion to be masked is already zero; no adjustment needed.
-            return x;
+            return FpResult::ok(x);
         }
 
         // Otherwise, raise an inexact exception.
-        force_eval!(x + F::MAX);
+        status = Status::INEXACT;
 
         if x.is_sign_positive() {
             ix += m;
@@ -40,7 +45,11 @@ pub fn ceil<F: Float>(x: F) -> F {
         F::from_bits(ix)
     } else {
         // |x| < 1.0, raise an inexact exception since truncation will happen (unless x == 0).
-        force_eval!(x + F::MAX);
+        if ix & F::SIG_MASK == F::Int::ZERO {
+            status = Status::OK;
+        } else {
+            status = Status::INEXACT;
+        }
 
         if x.is_sign_negative() {
             // -1.0 < x <= -0.0; rounding up goes toward -0.0.
@@ -52,18 +61,30 @@ pub fn ceil<F: Float>(x: F) -> F {
             // +0.0 remains unchanged
             x
         }
-    }
+    };
+
+    FpResult::new(res, status)
 }
 
 #[cfg(test)]
 mod tests {
     use super::*;
+    use crate::support::Hexf;
 
     /// Test against https://en.cppreference.com/w/cpp/numeric/math/ceil
-    fn spec_test<F: Float>() {
-        // Not Asserted: that the current rounding mode has no effect.
-        for f in [F::ZERO, F::NEG_ZERO, F::INFINITY, F::NEG_INFINITY].iter().copied() {
-            assert_biteq!(ceil(f), f);
+    fn spec_test<F: Float>(cases: &[(F, F, Status)]) {
+        let roundtrip = [F::ZERO, F::ONE, F::NEG_ONE, F::NEG_ZERO, F::INFINITY, F::NEG_INFINITY];
+
+        for x in roundtrip {
+            let FpResult { val, status } = ceil_status(x);
+            assert_biteq!(val, x, "{}", Hexf(x));
+            assert_eq!(status, Status::OK, "{}", Hexf(x));
+        }
+
+        for &(x, res, res_stat) in cases {
+            let FpResult { val, status } = ceil_status(x);
+            assert_biteq!(val, res, "{}", Hexf(x));
+            assert_eq!(status, res_stat, "{}", Hexf(x));
         }
     }
 
@@ -72,7 +93,17 @@ mod tests {
     #[test]
     #[cfg(f16_enabled)]
     fn spec_tests_f16() {
-        spec_test::<f16>();
+        let cases = [
+            (0.1, 1.0, Status::INEXACT),
+            (-0.1, -0.0, Status::INEXACT),
+            (0.9, 1.0, Status::INEXACT),
+            (-0.9, -0.0, Status::INEXACT),
+            (1.1, 2.0, Status::INEXACT),
+            (-1.1, -1.0, Status::INEXACT),
+            (1.9, 2.0, Status::INEXACT),
+            (-1.9, -1.0, Status::INEXACT),
+        ];
+        spec_test::<f16>(&cases);
     }
 
     #[test]
@@ -83,7 +114,17 @@ mod tests {
 
     #[test]
     fn spec_tests_f32() {
-        spec_test::<f32>();
+        let cases = [
+            (0.1, 1.0, Status::INEXACT),
+            (-0.1, -0.0, Status::INEXACT),
+            (0.9, 1.0, Status::INEXACT),
+            (-0.9, -0.0, Status::INEXACT),
+            (1.1, 2.0, Status::INEXACT),
+            (-1.1, -1.0, Status::INEXACT),
+            (1.9, 2.0, Status::INEXACT),
+            (-1.9, -1.0, Status::INEXACT),
+        ];
+        spec_test::<f32>(&cases);
     }
 
     #[test]
@@ -94,12 +135,32 @@ mod tests {
 
     #[test]
     fn spec_tests_f64() {
-        spec_test::<f64>();
+        let cases = [
+            (0.1, 1.0, Status::INEXACT),
+            (-0.1, -0.0, Status::INEXACT),
+            (0.9, 1.0, Status::INEXACT),
+            (-0.9, -0.0, Status::INEXACT),
+            (1.1, 2.0, Status::INEXACT),
+            (-1.1, -1.0, Status::INEXACT),
+            (1.9, 2.0, Status::INEXACT),
+            (-1.9, -1.0, Status::INEXACT),
+        ];
+        spec_test::<f64>(&cases);
     }
 
     #[test]
     #[cfg(f128_enabled)]
     fn spec_tests_f128() {
-        spec_test::<f128>();
+        let cases = [
+            (0.1, 1.0, Status::INEXACT),
+            (-0.1, -0.0, Status::INEXACT),
+            (0.9, 1.0, Status::INEXACT),
+            (-0.9, -0.0, Status::INEXACT),
+            (1.1, 2.0, Status::INEXACT),
+            (-1.1, -1.0, Status::INEXACT),
+            (1.9, 2.0, Status::INEXACT),
+            (-1.9, -1.0, Status::INEXACT),
+        ];
+        spec_test::<f128>(&cases);
     }
 }
diff --git a/library/compiler-builtins/libm/src/math/generic/floor.rs b/library/compiler-builtins/libm/src/math/generic/floor.rs
index 6754c08f870..7799551644f 100644
--- a/library/compiler-builtins/libm/src/math/generic/floor.rs
+++ b/library/compiler-builtins/libm/src/math/generic/floor.rs
@@ -7,9 +7,14 @@
 //! performance seems to be better (based on icount) and it does not seem to experience rounding
 //! errors on i386.
 
+use super::super::support::{FpResult, Status};
 use super::super::{Float, Int, IntTy, MinInt};
 
 pub fn floor<F: Float>(x: F) -> F {
+    floor_status(x).val
+}
+
+pub fn floor_status<F: Float>(x: F) -> FpResult<F> {
     let zero = IntTy::<F>::ZERO;
 
     let mut ix = x.to_bits();
@@ -17,20 +22,20 @@ pub fn floor<F: Float>(x: F) -> F {
 
     // If the represented value has no fractional part, no truncation is needed.
     if e >= F::SIG_BITS as i32 {
-        return x;
+        return FpResult::ok(x);
     }
 
-    if e >= 0 {
+    let status;
+    let res = if e >= 0 {
         // |x| >= 1.0
-
         let m = F::SIG_MASK >> e.unsigned();
         if ix & m == zero {
             // Portion to be masked is already zero; no adjustment needed.
-            return x;
+            return FpResult::ok(x);
         }
 
         // Otherwise, raise an inexact exception.
-        force_eval!(x + F::MAX);
+        status = Status::INEXACT;
 
         if x.is_sign_negative() {
             ix += m;
@@ -39,8 +44,12 @@ pub fn floor<F: Float>(x: F) -> F {
         ix &= !m;
         F::from_bits(ix)
     } else {
-        // |x| < 1.0, raise an inexact exception since truncation will happen (unless x == 0).
-        force_eval!(x + F::MAX);
+        // |x| < 1.0, raise an inexact exception since truncation will happen.
+        if ix & F::SIG_MASK == F::Int::ZERO {
+            status = Status::OK;
+        } else {
+            status = Status::INEXACT;
+        }
 
         if x.is_sign_positive() {
             // 0.0 <= x < 1.0; rounding down goes toward +0.0.
@@ -52,27 +61,40 @@ pub fn floor<F: Float>(x: F) -> F {
             // -0.0 remains unchanged
             x
         }
-    }
+    };
+
+    FpResult::new(res, status)
 }
 
 #[cfg(test)]
 mod tests {
     use super::*;
+    use crate::support::Hexf;
 
     /// Test against https://en.cppreference.com/w/cpp/numeric/math/floor
-    fn spec_test<F: Float>() {
-        // Not Asserted: that the current rounding mode has no effect.
-        for f in [F::ZERO, F::NEG_ZERO, F::INFINITY, F::NEG_INFINITY].iter().copied() {
-            assert_biteq!(floor(f), f);
+    fn spec_test<F: Float>(cases: &[(F, F, Status)]) {
+        let roundtrip = [F::ZERO, F::ONE, F::NEG_ONE, F::NEG_ZERO, F::INFINITY, F::NEG_INFINITY];
+
+        for x in roundtrip {
+            let FpResult { val, status } = floor_status(x);
+            assert_biteq!(val, x, "{}", Hexf(x));
+            assert_eq!(status, Status::OK, "{}", Hexf(x));
+        }
+
+        for &(x, res, res_stat) in cases {
+            let FpResult { val, status } = floor_status(x);
+            assert_biteq!(val, res, "{}", Hexf(x));
+            assert_eq!(status, res_stat, "{}", Hexf(x));
         }
     }
 
-    /* Skipping f16 / f128 "sanity_check"s due to rejected literal lexing at MSRV */
+    /* Skipping f16 / f128 "sanity_check"s and spec cases due to rejected literal lexing at MSRV */
 
     #[test]
     #[cfg(f16_enabled)]
     fn spec_tests_f16() {
-        spec_test::<f16>();
+        let cases = [];
+        spec_test::<f16>(&cases);
     }
 
     #[test]
@@ -84,7 +106,17 @@ mod tests {
 
     #[test]
     fn spec_tests_f32() {
-        spec_test::<f32>();
+        let cases = [
+            (0.1, 0.0, Status::INEXACT),
+            (-0.1, -1.0, Status::INEXACT),
+            (0.9, 0.0, Status::INEXACT),
+            (-0.9, -1.0, Status::INEXACT),
+            (1.1, 1.0, Status::INEXACT),
+            (-1.1, -2.0, Status::INEXACT),
+            (1.9, 1.0, Status::INEXACT),
+            (-1.9, -2.0, Status::INEXACT),
+        ];
+        spec_test::<f32>(&cases);
     }
 
     #[test]
@@ -95,12 +127,23 @@ mod tests {
 
     #[test]
     fn spec_tests_f64() {
-        spec_test::<f64>();
+        let cases = [
+            (0.1, 0.0, Status::INEXACT),
+            (-0.1, -1.0, Status::INEXACT),
+            (0.9, 0.0, Status::INEXACT),
+            (-0.9, -1.0, Status::INEXACT),
+            (1.1, 1.0, Status::INEXACT),
+            (-1.1, -2.0, Status::INEXACT),
+            (1.9, 1.0, Status::INEXACT),
+            (-1.9, -2.0, Status::INEXACT),
+        ];
+        spec_test::<f64>(&cases);
     }
 
     #[test]
     #[cfg(f128_enabled)]
     fn spec_tests_f128() {
-        spec_test::<f128>();
+        let cases = [];
+        spec_test::<f128>(&cases);
     }
 }
diff --git a/library/compiler-builtins/libm/src/math/generic/trunc.rs b/library/compiler-builtins/libm/src/math/generic/trunc.rs
index ca5f1bdd627..0fb3fa5ad3b 100644
--- a/library/compiler-builtins/libm/src/math/generic/trunc.rs
+++ b/library/compiler-builtins/libm/src/math/generic/trunc.rs
@@ -1,15 +1,20 @@
 /* SPDX-License-Identifier: MIT
  * origin: musl src/math/trunc.c */
 
+use super::super::support::{FpResult, Status};
 use super::super::{Float, Int, IntTy, MinInt};
 
 pub fn trunc<F: Float>(x: F) -> F {
+    trunc_status(x).val
+}
+
+pub fn trunc_status<F: Float>(x: F) -> FpResult<F> {
     let mut xi: F::Int = x.to_bits();
     let e: i32 = x.exp_unbiased();
 
     // C1: The represented value has no fractional part, so no truncation is needed
     if e >= F::SIG_BITS as i32 {
-        return x;
+        return FpResult::ok(x);
     }
 
     let mask = if e < 0 {
@@ -23,22 +28,68 @@ pub fn trunc<F: Float>(x: F) -> F {
 
     // C4: If the to-be-masked-out portion is already zero, we have an exact result
     if (xi & !mask) == IntTy::<F>::ZERO {
-        return x;
+        return FpResult::ok(x);
     }
 
     // C5: Otherwise the result is inexact and we will truncate. Raise `FE_INEXACT`, mask the
     // result, and return.
-    force_eval!(x + F::MAX);
+
+    let status = if xi & F::SIG_MASK == F::Int::ZERO { Status::OK } else { Status::INEXACT };
     xi &= mask;
-    F::from_bits(xi)
+    FpResult::new(F::from_bits(xi), status)
 }
 
 #[cfg(test)]
 mod tests {
     use super::*;
+    use crate::support::Hexf;
+
+    fn spec_test<F: Float>(cases: &[(F, F, Status)]) {
+        let roundtrip = [F::ZERO, F::ONE, F::NEG_ONE, F::NEG_ZERO, F::INFINITY, F::NEG_INFINITY];
+
+        for x in roundtrip {
+            let FpResult { val, status } = trunc_status(x);
+            assert_biteq!(val, x, "{}", Hexf(x));
+            assert_eq!(status, Status::OK, "{}", Hexf(x));
+        }
+
+        for &(x, res, res_stat) in cases {
+            let FpResult { val, status } = trunc_status(x);
+            assert_biteq!(val, res, "{}", Hexf(x));
+            assert_eq!(status, res_stat, "{}", Hexf(x));
+        }
+    }
+
+    /* Skipping f16 / f128 "sanity_check"s and spec cases due to rejected literal lexing at MSRV */
+
+    #[test]
+    #[cfg(f16_enabled)]
+    fn spec_tests_f16() {
+        let cases = [];
+        spec_test::<f16>(&cases);
+    }
+
+    #[test]
+    fn sanity_check_f32() {
+        assert_eq!(trunc(0.5f32), 0.0);
+        assert_eq!(trunc(1.1f32), 1.0);
+        assert_eq!(trunc(2.9f32), 2.0);
+    }
 
     #[test]
-    fn sanity_check() {
+    fn spec_tests_f32() {
+        let cases = [
+            (0.1, 0.0, Status::INEXACT),
+            (-0.1, -0.0, Status::INEXACT),
+            (0.9, 0.0, Status::INEXACT),
+            (-0.9, -0.0, Status::INEXACT),
+            (1.1, 1.0, Status::INEXACT),
+            (-1.1, -1.0, Status::INEXACT),
+            (1.9, 1.0, Status::INEXACT),
+            (-1.9, -1.0, Status::INEXACT),
+        ];
+        spec_test::<f32>(&cases);
+
         assert_biteq!(trunc(1.1f32), 1.0);
         assert_biteq!(trunc(1.1f64), 1.0);
 
@@ -54,4 +105,32 @@ mod tests {
         assert_biteq!(trunc(hf32!("-0x1p-1")), -0.0);
         assert_biteq!(trunc(hf64!("-0x1p-1")), -0.0);
     }
+
+    #[test]
+    fn sanity_check_f64() {
+        assert_eq!(trunc(1.1f64), 1.0);
+        assert_eq!(trunc(2.9f64), 2.0);
+    }
+
+    #[test]
+    fn spec_tests_f64() {
+        let cases = [
+            (0.1, 0.0, Status::INEXACT),
+            (-0.1, -0.0, Status::INEXACT),
+            (0.9, 0.0, Status::INEXACT),
+            (-0.9, -0.0, Status::INEXACT),
+            (1.1, 1.0, Status::INEXACT),
+            (-1.1, -1.0, Status::INEXACT),
+            (1.9, 1.0, Status::INEXACT),
+            (-1.9, -1.0, Status::INEXACT),
+        ];
+        spec_test::<f64>(&cases);
+    }
+
+    #[test]
+    #[cfg(f128_enabled)]
+    fn spec_tests_f128() {
+        let cases = [];
+        spec_test::<f128>(&cases);
+    }
 }