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

Subversion Repositories ft816float

[/] [ft816float/] [trunk/] [rtl/] [verilog2/] [DFPNormalize128.sv] - Blame information for rev 57

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 57 robfinch
// ============================================================================
2
//        __
3
//   \\__/ o\    (C) 2006-2020  Robert Finch, Waterloo
4
//    \  __ /    All rights reserved.
5
//     \/_//     robfinch@finitron.ca
6
//       ||
7
//
8
//      DFPNormalize128.sv
9
//    - decimal floating point normalization unit
10
//    - eight cycle latency
11
//    - parameterized width
12
//
13
//
14
// This source file is free software: you can redistribute it and/or modify
15
// it under the terms of the GNU Lesser General Public License as published
16
// by the Free Software Foundation, either version 3 of the License, or
17
// (at your option) any later version.
18
//
19
// This source file is distributed in the hope that it will be useful,
20
// but WITHOUT ANY WARRANTY; without even the implied warranty of
21
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
// GNU General Public License for more details.
23
//
24
// You should have received a copy of the GNU General Public License
25
// along with this program.  If not, see .
26
//
27
//      This unit takes a floating point number in an intermediate
28
// format and normalizes it. No normalization occurs
29
// for NaN's or infinities. The unit has a two cycle latency.
30
//
31
// The mantissa is assumed to start with two whole bits on
32
// the left. The remaining bits are fractional.
33
//
34
// The width of the incoming format is reduced via a generation
35
// of sticky bit in place of the low order fractional bits.
36
//
37
// On an underflowed input, the incoming exponent is assumed
38
// to be negative. A right shift is needed.
39
// ============================================================================
40
 
41
import DFPPkg::*;
42
 
43
module DFPNormalize128(clk, ce, i, o, under_i, under_o, inexact_o);
44
parameter N=34;
45
input clk;
46
input ce;
47
input DFP128UD i;               // expanded format input
48
output DFP128UN o;      // normalized output + guard, sticky and round bits, + 1 whole digit
49
input under_i;
50
output under_o;
51
output inexact_o;
52
 
53
integer n;
54
// ----------------------------------------------------------------------------
55
// No Clock required
56
// ----------------------------------------------------------------------------
57
reg [13:0] xo0;
58
reg so0;
59
reg sx0;
60
reg nan0, qnan0, snan0;
61
reg inf0;
62
 
63
always @*
64
        xo0 <= i.exp;
65
always @*
66
        so0 <= i.sign;          // sign doesn't change
67
always @*
68
        nan0 <= i.nan;
69
always @*
70
        qnan0 <= i.qnan;
71
always @*
72
        snan0 <= i.snan;
73
always @*
74
        inf0 <= i.infinity;
75
 
76
// ----------------------------------------------------------------------------
77
// Clock #1
78
// - Capture exponent information
79
// ----------------------------------------------------------------------------
80
reg xInf1a, xInf1b, xInf1c;
81
DFP128UD i1;
82
always @(posedge clk)
83
        if (ce)
84
                i1 <= i;
85
 
86
always @(posedge clk)
87
        if (ce) xInf1a <= xo0==14'h2FFF & !under_i;
88
always @(posedge clk)
89
        if (ce) xInf1b <= xo0==14'h2FFE & !under_i;
90
always @(posedge clk)
91
        if (ce) xInf1c <= xo0==14'h2FFF;
92
 
93
// ----------------------------------------------------------------------------
94
// Clock #2
95
// - determine exponent increment
96
// Since the there are *three* whole digits in the incoming format
97
// the number of whole digits needs to be reduced. If the MSB is
98
// set, then increment the exponent and no shift is needed.
99
// ----------------------------------------------------------------------------
100
wire xInf2c, xInf2b;
101
wire [13:0] xo2;
102
reg incExpByOne2;
103
delay #(.WID(1),.DEP(1)) u21 (.clk(clk), .ce(ce), .i(xInf1c), .o(xInf2c));
104
delay #(.WID(1),.DEP(1)) u22 (.clk(clk), .ce(ce), .i(xInf1b), .o(xInf2b));
105
delay #(.WID(14),.DEP(2)) u23 (.clk(clk), .ce(ce), .i(xo0), .o(xo2));
106
delay #(.WID(1),.DEP(2)) u24 (.clk(clk), .ce(ce), .i(under_i), .o(under2));
107
 
