-
Notifications
You must be signed in to change notification settings - Fork 9
Update documentation and add StatusAnd::unwrap()
#15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
//! Port of LLVM's APFloat software floating-point implementation from the | ||
//! following C++ sources (please update commit hash when backporting): | ||
//! https://github.com/llvm/llvm-project/commit/462a31f5a5abb905869ea93cc49b096079b11aa4 | ||
//! <https://github.com/llvm/llvm-project/commit/462a31f5a5abb905869ea93cc49b096079b11aa4> | ||
//! * `llvm/include/llvm/ADT/APFloat.h` -> `Float` and `FloatConvert` traits | ||
//! * `llvm/lib/Support/APFloat.cpp` -> `ieee` and `ppc` modules | ||
//! * `llvm/unittests/ADT/APFloatTest.cpp` -> `tests` directory | ||
|
@@ -64,6 +64,7 @@ bitflags! { | |
} | ||
} | ||
|
||
/// The result of a computation consisting of the output value and the exceptions, if any. | ||
#[must_use] | ||
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug)] | ||
pub struct StatusAnd<T> { | ||
|
@@ -72,12 +73,14 @@ pub struct StatusAnd<T> { | |
} | ||
|
||
impl Status { | ||
/// Add a value to this status to create a [`StatusAnd`]. | ||
pub fn and<T>(self, value: T) -> StatusAnd<T> { | ||
StatusAnd { status: self, value } | ||
} | ||
} | ||
|
||
impl<T> StatusAnd<T> { | ||
/// Keep the existing status but apply a transformation to `value`. | ||
pub fn map<F: FnOnce(T) -> U, U>(self, f: F) -> StatusAnd<U> { | ||
StatusAnd { | ||
status: self.status, | ||
|
@@ -86,6 +89,14 @@ impl<T> StatusAnd<T> { | |
} | ||
} | ||
|
||
impl<T: core::fmt::Debug> StatusAnd<T> { | ||
/// Extract the inner value if there were no errors. If there were errors, panic. | ||
pub fn unwrap(self) -> T { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can't really judge the usefulness(*) of this but I guess it's fine to include in the public API. (*): I don't know the consumer base of this crate. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the main use case of this crate is testing softfloat libraries, e.g. compiler_builtins and libm. This method is just a convenience because the vast majority of the time you don't expect NaN and just want the inner value, but NaN would be an error so you should at least detect it. |
||
assert_eq!(self.status, Status::OK, "called `StatusAnd::unwrap()` on an error value. Value: {:?}", self.value); | ||
self.value | ||
} | ||
} | ||
|
||
#[macro_export] | ||
macro_rules! unpack { | ||
($status:ident|=, $e:expr) => { | ||
|
@@ -145,6 +156,7 @@ pub const IEK_INF: ExpInt = ExpInt::max_value(); | |
pub const IEK_NAN: ExpInt = ExpInt::min_value(); | ||
pub const IEK_ZERO: ExpInt = ExpInt::min_value() + 1; | ||
|
||
/// An error which can occur when parsing a floating point number from a string. | ||
#[derive(Copy, Clone, PartialEq, Eq, Debug)] | ||
pub struct ParseError(pub &'static str); | ||
|
||
|
@@ -590,6 +602,7 @@ pub trait Float: | |
} | ||
} | ||
|
||
/// Convert between floating point types. | ||
pub trait FloatConvert<T: Float>: Float { | ||
/// Convert a value of one floating point type to another. | ||
/// The return value corresponds to the IEEE754 exceptions. *loses_info | ||
|
@@ -598,6 +611,8 @@ pub trait FloatConvert<T: Float>: Float { | |
/// original value (this is almost the same as return value==Status::OK, | ||
/// but there are edge cases where this is not so). | ||
fn convert_r(self, round: Round, loses_info: &mut bool) -> StatusAnd<T>; | ||
|
||
/// Convert with default [`NearestTiesToEven`](Round::NearestTiesToEven) rounding. | ||
fn convert(self, loses_info: &mut bool) -> StatusAnd<T> { | ||
self.convert_r(Round::NearestTiesToEven, loses_info) | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.