-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfloatmult
68 lines (59 loc) · 1.85 KB
/
floatmult
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
`timescale 100 ns / 10 ps
module floatMult (floatA,floatB,product);
input [15:0] floatA, floatB;
output reg [15:0] product;
reg sign;
reg signed [5:0] exponent; //6th bit is the sign
reg [9:0] mantissa;
reg [10:0] fractionA, fractionB; //fraction = {1,mantissa}
reg [21:0] fraction;
always @ (floatA or floatB) begin
if (floatA == 0 || floatB == 0) begin
product = 0;
end else begin
sign = floatA[15] ^ floatB[15];
exponent = floatA[14:10] + floatB[14:10] - 5'd15 + 5'd2;
fractionA = {1'b1,floatA[9:0]};
fractionB = {1'b1,floatB[9:0]};
fraction = fractionA * fractionB;
if (fraction[21] == 1'b1) begin
fraction = fraction << 1'b1;
exponent = exponent - 1'b1;
end else if (fraction[20] == 1'b1) begin
fraction = fraction << 2'b10;
exponent = exponent - 2'b10;
end else if (fraction[19] == 1'b1) begin
fraction = fraction << 2'b11;
exponent = exponent - 2'b11;
end else if (fraction[18] == 1'b1) begin
fraction = fraction << 3'b100;
exponent = exponent - 3'b100;
end else if (fraction[17] == 1'b1) begin
fraction = fraction << 3'b101;
exponent = exponent - 3'b101;
end else if (fraction[16] == 1'b1) begin
fraction = fraction << 3'b110;
exponent = exponent - 3'b110;
end else if (fraction[15] == 1'b1) begin
fraction = fraction << 3'b111;
exponent = exponent - 3'b111;
end else if (fraction[14] == 1'b1) begin
fraction = fraction << 4'b1000;
exponent = exponent - 4'b1000;
end else if (fraction[13] == 1'b1) begin
fraction = fraction << 4'b1001;
exponent = exponent - 4'b1001;
end else if (fraction[12] == 1'b0) begin
fraction = fraction << 4'b1010;
exponent = exponent - 4'b1010;
end
mantissa = fraction[21:12];
if(exponent[5]==1'b1) begin //exponent is negative
product=16'b0000000000000000;
end
else begin
product = {sign,exponent[4:0],mantissa};
end
end
end
endmodule