From 0337cc117d3c972b7b98e5c09212d58d3d16a009 Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Wed, 21 Aug 2019 00:37:17 +0000 Subject: [PATCH 1/3] Use more optimal Ord implementation for integers --- src/libcore/cmp.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libcore/cmp.rs b/src/libcore/cmp.rs index cb9feb074dd70..74e9ceb510a99 100644 --- a/src/libcore/cmp.rs +++ b/src/libcore/cmp.rs @@ -1012,9 +1012,9 @@ mod impls { impl Ord for $t { #[inline] fn cmp(&self, other: &$t) -> Ordering { - if *self == *other { Equal } - else if *self < *other { Less } - else { Greater } + if *self < *other { Less } + else if *self > *other { Greater } + else { Equal } } } )*) From 96983fc53009a2a2d2f93e7cec012634800be1fa Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Wed, 21 Aug 2019 06:25:37 +0000 Subject: [PATCH 2/3] Add comment to avoid accidentally remove the changes. --- src/libcore/cmp.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/libcore/cmp.rs b/src/libcore/cmp.rs index 74e9ceb510a99..167a9dd1c3620 100644 --- a/src/libcore/cmp.rs +++ b/src/libcore/cmp.rs @@ -1012,6 +1012,8 @@ mod impls { impl Ord for $t { #[inline] fn cmp(&self, other: &$t) -> Ordering { + // 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 } From f5b16f6212d2d72d505d4d6b1dedc2c9c61dd014 Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Wed, 21 Aug 2019 15:50:43 +0000 Subject: [PATCH 3/3] Add codegen test for integers compare --- src/test/codegen/integer-cmp.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 src/test/codegen/integer-cmp.rs 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) +}