Add a single-precision FPU (the F extension)

rtl/rv32_fpu.v: IEEE 754 single precision with all five rounding modes,
subnormals, the exception flags and the canonical NaN. Multiply-add
(DSPs, exact product, 77-bit sum) with add, subtract and multiply as its
special cases; divide and square root a bit a clock; one normalise-and-
round back end. 12 clocks for add/mul/fma, 36 for div/sqrt. About 3000
LUTs and 2 DSPs; the system still meets 90 MHz.

rv32_cpu.v: float registers, FLW/FSW, OP-FP and the R4 instructions,
fflags/frm/fcsr, mstatus.FS (Initial at reset), misa F.

tools/f32.py is an exact model (checked against x86 hardware) used by
scripts/fputest.sh (random vectors, every instruction and rounding mode)
and by tools/rv32emu.py, which now runs RV32IMF. cputest.sh adds rv32uf.

BASIC and the games are built for rv32imf/ilp32f (BASIC 36.4 -> 28.7 KB,
SIN about twice as fast); setjmp saves fs0-fs11; sqrtf is FSQRT.S. The
monitor saves and restores f0-f31 and fcsr, shows them (F), sets them
(R F1=...), and its disassembler and assembler know every F instruction
(checked against objdump and GNU as). Docs updated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
brad ArantandClaude Opus 5.5 committed 2026-11-30 20:29:08 -08:00
1 parent 19278d1d74
commit 71fd403294
31 files changed
+9143 -7638

No files matched your search

+7 -1
View File
@@ -1,7 +1,8 @@
/* Single-precision maths for BASIC: sinf cosf tanf atanf expf logf sqrtf
floorf. Range reduction by Cody and Waite, minimax polynomials as in the
Cephes library; good to an ulp or two, which BASIC's 6-7 printed digits
don't show. sqrtf and floorf are exact. (newlib's versions are larger and
don't show. sqrtf (the FPU's FSQRT.S when there is one) and floorf are
exact. (newlib's versions are larger and
bring in errno, stdio and malloc.) */
#include <math.h>
#include <stdint.h>
@@ -141,6 +142,10 @@ float logf(float x)
/* exact: one bit at a time (after fdlibm) */
float sqrtf(float x)
{
#ifdef __riscv_flen
__asm__("fsqrt.s %0, %1" : "=f"(x) : "f"(x)); /* the FPU's, correctly rounded */
return x;
#else
fbits u = { x };
int32_t ix = (int32_t)u.i, m, i;
uint32_t q, s, r, t;
@@ -167,6 +172,7 @@ float sqrtf(float x)
if (ix) q += q & 1;
u.i = (q >> 1) + 0x3F000000 + ((uint32_t)m << 23);
return u.f;
#endif
}
float floorf(float x)