| 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 |
48 |
robfinch |
import posit::*;
|
| 29 |
38 |
robfinch |
|
| 30 |
|
|
module positMul(a, b, o, zero, inf);
|
| 31 |
|
|
input [PSTWID-1:0] a;
|
| 32 |
|
|
input [PSTWID-1:0] b;
|
| 33 |
|
|
output reg [PSTWID-1:0] o;
|
| 34 |
|
|
output zero;
|
| 35 |
|
|
output inf;
|
| 36 |
|
|
|
| 37 |
|
|
wire sa, sb, so;
|
| 38 |
|
|
wire [rs:0] rgma, rgmb;
|
| 39 |
|
|
wire [rs+1:0] rgm1, rgm2;
|
| 40 |
|
|
wire rgsa, rgsb;
|
| 41 |
|
|
wire [es-1:0] expa, expb;
|
| 42 |
|
|
wire [PSTWID-es-1:0] siga, sigb;
|
| 43 |
|
|
wire [(PSTWID-es)*2-1:0] prod;
|
| 44 |
|
|
wire zera, zerb;
|
| 45 |
|
|
wire infa, infb;
|
| 46 |
|
|
wire [PSTWID-1:0] aa, bb;
|
| 47 |
|
|
wire inf = infa|infb;
|
| 48 |
|
|
wire zero = zera|zerb;
|
| 49 |
|
|
|
| 50 |
48 |
robfinch |
positDecompose #(PSTWID) u1 (
|
| 51 |
38 |
robfinch |
.i(a),
|
| 52 |
|
|
.sgn(sa),
|
| 53 |
|
|
.rgs(rgsa),
|
| 54 |
|
|
.rgm(rgma),
|
| 55 |
|
|
.exp(expa),
|
| 56 |
|
|
.sig(siga),
|
| 57 |
|
|
.zer(zera),
|
| 58 |
|
|
.inf(infa)
|
| 59 |
|
|
);
|
| 60 |
|
|
|
| 61 |
48 |
robfinch |
positDecompose #(PSTWID) u2 (
|
| 62 |
38 |
robfinch |
.i(b),
|
| 63 |
|
|
.sgn(sb),
|
| 64 |
|
|
.rgs(rgsb),
|
| 65 |
|
|
.rgm(rgmb),
|
| 66 |
|
|
.exp(expb),
|
| 67 |
|
|
.sig(sigb),
|
| 68 |
|
|
.zer(zerb),
|
| 69 |
|
|
.inf(infb)
|
| 70 |
|
|
);
|
| 71 |
|
|
|
| 72 |
|
|
assign so = sa ^ sb; // compute sign
|
| 73 |
|
|
assign prod = siga * sigb;
|
| 74 |
|
|
// The product could have one or two whole digits before the point. Detect which it is
|
| 75 |
|
|
// and realign the product.
|
| 76 |
|
|
wire mo = prod[(PSTWID-es)*2-1];
|
| 77 |
|
|
wire [(PSTWID-es)*2-1:0] prod1 = mo ? prod : prod << 1'b1; // left align product
|
| 78 |
|
|
// Convert to the real +/- regime value
|
| 79 |
|
|
assign rgm1 = rgsa ? rgma : -rgma;
|
| 80 |
|
|
assign rgm2 = rgsb ? rgmb : -rgmb;
|
| 81 |
|
|
// Compute regime and exponent, include product alignment shift.
|
| 82 |
|
|
wire [rs+es+1:0] rxtmp = {rgm1,expa} + {rgm2,expb} + mo;
|
| 83 |
|
|
// Make a negative rx positive
|
| 84 |
|
|
wire [rs+es+1:0] rxtmp2c = rxtmp[rs+es+1] ? ~rxtmp + 2'd1 : rxtmp;
|
| 85 |
|
|
// Break out the exponent and regime portions
|
| 86 |
|
|
wire [es-1:0] exp = rxtmp[es-1:0];
|
| 87 |
|
|
// Take absolute value of regime portion
|
| 88 |
|
|
wire srxtmp = rxtmp[rs+es+1];
|
| 89 |
|
|
wire [rs:0] rgm = srxtmp ? -rxtmp[rs+es+1:es] : rxtmp[rs+es+1:es];
|
| 90 |
|
|
// Compute the length of the regime bit string, +1 for positive regime
|
| 91 |
41 |
robfinch |
wire [rs+es+1:0] rxn = rxtmp[rs+es+1] ? rxtmp2c : rxtmp;
|
| 92 |
|
|
wire [rs:0] rgml = (~srxtmp | |(rxn[es-1:0])) ? rxtmp2c[rs+es:es] + 2'd1 : rxtmp2c[rs+es:es];
|
| 93 |
38 |
robfinch |
// Build expanded posit number:
|
| 94 |
|
|
// trim one leading bit off the product bits
|
| 95 |
|
|
// and keep guard, round bits, and create sticky bit
|
| 96 |
|
|
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]};
|
| 97 |
|
|
|
| 98 |
|
|
wire [PSTWID*3-1+3:0] tmp1 = {tmp,{PSTWID{1'b0}}} >> rgml;
|
| 99 |
|
|
|
| 100 |
|
|
// Rounding
|
| 101 |
41 |
robfinch |
// Guard, Round, and Sticky
|
| 102 |
38 |
robfinch |
wire L = tmp1[PSTWID+4], G = tmp1[PSTWID+3], R = tmp1[PSTWID+2], St = |tmp1[PSTWID+1:0],
|
| 103 |
|
|
ulp = ((G & (R | St)) | (L & G & ~(R | St)));
|
| 104 |
|
|
wire [PSTWID-1:0] rnd_ulp = {{PSTWID-1{1'b0}},ulp};
|
| 105 |
|
|
|
| 106 |
|
|
wire [PSTWID:0] tmp1_rnd_ulp = tmp1[2*PSTWID-1+3:PSTWID+3] + rnd_ulp;
|
| 107 |
|
|
wire [PSTWID-1:0] tmp1_rnd = (rgml < PSTWID-es-2) ? tmp1_rnd_ulp[PSTWID-1:0] : tmp1[2*PSTWID-1+3:PSTWID+3];
|
| 108 |
|
|
|
| 109 |
|
|
wire [PSTWID-1:0] abs_tmp = so ? -tmp1_rnd : tmp1_rnd;
|
| 110 |
|
|
|
| 111 |
|
|
always @*
|
| 112 |
|
|
casez({zero,inf})
|
| 113 |
|
|
2'b1?: o = {PSTWID{1'b0}};
|
| 114 |
|
|
2'b01: o = {1'b1,{PSTWID-1{1'b0}}};
|
| 115 |
|
|
default: o = {so,abs_tmp[PSTWID-1:1]};
|
| 116 |
|
|
endcase
|
| 117 |
|
|
|
| 118 |
|
|
endmodule
|