-
-
Notifications
You must be signed in to change notification settings - Fork 15k
Add extra unchecked_disjoint_bitor implementations.
#147368
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
base: main
Are you sure you want to change the base?
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,8 @@ | ||||||||||
| //! impl bool {} | ||||||||||
|
|
||||||||||
| use crate::intrinsics; | ||||||||||
| use crate::marker::Destruct; | ||||||||||
| use crate::ub_checks::assert_unsafe_precondition; | ||||||||||
|
|
||||||||||
| impl bool { | ||||||||||
| /// Returns `Some(t)` if the `bool` is [`true`](../std/keyword.true.html), | ||||||||||
|
|
@@ -137,4 +139,44 @@ impl bool { | |||||||||
| ) -> Result<(), E> { | ||||||||||
| if self { Ok(()) } else { Err(f()) } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /// Disjoint, bitwise or. Computes `self | rhs`, assuming inequality. | ||||||||||
| /// | ||||||||||
| /// Practically, this requires that `self | rhs` and `self ^ rhs` both yield the | ||||||||||
| /// same result, allowing for any of the two to be emitted in code gen -- depending | ||||||||||
| /// on whichever is cheapest. | ||||||||||
| /// | ||||||||||
| /// # Examples | ||||||||||
| /// | ||||||||||
| /// ``` | ||||||||||
| /// #![feature(disjoint_bitor)] | ||||||||||
| /// | ||||||||||
| /// assert_eq!( | ||||||||||
| /// // SAFETY: `false` and `true` are inequal. | ||||||||||
|
Contributor
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.
Suggested change
As above.
Contributor
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 actually requested this move away from talking about bits with booleans, since I don't think users should need to think about the representation when thinking about values is sufficient. But looks like I gave a bad suggestion here. So perhaps "has only one
Contributor
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. Adding a
Contributor
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.
Suggested change
|
||||||||||
| /// unsafe { false.unchecked_disjoint_bitor(true) }, | ||||||||||
| /// true, | ||||||||||
| /// ); | ||||||||||
| /// ``` | ||||||||||
| /// | ||||||||||
| /// # Safety | ||||||||||
| /// | ||||||||||
| /// This results in undefined behaviour if `self` and `rhs` are equal. | ||||||||||
|
Contributor
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.
Suggested change
As above. Nit: We generally use American spellings. |
||||||||||
| #[unstable(feature = "disjoint_bitor", issue = "135758")] | ||||||||||
| #[rustc_const_unstable(feature = "disjoint_bitor", issue = "135758")] | ||||||||||
| #[must_use = "this returns the result of the operation, \ | ||||||||||
| without modifying the original"] | ||||||||||
| #[inline] | ||||||||||
| pub const unsafe fn unchecked_disjoint_bitor(self, rhs: Self) -> Self { | ||||||||||
| assert_unsafe_precondition!( | ||||||||||
| check_language_ub, | ||||||||||
| "bool::unchecked_disjoint_bitor cannot bitor equal values", | ||||||||||
|
Contributor
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.
Suggested change
As above.
Contributor
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. Similarly, maybe "more than one
Contributor
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.
Suggested change
|
||||||||||
| ( | ||||||||||
| lhs: bool = self, | ||||||||||
| rhs: bool = rhs, | ||||||||||
| ) => lhs != rhs, | ||||||||||
|
Contributor
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.
Suggested change
Right? As above, I'd expect |
||||||||||
| ); | ||||||||||
|
|
||||||||||
| // SAFETY: Same precondition. | ||||||||||
| unsafe { intrinsics::disjoint_bitor(self, rhs) } | ||||||||||
| } | ||||||||||
| } | ||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1671,48 +1671,45 @@ macro_rules! uint_impl { | |
| self % rhs | ||
| } | ||
|
|
||
| /// Same value as `self | other`, but UB if any bit position is set in both inputs. | ||
| /// Disjoint, bitwise or. Computes `self | rhs`, assuming no one bits in common. | ||
|
Contributor
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. "assuming no one bits in common" -> "assuming there are no one bits in common." Or something like "Computes |
||
| /// | ||
| /// This is a situational micro-optimization for places where you'd rather | ||
| /// use addition on some platforms and bitwise or on other platforms, based | ||
| /// on exactly which instructions combine better with whatever else you're | ||
| /// doing. Note that there's no reason to bother using this for places | ||
| /// where it's clear from the operations involved that they can't overlap. | ||
| /// For example, if you're combining `u16`s into a `u32` with | ||
| /// `((a as u32) << 16) | (b as u32)`, that's fine, as the backend will | ||
| /// know those sides of the `|` are disjoint without needing help. | ||
| /// Practically, this requires that `self | rhs`, `self ^ rhs`, and `self + rhs` all | ||
| /// yield the same result, allowing for any of the three to be emitted in code gen | ||
|
Contributor
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. Nit: "code gen" -> either "codegen" or "code generation"
Contributor
Author
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. What is wrong with "code gen" ? It's already used in the repo: https://github.com/search?q=repo%3Arust-lang%2Frust%20%22code%20gen%22&type=code.
Contributor
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. Most of those hits are part of "code generation" https://github.com/search?q=repo%3Arust-lang%2Frust+%2Fcode+gen%5Cb%2F&type=code. Loosely I think "gen" is considered an abbreviation rather than a word that can stand on its own, but we do consider "codegen" to be one. Also we have |
||
| /// -- depending on whichever is cheapest. | ||
| /// | ||
| /// # Examples | ||
| /// | ||
| /// ``` | ||
| /// #![feature(disjoint_bitor)] | ||
| /// | ||
| /// // SAFETY: `1` and `4` have no bits in common. | ||
| /// unsafe { | ||
| #[doc = concat!(" assert_eq!(1_", stringify!($SelfT), ".unchecked_disjoint_bitor(4), 5);")] | ||
| /// } | ||
| /// assert_eq!( | ||
| /// // SAFETY: `1` and `4` have no ones in common. | ||
| #[doc = concat!(" unsafe { 1_", stringify!($SelfT), ".unchecked_disjoint_bitor(4) },")] | ||
| /// 5, | ||
| /// ); | ||
| /// ``` | ||
| /// | ||
| /// # Safety | ||
| /// | ||
| /// Requires that `(self & other) == 0`, otherwise it's immediate UB. | ||
| /// | ||
| /// Equivalently, requires that `(self | other) == (self + other)`. | ||
| /// This results in undefined behaviour if `self` and `rhs` are not fully disjoint, | ||
| /// i.e. if `self & rhs == 0` doesn't apply. | ||
| #[unstable(feature = "disjoint_bitor", issue = "135758")] | ||
| #[rustc_const_unstable(feature = "disjoint_bitor", issue = "135758")] | ||
| #[must_use = "this returns the result of the operation, \ | ||
| without modifying the original"] | ||
| #[inline] | ||
| pub const unsafe fn unchecked_disjoint_bitor(self, other: Self) -> Self { | ||
| pub const unsafe fn unchecked_disjoint_bitor(self, rhs: Self) -> Self { | ||
| assert_unsafe_precondition!( | ||
| check_language_ub, | ||
| concat!(stringify!($SelfT), "::unchecked_disjoint_bitor cannot have overlapping bits"), | ||
| concat!(stringify!($SelfT), "::unchecked_disjoint_bitor cannot bitor overlapping ones"), | ||
| ( | ||
| lhs: $SelfT = self, | ||
| rhs: $SelfT = other, | ||
| rhs: $SelfT = rhs, | ||
| ) => (lhs & rhs) == 0, | ||
| ); | ||
|
|
||
| // SAFETY: Same precondition | ||
| unsafe { intrinsics::disjoint_bitor(self, other) } | ||
| // SAFETY: Same precondition. | ||
| unsafe { intrinsics::disjoint_bitor(self, rhs) } | ||
| } | ||
|
|
||
| /// Returns the logarithm of the number with respect to an arbitrary base, | ||
|
|
@@ -3684,7 +3681,6 @@ macro_rules! uint_impl { | |
| self / rhs | ||
| } | ||
|
|
||
|
|
||
| /// Calculates the least remainder of `self` when divided by | ||
| /// `rhs`. | ||
| /// | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, or no? I'd expect
(false, false)to be valid.View changes since the review