aboutsummaryrefslogtreecommitdiff
path: root/src/cmp/cmp.go
diff options
context:
space:
mode:
authorkhr@golang.org <khr@golang.org>2024-04-13 19:21:15 -0700
committerKeith Randall <khr@google.com>2024-04-19 16:31:02 +0000
commit1a0b86375fad202048adb88cba4caec535a52a45 (patch)
tree3ba0daeb3bbb608d528e895cb14fd3a860520595 /src/cmp/cmp.go
parentd428a63875d335d1e1b9a3b3b45ad58f46e1e6bc (diff)
downloadgo-1a0b86375fad202048adb88cba4caec535a52a45.tar.gz
go-1a0b86375fad202048adb88cba4caec535a52a45.zip
cmd/compile: remove redundant calls to cmpstring
The results of cmpstring are reuseable if the second call has the same arguments and memory. Note that this gets rid of cmpstring, but we still generate a redundant </<= test and branch afterwards, because the compiler doesn't know that cmpstring only ever returns -1,0,1. Update #61725 Change-Id: I93a0d1ccca50d90b1e1a888240ffb75a3b10b59b Reviewed-on: https://go-review.googlesource.com/c/go/+/578835 Reviewed-by: David Chase <drchase@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Cherry Mui <cherryyz@google.com>
Diffstat (limited to 'src/cmp/cmp.go')
-rw-r--r--src/cmp/cmp.go14
1 files changed, 10 insertions, 4 deletions
diff --git a/src/cmp/cmp.go b/src/cmp/cmp.go
index 4d1af6a98c4..a13834c3985 100644
--- a/src/cmp/cmp.go
+++ b/src/cmp/cmp.go
@@ -40,13 +40,19 @@ func Less[T Ordered](x, y T) bool {
func Compare[T Ordered](x, y T) int {
xNaN := isNaN(x)
yNaN := isNaN(y)
- if xNaN && yNaN {
- return 0
+ if xNaN {
+ if yNaN {
+ return 0
+ }
+ return -1
+ }
+ if yNaN {
+ return +1
}
- if xNaN || x < y {
+ if x < y {
return -1
}
- if yNaN || x > y {
+ if x > y {
return +1
}
return 0