From a18294bb6aa8f0dd656f2bcb6742f9de2936d0dc Mon Sep 17 00:00:00 2001 From: Axel Wagner Date: Wed, 19 Nov 2025 09:28:16 +0100 Subject: cmd/internal/obj/arm64, image/gif, runtime, sort: use math/bits to calculate log2 In several places the integer log2 is calculated using loops or similar mechanisms. math/bits.Len* provide a simpler and more efficient mechanisms for this. Annoyingly, every usage has slightly different ideas of what "log2" means and how non-positive inputs should be handled. I verified the replacements in each case by comparing the result for inputs from 0 to 1<<16. Change-Id: Ie962a74674802da363e0038d34c06979ccb41cf3 Reviewed-on: https://go-review.googlesource.com/c/go/+/721880 Reviewed-by: Mark Freeman LUCI-TryBot-Result: Go LUCI Reviewed-by: Michael Knyszek --- src/runtime/stack.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'src/runtime/stack.go') diff --git a/src/runtime/stack.go b/src/runtime/stack.go index 55e97e77af..c92accf188 100644 --- a/src/runtime/stack.go +++ b/src/runtime/stack.go @@ -12,6 +12,7 @@ import ( "internal/runtime/atomic" "internal/runtime/gc" "internal/runtime/sys" + "math/bits" "unsafe" ) @@ -181,12 +182,10 @@ func stackinit() { // stacklog2 returns ⌊log_2(n)⌋. func stacklog2(n uintptr) int { - log2 := 0 - for n > 1 { - n >>= 1 - log2++ + if n == 0 { + return 0 } - return log2 + return bits.Len64(uint64(n)) } // Allocates a stack from the free pool. Must be called with -- cgit v1.3