Skip to content

Commit 9089e95

Browse files
committed
libm: Improve debug output for Status
1 parent a07474d commit 9089e95

File tree

1 file changed

+42
-5
lines changed

1 file changed

+42
-5
lines changed

libm/src/math/support/env.rs

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
//! side effects from elementary operations). Full support would require wrappers around basic
1212
//! operations, but there is no plan to add this at the current time.
1313
14+
use core::fmt;
15+
1416
/// A value combined with a floating point status.
1517
pub struct FpResult<T> {
1618
pub val: T,
@@ -49,7 +51,7 @@ pub enum Round {
4951
}
5052

5153
/// IEEE 754 exception status flags.
52-
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
54+
#[derive(Clone, Copy, PartialEq, Eq)]
5355
pub struct Status(u8);
5456

5557
impl Status {
@@ -74,22 +76,22 @@ impl Status {
7476
/// result is -inf.
7577
/// `x / y` when `x != 0.0` and `y == 0.0`,
7678
#[cfg_attr(not(feature = "unstable-public-internals"), allow(dead_code))]
77-
pub const DIVIDE_BY_ZERO: Self = Self(1 << 2);
79+
pub const DIVIDE_BY_ZERO: Self = Self(1 << 1);
7880

7981
/// The result exceeds the maximum finite value.
8082
///
8183
/// The default result depends on rounding mode. `Nearest*` rounds to +/- infinity, sign based
8284
/// on the intermediate result. `Zero` rounds to the signed maximum finite. `Positive` and
8385
/// `Negative` round to signed maximum finite in one direction, signed infinity in the other.
8486
#[cfg_attr(not(feature = "unstable-public-internals"), allow(dead_code))]
85-
pub const OVERFLOW: Self = Self(1 << 3);
87+
pub const OVERFLOW: Self = Self(1 << 2);
8688

8789
/// The result is subnormal and lost precision.
88-
pub const UNDERFLOW: Self = Self(1 << 4);
90+
pub const UNDERFLOW: Self = Self(1 << 3);
8991

9092
/// The finite-precision result does not match that of infinite precision, and the reason
9193
/// is not represented by one of the other flags.
92-
pub const INEXACT: Self = Self(1 << 5);
94+
pub const INEXACT: Self = Self(1 << 4);
9395

9496
/// True if `UNDERFLOW` is set.
9597
#[cfg_attr(not(feature = "unstable-public-internals"), allow(dead_code))]
@@ -128,3 +130,38 @@ impl Status {
128130
Self(self.0 | rhs.0)
129131
}
130132
}
133+
134+
#[cfg(any(test, feature = "unstable-public-internals"))]
135+
impl fmt::Debug for Status {
136+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
137+
// shift -> flag map
138+
let names = &[
139+
"INVALID",
140+
"DIVIDE_BY_ZERO",
141+
"OVERFLOW",
142+
"UNDERFLOW",
143+
"INEXACT",
144+
];
145+
146+
write!(f, "Status(")?;
147+
let mut any = false;
148+
for shift in 0..u8::BITS {
149+
if self.0 & (1 << shift) != 0 {
150+
if any {
151+
write!(f, " | ")?;
152+
}
153+
match names.get(shift as usize) {
154+
Some(name) => write!(f, "{name}")?,
155+
None => write!(f, "UNKNOWN(1 << {shift})")?,
156+
}
157+
any = true;
158+
}
159+
}
160+
161+
if !any {
162+
write!(f, "OK")?;
163+
}
164+
write!(f, ")")?;
165+
Ok(())
166+
}
167+
}

0 commit comments

Comments
 (0)