Files
Basys3RiscV/tools/fpu-vectors.py
T
brad ArantandClaude Opus 5.5 71fd403294 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>
2026-11-30 20:29:08 -08:00

93 lines
3.9 KiB
Python
Executable File

#!/usr/bin/env python3
"""Test vectors for rtl/rv32_fpu.v from tools/f32.py: every F instruction,
every rounding mode, special values, subnormals, boundaries and near-
cancellations. One line per case, hex: ins rm a b c result flags.
tools/fpu-vectors.py COUNT [SEED] > vectors.txt
"""
import os
import random
import sys
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import f32
SPECIAL = [0, 0x80000000, 0x7F800000, 0xFF800000, 0x7FC00000, 0xFFC00000, 0x7F800001,
0x7FBFFFFF, 0x00000001, 0x80000001, 0x007FFFFF, 0x807FFFFF, 0x00800000,
0x80800000, 0x7F7FFFFF, 0xFF7FFFFF, 0x3F800000, 0xBF800000, 0x3F000000,
0x4B000000, 0x4F000000, 0xCF000000, 0x4F800000, 0x3EFFFFFF, 0x3FC00000]
def rnd():
k = random.random()
if k < 0.12:
return random.choice(SPECIAL)
if k < 0.32:
e = random.choice([0, 1, 2, 23, 24, 25, 100, 125, 126, 127, 128, 150, 157, 158, 159, 253, 254])
return (random.getrandbits(1) << 31) | (e << 23) | random.choice(
[random.getrandbits(23), 0, 0x7FFFFF, 1, 0x400000, random.getrandbits(4)])
if k < 0.55:
e = random.randint(110, 145)
return (random.getrandbits(1) << 31) | (e << 23) | random.getrandbits(23)
return random.getrandbits(32)
def enc_op(f5, f3, rs2=0):
return (f5 << 27) | (rs2 << 20) | (f3 << 12) | 0x53
def main():
n = int(sys.argv[1])
random.seed(int(sys.argv[2]) if len(sys.argv) > 2 else 1)
kinds = (['fma'] * 6 + ['add'] * 4 + ['sub'] * 3 + ['mul'] * 3 + ['div'] * 3 + ['sqrt'] * 2 +
['sgnj', 'minmax', 'cmp', 'class', 'mvxw', 'mvwx', 'cvtw', 'cvtw', 'cvts', 'cvts'])
for _ in range(n):
k = random.choice(kinds)
rm = random.randint(0, 4)
f3 = rm
a, b, c = rnd(), rnd(), rnd()
if k == 'fma':
opc = random.choice([0x43, 0x47, 0x4B, 0x4F])
ins = (opc | (f3 << 12))
if random.random() < 0.4: # near cancellation
p, _ = f32.mul(a, b, rm)
if not f32.is_nan(p):
want_neg = bool(opc & 8) == bool(opc & 4) # makes c cancel the product
c = (p ^ (0x80000000 if want_neg else 0)) + random.choice([0, 0, 1, -1, 2, 0x80, -0x1000])
c &= 0xFFFFFFFF
elif k in ('add', 'sub', 'mul', 'div', 'sqrt'):
f5 = {'add': 0, 'sub': 1, 'mul': 2, 'div': 3, 'sqrt': 0x0B}[k]
ins = enc_op(f5, f3)
if k in ('add', 'sub') and random.random() < 0.35:
b = (a ^ (0x80000000 if k == 'add' else 0)) + random.choice([0, 1, -1, 3, 0x40, 0x800000, -0x800000])
b &= 0xFFFFFFFF
elif k == 'sgnj':
f3 = random.randint(0, 2); ins = enc_op(0x04, f3)
elif k == 'minmax':
f3 = random.randint(0, 1); ins = enc_op(0x05, f3)
if random.random() < 0.3: b = a ^ random.choice([0, 0x80000000, 1])
elif k == 'cmp':
f3 = random.randint(0, 2); ins = enc_op(0x14, f3)
if random.random() < 0.3: b = a ^ random.choice([0, 0x80000000, 1])
elif k == 'class':
f3 = 1; ins = enc_op(0x1C, 1)
elif k == 'mvxw':
f3 = 0; ins = enc_op(0x1C, 0)
elif k == 'mvwx':
f3 = 0; ins = enc_op(0x1E, 0)
elif k == 'cvtw':
u = random.randint(0, 1); ins = enc_op(0x18, f3, u)
if random.random() < 0.5:
e = random.randint(120, 160)
a = (random.getrandbits(1) << 31) | (e << 23) | random.getrandbits(23)
else:
u = random.randint(0, 1); ins = enc_op(0x1A, f3, u)
if random.random() < 0.5:
a = random.getrandbits(random.randint(1, 32))
if random.random() < 0.3: a = (-a) & 0xFFFFFFFF
r, fl, _ = f32.execute(ins, rm, a, b, c)
print('%08x %x %08x %08x %08x %08x %02x' % (ins, rm, a, b, c, r, fl))
if __name__ == '__main__':
main()