Skip to content

Commit bc5ffe5

Browse files
4a6f656cgopherbot
authored andcommitted
internal/runtime/sys,math/bits: eliminate bounds checks on len8tab
The compiler cannot currently determine that the accesses to len8tab are within bounds. Cast to uint8 to avoid unnecessary bounds checks. Fixes #76166 Change-Id: I1fd930bba2b20d3998252c476308642e08ce00b4 Reviewed-on: https://go-review.googlesource.com/c/go/+/718040 Reviewed-by: Meng Zhuo <mengzhuo1203@gmail.com> Reviewed-by: Keith Randall <khr@google.com> Reviewed-by: Keith Randall <khr@golang.org> Auto-Submit: Keith Randall <khr@golang.org> Reviewed-by: Junyang Shao <shaojunyang@google.com> Auto-Submit: Joel Sing <joel@sing.id.au> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
1 parent 32f8d64 commit bc5ffe5

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

src/internal/runtime/sys/intrinsics.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func Len64(x uint64) (n int) {
109109
x >>= 8
110110
n += 8
111111
}
112-
return n + int(len8tab[x])
112+
return n + int(len8tab[uint8(x)])
113113
}
114114

115115
// --- OnesCount ---

src/math/bits/bits.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ func Len16(x uint16) (n int) {
317317
x >>= 8
318318
n = 8
319319
}
320-
return n + int(len8tab[x])
320+
return n + int(len8tab[uint8(x)])
321321
}
322322

323323
// Len32 returns the minimum number of bits required to represent x; the result is 0 for x == 0.
@@ -330,7 +330,7 @@ func Len32(x uint32) (n int) {
330330
x >>= 8
331331
n += 8
332332
}
333-
return n + int(len8tab[x])
333+
return n + int(len8tab[uint8(x)])
334334
}
335335

336336
// Len64 returns the minimum number of bits required to represent x; the result is 0 for x == 0.
@@ -347,7 +347,7 @@ func Len64(x uint64) (n int) {
347347
x >>= 8
348348
n += 8
349349
}
350-
return n + int(len8tab[x])
350+
return n + int(len8tab[uint8(x)])
351351
}
352352

353353
// --- Add with carry ---

0 commit comments

Comments
 (0)