Init commit
This commit is contained in:
Binary file not shown.
@@ -0,0 +1,37 @@
|
||||
//This module is given for the exercises
|
||||
module fp_mult_top (
|
||||
clk, rst, rnd, a, b, z, status
|
||||
);
|
||||
|
||||
input logic [31:0] a, b; // Floating-Point numbers
|
||||
input logic [2:0] rnd; // Rounding signal
|
||||
output logic [31:0] z; // a ± b
|
||||
output logic [7:0] status; // Status Flags
|
||||
input logic clk, rst;
|
||||
|
||||
logic [31:0] a1, b1; // Floating-Point numbers
|
||||
logic [2:0] rnd1; // Rounding signal
|
||||
logic [31:0] z1; // a ± b
|
||||
logic [7:0] status1; // Status Flags
|
||||
|
||||
fp_mult multiplier(a1,b1,rnd1,z1,status1,clk,rst);
|
||||
|
||||
always @(posedge clk)
|
||||
if (!rst)
|
||||
begin
|
||||
a1 <= '0;
|
||||
b1 <= '0;
|
||||
rnd1 <= '0;
|
||||
z <= '0;
|
||||
status <= '0;
|
||||
end
|
||||
else
|
||||
begin
|
||||
a1 <= a;
|
||||
b1 <= b;
|
||||
rnd1 <= rnd;
|
||||
z <= z1;
|
||||
status <= status1;
|
||||
end
|
||||
|
||||
endmodule
|
||||
Binary file not shown.
@@ -0,0 +1,155 @@
|
||||
//////
|
||||
// Please consider this function a black box. DO NOT try to copy any part of this function in your code.
|
||||
//////
|
||||
|
||||
function [31:0] multiplication (string round, logic [31:0] a, logic [31:0] b);
|
||||
bit [31:0] result;
|
||||
// Convert every denormal in zero
|
||||
if(a[30:23] == '0) a[22:0] = '0;
|
||||
if(b[30:23] == '0) b[22:0] = '0;
|
||||
if(a[30:23] == '1) a[22:0] = '0;
|
||||
if(b[30:23] == '1) b[22:0] = '0;
|
||||
|
||||
// If (a is inf or NaN) or (b is inf or NaN) => Result Inf
|
||||
if((a[30:23] == '1 && b[30:23] == '0) || (a[30:23] == '0 && b[30:23] == '1)) result = {1'b0, {8{1'b1}}, {23{1'b0}}};
|
||||
else begin
|
||||
result = $shortrealtobits($bitstoshortreal(a) * $bitstoshortreal(b));
|
||||
case (round)
|
||||
//IEEE_NEAR ROUNDING
|
||||
"IEEE_near": begin
|
||||
if(result[30:23] == '1) result[22:0] = '0;
|
||||
if(result[30:23] == '0) result[22:0] = '0;
|
||||
end
|
||||
//IEEE_ZERO ROUNDING
|
||||
"IEEE_zero": begin
|
||||
//Round towards zero instead if rounded up and positive
|
||||
if($bitstoshortreal(result) > ($bitstoshortreal(a) * $bitstoshortreal(b)))
|
||||
begin
|
||||
if($bitstoshortreal(result) > 0)
|
||||
begin
|
||||
result = result - 1;
|
||||
end
|
||||
end
|
||||
//Round towards zero instead if rounded down and negative
|
||||
if($bitstoshortreal(result) < ($bitstoshortreal(a) * $bitstoshortreal(b)))
|
||||
begin
|
||||
if($bitstoshortreal(result) < 0)
|
||||
begin
|
||||
result = result - 1;
|
||||
end
|
||||
end
|
||||
if(result[30:23] == '0) result[22:0] = '0;
|
||||
end
|
||||
//AWAY_ZERO ROUNDING
|
||||
"away_zero": begin
|
||||
//Check if the result is denormal and round to minNormal
|
||||
if((result[30:23] == '0 && result[22:0] != '0) || (a[30:23] != '0 && b[30:23] != '0 && result[30:23] == '0 && result[22:0] == '0)) begin
|
||||
result = {result[31], {7{1'b0}}, 1'b1, {23{1'b0}}};
|
||||
end
|
||||
else begin
|
||||
//Round away from zero instead if rounded up and negative
|
||||
if($bitstoshortreal(result) > ($bitstoshortreal(a) * $bitstoshortreal(b)))
|
||||
begin
|
||||
if($bitstoshortreal(result) < 0)
|
||||
begin
|
||||
result = result + 1;
|
||||
end
|
||||
end
|
||||
//Round away from zero instead if rounded down and positive
|
||||
if($bitstoshortreal(result) < ($bitstoshortreal(a) * $bitstoshortreal(b)))
|
||||
begin
|
||||
if($bitstoshortreal(result) > 0)
|
||||
begin
|
||||
result = result + 1;
|
||||
end
|
||||
end
|
||||
end
|
||||
if(result[30:23] == '1) result[22:0] = '0;
|
||||
end
|
||||
//IEEE_PINF ROUNDING
|
||||
"IEEE_pinf": begin
|
||||
//Check if the result is denormal and round to minNormal, but only for positives
|
||||
if((result[31] == 0 && result[30:23] == '0 && result[22:0] != '0) || (result[31] == 0 && a[30:23] != '0 && b[30:23] != '0 && result[30:23] == '0 && result[22:0] == '0)) begin
|
||||
result = {{8{1'b0}}, 1'b1, {23{1'b0}}};
|
||||
end
|
||||
else begin
|
||||
//Round towards positive infinity instead if rounded down and positive
|
||||
if($bitstoshortreal(result) < ($bitstoshortreal(a) * $bitstoshortreal(b)))
|
||||
begin
|
||||
if($bitstoshortreal(result) > 0)
|
||||
begin
|
||||
result = result + 1;
|
||||
end
|
||||
end
|
||||
//Round towards positive infinity instead if rounded down and negative
|
||||
if($bitstoshortreal(result) < ($bitstoshortreal(a) * $bitstoshortreal(b)))
|
||||
begin
|
||||
if($bitstoshortreal(result) < 0)
|
||||
begin
|
||||
result = result - 1;
|
||||
end
|
||||
end
|
||||
end
|
||||
if(result[30:23] == '0) result[22:0] = '0;
|
||||
end
|
||||
//IEEE_NINF ROUNDING
|
||||
"IEEE_ninf": begin
|
||||
//Check if the result is denormal and round to minNormal, but only for negatives
|
||||
if((result[31] == 1 && result[30:23] == '0 && result[22:0] != '0) || (result[31] == 1 && a[30:23] != '0 && b[30:23] != '0 && result[30:23] == '0 && result[22:0] == '0)) begin
|
||||
result = {1'b1, {7{1'b0}}, 1'b1, {23{1'b0}}};
|
||||
end
|
||||
else begin
|
||||
//Round towards negative infinity instead if rounded up and positive
|
||||
if($bitstoshortreal(result) > ($bitstoshortreal(a) * $bitstoshortreal(b)))
|
||||
begin
|
||||
if($bitstoshortreal(result) > 0)
|
||||
begin
|
||||
result = result - 1;
|
||||
end
|
||||
end
|
||||
//Round towards negative infinity instead if rounded up and negative
|
||||
if($bitstoshortreal(result) > ($bitstoshortreal(a) * $bitstoshortreal(b)))
|
||||
begin
|
||||
if($bitstoshortreal(result) < 0)
|
||||
begin
|
||||
result = result + 1;
|
||||
end
|
||||
end
|
||||
end
|
||||
if(result[30:23] == '0) result[22:0] = '0;
|
||||
end
|
||||
//NEAR_UP ROUNDING
|
||||
"near_up": begin
|
||||
//Round towards positive infinity if rounded down, negative and in the middle
|
||||
if(($bitstoshortreal(result) < ($bitstoshortreal(a) * $bitstoshortreal(b))))
|
||||
begin
|
||||
if(($bitstoshortreal(result) - ($bitstoshortreal(a) * $bitstoshortreal(b))) == (($bitstoshortreal(a) * $bitstoshortreal(b)) - $bitstoshortreal(result-1)))
|
||||
begin
|
||||
if($bitstoshortreal(result) < 0)
|
||||
begin
|
||||
result = result - 1;
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
//Round towards positive infinity if rounded down, positive and in the middle
|
||||
if(($bitstoshortreal(result) < ($bitstoshortreal(a) * $bitstoshortreal(b))))
|
||||
begin
|
||||
if(($bitstoshortreal(result+1) - ($bitstoshortreal(a) * $bitstoshortreal(b))) == (($bitstoshortreal(a) * $bitstoshortreal(b)) - $bitstoshortreal(result)))
|
||||
begin
|
||||
if($bitstoshortreal(result) > 0)
|
||||
begin
|
||||
result = result + 1;
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if(result[30:23] == '1) result[22:0] = '0;
|
||||
if(result[30:23] == '0) result[22:0] = '0;
|
||||
end
|
||||
endcase
|
||||
end
|
||||
|
||||
return result;
|
||||
endfunction
|
||||
|
||||
Reference in New Issue
Block a user