feat(riscv): add RISC-V RV64A, FP and CSR instruction support

Assisted-by: Kimi K3
This commit is contained in:
2026-08-02 11:45:00 +02:00
parent 801fb963c9
commit eee7a6d4a4
6 changed files with 455 additions and 0 deletions
+20
View File
@@ -0,0 +1,20 @@
// Copyright (c) 2026 Petr Balvín <opensource@petrbalvin.org> (https://petrbalvin.org)
// SPDX-License-Identifier: BSD-3-Clause
#include "textflag.h"
// func atomicAdd(ptr *int64, val int64) int64
TEXT ·atomicAdd(SB), NOSPLIT, $0-24
LD a+0(FP), A0
LD b+8(FP), A1
AMOADDD A1, (A0), A2
SD A2, ret+16(FP)
RET
// func fpAdd(a, b float64) float64
TEXT ·fpAdd(SB), NOSPLIT, $0-24
FLD a+0(FP), FA0
FLD b+8(FP), FA1
FADDD FA0, FA1, FA2
FSD FA2, ret+16(FP)
RET
+27
View File
@@ -0,0 +1,27 @@
// Copyright (c) 2026 Petr Balvín <opensource@petrbalvin.org> (https://petrbalvin.org)
// SPDX-License-Identifier: BSD-3-Clause
#include "textflag.h"
// func readCSR(csr int64) int64
// Reads a CSR into the return value.
TEXT ·readCSR(SB), NOSPLIT, $0-16
LD a+0(FP), A0
CSRRS $0x300, X0, A1
SD A1, ret+8(FP)
RET
// func setCSRBit(csr, bit int64) int64
TEXT ·setCSRBit(SB), NOSPLIT, $0-24
LD a+0(FP), A0
LD b+8(FP), A1
CSRRS $0x304, A1, A2
SD A2, ret+16(FP)
RET
// func writeCSR(val int64) int64
TEXT ·writeCSR(SB), NOSPLIT, $0-16
LD a+0(FP), A0
CSRRW $0x305, A0, A1
SD A1, ret+8(FP)
RET
+24
View File
@@ -0,0 +1,24 @@
// Copyright (c) 2026 Petr Balvín <opensource@petrbalvin.org> (https://petrbalvin.org)
// SPDX-License-Identifier: BSD-3-Clause
#include "textflag.h"
// func fma(a, b, c float64) float64
// Computes a*b + c using fused multiply-add.
TEXT ·fma(SB), NOSPLIT, $0-32
FLD a+0(FP), FA0
FLD b+8(FP), FA1
FLD c+16(FP), FA2
FMADDD FA0, FA1, FA2, FA3
FSD FA3, ret+24(FP)
RET
// func fms(a, b, c float64) float64
// Computes a*b - c using fused multiply-subtract.
TEXT ·fms(SB), NOSPLIT, $0-32
FLD a+0(FP), FA0
FLD b+8(FP), FA1
FLD c+16(FP), FA2
FMSUBD FA0, FA1, FA2, FA3
FSD FA3, ret+24(FP)
RET
+37
View File
@@ -0,0 +1,37 @@
// Copyright (c) 2026 Petr Balvín <opensource@petrbalvin.org> (https://petrbalvin.org)
// SPDX-License-Identifier: BSD-3-Clause
#include "textflag.h"
// func casLoop(ptr *int64, old, new int64) bool
// Compare-and-swap using LR/SC.
TEXT ·casLoop(SB), NOSPLIT, $0-32
cas_retry:
LD a+0(FP), A0
LRD (A0), A1
LD b+8(FP), A2
BNE A1, A2, cas_fail
LD c+16(FP), A3
SCD A3, (A0), A4
BNE A4, X0, cas_retry
ADDI X0, $1, A5
SD A5, ret+24(FP)
RET
cas_fail:
SD X0, ret+24(FP)
RET
// func intToFloat(x int64) float64
TEXT ·intToFloat(SB), NOSPLIT, $0-16
LD a+0(FP), A0
FCVTDL A0, FA0
FSD FA0, ret+8(FP)
RET
// func compare(a, b float64) bool
TEXT ·compare(SB), NOSPLIT, $0-24
FLD a+0(FP), FA0
FLD b+8(FP), FA1
FLTD FA0, FA1, A0
SD A0, ret+16(FP)
RET