Skip to content

Commit bd94ae8

Browse files
cuonglmgopherbot
authored andcommitted
cmd/compile: use unsigned power-of-two detector for unsigned mod
Same as CL 689815, but for modulus instead of division. Updates #74485 Change-Id: I73000231c886a987a1093669ff207fd9117a8160 Reviewed-on: https://go-review.googlesource.com/c/go/+/689895 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: David Chase <[email protected]> Auto-Submit: Cuong Manh Le <[email protected]> Reviewed-by: Keith Randall <[email protected]> Reviewed-by: Keith Randall <[email protected]>
1 parent f3582fc commit bd94ae8

File tree

3 files changed

+32
-27
lines changed

3 files changed

+32
-27
lines changed

src/cmd/compile/internal/ssa/_gen/generic.rules

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1293,11 +1293,10 @@
12931293
(Const64 <typ.UInt64> [63])))
12941294

12951295
// Unsigned mod by power of 2 constant.
1296-
(Mod8u <t> n (Const8 [c])) && isPowerOfTwo(c) => (And8 n (Const8 <t> [c-1]))
1297-
(Mod16u <t> n (Const16 [c])) && isPowerOfTwo(c) => (And16 n (Const16 <t> [c-1]))
1298-
(Mod32u <t> n (Const32 [c])) && isPowerOfTwo(c) => (And32 n (Const32 <t> [c-1]))
1299-
(Mod64u <t> n (Const64 [c])) && isPowerOfTwo(c) => (And64 n (Const64 <t> [c-1]))
1300-
(Mod64u <t> n (Const64 [-1<<63])) => (And64 n (Const64 <t> [1<<63-1]))
1296+
(Mod8u <t> n (Const8 [c])) && isUnsignedPowerOfTwo(uint8(c)) => (And8 n (Const8 <t> [c-1]))
1297+
(Mod16u <t> n (Const16 [c])) && isUnsignedPowerOfTwo(uint16(c)) => (And16 n (Const16 <t> [c-1]))
1298+
(Mod32u <t> n (Const32 [c])) && isUnsignedPowerOfTwo(uint32(c)) => (And32 n (Const32 <t> [c-1]))
1299+
(Mod64u <t> n (Const64 [c])) && isUnsignedPowerOfTwo(uint64(c)) => (And64 n (Const64 <t> [c-1]))
13011300

13021301
// Signed non-negative mod by power of 2 constant.
13031302
(Mod8 <t> n (Const8 [c])) && isNonNegative(n) && isPowerOfTwo(c) => (And8 n (Const8 <t> [c-1]))

src/cmd/compile/internal/ssa/rewritegeneric.go

Lines changed: 8 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/codegen/issue74485.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,23 @@ func divUint8(b uint8) uint8 {
2525
// amd64:"SHRB [$]7, AL"
2626
return b / 128
2727
}
28+
29+
func modUint64(b uint64) uint64 {
30+
// amd64:"BTRQ [$]63, AX"
31+
return b % 9223372036854775808
32+
}
33+
34+
func modUint32(b uint32) uint32 {
35+
// amd64:"ANDL [$]2147483647, AX"
36+
return b % 2147483648
37+
}
38+
39+
func modUint16(b uint16) uint16 {
40+
// amd64:"ANDL [$]32767, AX"
41+
return b % 32768
42+
}
43+
44+
func modUint8(b uint8) uint8 {
45+
// amd64:"ANDL [$]127, AX"
46+
return b % 128
47+
}

0 commit comments

Comments
 (0)