]> Cypherpunks repositories - gostls13.git/commitdiff
math: optimize Signbit implementation slightly
authorMichael Munday <mike.munday@lowrisc.org>
Fri, 12 Jul 2024 21:00:01 +0000 (22:00 +0100)
committerGopher Robot <gobot@golang.org>
Mon, 25 Aug 2025 19:35:20 +0000 (12:35 -0700)
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 <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Sean Liao <sean@liao.dev>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Auto-Submit: Sean Liao <sean@liao.dev>
Reviewed-by: Florian Lehner <lehner.florian86@gmail.com>
Reviewed-by: Carlos Amedee <carlos@golang.org>
src/math/signbit.go

index f6e61d660e27d9d8376ecb13c3fe8b535d0acc71..fe6b5a22af69ee026c96ef7ef9b2efba61d79572 100644 (file)
@@ -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
 }