Skip to content
This repository was archived by the owner on Apr 28, 2025. It is now read-only.

Commit 68bfe1d

Browse files
committed
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.
1 parent e66ec88 commit 68bfe1d

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/math/fenv.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ pub(crate) const FE_UNDERFLOW: i32 = 0;
55
pub(crate) const FE_INEXACT: i32 = 0;
66

77
pub(crate) const FE_TONEAREST: i32 = 0;
8+
pub(crate) const FE_DOWNWARD: i32 = 1;
9+
pub(crate) const FE_UPWARD: i32 = 2;
10+
pub(crate) const FE_TOWARDZERO: i32 = 3;
811

912
#[inline]
1013
pub(crate) fn feclearexcept(_mask: i32) -> i32 {
@@ -25,3 +28,22 @@ pub(crate) fn fetestexcept(_mask: i32) -> i32 {
2528
pub(crate) fn fegetround() -> i32 {
2629
FE_TONEAREST
2730
}
31+
32+
#[derive(Clone, Copy, Debug, PartialEq)]
33+
pub(crate) enum Rounding {
34+
Nearest = FE_TONEAREST as isize,
35+
Downward = FE_DOWNWARD as isize,
36+
Upward = FE_UPWARD as isize,
37+
ToZero = FE_TOWARDZERO as isize,
38+
}
39+
40+
impl Rounding {
41+
pub(crate) fn get() -> Self {
42+
match fegetround() {
43+
x if x == FE_DOWNWARD => Self::Downward,
44+
x if x == FE_UPWARD => Self::Upward,
45+
x if x == FE_TOWARDZERO => Self::ToZero,
46+
_ => Self::Nearest,
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)