Skip to content

cmd/compile: optimize int % powerOfTwo != 0 #34166

@rillig

Description

@rillig

What version of Go are you using?

go version go1.12.7 windows/amd64

What did you do?

func bitTestArith(i int) int {
	if i%2 != 0 {
		return 1
	}
	return 0
}

func bitTestBinary(i int) int {
	if i&1 != 0 {
		return 1
	}
	return 0
}

What did you expect to see?

The two conditions in the if statements compile to the same code.

What did you see instead?

The first condition is compiled to:

movq     0x8(sp), ax
movq     ax, cx
shrq     $0x3f, ax
addq     cx, ax
sarq     $0x1, ax
shlq     $0x1, ax
cmpq     ax, cx
je       0x1b9dbb (line 13, varalignblock.go:713)

The second condition is compiled to:

movq     0x8(sp), ax
btl      $0x0, ax
jae      0x1b9df1 (line 24, varalignblock.go:720)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions