From: Michael Munday Date: Fri, 12 Jul 2024 21:00:01 +0000 (+0100) Subject: math: optimize Signbit implementation slightly X-Git-Tag: go1.26rc1~1022 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=1eed4f32a06ac1083120663a8451cb9cfeb7b496;p=gostls13.git math: optimize Signbit implementation slightly This small tweak to Signbit improves code generation on riscv64 and possibly other architectures by removing the need to apply a 64 bit mask. Before: MOV $-9223372036854775808, X6 AND X6, X5, X5 SNEZ X5, X10 After: SLTI $0, X5, X10 This transformation could also be added to the optimization rules but it is quite a special case. goos: linux goarch: riscv64 pkg: math cpu: Spacemit(R) X60 │ sec/op │ sec/op vs base │ Signbit 13.05n ± 0% 11.42n ± 0% -12.49% (p=0.000 n=10) Change-Id: Ic218017c5bbb720ec24c6fe7cc230df539b2630c Reviewed-on: https://go-review.googlesource.com/c/go/+/698419 LUCI-TryBot-Result: Go LUCI Reviewed-by: Sean Liao Reviewed-by: Cherry Mui Auto-Submit: Sean Liao Reviewed-by: Florian Lehner Reviewed-by: Carlos Amedee --- diff --git a/src/math/signbit.go b/src/math/signbit.go index f6e61d660e..fe6b5a22af 100644 --- a/src/math/signbit.go +++ b/src/math/signbit.go @@ -6,5 +6,5 @@ package math // Signbit reports whether x is negative or negative zero. func Signbit(x float64) bool { - return Float64bits(x)&(1<<63) != 0 + return int64(Float64bits(x)) < 0 }