Skip to content
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

Use BinOp::Cmp for iNN::signum #137835

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions library/core/src/intrinsics/mod.rs
Original file line number Diff line number Diff line change
@@ -2528,13 +2528,15 @@ pub const fn bswap<T: Copy>(_x: T) -> T;
#[rustc_intrinsic]
pub const fn bitreverse<T: Copy>(_x: T) -> T;

/// Does a three-way comparison between the two integer arguments.
/// Does a three-way comparison between the two arguments,
/// which must be of character or integer (signed or unsigned) type.
///
/// This is included as an intrinsic as it's useful to let it be one thing
/// in MIR, rather than the multiple checks and switches that make its IR
/// large and difficult to optimize.
/// This was originally added because it greatly simplified the MIR in `cmp`
/// implementations, and then LLVM 20 added a backend intrinsic for it too.
///
/// The stabilized version of this intrinsic is [`Ord::cmp`].
#[rustc_intrinsic_const_stable_indirect]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RalfJung, can you remind me which teams need to sign off on using this in const?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually lang.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the interpreter, this operator is implemented for integers and characters. That matches what the validator does. Seems worth mentioning in the doc comment that char is supported.

Other than that, the implementation looks good and doesn't do anything odd, so seems fine to stabilize. Cc @rust-lang/wg-const-eval

#[rustc_nounwind]
#[rustc_intrinsic]
pub const fn three_way_compare<T: Copy>(_lhs: T, _rhss: T) -> crate::cmp::Ordering;

5 changes: 1 addition & 4 deletions library/core/src/num/int_macros.rs
Original file line number Diff line number Diff line change
@@ -3571,10 +3571,7 @@ macro_rules! int_impl {
// so delegate it to `Ord` which is already producing -1/0/+1
// exactly like we need and can be the place to deal with the complexity.

// FIXME(const-hack): replace with cmp
if self < 0 { -1 }
else if self == 0 { 0 }
else { 1 }
crate::intrinsics::three_way_compare(self, 0) as Self
}

/// Returns `true` if `self` is positive and `false` if the number is zero or
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@
_4 = copy _1;
StorageLive(_5);
_5 = copy _2;
- _3 = three_way_compare::<char>(move _4, move _5) -> [return: bb1, unwind continue];
- _3 = three_way_compare::<char>(move _4, move _5) -> [return: bb1, unwind unreachable];
+ _3 = Cmp(move _4, move _5);
+ goto -> bb1;
}
Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@
_4 = copy _1;
StorageLive(_5);
_5 = copy _2;
- _3 = three_way_compare::<i16>(move _4, move _5) -> [return: bb1, unwind continue];
- _3 = three_way_compare::<i16>(move _4, move _5) -> [return: bb1, unwind unreachable];
+ _3 = Cmp(move _4, move _5);
+ goto -> bb1;
}
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@
_4 = copy _1;
StorageLive(_5);
_5 = copy _2;
- _3 = three_way_compare::<u32>(move _4, move _5) -> [return: bb1, unwind continue];
- _3 = three_way_compare::<u32>(move _4, move _5) -> [return: bb1, unwind unreachable];
+ _3 = Cmp(move _4, move _5);
+ goto -> bb1;
}
Loading