108
always @(posedge clk)
109
        if (ce) incExpByOne2 <= !xInf1a & |i1.sig[279:276];
110
 
111
// ----------------------------------------------------------------------------
112
// Clock #3
113
// - increment exponent
114
// - detect a zero mantissa
115
// ----------------------------------------------------------------------------
116
 
117
wire incExpByOne3;
118
DFP128UD i3;
119
reg [13:0] xo3;
120
reg zeroMan3;
121
delay #(.WID(1),.DEP(1)) u32 (.clk(clk), .ce(ce), .i(incExpByOne2), .o(incExpByOne3));
122
delay #(.WID($bits(i3)),.DEP(3)) u33 (.clk(clk), .ce(ce), .i(i), .o(i3));
123
 
124
wire [13:0] xo2a = xo2 + 1'd1;
125
 
126
always @(posedge clk)
127
        if (ce) xo3 <= (incExpByOne2 ? xo2a : xo2);
128
 
129
always @(posedge clk)
130
        if(ce) zeroMan3 <= 1'b0;
131
 
132
// ----------------------------------------------------------------------------
133
// Clock #4
134
// - Shift mantissa left
135
// - If infinity is reached then set the mantissa to zero
136
//   shift mantissa left to reduce to a single whole digit
137
// - create sticky bit
138
// ----------------------------------------------------------------------------
139
 
140
reg [(N+2)*4-1:0] mo4;
141
reg inexact4;
142
 
