Skip to content

Commit

Permalink
math: add round assembly implementations on riscv64
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-by: Russ Cox <[email protected]>
Reviewed-by: M Zhuo <[email protected]>
Reviewed-by: Cherry Mui <[email protected]>
Run-TryBot: M Zhuo <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>
Reviewed-by: David Chase <[email protected]>
Reviewed-by: Joel Sing <[email protected]>
  • Loading branch information
mengzhuo committed Feb 23, 2024
1 parent 27e104b commit ad377e9
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/math/floor_asm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/math/floor_noasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
41 changes: 41 additions & 0 deletions src/math/floor_riscv64.s
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit ad377e9

Please sign in to comment.