Skip to content

Commit 680e216

Browse files
committed
Use partial_cmp to implement tuple lt/le/ge/gt
1 parent dc37e37 commit 680e216

File tree

2 files changed

+40
-15
lines changed

2 files changed

+40
-15
lines changed

library/core/src/tuple.rs

+35-13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// See src/libstd/primitive_docs.rs for documentation.
22

3-
use crate::cmp::Ordering::*;
4-
use crate::cmp::*;
3+
use crate::cmp::Ordering::{self, *};
4+
use crate::mem::transmute;
55

66
// Recursive macro for implementing n-ary tuple functions and operations
77
//
@@ -61,19 +61,19 @@ macro_rules! tuple_impls {
6161
}
6262
#[inline]
6363
fn lt(&self, other: &($($T,)+)) -> bool {
64-
lexical_ord!(lt, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
64+
lexical_ord!(lt, Less, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
6565
}
6666
#[inline]
6767
fn le(&self, other: &($($T,)+)) -> bool {
68-
lexical_ord!(le, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
68+
lexical_ord!(le, Less, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
6969
}
7070
#[inline]
7171
fn ge(&self, other: &($($T,)+)) -> bool {
72-
lexical_ord!(ge, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
72+
lexical_ord!(ge, Greater, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
7373
}
7474
#[inline]
7575
fn gt(&self, other: &($($T,)+)) -> bool {
76-
lexical_ord!(gt, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
76+
lexical_ord!(gt, Greater, $( ${ignore(T)} self.${index()}, other.${index()} ),+)
7777
}
7878
}
7979
}
@@ -123,16 +123,38 @@ macro_rules! maybe_tuple_doc {
123123
};
124124
}
125125

126-
// Constructs an expression that performs a lexical ordering using method $rel.
126+
#[inline]
127+
const fn ordering_is_some(c: Option<Ordering>, x: Ordering) -> bool {
128+
// FIXME: Just use `==` once that's const-stable on `Option`s.
129+
// This isn't using `match` because that optimizes worse due to
130+
// making a two-step check (`Some` *then* the inner value).
131+
132+
// SAFETY: There's no public guarantee for `Option<Ordering>`,
133+
// but we're core so we know that it's definitely a byte.
134+
unsafe {
135+
let c: i8 = transmute(c);
136+
let x: i8 = transmute(Some(x));
137+
c == x
138+
}
139+
}
140+
141+
// Constructs an expression that performs a lexical ordering using method `$rel`.
127142
// The values are interleaved, so the macro invocation for
128-
// `(a1, a2, a3) < (b1, b2, b3)` would be `lexical_ord!(lt, a1, b1, a2, b2,
129-
// a3, b3)` (and similarly for `lexical_cmp`)
143+
// `(a1, a2, a3) < (b1, b2, b3)` would be `lexical_ord!(lt, opt_is_lt, a1, b1,
144+
// a2, b2, a3, b3)` (and similarly for `lexical_cmp`)
145+
//
146+
// `$ne_rel` is only used to determine the result after checking that they're
147+
// not equal, so `lt` and `le` can both just use `Less`.
130148
macro_rules! lexical_ord {
131-
($rel: ident, $a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {
132-
if $a != $b { lexical_ord!($rel, $a, $b) }
133-
else { lexical_ord!($rel, $($rest_a, $rest_b),+) }
149+
($rel: ident, $ne_rel: ident, $a:expr, $b:expr, $($rest_a:expr, $rest_b:expr),+) => {{
150+
let c = PartialOrd::partial_cmp(&$a, &$b);
151+
if !ordering_is_some(c, Equal) { ordering_is_some(c, $ne_rel) }
152+
else { lexical_ord!($rel, $ne_rel, $($rest_a, $rest_b),+) }
153+
}};
154+
($rel: ident, $ne_rel: ident, $a:expr, $b:expr) => {
155+
// Use the specific method for the last element
156+
PartialOrd::$rel(&$a, &$b)
134157
};
135-
($rel: ident, $a:expr, $b:expr) => { ($a) . $rel (& $b) };
136158
}
137159

138160
macro_rules! lexical_partial_cmp {

tests/codegen/comparison-operators-2-tuple.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ type TwoTuple = (i16, u16);
1111
//
1212
// The operators are all overridden directly, so should optimize easily.
1313
//
14+
// Yes, the `s[lg]t` is correct for the `[lg]e` version because it's only used
15+
// in the side of the select where we know the values are *not* equal.
16+
//
1417

1518
// CHECK-LABEL: @check_lt_direct
1619
// CHECK-SAME: (i16 noundef %[[A0:.+]], i16 noundef %[[A1:.+]], i16 noundef %[[B0:.+]], i16 noundef %[[B1:.+]])
@@ -29,7 +32,7 @@ pub fn check_lt_direct(a: TwoTuple, b: TwoTuple) -> bool {
2932
#[no_mangle]
3033
pub fn check_le_direct(a: TwoTuple, b: TwoTuple) -> bool {
3134
// CHECK-DAG: %[[EQ:.+]] = icmp eq i16 %[[A0]], %[[B0]]
32-
// CHECK-DAG: %[[CMP0:.+]] = icmp sle i16 %[[A0]], %[[B0]]
35+
// CHECK-DAG: %[[CMP0:.+]] = icmp slt i16 %[[A0]], %[[B0]]
3336
// CHECK-DAG: %[[CMP1:.+]] = icmp ule i16 %[[A1]], %[[B1]]
3437
// CHECK: %[[R:.+]] = select i1 %[[EQ]], i1 %[[CMP1]], i1 %[[CMP0]]
3538
// CHECK: ret i1 %[[R]]
@@ -53,7 +56,7 @@ pub fn check_gt_direct(a: TwoTuple, b: TwoTuple) -> bool {
5356
#[no_mangle]
5457
pub fn check_ge_direct(a: TwoTuple, b: TwoTuple) -> bool {
5558
// CHECK-DAG: %[[EQ:.+]] = icmp eq i16 %[[A0]], %[[B0]]
56-
// CHECK-DAG: %[[CMP0:.+]] = icmp sge i16 %[[A0]], %[[B0]]
59+
// CHECK-DAG: %[[CMP0:.+]] = icmp sgt i16 %[[A0]], %[[B0]]
5760
// CHECK-DAG: %[[CMP1:.+]] = icmp uge i16 %[[A1]], %[[B1]]
5861
// CHECK: %[[R:.+]] = select i1 %[[EQ]], i1 %[[CMP1]], i1 %[[CMP0]]
5962
// CHECK: ret i1 %[[R]]

0 commit comments

Comments
 (0)