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/cmd/internal/obj/arm64/asm7.go | 27 +-------------------------- 1 file changed, 1 insertion(+), 26 deletions(-) (limited to 'src/cmd/internal/obj') diff --git a/src/cmd/internal/obj/arm64/asm7.go b/src/cmd/internal/obj/arm64/asm7.go index ccf8eda495..7addf0ddad 100644 --- a/src/cmd/internal/obj/arm64/asm7.go +++ b/src/cmd/internal/obj/arm64/asm7.go @@ -1674,32 +1674,7 @@ func log2(x uint64) uint32 { if x == 0 { panic("log2 of 0") } - n := uint32(0) - if x >= 1<<32 { - x >>= 32 - n += 32 - } - if x >= 1<<16 { - x >>= 16 - n += 16 - } - if x >= 1<<8 { - x >>= 8 - n += 8 - } - if x >= 1<<4 { - x >>= 4 - n += 4 - } - if x >= 1<<2 { - x >>= 2 - n += 2 - } - if x >= 1<<1 { - x >>= 1 - n += 1 - } - return n + return uint32(bits.Len64(x) - 1) } func autoclass(l int64) int { -- cgit v1.3