]> Cypherpunks repositories - gostls13.git/commitdiff
math: add round assembly implementations on riscv64
authorMeng Zhuo <mzh@golangcn.org>
Tue, 20 Jun 2023 12:43:11 +0000 (20:43 +0800)
committerM Zhuo <mzh@golangcn.org>
Fri, 23 Feb 2024 08:34:12 +0000 (08:34 +0000)
goos: linux
goarch: riscv64
pkg: math
            │ floor_old.bench │           floor_new.bench           │
            │     sec/op      │   sec/op     vs base                │
Ceil              54.12n ± 0%   22.05n ± 0%  -59.26% (p=0.000 n=10)
Floor             40.80n ± 0%   22.05n ± 0%  -45.96% (p=0.000 n=10)
Round             20.73n ± 0%   20.74n ± 0%        ~ (p=0.441 n=10)
RoundToEven       24.07n ± 0%   24.07n ± 0%        ~ (p=1.000 n=10)
Trunc             38.73n ± 0%   22.05n ± 0%  -43.07% (p=0.000 n=10)
geomean           33.58n        22.17n       -33.98%

Change-Id: I24fb9e3bbf8146da253b6791b21377bea1afbd16
Reviewed-on: https://go-review.googlesource.com/c/go/+/504737
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
Reviewed-by: M Zhuo <mengzhuo1203@gmail.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Run-TryBot: M Zhuo <mengzhuo1203@gmail.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: David Chase <drchase@google.com>
Reviewed-by: Joel Sing <joel@sing.id.au>
src/math/floor_asm.go
src/math/floor_noasm.go
src/math/floor_riscv64.s [new file with mode: 0644]

index fb419d6da2f6bfc98fcf8d12a82b39dc987f86d0..5cb45f5a7e84e16f1e08884892c95293fd131d79 100644 (file)
@@ -2,7 +2,7 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-//go:build 386 || amd64 || arm64 || ppc64 || ppc64le || s390x || wasm
+//go:build 386 || amd64 || arm64 || ppc64 || ppc64le || riscv64 || s390x || wasm
 
 package math
 
index 5641c7ea0a49caf563cf496e2be234612dd0d2c8..6754ca8fc806e18cba71cd55c913175468472fad 100644 (file)
@@ -2,7 +2,7 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-//go:build !386 && !amd64 && !arm64 && !ppc64 && !ppc64le && !s390x && !wasm
+//go:build !386 && !amd64 && !arm64 && !ppc64 && !ppc64le && !riscv64 && !s390x && !wasm
 
 package math
 
diff --git a/src/math/floor_riscv64.s b/src/math/floor_riscv64.s
new file mode 100644 (file)
index 0000000..62ce963
--- /dev/null
@@ -0,0 +1,41 @@
+// Copyright 2023 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+#include "textflag.h"
+
+#define PosInf 0x7FF0000000000000
+
+// The rounding mode of RISC-V is different from Go spec.
+
+#define ROUNDFN(NAME, MODE)    \
+TEXT NAME(SB),NOSPLIT,$0;      \
+       MOVD    x+0(FP), F0;    \
+       /* whether x is NaN */; \
+       FEQD    F0, F0, X6;     \
+       BNEZ    X6, 3(PC);      \
+       /* return NaN if x is NaN */; \
+       MOVD    F0, ret+8(FP);  \
+       RET;                    \
+       MOV     $PosInf, X6;    \
+       FMVDX   X6, F1;         \
+       FABSD   F0, F2;         \
+       /* if abs(x) > +Inf, return Inf instead of round(x) */; \
+       FLTD    F1, F2, X6;     \
+       /* Inf should keep same signed with x then return */;   \
+       BEQZ    X6, 3(PC); \
+       FCVTLD.MODE     F0, X6; \
+       FCVTDL  X6, F1;         \
+       /* rounding will drop signed bit in RISCV, restore it */; \
+       FSGNJD  F0, F1, F0;     \
+       MOVD    F0, ret+8(FP);  \
+       RET
+
+// func archFloor(x float64) float64
+ROUNDFN(·archFloor, RDN)
+
+// func archCeil(x float64) float64
+ROUNDFN(·archCeil, RUP)
+
+// func archTrunc(x float64) float64
+ROUNDFN(·archTrunc, RTZ)