]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: make isUint{32,64}PowerOfTwo implementations clearer
authorCuong Manh Le <cuong.manhle.vn@gmail.com>
Mon, 4 Aug 2025 11:48:43 +0000 (18:48 +0700)
committerGopher Robot <gobot@golang.org>
Tue, 5 Aug 2025 15:32:51 +0000 (08:32 -0700)
Since these functions cast the input to uint64, so the result always
non-negative. The condition should be changed to comparing with zero,
thus maaking it clearer to reader, and open room for simplifying in the
future by using the generic isUnsignedPowerOfTwo function.

Separated this change, so it's easier to do bisecting if there's any
problems happened.

Change-Id: Ibec28c2590f4c52caa36384b710d526459725e49
Reviewed-on: https://go-review.googlesource.com/c/go/+/692915
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Auto-Submit: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Reviewed-by: Keith Randall <khr@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Keith Randall <khr@google.com>
src/cmd/compile/internal/ssa/rewrite.go

index f9a35deecc3d0bc4d9709f6aeb2cb1164c7bedb5..5edcc86a4265d04c582f49649b2506670e7fedfa 100644 (file)
@@ -507,13 +507,13 @@ func isUnsignedPowerOfTwo[T uint8 | uint16 | uint32 | uint64](n T) bool {
 // isUint64PowerOfTwo reports whether uint64(n) is a power of 2.
 func isUint64PowerOfTwo(in int64) bool {
        n := uint64(in)
-       return n > 0 && n&(n-1) == 0
+       return n != 0 && n&(n-1) == 0
 }
 
 // isUint32PowerOfTwo reports whether uint32(n) is a power of 2.
 func isUint32PowerOfTwo(in int64) bool {
        n := uint64(uint32(in))
-       return n > 0 && n&(n-1) == 0
+       return n != 0 && n&(n-1) == 0
 }
 
 // is32Bit reports whether n can be represented as a signed 32 bit integer.