@@ -638,6 +638,22 @@ impl PartialOrd for Ordering {
638
638
fn partial_cmp ( & self , other : & Ordering ) -> Option < Ordering > {
639
639
( * self as i32 ) . partial_cmp ( & ( * other as i32 ) )
640
640
}
641
+ #[ inline]
642
+ fn lt ( & self , other : & Ordering ) -> bool {
643
+ ( * self as i32 ) . lt ( & ( * other as i32 ) )
644
+ }
645
+ #[ inline]
646
+ fn le ( & self , other : & Ordering ) -> bool {
647
+ ( * self as i32 ) . le ( & ( * other as i32 ) )
648
+ }
649
+ #[ inline]
650
+ fn gt ( & self , other : & Ordering ) -> bool {
651
+ ( * self as i32 ) . gt ( & ( * other as i32 ) )
652
+ }
653
+ #[ inline]
654
+ fn ge ( & self , other : & Ordering ) -> bool {
655
+ ( * self as i32 ) . ge ( & ( * other as i32 ) )
656
+ }
641
657
}
642
658
643
659
/// Trait for values that can be compared for a sort-order.
@@ -1012,11 +1028,14 @@ mod impls {
1012
1028
impl Ord for $t {
1013
1029
#[ inline]
1014
1030
fn cmp( & self , other: & $t) -> Ordering {
1015
- // The order here is important to generate more optimal assembly.
1016
- // See <https://github.com/rust-lang/rust/issues/63758> for more info.
1017
- if * self < * other { Less }
1018
- else if * self == * other { Equal }
1019
- else { Greater }
1031
+ // Like in `signum`, this approach allows the result to be
1032
+ // computed with a simpler subtraction instead of needing a
1033
+ // conditional move (which, as of 2018 x86 hardware, are never
1034
+ // predicted), for a small cycles & code size improvement.
1035
+ // See <https://github.com/rust-lang/rust/pull/64082> for more info.
1036
+ let diff = ( * self > * other) as i8 - ( * self < * other) as i8 ;
1037
+ // Sound because Ordering's three variants are {-1, 0, 1}.
1038
+ unsafe { crate :: mem:: transmute( diff) }
1020
1039
}
1021
1040
}
1022
1041
) * )
0 commit comments