OpenCores
URL https://opencores.org/ocsvn/ft816float/ft816float/trunk

Subversion Repositories ft816float

[/] [ft816float/] [trunk/] [rtl/] [positVerilog/] [positAddsub.sv] - Blame information for rev 43

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 36 robfinch
// ============================================================================
2
//        __
3
//   \\__/ o\    (C) 2020  Robert Finch, Waterloo
4
//    \  __ /    All rights reserved.
5
//     \/_//     robfinch@finitron.ca
6
//       ||
7
//
8
//      positAddsub.v
9
//    - posit number adder/subtracter
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 positAddsub(op, a, b, o);
31
`include "positSize.sv"
32
localparam rs = $clog2(PSTWID-1)-1;
33
input op;
34
input [PSTWID-1:0] a;
35
input [PSTWID-1:0] b;
36
output reg [PSTWID-1:0] o;
37
 
38
wire sa, sb;
39
reg so;
40
wire rop;
41
wire [rs:0] rgma, rgmb, rgm1, rgm2, argm1, argm2;
42
wire rgsa, rgsb, rgs1, rgs2;
43
wire [rs+es+1:0] diff;
44
wire [es-1:0] expa, expb, exp1, exp2;
45
wire [PSTWID-es-1:0] siga, sigb, sig1, sig2;
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 aa = sa ? -a : a;
75
assign bb = sb ? -b : b;
76
 
77
wire aa_gt_bb = aa >= bb;
78
// Determine op really wanted
79
assign rop = sa ^ sb ^ op;
80
// Sort operand components
81
assign rgs1 = aa_gt_bb ? rgsa : rgsb;
82
assign rgs2 = aa_gt_bb ? rgsb : rgsa;
83
assign rgm1 = aa_gt_bb ? rgma : rgmb;
84
assign rgm2 = aa_gt_bb ? rgmb : rgma;
85
assign exp1 = aa_gt_bb ? expa : expb;
86
assign exp2 = aa_gt_bb ? expb : expa;
87
assign sig1 = aa_gt_bb ? siga : sigb;
88
assign sig2 = aa_gt_bb ? sigb : siga;
89
 
90
assign argm1 = rgs1 ? rgm1 : -rgm1;
91
assign argm2 = rgs2 ? rgm2 : -rgm2;
92
 
93
assign diff = {argm1,exp1} - {argm2,exp2};
94
wire [rs-1:0] exp_diff = (|diff[es+rs:rs]) ? {rs{1'b1}} : diff[rs-1:0];
95
wire [PSTWID*2-1:0] sig2s = {sig2,{PSTWID{1'b0}}} >> exp_diff;
96
wire [PSTWID*2-1:0] sig1s = {sig1,{PSTWID{1'b0}}};
97
wire [PSTWID*2:0] sig_sd = rop ? sig1s - sig2s : sig1s + sig2s;
98
wire [1:0] sigov = sig_sd[PSTWID*2:PSTWID*2-1];
99
 
100
wire [$clog2(PSTWID-1):0] lzcnt;
101
wire [PSTWID-1:0] sigi = {|sigov,sig_sd[PSTWID*2-2:PSTWID]};
102
generate begin : gClz
103
  case(PSTWID)
104
  16: cntlz16 u1 (.i({sigi}), .o(lzcnt));
105
  20: cntlz24 u1 (.i({sigi,4'hF}), .o(lzcnt));
106
  32: cntlz32 u1 (.i({sigi}), .o(lzcnt));
107
  40: cntlz48 u1 (.i({sigi,8'hFF}), .o(lzcnt));
108
  52: cntlz64 u1 (.i({sigi,12'hFFF}), .o(lzcnt));
109
  64: cntlz64 u1 (.i({sigi}), .o(lzcnt));
110
  80: cntlz80 u1 (.i({sigi}), .o(lzcnt));
111
  default:  ;
112
  endcase
113
end
114
endgenerate
115
 
116
//positCntlz #(.PSTWID(PSTWID)) u3 (.i({|sigov,sig_sd[PSTWID-2:0]}), .o(lzcnt));
117
wire [PSTWID*2-1:0] sig_ls = sig_sd[PSTWID*2-1:0] << lzcnt;
118
 
119
wire [rs:0] absrgm1 = rgs1 ? rgm1 : -rgm1;  // rgs1 = 1 = positive
120
wire [es+rs+1:0] rxtmp;
121
wire [es+rs+1:0] rxtmp1;
122
wire srxtmp1;
123
wire [es+rs:0] abs_rxtmp;
124
wire [(es==0 ? 0 : es-1):0] expo;
125
wire [rs:0] rgmo;
126
generate begin : gEsz
127
if (es > 0) begin
128
assign rxtmp = {absrgm1,exp1} - {{es+1{1'b0}},lzcnt-es};
129
assign rxtmp1 = rxtmp + sigov[1]; // add in overflow if any
130
assign srxtmp1 = rxtmp1[es+rs+1];
131
assign abs_rxtmp = srxtmp1 ? -rxtmp1 : rxtmp1;
132
 
133
assign expo = (srxtmp1 & |abs_rxtmp[es-1:0]) ? rxtmp1[es-1:0] : abs_rxtmp[es-1:0];
134
assign rgmo = (~srxtmp1 || (srxtmp1 & |abs_rxtmp[es-1:0])) ? abs_rxtmp[es+rs:es] + 1'b1 : abs_rxtmp[es+rs:es];
135
end
136
else begin
137
assign rxtmp = absrgm1 - {{1{1'b0}},lzcnt};
138
assign rxtmp1 = rxtmp + sigov[1]; // add in overflow if any
139
assign srxtmp1 = rxtmp1[rs+1];
140
assign abs_rxtmp = srxtmp1 ? -rxtmp1 : rxtmp1;
141
assign expo = 1'b0;
142
assign rgmo = (~srxtmp1) ? abs_rxtmp[rs:0] + 1'b1 : abs_rxtmp[rs:0];
143
end
144
end
145
endgenerate
146
 
147
// Exponent and Significand Packing
148
reg [2*PSTWID-1+3:0] tmp;
149
always @*
150
case(es)
151
0:  tmp = { {PSTWID{~srxtmp1}}, srxtmp1, sig_ls[PSTWID*2-2:PSTWID-2], |sig_ls[PSTWID-3:0]};
152
1:  tmp = { {PSTWID{~srxtmp1}}, srxtmp1, expo, sig_ls[PSTWID*2-2:PSTWID-1], |sig_ls[PSTWID-2:0]};
153
2:  tmp = { {PSTWID{~srxtmp1}}, srxtmp1, expo, sig_ls[PSTWID*2-2:PSTWID], |sig_ls[PSTWID-1:0]};
154
default:  tmp = { {PSTWID{~srxtmp1}}, srxtmp1, expo, sig_ls[PSTWID*2-2:PSTWID+es-2], |sig_ls[PSTWID-1+es-2:0]};
155
endcase
156
 
157
wire [3*PSTWID-1+3:0] tmp1 = {tmp,{PSTWID{1'b0}}} >> rgmo;
158
 
159
// Rounding
160
// Gaurd, Round, and Sticky
161
wire L = tmp1[PSTWID+4], G = tmp1[PSTWID+3], R = tmp1[PSTWID+2], St = |tmp1[PSTWID+1:0],
162
     ulp = ((G & (R | St)) | (L & G & ~(R | St)));
163
wire [PSTWID-1:0] rnd_ulp = {{PSTWID-1{1'b0}},ulp};
164
 
165
wire [PSTWID:0] tmp1_rnd_ulp = tmp1[2*PSTWID-1+3:PSTWID+3] + rnd_ulp;
166
wire [PSTWID-1:0] tmp1_rnd = (rgmo < PSTWID-es-2) ? tmp1_rnd_ulp[PSTWID-1:0] : tmp1[2*PSTWID-1+3:PSTWID+3];
167
 
168
// Compute output sign
169
always @*
170
        casez ({zero,sa,op,sb})
171
        4'b0000: so = 1'b0;                     // + + + = +
172
        4'b0001: so = !aa_gt_bb;        // + + - = sign of larger
173
        4'b0010: so = !aa_gt_bb;        // + - + = sign of larger
174
        4'b0011: so = 1'b0;                     // + - - = +
175
        4'b0100: so = aa_gt_bb; // - + + = sign of larger
176
        4'b0101: so = 1'b1;                     // - + - = -
177
        4'b0110: so = 1'b1;                     // - - + = -
178
        4'b0111: so = aa_gt_bb; // - - - = sign of larger
179
        4'b1???: so = 1'b0;
180
        endcase
181
 
182
wire [PSTWID-1:0] abs_tmp = so ? -tmp1_rnd : tmp1_rnd;
183
 
184
always @*
185
  casez({zero,inf,sig_ls[PSTWID]})
186
  3'b1??: o = {PSTWID{1'b0}};
187
  3'b01?: o = {1'b1,{PSTWID-1{1'b0}}};
188
  3'b001: o = {PSTWID{1'b0}};
189
  default:  o = {so, abs_tmp[PSTWID-1:1]};
190
  endcase
191
 
192
endmodule

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.