]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: removing log2uint32 function
authorCuong Manh Le <cuong.manhle.vn@gmail.com>
Tue, 29 Jul 2025 16:55:20 +0000 (23:55 +0700)
committerGopher Robot <gobot@golang.org>
Tue, 29 Jul 2025 23:22:48 +0000 (16:22 -0700)
Just using isUnsignedPowerOfTwo and log32u is enough.

Change-Id: I93d49ab71c6245d05f6507adbcb9ef2a696e75d6
Reviewed-on: https://go-review.googlesource.com/c/go/+/691476
Reviewed-by: Keith Randall <khr@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Reviewed-by: David Chase <drchase@google.com>
src/cmd/compile/internal/ssa/_gen/MIPS.rules
src/cmd/compile/internal/ssa/rewrite.go
src/cmd/compile/internal/ssa/rewriteMIPS.go

index a9bac5fabe1b72fe368b12a8e7c1ad4e269961b0..d417bafa1db08126df44ca9fe2f349a63d25ca60 100644 (file)
 (Select0 (MULTU (MOVWconst [1])  _ )) => (MOVWconst [0])
 (Select1 (MULTU (MOVWconst [-1]) x )) => (NEG <x.Type> x)
 (Select0 (MULTU (MOVWconst [-1]) x )) => (CMOVZ (ADDconst <x.Type> [-1] x) (MOVWconst [0]) x)
-(Select1 (MULTU (MOVWconst [c])  x )) && isPowerOfTwo(int64(uint32(c))) => (SLLconst [int32(log2uint32(int64(c)))] x)
-(Select0 (MULTU (MOVWconst [c])  x )) && isPowerOfTwo(int64(uint32(c))) => (SRLconst [int32(32-log2uint32(int64(c)))] x)
+(Select1 (MULTU (MOVWconst [c])  x )) && isUnsignedPowerOfTwo(uint32(c)) => (SLLconst [int32(log32u(uint32(c)))] x)
+(Select0 (MULTU (MOVWconst [c])  x )) && isUnsignedPowerOfTwo(uint32(c)) => (SRLconst [int32(32-log32u(uint32(c)))] x)
 
 (MUL (MOVWconst [0])  _ ) => (MOVWconst [0])
 (MUL (MOVWconst [1])  x ) => x
 (MUL (MOVWconst [-1]) x ) => (NEG x)
-(MUL (MOVWconst [c]) x ) && isPowerOfTwo(int64(uint32(c))) => (SLLconst [int32(log2uint32(int64(c)))] x)
+(MUL (MOVWconst [c]) x ) && isUnsignedPowerOfTwo(uint32(c)) => (SLLconst [int32(log32u(uint32(c)))] x)
 
 // generic simplifications
 (ADD x (NEG y)) => (SUB x y)
index 6229992bd1ff6df2a20420bff33a556479052437..f6bd4cee5754075665b10f763db3fde6b2438879 100644 (file)
@@ -492,12 +492,6 @@ func log16u(n uint16) int64 { return int64(bits.Len16(n)) - 1 }
 func log32u(n uint32) int64 { return int64(bits.Len32(n)) - 1 }
 func log64u(n uint64) int64 { return int64(bits.Len64(n)) - 1 }
 
-// log2uint32 returns logarithm in base 2 of uint32(n), with log2(0) = -1.
-// Rounds down.
-func log2uint32(n int64) int64 {
-       return int64(bits.Len32(uint32(n))) - 1
-}
-
 // isPowerOfTwoX functions report whether n is a power of 2.
 func isPowerOfTwo[T int8 | int16 | int32 | int64](n T) bool {
        return n > 0 && n&(n-1) == 0
index 4c5edb8694d28499401c1f0515c6745a22757f96..97847f082389d8dfadf38f2ff499738c0a67738b 100644 (file)
@@ -4058,8 +4058,8 @@ func rewriteValueMIPS_OpMIPSMUL(v *Value) bool {
                break
        }
        // match: (MUL (MOVWconst [c]) x )
-       // cond: isPowerOfTwo(int64(uint32(c)))
-       // result: (SLLconst [int32(log2uint32(int64(c)))] x)
+       // cond: isUnsignedPowerOfTwo(uint32(c))
+       // result: (SLLconst [int32(log32u(uint32(c)))] x)
        for {
                for _i0 := 0; _i0 <= 1; _i0, v_0, v_1 = _i0+1, v_1, v_0 {
                        if v_0.Op != OpMIPSMOVWconst {
@@ -4067,11 +4067,11 @@ func rewriteValueMIPS_OpMIPSMUL(v *Value) bool {
                        }
                        c := auxIntToInt32(v_0.AuxInt)
                        x := v_1
-                       if !(isPowerOfTwo(int64(uint32(c)))) {
+                       if !(isUnsignedPowerOfTwo(uint32(c))) {
                                continue
                        }
                        v.reset(OpMIPSSLLconst)
-                       v.AuxInt = int32ToAuxInt(int32(log2uint32(int64(c))))
+                       v.AuxInt = int32ToAuxInt(int32(log32u(uint32(c))))
                        v.AddArg(x)
                        return true
                }
@@ -6611,8 +6611,8 @@ func rewriteValueMIPS_OpSelect0(v *Value) bool {
                break
        }
        // match: (Select0 (MULTU (MOVWconst [c]) x ))
-       // cond: isPowerOfTwo(int64(uint32(c)))
-       // result: (SRLconst [int32(32-log2uint32(int64(c)))] x)
+       // cond: isUnsignedPowerOfTwo(uint32(c))
+       // result: (SRLconst [int32(32-log32u(uint32(c)))] x)
        for {
                if v_0.Op != OpMIPSMULTU {
                        break
@@ -6626,11 +6626,11 @@ func rewriteValueMIPS_OpSelect0(v *Value) bool {
                        }
                        c := auxIntToInt32(v_0_0.AuxInt)
                        x := v_0_1
-                       if !(isPowerOfTwo(int64(uint32(c)))) {
+                       if !(isUnsignedPowerOfTwo(uint32(c))) {
                                continue
                        }
                        v.reset(OpMIPSSRLconst)
-                       v.AuxInt = int32ToAuxInt(int32(32 - log2uint32(int64(c))))
+                       v.AuxInt = int32ToAuxInt(int32(32 - log32u(uint32(c))))
                        v.AddArg(x)
                        return true
                }
@@ -6807,8 +6807,8 @@ func rewriteValueMIPS_OpSelect1(v *Value) bool {
                break
        }
        // match: (Select1 (MULTU (MOVWconst [c]) x ))
-       // cond: isPowerOfTwo(int64(uint32(c)))
-       // result: (SLLconst [int32(log2uint32(int64(c)))] x)
+       // cond: isUnsignedPowerOfTwo(uint32(c))
+       // result: (SLLconst [int32(log32u(uint32(c)))] x)
        for {
                if v_0.Op != OpMIPSMULTU {
                        break
@@ -6822,11 +6822,11 @@ func rewriteValueMIPS_OpSelect1(v *Value) bool {
                        }
                        c := auxIntToInt32(v_0_0.AuxInt)
                        x := v_0_1
-                       if !(isPowerOfTwo(int64(uint32(c)))) {
+                       if !(isUnsignedPowerOfTwo(uint32(c))) {
                                continue
                        }
                        v.reset(OpMIPSSLLconst)
-                       v.AuxInt = int32ToAuxInt(int32(log2uint32(int64(c))))
+                       v.AuxInt = int32ToAuxInt(int32(log32u(uint32(c))))
                        v.AddArg(x)
                        return true
                }