Skip to content

Commit a3375e0

Browse files
committed
omg it passes
1 parent c7c593f commit a3375e0

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

src/fp8.v

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,18 @@ module fp8mul (
6666
output [2:0] mant_out
6767
);
6868
parameter EXP_BIAS = 7;
69-
wire [7:0] full_mant = ({exp1 != 0, mant1} * {exp2 != 0, mant2});
7069
wire isnan = (sign1 == 1 && exp1 == 0 && mant1 == 0) || (sign2 == 1 && exp2 == 0 && mant2 == 0);
71-
wire overflow_mant = full_mant[7]; // 01001000
72-
wire underflow = (exp1 + exp2) < 1 - (overflow_mant || (full_mant[6] && (full_mant[5:0] != 0))) + EXP_BIAS;
73-
assign exp_out = (exp1 == 0 || exp2 == 0 || isnan || underflow) ? 0 : (exp1 + exp2 - EXP_BIAS + overflow_mant); // Exponent bias is 7
74-
wire [6:0] shifted_mant = overflow_mant ? full_mant[7:1] : full_mant[6:0];
75-
assign mant_out = (exp1 == 0 || exp2 == 0 || isnan || underflow) ? 0 : (shifted_mant[6:4] + (shifted_mant[3:0] > 8 || (shifted_mant[3:0] == 8 && shifted_mant[4])));
76-
assign sign_out = ((sign1 ^ sign2) && (exp1 != 0 && exp2 != 0)) || isnan;
70+
wire [7:0] full_mant = ({exp1 != 0, mant1} * {exp2 != 0, mant2});
71+
wire overflow_mant = full_mant[7];
72+
wire [6:0] shifted_mant = overflow_mant ? full_mant[6:0] : {full_mant[5:0], 1'b0};
73+
// is the mantissa overflowing up to the next exponent?
74+
wire roundup = (exp1 + exp2 + overflow_mant < 1 + EXP_BIAS) && (shifted_mant[6:0] != 0)
75+
|| (shifted_mant[6:4] == 3'b111 && shifted_mant[3]);
76+
wire underflow = (exp1 + exp2 + overflow_mant) < 1 - roundup + EXP_BIAS;
77+
wire is_zero = exp1 == 0 || exp2 == 0 || isnan || underflow;
78+
// note: you can't use negative numbers reliably. just keep things positive during compares.
79+
wire [4:0] exp_out_tmp = (exp1 + exp2 + overflow_mant + roundup) < EXP_BIAS ? 0 : (exp1 + exp2 + overflow_mant + roundup - EXP_BIAS);
80+
assign exp_out = exp_out_tmp > 15 ? 4'b1111 : (is_zero) ? 0 : exp_out_tmp[3:0]; // Exponent bias is 7
81+
assign mant_out = exp_out_tmp > 15 ? 3'b111 : (is_zero || roundup) ? 0 : (shifted_mant[6:4] + (shifted_mant[3:0] > 8 || (shifted_mant[3:0] == 8 && shifted_mant[4])));
82+
assign sign_out = ((sign1 ^ sign2) && !(is_zero)) || isnan;
7783
endmodule

0 commit comments

Comments
 (0)