]> Cypherpunks repositories - gostls13.git/commitdiff
internal/runtime/sys,math/bits: eliminate bounds checks on len8tab
authorJoel Sing <joel@sing.id.au>
Wed, 5 Nov 2025 14:05:29 +0000 (01:05 +1100)
committerGopher Robot <gobot@golang.org>
Mon, 10 Nov 2025 17:48:20 +0000 (09:48 -0800)
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>

src/internal/runtime/sys/intrinsics.go
src/math/bits/bits.go

index 147d5581f254f32e2060d2ec947f024ae194fe48..69609192ce9830634cfc9285c64770ebd66b69aa 100644 (file)
@@ -109,7 +109,7 @@ func Len64(x uint64) (n int) {
                x >>= 8
                n += 8
        }
-       return n + int(len8tab[x])
+       return n + int(len8tab[uint8(x)])
 }
 
 // --- OnesCount ---
index 76ed1d03fc416bd356b6db9d9938fbdee9a717c4..6f9142f91a38e30869d71cf8cca779c39cd39f82 100644 (file)
@@ -317,7 +317,7 @@ func Len16(x uint16) (n int) {
                x >>= 8
                n = 8
        }
-       return n + int(len8tab[x])
+       return n + int(len8tab[uint8(x)])
 }
 
 // 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) {
                x >>= 8
                n += 8
        }
-       return n + int(len8tab[x])
+       return n + int(len8tab[uint8(x)])
 }
 
 // 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) {
                x >>= 8
                n += 8
        }
-       return n + int(len8tab[x])
+       return n + int(len8tab[uint8(x)])
 }
 
 // --- Add with carry ---