Skip to content

Commit ad377e9

Browse files
committed
math: add round assembly implementations on riscv64
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>
1 parent 27e104b commit ad377e9

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

src/math/floor_asm.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
//go:build 386 || amd64 || arm64 || ppc64 || ppc64le || s390x || wasm
5+
//go:build 386 || amd64 || arm64 || ppc64 || ppc64le || riscv64 || s390x || wasm
66

77
package math
88

src/math/floor_noasm.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
//go:build !386 && !amd64 && !arm64 && !ppc64 && !ppc64le && !s390x && !wasm
5+
//go:build !386 && !amd64 && !arm64 && !ppc64 && !ppc64le && !riscv64 && !s390x && !wasm
66

77
package math
88

src/math/floor_riscv64.s

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright 2023 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
#include "textflag.h"
6+
7+
#define PosInf 0x7FF0000000000000
8+
9+
// The rounding mode of RISC-V is different from Go spec.
10+
11+
#define ROUNDFN(NAME, MODE) \
12+
TEXT NAME(SB),NOSPLIT,$0; \
13+
MOVD x+0(FP), F0; \
14+
/* whether x is NaN */; \
15+
FEQD F0, F0, X6; \
16+
BNEZ X6, 3(PC); \
17+
/* return NaN if x is NaN */; \
18+
MOVD F0, ret+8(FP); \
19+
RET; \
20+
MOV $PosInf, X6; \
21+
FMVDX X6, F1; \
22+
FABSD F0, F2; \
23+
/* if abs(x) > +Inf, return Inf instead of round(x) */; \
24+
FLTD F1, F2, X6; \
25+
/* Inf should keep same signed with x then return */; \
26+
BEQZ X6, 3(PC); \
27+
FCVTLD.MODE F0, X6; \
28+
FCVTDL X6, F1; \
29+
/* rounding will drop signed bit in RISCV, restore it */; \
30+
FSGNJD F0, F1, F0; \
31+
MOVD F0, ret+8(FP); \
32+
RET
33+
34+
// func archFloor(x float64) float64
35+
ROUNDFN(·archFloor, RDN)
36+
37+
// func archCeil(x float64) float64
38+
ROUNDFN(·archCeil, RUP)
39+
40+
// func archTrunc(x float64) float64
41+
ROUNDFN(·archTrunc, RTZ)

0 commit comments

Comments
 (0)