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

Subversion Repositories ft816float

[/] [ft816float/] [trunk/] [rtl/] [positVerilog/] [positFDPAddsub.sv] - Blame information for rev 53

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

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

powered by: WebSVN 2.1.0

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