Skip to content

Commit 7b62e98

Browse files
committed
runtime: always mask shift amount regardless of architecture
Currently the shift amount is only masked on x86. Change it so it is masked on all architectures. In the worst case we generate a couple of extra instructions to perform the masking and in the best case we can elide overflow checks. This particular shift could also be replaced with a rotate instruction during optimization which would remove both the masking instructions and overflow checks on all architectures. Fixes #31165. Change-Id: I16b7a8800b4ba8813dc83735dfc59564e661d3b4 Reviewed-on: https://go-review.googlesource.com/c/go/+/170122 Run-TryBot: Michael Munday <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Keith Randall <[email protected]>
1 parent e6ad619 commit 7b62e98

File tree

1 file changed

+2
-4
lines changed

1 file changed

+2
-4
lines changed

src/runtime/map.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,8 @@ type hiter struct {
181181

182182
// bucketShift returns 1<<b, optimized for code generation.
183183
func bucketShift(b uint8) uintptr {
184-
if sys.GoarchAmd64|sys.GoarchAmd64p32|sys.Goarch386 != 0 {
185-
b &= sys.PtrSize*8 - 1 // help x86 archs remove shift overflow checks
186-
}
187-
return uintptr(1) << b
184+
// Masking the shift amount allows overflow checks to be elided.
185+
return uintptr(1) << (b & (sys.PtrSize*8 - 1))
188186
}
189187

190188
// bucketMask returns 1<<b - 1, optimized for code generation.

0 commit comments

Comments
 (0)