From 1eed4f32a06ac1083120663a8451cb9cfeb7b496 Mon Sep 17 00:00:00 2001 From: Michael Munday Date: Fri, 12 Jul 2024 22:00:01 +0100 Subject: [PATCH] math: optimize Signbit implementation slightly MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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 --- src/math/signbit.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 } -- 2.52.0