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.
This commit is contained in:
Trevor Gross 2025-01-25 00:29:04 +00:00
parent f45cc66e8e
commit f1afc26b8a

View file

@ -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,
}
}
}