about summary refs log tree commit diff
diff options
context:
space:
mode:
authorTrevor Gross <tmgross@umich.edu>2025-01-25 00:29:04 +0000
committerTrevor Gross <tmgross@umich.edu>2025-02-07 23:04:53 +0000
commitf1afc26b8a7691808f6be04493798620fe90e5fb (patch)
tree76cb229580874786380049baafdac0887c457d36
parentf45cc66e8e4b09ee447e3519e7d8e85f06a00001 (diff)
downloadrust-f1afc26b8a7691808f6be04493798620fe90e5fb.tar.gz
rust-f1afc26b8a7691808f6be04493798620fe90e5fb.zip
Add an enum representation of rounding mode
We only round using nearest, but some incoming code has more handling of
rounding modes that would be nice to `match` on. Rather than checking
integer values, add an enum representation.
-rw-r--r--library/compiler-builtins/libm/src/math/fenv.rs22
1 files changed, 22 insertions, 0 deletions
diff --git a/library/compiler-builtins/libm/src/math/fenv.rs b/library/compiler-builtins/libm/src/math/fenv.rs
index c91272e8268..328c9f3467c 100644
--- a/library/compiler-builtins/libm/src/math/fenv.rs
+++ b/library/compiler-builtins/libm/src/math/fenv.rs
@@ -5,6 +5,9 @@ pub(crate) const FE_UNDERFLOW: i32 = 0;
 pub(crate) const FE_INEXACT: i32 = 0;
 
 pub(crate) const FE_TONEAREST: i32 = 0;
+pub(crate) const FE_DOWNWARD: i32 = 1;
+pub(crate) const FE_UPWARD: i32 = 2;
+pub(crate) const FE_TOWARDZERO: i32 = 3;
 
 #[inline]
 pub(crate) fn feclearexcept(_mask: i32) -> i32 {
@@ -25,3 +28,22 @@ pub(crate) fn fetestexcept(_mask: i32) -> i32 {
 pub(crate) fn fegetround() -> i32 {
     FE_TONEAREST
 }
+
+#[derive(Clone, Copy, Debug, PartialEq)]
+pub(crate) enum Rounding {
+    Nearest = FE_TONEAREST as isize,
+    Downward = FE_DOWNWARD as isize,
+    Upward = FE_UPWARD as isize,
+    ToZero = FE_TOWARDZERO as isize,
+}
+
+impl Rounding {
+    pub(crate) fn get() -> Self {
+        match fegetround() {
+            x if x == FE_DOWNWARD => Self::Downward,
+            x if x == FE_UPWARD => Self::Upward,
+            x if x == FE_TOWARDZERO => Self::ToZero,
+            _ => Self::Nearest,
+        }
+    }
+}