diff --git a/src/libcore/cmp.rs b/src/libcore/cmp.rs index cb9feb074dd70..167a9dd1c3620 100644 --- a/src/libcore/cmp.rs +++ b/src/libcore/cmp.rs @@ -1012,9 +1012,11 @@ mod impls { impl Ord for $t { #[inline] fn cmp(&self, other: &$t) -> Ordering { - if *self == *other { Equal } - else if *self < *other { Less } - else { Greater } + // The order here is important to generate more optimal assembly. + // See for more info. + if *self < *other { Less } + else if *self > *other { Greater } + else { Equal } } } )*) diff --git a/src/test/codegen/integer-cmp.rs b/src/test/codegen/integer-cmp.rs new file mode 100644 index 0000000000000..1373b12e3721c --- /dev/null +++ b/src/test/codegen/integer-cmp.rs @@ -0,0 +1,28 @@ +// This is test for more optimal Ord implementation for integers. +// See for more info. + +// compile-flags: -C opt-level=3 + +#![crate_type = "lib"] + +use std::cmp::Ordering; + +// CHECK-LABEL: @cmp_signed +#[no_mangle] +pub fn cmp_signed(a: i64, b: i64) -> Ordering { +// CHECK: icmp slt +// CHECK: icmp sgt +// CHECK: zext i1 +// CHECK: select i1 + a.cmp(&b) +} + +// CHECK-LABEL: @cmp_unsigned +#[no_mangle] +pub fn cmp_unsigned(a: u32, b: u32) -> Ordering { +// CHECK: icmp ult +// CHECK: icmp ugt +// CHECK: zext i1 +// CHECK: select i1 + a.cmp(&b) +}