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

Subversion Repositories ft816float

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

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

powered by: WebSVN 2.1.0

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