From: Joel Sing Date: Wed, 5 Nov 2025 14:05:29 +0000 (+1100) Subject: internal/runtime/sys,math/bits: eliminate bounds checks on len8tab X-Git-Tag: go1.26rc1~337 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=bc5ffe5c79d45447648a012465e158f29ff5efa2;p=gostls13.git 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 Reviewed-by: Keith Randall Reviewed-by: Keith Randall Auto-Submit: Keith Randall Reviewed-by: Junyang Shao Auto-Submit: Joel Sing LUCI-TryBot-Result: Go LUCI --- diff --git a/src/internal/runtime/sys/intrinsics.go b/src/internal/runtime/sys/intrinsics.go index 147d5581f2..69609192ce 100644 --- a/src/internal/runtime/sys/intrinsics.go +++ b/src/internal/runtime/sys/intrinsics.go @@ -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 --- diff --git a/src/math/bits/bits.go b/src/math/bits/bits.go index 76ed1d03fc..6f9142f91a 100644 --- a/src/math/bits/bits.go +++ b/src/math/bits/bits.go @@ -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 ---