cmd/compile: optimize comparisons with single bit difference

Optimize comparisons with constants that only differ by 1 bit (i.e.
a power of 2). For example:

    x == 4 || x == 6 -> x|2 == 6
    x != 1 && x != 5 -> x|4 != 5

Change-Id: Ic61719e5118446d21cf15652d9da22f7d95b2a15
Reviewed-on: https://go-review.googlesource.com/c/go/+/719420
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Junyang Shao <shaojunyang@google.com>
Auto-Submit: Keith Randall <khr@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Keith Randall <khr@google.com>
This commit is contained in:
Michael Munday 2025-08-26 21:17:36 +01:00 committed by Gopher Robot
parent 1e5e6663e9
commit 0a569528ea
5 changed files with 530 additions and 1 deletions

View file

@ -198,6 +198,126 @@ func ui4d(c <-chan uint8) {
}
}
// ------------------------------------ //
// single bit difference (conjunction) //
// ------------------------------------ //
func sisbc64(c <-chan int64) {
// amd64: "ORQ [$]2,"
// riscv64: "ORI [$]2,"
for x := <-c; x != 4 && x != 6; x = <-c {
}
}
func sisbc32(c <-chan int32) {
// amd64: "ORL [$]4,"
// riscv64: "ORI [$]4,"
for x := <-c; x != -1 && x != -5; x = <-c {
}
}
func sisbc16(c <-chan int16) {
// amd64: "ORL [$]32,"
// riscv64: "ORI [$]32,"
for x := <-c; x != 16 && x != 48; x = <-c {
}
}
func sisbc8(c <-chan int8) {
// amd64: "ORL [$]16,"
// riscv64: "ORI [$]16,"
for x := <-c; x != -15 && x != -31; x = <-c {
}
}
func uisbc64(c <-chan uint64) {
// amd64: "ORQ [$]4,"
// riscv64: "ORI [$]4,"
for x := <-c; x != 1 && x != 5; x = <-c {
}
}
func uisbc32(c <-chan uint32) {
// amd64: "ORL [$]4,"
// riscv64: "ORI [$]4,"
for x := <-c; x != 2 && x != 6; x = <-c {
}
}
func uisbc16(c <-chan uint16) {
// amd64: "ORL [$]32,"
// riscv64: "ORI [$]32,"
for x := <-c; x != 16 && x != 48; x = <-c {
}
}
func uisbc8(c <-chan uint8) {
// amd64: "ORL [$]64,"
// riscv64: "ORI [$]64,"
for x := <-c; x != 64 && x != 0; x = <-c {
}
}
// ------------------------------------ //
// single bit difference (disjunction) //
// ------------------------------------ //
func sisbd64(c <-chan int64) {
// amd64: "ORQ [$]2,"
// riscv64: "ORI [$]2,"
for x := <-c; x == 4 || x == 6; x = <-c {
}
}
func sisbd32(c <-chan int32) {
// amd64: "ORL [$]4,"
// riscv64: "ORI [$]4,"
for x := <-c; x == -1 || x == -5; x = <-c {
}
}
func sisbd16(c <-chan int16) {
// amd64: "ORL [$]32,"
// riscv64: "ORI [$]32,"
for x := <-c; x == 16 || x == 48; x = <-c {
}
}
func sisbd8(c <-chan int8) {
// amd64: "ORL [$]16,"
// riscv64: "ORI [$]16,"
for x := <-c; x == -15 || x == -31; x = <-c {
}
}
func uisbd64(c <-chan uint64) {
// amd64: "ORQ [$]4,"
// riscv64: "ORI [$]4,"
for x := <-c; x == 1 || x == 5; x = <-c {
}
}
func uisbd32(c <-chan uint32) {
// amd64: "ORL [$]4,"
// riscv64: "ORI [$]4,"
for x := <-c; x == 2 || x == 6; x = <-c {
}
}
func uisbd16(c <-chan uint16) {
// amd64: "ORL [$]32,"
// riscv64: "ORI [$]32,"
for x := <-c; x == 16 || x == 48; x = <-c {
}
}
func uisbd8(c <-chan uint8) {
// amd64: "ORL [$]64,"
// riscv64: "ORI [$]64,"
for x := <-c; x == 64 || x == 0; x = <-c {
}
}
// -------------------------------------//
// merge NaN checks //
// ------------------------------------ //