143
always @(posedge clk)
144
if(ce)
145
casez({zeroMan3,incExpByOne3})
146
2'b1?:  mo4 <= 1'd0;
147
2'b01:  mo4 <= {i3[(N+1)*4*2-1:(N+1)*4],3'b0,|i3[(N+1)*4-1:0]};
148
default:        mo4 <= {i3[(N+1)*4*2-1-4:N*4],3'b0,|i3[N*4-1:0]};
149
endcase
150
 
151
always @(posedge clk)
152
if(ce)
153
casez({zeroMan3,incExpByOne3})
154
2'b1?:  inexact4 <= 1'd0;
155
2'b01:  inexact4 <= |i3[(N+1)*4-1:0];
156
default:        inexact4 <= |i3[N*4-1:0];
157
endcase
158
 
159
// ----------------------------------------------------------------------------
160
// Clock edge #5
161
// - count leading zeros
162
// ----------------------------------------------------------------------------
163
reg [7:0] leadingZeros5;
164
wire [13:0] xo5;
165
wire xInf5;
166
delay #(.WID(14),.DEP(2)) u51 (.clk(clk), .ce(ce), .i(xo3), .o(xo5));
167
delay #(.WID(1),.DEP(3)) u52 (.clk(clk), .ce(ce), .i(xInf2c), .o(xInf5) );
168
 
169
/* Lookup table based leading zero count modules give slightly better
170
   performance but cases must be coded.
171
generate
172
begin
173
if (FPWID <= 32) begin
174
cntlz32Reg clz0 (.clk(clk), .ce(ce), .i({mo4,4'b0}), .o(leadingZeros5) );
175
assign leadingZeros5[7:6] = 2'b00;
176
end
177
else if (FPWID<=64) begin
178
assign leadingZeros5[7] = 1'b0;
179
cntlz64Reg clz0 (.clk(clk), .ce(ce), .i({mo4,7'h0}), .o(leadingZeros5) );
180
end
181
else if (FPWID<=80) begin
182
assign leadingZeros5[7] = 1'b0;
183
cntlz80Reg clz0 (.clk(clk), .ce(ce), .i({mo4,11'b0}), .o(leadingZeros5) );
184
end
185
else if (FPWID<=84) begin
186
assign leadingZeros5[7] = 1'b0;
187
cntlz96Reg clz0 (.clk(clk), .ce(ce), .i({mo4,23'b0}), .o(leadingZeros5) );
188
end
189
else if (FPWID<=96) begin
190
assign leadingZeros5[7] = 1'b0;
191
cntlz96Reg clz0 (.clk(clk), .ce(ce), .i({mo4,11'b0}), .o(leadingZeros5) );
192
end
193
else if (FPWID<=128)
194
cntlz128Reg clz0 (.clk(clk), .ce(ce), .i({mo4,11'b0}), .o(leadingZeros5) );
195
end
196
endgenerate
197
*/
198
 
199
// Sideways add.
200
// Normally there would be only one to two leading zeros. It is tempting then
201
// to check for only one or two. But, denormalized numbers might have more
202
// leading zeros. If denormals were not supported this could be made smaller
203
// and faster.
204
`ifdef SUPPORT_DENORMALS
205
reg [7:0] lzc;
206
reg got_one;
207
always @*
208
begin
209
  got_one = 1'b0;
210
  lzc = 8'h00;
211
  for (n = (N+2)*4-1; n >= 0; n = n - 4) begin
212
    if (!got_one) begin
213
      if (mo4[n]|mo4[n-1]|mo4[n-2]|mo4[n-3])
214
        got_one = 1'b1;
215
      else
216
        lzc = lzc + 1'b1;
217
    end
218
  end
219
end
220
always @(posedge clk)
221
  if (ce) leadingZeros5 <= lzc;
222
`else
223
wire [7:0] lead2 = mo4[(N+2)*4-1:N*4];
224
always @(posedge clk)
225
if (ce)
226
casez(lead2)
227
8'b00000000:    leadingZeros5 <= 8'd2;
228
8'b0000????:    leadingZeros5 <= 8'd1;
229
default:                        leadingZeros5 <= 8'd0;
230
endcase
231
`endif
232
 
233
 
234
// ----------------------------------------------------------------------------
235
// Clock edge #6
236
// - Compute how much we want to decrement exponent by
237
// - compute amount to shift left and right
238
// - at infinity the exponent can't be incremented, so we can't shift right
239
//   otherwise it was an underflow situation so the exponent was negative
240
//   shift amount needs to be negated for shift register
241
// If the exponent underflowed, then the shift direction must be to the
242
// right regardless of mantissa bits; the number is denormalized.
243
// Otherwise the shift direction must be to the left.
244
// ----------------------------------------------------------------------------
245
reg [7:0] lshiftAmt6;
246
reg [7:0] rshiftAmt6;
247
wire rightOrLeft6;      // 0=left,1=right
248
wire xInf6;
249
wire [13:0] xo6;
250
wire [(N+2)*4-1:0] mo6;
251
wire zeroMan6;
252
vtdl #(1) u61 (.clk(clk), .ce(ce), .a(4'd5), .d(under_i), .q(rightOrLeft6) );
253
delay #(.WID(14),.DEP(1)) u62 (.clk(clk), .ce(ce), .i(xo5), .o(xo6));
254
delay #(.WID((N+2)*4),.DEP(2)) u63 (.clk(clk), .ce(ce), .i(mo4), .o(mo6) );
255
delay #(.WID(1),.DEP(1)) u64 (.clk(clk), .ce(ce), .i(xInf5), .o(xInf6) );
256
delay #(.WID(1),.DEP(3)) u65 (.clk(clk), .ce(ce),  .i(zeroMan3), .o(zeroMan6));
257
delay #(.WID(1),.DEP(5)) u66 (.clk(clk), .ce(ce), .i(sx0), .o(sx5) );
258
 
259
always @(posedge clk)
260
        if (ce) lshiftAmt6 <= {leadingZeros5 > xo5 ? xo5 : leadingZeros5,2'b0};
261
 
262
always @(posedge clk)
263
        if (ce) rshiftAmt6 <= {xInf5 ? 1'd0 : $signed(xo5) > 14'd0 ? 8'd0 : ~xo5+2'd1,2'b00};    // xo2 is negative !
264
 
265
// ----------------------------------------------------------------------------
266
// Clock edge #7
267
// - figure exponent
268
// - shift mantissa
269
// - figure sticky bit
270
// ----------------------------------------------------------------------------
271
 
272
reg [15:0] xo7;
273
wire rightOrLeft7;
274
reg [(N+2)*4-1:0] mo7l, mo7r;
275
reg St6,St7;
276
delay #(.WID(1),.DEP(1)) u71 (.clk(clk), .ce(ce), .i(rightOrLeft6), .o(rightOrLeft7));
277
 
278
wire [13:0] xo7d = xo6 - lshiftAmt6;
279
 
280
always @(posedge clk)
281
if (ce)
282
        xo7 <= zeroMan6 ? xo6 :
283
                xInf6 ? xo6 :                                   // an infinite exponent is either a NaN or infinity; no need to change
284
                rightOrLeft6 ? 1'd0 :   // on a right shift, the exponent was negative, it's being made to zero
285
                xo7d;                   // on a left shift, the exponent can't be decremented below zero
286
 
287
always @(posedge clk)
288
        if (ce) mo7r <= mo6 >> rshiftAmt6;
289
always @(posedge clk)
290
        if (ce) mo7l <= mo6 << lshiftAmt6;
291
 
292
// The sticky bit is set if the bits shifted out on a right shift are set.
293
always @*
294
begin
295
  St6 = 1'b0;
296
  for (n = 0; n < (N+2)*4; n = n + 1)
297
    if (n <= rshiftAmt6 + 1) St6 = St6|mo6[n];
298
end
299
always @(posedge clk)
300
  if (ce) St7 <= St6;
301
 
302
// ----------------------------------------------------------------------------
303
// Clock edge #8
304
// - select mantissa
305
// ----------------------------------------------------------------------------
306
 
307
wire so,sxo,nano,info,qnano,snano;
308
wire [13:0] xo;
309
reg [(N+2)*4-1:0] mo;
310
vtdl #(1) u81 (.clk(clk), .ce(ce), .a(4'd7), .d(so0), .q(so) );
311
delay #(.WID(14),.DEP(1)) u82 (.clk(clk), .ce(ce), .i(xo7), .o(xo));
312
vtdl #(.WID(1)) u83 (.clk(clk), .ce(ce), .a(4'd3), .d(inexact4), .q(inexact_o));
313
delay #(.WID(1),.DEP(1)) u84 (.clk(clk), .ce(ce), .i(rightOrLeft7), .o(under_o));
314
vtdl #(1) u86 (.clk(clk), .ce(ce), .a(4'd7), .d(nan0), .q(nano) );
315
vtdl #(1) u87 (.clk(clk), .ce(ce), .a(4'd7), .d(qnan0), .q(qnano) );
316
vtdl #(1) u88 (.clk(clk), .ce(ce), .a(4'd7), .d(snan0), .q(snano) );
317
vtdl #(1) u89 (.clk(clk), .ce(ce), .a(4'd7), .d(inf0), .q(info) );
318
 
319
always @(posedge clk)
320
        if (ce) mo <= rightOrLeft7 ? mo7r|{St7,4'b0} : mo7l;
321
 
322
assign o.nan = nano;
323
assign o.qnan = qnano;
324
assign o.snan = snano;
325
assign o.infinity = info;
326
assign o.sign = so;
327
assign o.exp = xo;
328
assign o.sig = mo[(N+2)*4-1:4];
329
 
330
endmodule
331
 

powered by: WebSVN 2.1.0

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