-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Optimise floating point is_finite
(2x) and is_infinite
(1.6x).
#57353
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # 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 |
---|---|---|
|
@@ -161,6 +161,14 @@ impl f32 { | |
self != self | ||
} | ||
|
||
// FIXME(#50145): `abs` is publicly unavailable in libcore due to | ||
// concerns about portability, so this implementation is for | ||
// private use internally. | ||
#[inline] | ||
fn abs_private(self) -> f32 { | ||
f32::from_bits(self.to_bits() & 0x7fff_ffff) | ||
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. Why is this necessary, rather than using the existing 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. That's only available in 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'm bikeshedding here, but given typical naming conventions, I think it'd make sense to simply call this // FIXME(#50145): `abs` is publicly unavailable in libcore due to concerns
// about portability, so this implementation is for private use internally. 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 agree about adding a comment, but I don't think that naming it 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. How about 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. Added that. |
||
} | ||
|
||
/// Returns `true` if this value is positive infinity or negative infinity and | ||
/// false otherwise. | ||
/// | ||
|
@@ -181,7 +189,7 @@ impl f32 { | |
#[stable(feature = "rust1", since = "1.0.0")] | ||
#[inline] | ||
pub fn is_infinite(self) -> bool { | ||
self == INFINITY || self == NEG_INFINITY | ||
self.abs_private() == INFINITY | ||
} | ||
|
||
/// Returns `true` if this number is neither infinite nor `NaN`. | ||
|
@@ -203,7 +211,9 @@ impl f32 { | |
#[stable(feature = "rust1", since = "1.0.0")] | ||
#[inline] | ||
pub fn is_finite(self) -> bool { | ||
!(self.is_nan() || self.is_infinite()) | ||
// There's no need to handle NaN separately: if self is NaN, | ||
// the comparison is not true, exactly as desired. | ||
self.abs_private() < INFINITY | ||
} | ||
|
||
/// Returns `true` if the number is neither zero, infinite, | ||
|
Uh oh!
There was an error while loading. Please reload this page.