1 |
42 |
robfinch |
// ============================================================================
|
2 |
|
|
// __
|
3 |
|
|
// \\__/ o\ (C) 2020 Robert Finch, Waterloo
|
4 |
|
|
// \ __ / All rights reserved.
|
5 |
|
|
// \/_// robfinch@finitron.ca
|
6 |
|
|
// ||
|
7 |
|
|
//
|
8 |
|
|
// positFDPMul.v
|
9 |
|
|
// - fused dot product posit number multiplier
|
10 |
|
|
// - parameterized width
|
11 |
|
|
// - perform a multiplication but retain all the product bits
|
12 |
|
|
// in the result in preparation for addition.
|
13 |
|
|
//
|
14 |
|
|
//
|
15 |
|
|
// This source file is free software: you can redistribute it and/or modify
|
16 |
|
|
// it under the terms of the GNU Lesser General Public License as published
|
17 |
|
|
// by the Free Software Foundation, either version 3 of the License, or
|
18 |
|
|
// (at your option) any later version.
|
19 |
|
|
//
|
20 |
|
|
// This source file is distributed in the hope that it will be useful,
|
21 |
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
22 |
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
23 |
|
|
// GNU General Public License for more details.
|
24 |
|
|
//
|
25 |
|
|
// You should have received a copy of the GNU General Public License
|
26 |
|
|
// along with this program. If not, see .
|
27 |
|
|
//
|
28 |
|
|
// ============================================================================
|
29 |
|
|
|
30 |
|
|
`include "positConfig.sv"
|
31 |
|
|
|
32 |
|
|
module positFDPMul(a, b, o, zero, inf);
|
33 |
|
|
`include "positSize.sv"
|
34 |
|
|
localparam rs = $clog2(PSTWID-1);
|
35 |
|
|
input [PSTWID-1:0] a;
|
36 |
|
|
input [PSTWID-1:0] b;
|
37 |
|
|
output reg [PSTWID+es+(PSTWID-es)*2-1:0] o;
|
38 |
|
|
output zero;
|
39 |
|
|
output inf;
|
40 |
|
|
|
41 |
|
|
wire sa, sb, so;
|
42 |
|
|
wire [rs:0] rgma, rgmb;
|
43 |
|
|
wire [rs+1:0] rgm1, rgm2;
|
44 |
|
|
wire rgsa, rgsb;
|
45 |
|
|
wire [es-1:0] expa, expb;
|
46 |
|
|
wire [PSTWID-es-1:0] siga, sigb;
|
47 |
|
|
wire [(PSTWID-es)*2-1:0] prod;
|
48 |
|
|
wire zera, zerb;
|
49 |
|
|
wire infa, infb;
|
50 |
|
|
wire inf = infa|infb;
|
51 |
|
|
wire zero = zera|zerb;
|
52 |
|
|
|
53 |
|
|
positDecompose #(PSTWID,es) u1 (
|
54 |
|
|
.i(a),
|
55 |
|
|
.sgn(sa),
|
56 |
|
|
.rgs(rgsa),
|
57 |
|
|
.rgm(rgma),
|
58 |
|
|
.exp(expa),
|
59 |
|
|
.sig(siga),
|
60 |
|
|
.zer(zera),
|
61 |
|
|
.inf(infa)
|
62 |
|
|
);
|
63 |
|
|
|
64 |
|
|
positDecompose #(PSTWID,es) u2 (
|
65 |
|
|
.i(b),
|
66 |
|
|
.sgn(sb),
|
67 |
|
|
.rgs(rgsb),
|
68 |
|
|
.rgm(rgmb),
|
69 |
|
|
.exp(expb),
|
70 |
|
|
.sig(sigb),
|
71 |
|
|
.zer(zerb),
|
72 |
|
|
.inf(infb)
|
73 |
|
|
);
|
74 |
|
|
|
75 |
|
|
assign so = sa ^ sb; // compute sign
|
76 |
|
|
assign prod = siga * sigb;
|
77 |
|
|
// The product could have one or two whole digits before the point. Detect which it is
|
78 |
|
|
// and realign the product.
|
79 |
|
|
wire mo = prod[(PSTWID-es)*2-1];
|
80 |
|
|
wire [(PSTWID-es)*2-1:0] prod1 = mo ? prod : prod << 1'b1; // left align product
|
81 |
|
|
// Convert to the real +/- regime value
|
82 |
|
|
assign rgm1 = rgsa ? rgma : -rgma;
|
83 |
|
|
assign rgm2 = rgsb ? rgmb : -rgmb;
|
84 |
|
|
// Compute regime and exponent, include product alignment shift.
|
85 |
|
|
wire [rs+es+1:0] rxtmp = {rgm1,expa} + {rgm2,expb} + mo;
|
86 |
|
|
// Make a negative rx positive
|
87 |
|
|
wire [rs+es+1:0] rxtmp2c = rxtmp[rs+es+1] ? ~rxtmp + 2'd1 : rxtmp;
|
88 |
|
|
// Break out the exponent and regime portions
|
89 |
|
|
wire [es-1:0] exp = |es ? rxtmp[es-1:0] : 0;
|
90 |
|
|
// Take absolute value of regime portion
|
91 |
|
|
wire srxtmp = rxtmp[rs+es+1];
|
92 |
|
|
wire [rs:0] rgm = srxtmp ? -rxtmp[rs+es+1:es] : rxtmp[rs+es+1:es];
|
93 |
|
|
// Compute the length of the regime bit string, +1 for positive regime
|
94 |
|
|
wire [rs+es+1:0] rxn = rxtmp[rs+es+1] ? rxtmp2c : rxtmp;
|
95 |
|
|
wire [rs:0] rgml;
|
96 |
|
|
// Build expanded posit number:
|
97 |
|
|
// trim one leading bit off the product bits
|
98 |
|
|
// and keep guard, round bits, and create sticky bit
|
99 |
|
|
wire [PSTWID+es+(PSTWID-es)*2-2:0] tmp;
|
100 |
|
|
generate begin : gTmp
|
101 |
|
|
if (es > 0) begin
|
102 |
|
|
assign rgml = (~srxtmp | |(rxn[es-1:0])) ? rxtmp2c[rs+es:es] + 2'd1 : rxtmp2c[rs+es:es];
|
103 |
|
|
assign tmp = {{PSTWID-1{~srxtmp}},srxtmp,exp,prod1[(PSTWID-es)*2-2:0]};
|
104 |
|
|
end
|
105 |
|
|
else begin
|
106 |
|
|
assign rgml = (~srxtmp) ? rxtmp2c[rs+es:es] + 2'd1 : rxtmp2c[rs+es:es];
|
107 |
|
|
assign tmp = {{PSTWID-1{~srxtmp}},srxtmp,prod1[(PSTWID-es)*2-2:0]};
|
108 |
|
|
end
|
109 |
|
|
end
|
110 |
|
|
endgenerate
|
111 |
|
|
wire [PSTWID+es+(PSTWID-es)*2-2:0] tmp1 = tmp << (PSTWID-rgml-1);
|
112 |
|
|
wire [PSTWID+es+(PSTWID-es)*2-1:0] abstmp = so ? {1'b1,-tmp1} : {1'b0,tmp1};
|
113 |
|
|
|
114 |
|
|
always @*
|
115 |
|
|
casez({zero,inf})
|
116 |
|
|
2'b1?: o = {PSTWID+es+(PSTWID-es)*2-2{1'b0}};
|
117 |
|
|
2'b01: o = {1'b1,{PSTWID+es+(PSTWID-es)*2-2-1{1'b0}}};
|
118 |
|
|
default: o = abstmp;
|
119 |
|
|
endcase
|
120 |
|
|
|
121 |
|
|
endmodule
|