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

+106 -18
View File
@@ -1,12 +1,16 @@
`timescale 1ns / 1ps
// RV32IM processor, machine mode only.
// RV32IMF processor, machine mode only.
//
// A multi-cycle core with one instruction in flight. Each instruction is
// decoded in one cycle (D) and executed in the next (E); the decode cycle
// fetches the following word, so straight-line code takes 2 clocks per
// instruction. Taken branches and jumps take 3, loads and stores 3 (more if
// the bus waits), multiplies 4 and divides 35.
// the bus waits), multiplies 4 and divides 35. Floating-point operations run
// in rv32_fpu.v: 4 clocks for moves, sign injection, compares, min/max and
// class, 5 for float -> int, 8 for int -> float, 12 for add, subtract,
// multiply and the multiply-adds (4 if an operand is infinite, NaN or zero),
// 36 for divide and square root; float loads and stores as integer ones.
//
// Instruction port: synchronous read. i_rdata is the word at the i_addr given
// in the last cycle with i_en high, and must hold while i_en is low.
@@ -28,7 +32,9 @@
// Speed: 90 MHz with 1.5 ns to spare on the XC7A35T-1 (bench/), about 2100
// LUTs, 900 flip-flops and 4 DSP slices.
//
// Implemented: RV32I, M, Zicsr, Zicntr (cycle, time = cycle, instret), Zifencei,
// Implemented: RV32I, M, F (rv32_fpu.v; fflags, frm, fcsr; mstatus.FS, which
// resets to Initial: F instructions and CSRs are illegal while it is Off,
// and any F instruction or F CSR write makes it Dirty), Zicsr, Zicntr (cycle, time = cycle, instret), Zifencei,
// mret and wfi (a no-op). Machine-mode CSRs: mstatus (MIE, MPIE; MPP reads 3),
// misa, mie, mip, mtvec (direct mode), mscratch, mepc, mcause, mtval, mcycle,
// minstret, mhartid etc. (read 0), and two Sdtrig match triggers (tselect,
@@ -74,9 +80,9 @@ module rv32_cpu #(
output dbg_decode, // high in an instruction's first cycle
output reg retire // pulse: an instruction completed
);
localparam S_IF = 3'd0, S_D = 3'd1, S_E = 3'd2, S_MEM = 3'd3,
S_MUL = 3'd4, S_DIV = 3'd5, S_DIVF = 3'd6, S_XT = 3'd7;
reg [2:0] state;
localparam S_IF = 4'd0, S_D = 4'd1, S_E = 4'd2, S_MEM = 4'd3,
S_MUL = 4'd4, S_DIV = 4'd5, S_DIVF = 4'd6, S_XT = 4'd7, S_FP = 4'd8;
reg [3:0] state;
reg [31:0] pc;
assign i_en = state == S_IF || state == S_D;
@@ -94,6 +100,15 @@ module rv32_cpu #(
reg [31:0] rf_wd;
always @(posedge clk) if (rf_we && rf_wa != 5'd0) rf[rf_wa] <= rf_wd;
// Floating-point registers: distributed RAM, three read ports.
reg [31:0] frf [0:31];
initial for (k = 0; k < 32; k = k + 1) frf[k] = 32'd0;
reg frf_we;
reg [31:0] frf_wd;
reg [1:0] fs; // mstatus.FS
reg [4:0] fflags;
reg [2:0] frm;
// ------------------------------------------------------------------
// Decode (state D): inst is the fetched word.
wire [31:0] inst = i_rdata;
@@ -103,6 +118,8 @@ module rv32_cpu #(
wire [4:0] rs1 = inst[19:15], rs2 = inst[24:20];
wire [31:0] rv1 = rs1 == 5'd0 ? 32'd0 : rf[rs1];
wire [31:0] rv2 = rs2 == 5'd0 ? 32'd0 : rf[rs2];
wire [4:0] rs3 = inst[31:27];
wire [31:0] fv1 = frf[rs1], fv2 = frf[rs2], fv3 = frf[rs3];
wire [31:0] imm_i = {{20{inst[31]}}, inst[31:20]};
wire [31:0] imm_s = {{20{inst[31]}}, inst[31:25], inst[11:7]};
@@ -133,9 +150,31 @@ module rv32_cpu #(
wire is_mret = inst == 32'h3020_0073;
wire is_wfi = inst == 32'h1050_0073;
wire is_mnret = inst == 32'h7020_0073;
// F: loads, stores, the multiply-adds and OP-FP
wire fp_on = fs != 2'b00;
wire [2:0] rm_d = f3 == 3'd7 ? frm : f3;
wire rm_ok = rm_d <= 3'd4;
wire [4:0] f5 = inst[31:27];
wire is_flw = opc == 7'b0000111 && f3 == 3'd2;
wire is_fsw = opc == 7'b0100111 && f3 == 3'd2;
wire is_fr4 = opc[6:4] == 3'b100 && opc[1:0] == 2'b11 && inst[26:25] == 2'b00 && rm_ok;
wire is_fop = opc == 7'b1010011 && inst[26:25] == 2'b00 &&
((f5 <= 5'h03 && rm_ok) || // add sub mul div
(f5 == 5'h0B && rs2 == 5'd0 && rm_ok) || // sqrt
(f5 == 5'h04 && f3 <= 3'd2) || // sgnj
(f5 == 5'h05 && f3 <= 3'd1) || // min max
(f5 == 5'h14 && f3 <= 3'd2) || // le lt eq
(f5 == 5'h18 && rs2[4:1] == 4'd0 && rm_ok) || // fcvt.w[u].s
(f5 == 5'h1A && rs2[4:1] == 4'd0 && rm_ok) || // fcvt.s.w[u]
(f5 == 5'h1C && rs2 == 5'd0 && f3 <= 3'd1) || // fmv.x.w fclass
(f5 == 5'h1E && rs2 == 5'd0 && f3 == 3'd0)); // fmv.w.x
wire fp_toint = opc == 7'b1010011 && (f5 == 5'h18 || f5 == 5'h1C || f5 == 5'h14);
wire fp_frint = opc == 7'b1010011 && (f5 == 5'h1A || f5 == 5'h1E);
wire is_fp = fp_on && (is_fr4 || is_fop);
wire legal = is_lui | is_auipc | is_jal | is_jalr | is_br | is_ld | is_st |
is_opi | is_op | is_mdv | is_fence | is_fencei | is_csr |
is_ecall | is_ebrk | is_mret | is_wfi | is_mnret;
is_ecall | is_ebrk | is_mret | is_wfi | is_mnret |
(fp_on && (is_flw || is_fsw)) | is_fp;
// ------------------------------------------------------------------
// Execute-stage registers, loaded at the end of D.
@@ -148,7 +187,10 @@ module rv32_cpu #(
reg x_trap; // the deferred trap, below
reg [31:0] x_cause, x_val;
reg e_alu, e_sub, e_jal, e_jalr, e_br, e_ld, e_st, e_mul, e_div,
e_csr, e_ecall, e_ebrk, e_mret, e_mnret, e_fencei, e_ill;
e_csr, e_ecall, e_ebrk, e_mret, e_mnret, e_fencei, e_ill,
e_fp, e_fpint, e_fd; // F operation, its result to x, a load to f
reg [31:0] fa, fb, fc; // F operands
reg [2:0] e_rm;
wire [2:0] e_f3 = ir[14:12];
wire [4:0] e_rd = ir[11:7];
reg [2:0] alu_fn;
@@ -221,8 +263,11 @@ module rv32_cpu #(
always @* begin
csr_ok = 1'b1;
case (csr_a)
12'h300: csr_rd = {19'd0, 2'b11, 3'd0, mpie_b, 3'd0, mie_b, 3'd0};
12'h301: csr_rd = 32'h4000_1100; // RV32 I M
12'h001: begin csr_rd = {27'd0, fflags}; csr_ok = fp_on; end
12'h002: begin csr_rd = {29'd0, frm}; csr_ok = fp_on; end
12'h003: begin csr_rd = {24'd0, frm, fflags}; csr_ok = fp_on; end
12'h300: csr_rd = {fs == 2'b11, 16'd0, fs, 2'b11, 3'd0, mpie_b, 3'd0, mie_b, 3'd0};
12'h301: csr_rd = 32'h4000_1120; // RV32 I M F
12'h304: csr_rd = mie_r;
12'h305: csr_rd = mtvec;
12'h310: csr_rd = 32'd0; // mstatush
@@ -346,16 +391,34 @@ module rv32_cpu #(
end
end
// floating point
wire fpu_done;
wire [31:0] fpu_res;
wire [4:0] fpu_flags;
rv32_fpu fpu (
.clk(clk), .rst(rst), .start(state == S_E && e_fp && !trap),
.ins(ir), .rm(e_rm), .a(fa), .b(fb), .c(fc),
.done(fpu_done), .result(fpu_res), .flags(fpu_flags)
);
// ------------------------------------------------------------------
// Register write-back
always @* begin
rf_we = 1'b0; rf_wa = e_rd; rf_wd = alu_out;
frf_we = 1'b0; frf_wd = fpu_res;
case (state)
S_E: begin
rf_we = !trap && (e_alu || e_jal || e_jalr || e_csr);
rf_wd = e_csr ? csr_rd : (e_jal || e_jalr) ? pc4 : alu_out;
end
S_MEM: begin rf_we = d_ack && e_ld; rf_wd = ld_val; end
S_MEM: begin
rf_we = d_ack && e_ld && !e_fd; rf_wd = ld_val;
frf_we = d_ack && e_fd; frf_wd = d_rdata;
end
S_FP: begin
rf_we = fpu_done && e_fpint; rf_wd = fpu_res;
frf_we = fpu_done && !e_fpint;
end
S_MUL: begin rf_we = m_cnt; rf_wd = m_hi ? m_p2[63:32] : m_p2[31:0]; end
S_DIVF: begin
rf_we = 1'b1;
@@ -368,9 +431,12 @@ module rv32_cpu #(
endcase
end
always @(posedge clk) if (frf_we) frf[e_rd] <= frf_wd;
// an instruction completes (counted by instret unless it writes instret)
reg count_i;
wire done = (state == S_E && !trap && !e_ld && !e_st && !e_mul && !e_div) ||
wire done = (state == S_E && !trap && !e_ld && !e_st && !e_mul && !e_div && !e_fp) ||
(state == S_FP && fpu_done) ||
(state == S_MEM && d_ack) || (state == S_MUL && m_cnt) ||
state == S_DIVF || (state == S_XT && !x_trap);
@@ -387,7 +453,10 @@ module rv32_cpu #(
if (csr_go) begin
case (csr_a)
12'h300: begin mie_b <= csr_wv[3]; mpie_b <= csr_wv[7]; end
12'h001: begin fflags <= csr_wv[4:0]; fs <= 2'b11; end
12'h002: begin frm <= csr_wv[2:0]; fs <= 2'b11; end
12'h003: begin {frm, fflags} <= csr_wv[7:0]; fs <= 2'b11; end
12'h300: begin mie_b <= csr_wv[3]; mpie_b <= csr_wv[7]; fs <= csr_wv[14:13]; end
12'h304: mie_r <= csr_wv & MIE_MASK;
12'h305: mtvec <= {csr_wv[31:2], 2'b00};
12'h340: mscratch <= csr_wv;
@@ -421,9 +490,16 @@ module rv32_cpu #(
pc4 <= pc + 32'd4;
tgt <= pc + (is_jal ? imm_j : imm_b);
a <= is_lui ? 32'd0 : (is_auipc || is_jal) ? pc : rv1;
b <= (is_lui || is_auipc) ? imm_u : is_st ? imm_s :
b <= (is_lui || is_auipc) ? imm_u : (is_st || is_fsw) ? imm_s :
(is_op || is_br || is_mdv) ? rv2 : imm_i;
sv <= rv2;
sv <= is_fsw ? fv2 : rv2;
fa <= fp_frint ? rv1 : fv1;
fb <= fv2;
fc <= fv3;
e_rm <= rm_d;
e_fp <= is_fp;
e_fpint <= fp_toint;
e_fd <= is_flw && fp_on;
alu_fn <= (is_lui || is_auipc) ? 3'd0 : f3;
alu_alt <= inst[30];
e_alu <= is_lui | is_auipc | is_opi | is_op;
@@ -432,8 +508,8 @@ module rv32_cpu #(
e_jalr <= is_jalr;
e_br <= is_br;
e_tmis <= is_jal ? inst[21] : inst[8];
e_ld <= is_ld;
e_st <= is_st;
e_ld <= is_ld || (is_flw && fp_on);
e_st <= is_st || (is_fsw && fp_on);
e_mul <= is_mdv & !f3[2];
e_div <= is_mdv & f3[2];
e_csr <= is_csr;
@@ -471,6 +547,8 @@ module rv32_cpu #(
state <= S_MUL;
else if (e_div)
state <= S_DIV;
else if (e_fp)
state <= S_FP;
else if (e_br && e_tmis)
state <= S_XT;
else if (e_mret) begin
@@ -492,7 +570,16 @@ module rv32_cpu #(
end
end
S_MEM: if (d_ack) begin pc <= pc4; state <= S_D; end
S_MEM: if (d_ack) begin
pc <= pc4; state <= S_D;
if (e_fd) fs <= 2'b11;
end
S_FP: if (fpu_done) begin
pc <= pc4; state <= S_D;
fflags <= fflags | fpu_flags;
fs <= 2'b11;
end
S_MUL: begin
m_cnt <= 1'b1;
@@ -546,6 +633,7 @@ module rv32_cpu #(
mcycle <= 64'd0; minstret <= 64'd0;
retire <= 1'b0; count_i <= 1'b0;
nmie <= 1'b1; stepped <= 1'b0; nmi_taken <= 1'b0;
fs <= 2'b01; fflags <= 5'd0; frm <= 3'd0;
end
end
endmodule
+473
View File
@@ -0,0 +1,473 @@
`timescale 1ns / 1ps
// Single-precision floating point for rv32_cpu (the F extension), IEEE 754
// with all five rounding modes, subnormals, the five exception flags and
// RISC-V's canonical NaN. tools/f32.py is the reference it is tested against
// (scripts/fputest.sh).
//
// One operation at a time: `start` (one cycle) with the instruction word, the
// resolved rounding mode and the operands; `done` pulses with the result and
// the flags. a is the integer register for FMV.W.X and FCVT.S.W[U].
//
// Every arithmetic operation ends in the same normalise-and-round back end,
// which takes an unrounded value as sign, a 77-bit significand X and the
// exponent ex the value would have with X's top bit set (X * 2^(ex-127-76)).
// multiply-add a*b (DSPs, exact) and c aligned to it, added in 77 bits:
// add, subtract and multiply are a*1+c, a*1-c and a*b alone
// divide 27 quotient bits, one a clock, and a remainder sticky bit
// square root 27 root bits, one a clock, and a remainder sticky bit
// int -> float the integer itself
// The rest (sign injection, min/max, compares, class, moves, float -> int)
// finish without it.
//
// Clocks from start to done: multiply-add family 10 (2 when an operand is
// infinite, NaN or zero), divide and square root 34, int -> float 6,
// float -> int 3, the rest 2.
module rv32_fpu (
input clk,
input rst,
input start,
input [31:0] ins,
input [2:0] rm,
input [31:0] a, b, c,
output reg done,
output reg [31:0] result,
output reg [4:0] flags // NV DZ OF UF NX
);
localparam NV = 5'b10000, DZ = 5'b01000, OF = 5'b00100, UF = 5'b00010, NX = 5'b00001;
localparam [31:0] QNAN = 32'h7FC0_0000;
localparam S_IDLE = 4'd0, S_U = 4'd1, S_M1 = 4'd2, S_M2 = 4'd3, S_ADD = 4'd4,
S_ABS = 4'd5, S_N1 = 4'd6, S_N2 = 4'd7, S_R1 = 4'd8, S_R2 = 4'd9,
S_DIV = 4'd10, S_SQRT = 4'd11, S_CW = 4'd12, S_DIVF = 4'd13,
S_SQRTF = 4'd14;
reg [3:0] st;
// ------------------------------------------------------------ operands
reg [31:0] ra, rb, rc, ri;
reg [2:0] rrm;
wire [6:0] opc = ri[6:0];
wire [2:0] f3 = ri[14:12];
wire [4:0] f5 = ri[31:27];
wire r4 = opc[6:4] == 3'b100; // FMADD FMSUB FNMSUB FNMADD
wire op_add = !r4 && (f5 == 5'h00 || f5 == 5'h01);
wire op_mul = !r4 && f5 == 5'h02;
wire op_div = !r4 && f5 == 5'h03;
wire op_sqrt = !r4 && f5 == 5'h0B;
wire op_cvtw = !r4 && f5 == 5'h18; // float -> int
wire op_cvts = !r4 && f5 == 5'h1A; // int -> float
wire op_fma = r4 || op_add || op_mul;
wire uns = ri[20];
// a (for FADD/FSUB the multiplier is 1.0), b, c
wire [31:0] xa = ra;
wire [31:0] xb = op_add ? 32'h3F80_0000 : rb;
wire [31:0] xc = op_add ? rb : rc;
wire negp = r4 && opc[3];
wire negc = (r4 && opc[2]) || (op_add && f5[0]);
wire use_c = !op_mul;
function [7:0] ex8(input [31:0] x); ex8 = x[30:23]; endfunction
`define FZ(x) (x[30:0] == 31'd0)
`define FINF(x) (x[30:23] == 8'hFF && x[22:0] == 23'd0)
`define FNAN(x) (x[30:23] == 8'hFF && x[22:0] != 23'd0)
`define FSNAN(x) (`FNAN(x) && !x[22])
wire az = `FZ(xa), bz = `FZ(xb), cz = `FZ(xc);
wire ai = `FINF(xa), bi = `FINF(xb), ci = `FINF(xc);
wire an = `FNAN(xa), bn = `FNAN(xb), cn = `FNAN(xc);
wire as_ = `FSNAN(xa), bs = `FSNAN(xb), cs = `FSNAN(xc);
wire [23:0] ma = {ex8(xa) != 8'd0, xa[22:0]};
wire [23:0] mb = {ex8(xb) != 8'd0, xb[22:0]};
wire [23:0] mc = {ex8(xc) != 8'd0, xc[22:0]};
wire signed [11:0] ea = ex8(xa) == 8'd0 ? 12'sd1 : {4'd0, ex8(xa)};
wire signed [11:0] eb = ex8(xb) == 8'd0 ? 12'sd1 : {4'd0, ex8(xb)};
wire signed [11:0] ec = ex8(xc) == 8'd0 ? 12'sd1 : {4'd0, ex8(xc)};
wire sp = xa[31] ^ xb[31] ^ negp;
wire sc = xc[31] ^ negc;
// ------------------------------------------------- simple operations
// a < b and a == b for numbers (not NaN)
wire a_lt_b = (az && bz) ? 1'b0 :
(ra[31] != rb[31]) ? ra[31] :
ra[31] ? ra[30:0] > rb[30:0] : ra[30:0] < rb[30:0];
wire b_lt_a = (az && `FZ(rb)) ? 1'b0 :
(ra[31] != rb[31]) ? rb[31] :
ra[31] ? rb[30:0] > ra[30:0] : rb[30:0] < ra[30:0];
wire a_eq_b = ra == rb || (`FZ(ra) && `FZ(rb));
wire rbn = `FNAN(rb), rbs = `FSNAN(rb), rbz = `FZ(rb);
reg [31:0] simple_r;
reg [4:0] simple_f;
always @* begin
simple_r = 32'd0; simple_f = 5'd0;
case (f5)
5'h04: simple_r = {f3 == 3'd0 ? rb[31] : f3 == 3'd1 ? ~rb[31] : ra[31] ^ rb[31], ra[30:0]};
5'h05: begin // FMIN / FMAX
simple_f = (as_ || rbs) ? NV : 5'd0;
if (an && rbn) simple_r = QNAN;
else if (an) simple_r = rb;
else if (rbn) simple_r = ra;
else if (az && rbz) simple_r = (f3[0] ? !ra[31] : ra[31]) ? ra : rb;
else if (f3[0]) simple_r = b_lt_a ? ra : (a_lt_b ? rb : ra);
else simple_r = a_lt_b ? ra : rb;
end
5'h14: begin // FLE FLT FEQ
if (an || rbn) begin
simple_r = 32'd0;
simple_f = (f3 == 3'd2) ? ((as_ || rbs) ? NV : 5'd0) : NV;
end else
simple_r = {31'd0, f3 == 3'd2 ? a_eq_b : f3 == 3'd1 ? a_lt_b : (a_lt_b || a_eq_b)};
end
5'h1C: begin
if (f3 == 3'd1) begin // FCLASS
simple_r[0] = ra[31] && ai;
simple_r[1] = ra[31] && ex8(ra) != 8'd0 && ex8(ra) != 8'hFF;
simple_r[2] = ra[31] && ex8(ra) == 8'd0 && !az;
simple_r[3] = ra[31] && az;
simple_r[4] = !ra[31] && az;
simple_r[5] = !ra[31] && ex8(ra) == 8'd0 && !az;
simple_r[6] = !ra[31] && ex8(ra) != 8'd0 && ex8(ra) != 8'hFF;
simple_r[7] = !ra[31] && ai;
simple_r[8] = as_;
simple_r[9] = an && !as_;
end else
simple_r = ra; // FMV.X.W
end
default: simple_r = ra; // FMV.W.X
endcase
end
// ---------------------------------------------------- the multiply-add
reg [23:0] pa, pb;
reg [47:0] p1, p2; // the product, two pipeline stages
always @(posedge clk) begin
p1 <= pa * pb;
p2 <= p1;
end
reg signed [11:0] ep; // biased exponent of the product
reg [23:0] c_m;
reg signed [11:0] c_e;
reg c_zero, s_p, s_c, noadd;
reg [76:0] c_al; // c aligned with the product (<< 3)
reg [77:0] sum;
reg signed [11:0] fx; // the back end's ex for the sum
// c's place: its lowest bit is at bit s+3 of the 77-bit window, with the
// product's at bit 3. s > 50 would leave a gap above the product; then c
// sits at the top and the exponent comes from c (the product only adds
// stickiness, which rounds the same at any distance that far down).
// (worked out from the operands in S_U, used in S_M1)
wire signed [12:0] s_u = {ec[11], ec} - ({ea[11], ea} + {eb[11], eb} - 13'sd127) + 13'sd23;
reg signed [12:0] s_amt;
reg s_big;
reg [76:0] c_sh;
reg c_lost;
always @* begin
if (s_amt >= 0) begin
c_sh = {53'd0, c_m} << (s_amt + 3);
c_lost = 1'b0;
end else begin
// right by -s-3 (when negative), the lost bits make a sticky bit
c_sh = ({53'd0, c_m} << 3) >> (-s_amt);
c_lost = (-s_amt >= 13'sd27) ? c_m != 24'd0 :
((({53'd0, c_m} << 3) & ~({77{1'b1}} << (-s_amt))) != 77'd0);
end
end
// ------------------------------------------------------- the back end
reg n_sign;
reg [76:0] n_x;
reg signed [11:0] n_ex;
// leading one of n_x
reg [6:0] lz;
integer q;
always @* begin
lz = 7'd77;
for (q = 0; q <= 76; q = q + 1) if (n_x[q]) lz = 7'd76 - q[6:0];
end
reg signed [11:0] n_sh; // left shift (negative: right)
reg n_tiny; // below the normal range before rounding
reg n_zero;
reg [76:0] y;
reg y_st; // sticky from a right shift
wire signed [11:0] e_norm = n_ex - {5'd0, lz};
reg signed [11:0] r_e; // the result's exponent (1 if subnormal)
// rounding
wire [23:0] y_m = y[76:53];
wire y_g = y[52];
wire y_s = y[51:0] != 52'd0 || y_st;
function rnd_up(input [2:0] m, input s, input lsb, input g, input st);
case (m)
3'd0: rnd_up = g && (st || lsb);
3'd1: rnd_up = 1'b0;
3'd2: rnd_up = s && (g || st);
3'd3: rnd_up = !s && (g || st);
default: rnd_up = g;
endcase
endfunction
reg r_inc, r_inexact, r_fine;
reg [23:0] r_m;
wire [24:0] r_mi = {1'b0, r_m} + {24'd0, r_inc};
// ------------------------------------------------- divide, square root
reg [26:0] dq;
reg [25:0] drem; // divide: remainder (< 2 * divisor)
reg [23:0] dden;
reg [4:0] dcnt;
wire [26:0] dtry = {1'b0, drem} - {3'd0, dden};
reg [53:0] srad; // square root: radicand, two bits a clock
reg [28:0] srem;
wire [30:0] stry_r = {srem, srad[53:52]};
wire [30:0] stry = stry_r - {2'd0, dq, 2'b01};
// leading zeros of a 24-bit significand (to normalise subnormals)
function [4:0] lz24(input [23:0] m);
integer i;
begin
lz24 = 5'd24;
for (i = 0; i < 24; i = i + 1) if (m[i]) lz24 = 5'd23 - i[4:0];
end
endfunction
wire [4:0] lza = lz24(ma), lzb = lz24(mb);
wire [23:0] na = ma << lza, nb = mb << lzb;
wire signed [11:0] nea = ea - {7'd0, lza}, neb = eb - {7'd0, lzb};
// ------------------------------------------------------- float -> int
reg [32:0] cw_int;
reg cw_g, cw_s, cw_neg;
wire [47:0] cw_sh = {ma, 24'd0} >> (8'd150 - ex8(ra));
wire cw_inc = rnd_up(rrm, cw_neg, cw_int[0], cw_g, cw_s);
wire [32:0] cw_mag = cw_int + {32'd0, cw_inc};
// --------------------------------------------------------------------
always @(posedge clk) begin
done <= 1'b0;
case (st)
S_IDLE: if (start) begin
ra <= a; rb <= b; rc <= c; ri <= ins; rrm <= rm;
st <= S_U;
end
S_U: begin
// multiply-add specials (only used by the multiply-add family)
pa <= ma; pb <= mb;
ep <= ea + eb - 12'sd127;
c_m <= mc; c_e <= ec; c_zero <= cz || !use_c;
s_big <= !(cz || !use_c) && s_u > 13'sd50;
s_amt <= (!(cz || !use_c) && s_u > 13'sd50) ? 13'sd50 : s_u;
s_p <= sp; s_c <= sc; noadd <= !use_c;
if (op_fma) begin
if (an || bn || (use_c && cn) || (ai && bz) || (az && bi) ||
((ai || bi) && use_c && ci && sp != sc)) begin
result <= QNAN;
flags <= (as_ || bs || (use_c && cs) || (ai && bz) || (az && bi) ||
((ai || bi) && !an && !bn && use_c && ci && !cn && sp != sc)) ? NV : 5'd0;
done <= 1'b1; st <= S_IDLE;
end else if (ai || bi) begin
result <= {sp, 31'h7F80_0000}; flags <= 5'd0; done <= 1'b1; st <= S_IDLE;
end else if (use_c && ci) begin
result <= {sc, 31'h7F80_0000}; flags <= 5'd0; done <= 1'b1; st <= S_IDLE;
end else if (az || bz) begin
flags <= 5'd0; done <= 1'b1; st <= S_IDLE;
if (!use_c) result <= {sp, 31'd0};
else if (!cz) result <= {sc, xc[30:0]};
else if (sp == sc) result <= {sp, 31'd0};
else result <= {rrm == 3'd2, 31'd0};
end else
st <= S_M1;
end else if (op_div) begin
n_sign <= ra[31] ^ rb[31];
if (an || bn || (ai && bi) || (az && bz)) begin
result <= QNAN;
flags <= (as_ || bs || (ai && bi && !an && !bn) || (az && bz)) ? NV : 5'd0;
done <= 1'b1; st <= S_IDLE;
end else if (ai || bz) begin
result <= {ra[31] ^ rb[31], 31'h7F80_0000};
flags <= ai ? 5'd0 : DZ; done <= 1'b1; st <= S_IDLE;
end else if (bi || az) begin
result <= {ra[31] ^ rb[31], 31'd0}; flags <= 5'd0; done <= 1'b1; st <= S_IDLE;
end else begin
// quotient bits of na/nb from 2^0 down to 2^-26
drem <= {2'b00, na}; dden <= nb; dq <= 27'd0; dcnt <= 5'd0;
n_ex <= nea - neb + 12'sd127;
st <= S_DIV;
end
end else if (op_sqrt) begin
n_sign <= 1'b0;
if (an) begin
result <= QNAN; flags <= as_ ? NV : 5'd0; done <= 1'b1; st <= S_IDLE;
end else if (az) begin
result <= ra; flags <= 5'd0; done <= 1'b1; st <= S_IDLE;
end else if (ra[31]) begin
result <= QNAN; flags <= NV; done <= 1'b1; st <= S_IDLE;
end else if (ai) begin
result <= ra; flags <= 5'd0; done <= 1'b1; st <= S_IDLE;
end else begin
// value = na * 2^(nea-150); an even exponent for the root
srad <= nea[0] ? {1'b0, na, 29'd0} : {na, 30'd0};
srem <= 29'd0; dq <= 27'd0; dcnt <= 5'd0;
// the root of na*2^30 (nea even) or na*2^29 (odd) has 27 bits
n_ex <= ((nea - (nea[0] ? 12'sd127 : 12'sd128)) >>> 1) + 12'sd127;
st <= S_SQRT;
end
end else if (op_cvts) begin
n_sign <= !uns && ra[31];
n_x <= {(!uns && ra[31] ? -ra : ra), 45'd0};
n_ex <= 12'sd158;
st <= S_N1;
end else if (op_cvtw) begin
cw_neg <= ra[31];
if (ex8(ra) >= 8'd150) begin
cw_int <= {9'd0, ma} << (ex8(ra) - 8'd150);
cw_g <= 1'b0; cw_s <= 1'b0;
end else if (ex8(ra) >= 8'd126) begin
cw_int <= {9'd0, cw_sh[47:24]};
cw_g <= cw_sh[23]; cw_s <= cw_sh[22:0] != 23'd0;
end else begin
cw_int <= 33'd0; cw_g <= 1'b0; cw_s <= !az;
end
st <= S_CW;
end else begin
result <= simple_r; flags <= simple_f; done <= 1'b1; st <= S_IDLE;
end
end
S_CW: begin
done <= 1'b1; st <= S_IDLE;
flags <= 5'd0;
if (an || ex8(ra) >= 8'd159 ||
(uns ? (cw_neg ? cw_mag != 33'd0 : cw_mag[32])
: (cw_neg ? cw_mag > 33'h0_8000_0000 : cw_mag > 33'h0_7FFF_FFFF))) begin
flags <= NV;
if (uns) result <= (cw_neg && !an) ? 32'd0 : 32'hFFFF_FFFF;
else result <= (cw_neg && !an) ? 32'h8000_0000 : 32'h7FFF_FFFF;
end else begin
result <= cw_neg ? -cw_mag[31:0] : cw_mag[31:0];
flags <= (cw_g || cw_s) ? NX : 5'd0;
end
end
S_M1: begin // the product's first stage; align c
c_al <= c_zero ? 77'd0 : (c_sh | {76'd0, c_lost});
fx <= s_big ? c_e : ep + 12'sd27;
st <= S_M2;
end
S_M2: st <= S_ADD; // the product's second stage
S_ADD: begin
if (noadd || s_p == s_c)
sum <= {1'b0, c_al} + {27'd0, p2, 3'b000};
else
sum <= {1'b0, c_al} - {27'd0, p2, 3'b000};
st <= S_ABS;
end
S_ABS: begin
// the sum's sign: c's unless the subtraction went negative
if (sum[77]) begin
n_x <= -sum[76:0];
n_sign <= s_p;
end else begin
n_x <= sum[76:0];
n_sign <= noadd ? s_p : s_c;
end
n_ex <= fx;
st <= S_N1;
end
S_N1: begin // where to put the top bit
n_zero <= n_x == 77'd0;
if (e_norm < 12'sd1) begin
n_sh <= n_ex - 12'sd1; // subnormal: as far as exponent 1
r_e <= 12'sd1;
n_tiny <= 1'b1;
end else begin
n_sh <= {5'd0, lz};
r_e <= e_norm;
n_tiny <= 1'b0;
end
st <= S_N2;
end
S_N2: begin
if (n_sh >= 0) begin
y <= n_x << n_sh;
y_st <= 1'b0;
end else if (n_sh > -12'sd78) begin
y <= n_x >> (-n_sh);
y_st <= (n_x & ~({77{1'b1}} << (-n_sh))) != 77'd0;
end else begin
y <= 77'd0;
y_st <= n_x != 77'd0;
end
st <= S_R1;
end
S_R1: begin
r_m <= y_m;
r_inc <= rnd_up(rrm, n_sign, y[53], y_g, y_s);
r_inexact <= y_g || y_s;
// would rounding one bit further down (an unbounded exponent)
// also reach the smallest normal? (tininess after rounding)
r_fine <= y[75:52] == 24'hFF_FFFF &&
rnd_up(rrm, n_sign, 1'b1, y[51], y[50:0] != 51'd0 || y_st);
st <= S_R2;
end
S_R2: begin
done <= 1'b1; st <= S_IDLE;
if (n_zero) begin
// an exact zero: cancellation (or a zero integer)
result <= {(op_cvts ? 1'b0 : rrm == 3'd2), 31'd0};
flags <= 5'd0;
end else if (r_e + {11'd0, r_mi[24]} >= 12'sd255) begin
flags <= OF | NX;
if (rrm == 3'd1 || (rrm == 3'd2 && !n_sign) || (rrm == 3'd3 && n_sign))
result <= {n_sign, 31'h7F7F_FFFF};
else
result <= {n_sign, 31'h7F80_0000};
end else begin
if (r_mi[24])
result <= {n_sign, r_e[7:0] + 8'd1, 23'd0};
else
result <= {n_sign, r_mi[23] ? r_e[7:0] : 8'd0, r_mi[22:0]};
flags <= {2'b00, 1'b0,
n_tiny && r_inexact && !(r_mi[23] && r_fine),
r_inexact};
end
end
S_DIV: begin
if (!dtry[26]) begin drem <= {dtry[24:0], 1'b0}; dq <= {dq[25:0], 1'b1}; end
else begin drem <= {drem[24:0], 1'b0}; dq <= {dq[25:0], 1'b0}; end
dcnt <= dcnt + 5'd1;
if (dcnt == 5'd26) st <= S_DIVF;
end
S_SQRT: begin
srad <= {srad[51:0], 2'b00};
if (!stry[30]) begin srem <= stry[28:0]; dq <= {dq[25:0], 1'b1}; end
else begin srem <= stry_r[28:0]; dq <= {dq[25:0], 1'b0}; end
dcnt <= dcnt + 5'd1;
if (dcnt == 5'd26) st <= S_SQRTF;
end
S_DIVF: begin
n_x <= {dq, drem != 26'd0, 49'd0};
st <= S_N1;
end
S_SQRTF: begin
n_x <= {dq, srem != 29'd0, 49'd0};
st <= S_N1;
end
default: st <= S_IDLE;
endcase
if (rst) begin
st <= S_IDLE;
done <= 1'b0;
end
end
endmodule