pub struct FpResult<T> {
pub val: T,
#[cfg_attr(not(feature = "unstable-public-internals"), allow(dead_code))]
pub status: Status,
}
impl<T> FpResult<T> {
pub fn new(val: T, status: Status) -> Self {
Self { val, status }
}
pub fn ok(val: T) -> Self {
Self {
val,
status: Status::OK,
}
}
}
#[cfg_attr(not(feature = "unstable-public-internals"), allow(dead_code))]
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Round {
Nearest = 0,
Negative = 1,
Positive = 2,
Zero = 3,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Status(u8);
impl Status {
pub const OK: Self = Self(0);
pub const INVALID: Self = Self(1);
#[cfg_attr(not(feature = "unstable-public-internals"), allow(dead_code))]
pub const DIVIDE_BY_ZERO: Self = Self(1 << 2);
#[cfg_attr(not(feature = "unstable-public-internals"), allow(dead_code))]
pub const OVERFLOW: Self = Self(1 << 3);
pub const UNDERFLOW: Self = Self(1 << 4);
pub const INEXACT: Self = Self(1 << 5);
#[cfg_attr(not(feature = "unstable-public-internals"), allow(dead_code))]
pub const fn underflow(self) -> bool {
self.0 & Self::UNDERFLOW.0 != 0
}
#[cfg_attr(not(feature = "unstable-public-internals"), allow(dead_code))]
pub const fn overflow(self) -> bool {
self.0 & Self::OVERFLOW.0 != 0
}
pub fn set_underflow(&mut self, val: bool) {
self.set_flag(val, Self::UNDERFLOW);
}
pub const fn inexact(self) -> bool {
self.0 & Self::INEXACT.0 != 0
}
pub fn set_inexact(&mut self, val: bool) {
self.set_flag(val, Self::INEXACT);
}
fn set_flag(&mut self, val: bool, mask: Self) {
if val {
self.0 |= mask.0;
} else {
self.0 &= !mask.0;
}
}
pub(crate) const fn with(self, rhs: Self) -> Self {
Self(self.0 | rhs.0)
}
}