diff --git a/src/math/floor_asm.go b/src/math/floor_asm.go index fb419d6da2f..5cb45f5a7e8 100644 --- a/src/math/floor_asm.go +++ b/src/math/floor_asm.go @@ -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_noasm.go b/src/math/floor_noasm.go index 5641c7ea0a4..6754ca8fc80 100644 --- a/src/math/floor_noasm.go +++ b/src/math/floor_noasm.go @@ -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 index 00000000000..d9fe0ed8e28 --- /dev/null +++ b/src/math/floor_riscv64.s @@ -0,0 +1,48 @@ +// Copyright 2024 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" + +// RISC-V offered floating-point (FP) rounding by FP conversion instructions (FCVT) +// with rounding mode field. +// As Go spec expects FP rounding result in FP, we have to use FCVT integer +// back to FP (fp -> int -> fp). +// RISC-V only set Inexact flag during invalid FP-integer conversion without changing any data, +// on the other hand, RISC-V sets out of integer represent range yet valid FP into NaN. +// When it comes to integer-FP conversion, invalid FP like NaN, +-Inf will be +// converted into the closest valid FP, for example: +// +// `Floor(-Inf) -> int64(0x7fffffffffffffff) -> float64(9.22e+18)` +// `Floor(18446744073709549568.0) -> int64(0x7fffffffffffffff) -> float64(9.22e+18)` +// +// This ISA conversion limitation requires we skip all invalid or out of range FP +// before any normal rounding operations. + +#define ROUNDFN(NAME, MODE) \ +TEXT NAME(SB),NOSPLIT,$0; \ + MOVD x+0(FP), F10; \ + FMVXD F10, X10; \ + /* Drop all fraction bits */;\ + SRL $52, X10, X12; \ + /* Remove sign bit */; \ + AND $0x7FF, X12, X12;\ + /* Return either input is +-Inf, NaN(0x7FF) or out of precision limitation */;\ + /* 1023: bias of exponent, [-2^53, 2^53]: exactly integer represent range */;\ + MOV $1023+53, X11; \ + BLTU X11, X12, 4(PC);\ + FCVTLD.MODE F10, X11; \ + FCVTDL X11, F11; \ + /* RISC-V rounds negative values to +0, restore original sign */;\ + FSGNJD F10, F11, F10; \ + MOVD F10, 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)