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