`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 11 (2 when an operand is // infinite, NaN or zero), divide and square root 35, int -> float 7, // float -> int 3, the rest 2. (Exponents, leading zeros and the FADD/FSUB // operand swap are worked out as the operands are taken, and the back end // finds the leading one a clock before using it, for 100 MHz.) 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, S_NZ = 4'd15; 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: picked, with their // exponents and leading zeros, as the operands arrive (see S_IDLE) reg [31:0] rxb, rxc; wire [31:0] xa = ra; wire [31:0] xb = rxb; wire [31:0] xc = rxc; 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]}; reg signed [11:0] ea, eb, ec; // exponents (1 for a subnormal) reg signed [11:0] eab; // ea + eb - 127, the product's 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} - {eab[11], eab} + 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 reg [6:0] n_lz; // lz, registered in S_NZ reg signed [11:0] e_norm; // n_ex - 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 reg [4:0] lza, lzb; wire [23:0] na = ma << lza, nb = mb << lzb; reg signed [11:0] nea, neb; // exponents of na, nb // the same from the operands as they arrive function signed [11:0] exb(input [31:0] x); exb = x[30:23] == 8'd0 ? 12'sd1 : {4'd0, x[30:23]}; endfunction function [23:0] mant(input [31:0] x); mant = {x[30:23] != 8'd0, x[22:0]}; endfunction wire i_add = ins[6:4] != 3'b100 && ins[31:28] == 4'h0; // FADD, FSUB wire [31:0] i_b = i_add ? 32'h3F80_0000 : b; wire [31:0] i_c = i_add ? b : c; wire [4:0] i_lza = lz24(mant(a)), i_lzb = lz24(mant(i_b)); // ------------------------------------------------------- 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; rxb <= i_b; rxc <= i_c; ea <= exb(a); eb <= exb(i_b); ec <= exb(i_c); eab <= exb(a) + exb(i_b) - 12'sd127; lza <= i_lza; lzb <= i_lzb; nea <= exb(a) - {7'd0, i_lza}; neb <= exb(i_b) - {7'd0, i_lzb}; st <= S_U; end S_U: begin // multiply-add specials (only used by the multiply-add family) pa <= ma; pb <= mb; ep <= eab; 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_NZ; 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_NZ; end S_NZ: begin // find the top bit n_zero <= n_x == 77'd0; n_lz <= lz; e_norm <= n_ex - {5'd0, lz}; st <= S_N1; end S_N1: begin // where to put it 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, n_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_NZ; end S_SQRTF: begin n_x <= {dq, srem != 29'd0, 49'd0}; st <= S_NZ; end default: st <= S_IDLE; endcase if (rst) begin st <= S_IDLE; done <= 1'b0; end end endmodule