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

Subversion Repositories ft816float

[/] [ft816float/] [trunk/] [rtl/] [verilog2/] [DFPNormalize.sv] - Blame information for rev 61

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

Line No. Rev Author Line
1 50 robfinch
// ============================================================================
2
//        __
3
//   \\__/ o\    (C) 2006-2020  Robert Finch, Waterloo
4
//    \  __ /    All rights reserved.
5
//     \/_//     robfinch@finitron.ca
6
//       ||
7
//
8
//      DFPNormalize.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 fp::*;
42
 
43
module DFPNormalize(clk, ce, i, o, under_i, under_o, inexact_o);
44 55 robfinch
parameter N=33;
45 50 robfinch
input clk;
46
input ce;
47 55 robfinch
input [(N+1)*4*2+16+4-1:0] i;           // expanded format input
48
output [N*4+16+4-1+4:0] o;              // normalized output + guard, sticky and round bits, + 1 whole digit
49 50 robfinch
input under_i;
50
output under_o;
51
output inexact_o;
52
 
53
integer n;
54
// ----------------------------------------------------------------------------
55
// No Clock required
56
// ----------------------------------------------------------------------------
57
reg [15:0] xo0;
58
reg so0;
59
reg sx0;
60
reg nan0;
61
reg inf0;
62
 
63
always @*
64 55 robfinch
        xo0 <= i[(N+1)*4*2+15:(N+1)*4*2];
65 50 robfinch
always @*
66 55 robfinch
        so0 <= i[(N+1)*4*2+16+4-2];             // sign doesn't change
67 50 robfinch
always @*
68 55 robfinch
        sx0 <= i[(N+1)*4*2+16+4-4];
69 50 robfinch
always @*
70 55 robfinch
        nan0 <= i[(N+1)*4*2+16+4-1];
71 50 robfinch
always @*
72 55 robfinch
        inf0 <= i[(N+1)*4*2+16+4-3] || xo0==16'h9999 && i[(N+1)*4*2-4];
73 50 robfinch
 
74
// ----------------------------------------------------------------------------
75
// Clock #1
76
// - Capture exponent information
77
// ----------------------------------------------------------------------------
78
reg xInf1a, xInf1b, xInf1c;
79 55 robfinch
wire [(N+1)*4*2+16+4-1:0] i1;
80
delay #(.WID((N+2)*4*2+16+4),.DEP(1)) u11 (.clk(clk), .ce(ce), .i(i), .o(i1));
81 50 robfinch
 
82
always @(posedge clk)
83
        if (ce) xInf1a <= xo0==16'h9999 & !under_i;
84
always @(posedge clk)
85
        if (ce) xInf1b <= xo0==16'h9998 & !under_i;
86
always @(posedge clk)
87
        if (ce) xInf1c <= xo0==16'h9999;
88
 
89
// ----------------------------------------------------------------------------
90
// Clock #2
91
// - determine exponent increment
92
// Since the there are *three* whole digits in the incoming format
93
// the number of whole digits needs to be reduced. If the MSB is
94
// set, then increment the exponent and no shift is needed.
95
// ----------------------------------------------------------------------------
96
wire xInf2c, xInf2b;
97
wire [15:0] xo2;
98
reg incExpByOne2, incExpByTwo2;
99
delay #(.WID(1),.DEP(1)) u21 (.clk(clk), .ce(ce), .i(xInf1c), .o(xInf2c));
100
delay #(.WID(1),.DEP(1)) u22 (.clk(clk), .ce(ce), .i(xInf1b), .o(xInf2b));
101
delay #(.WID(16),.DEP(2)) u23 (.clk(clk), .ce(ce), .i(xo0), .o(xo2));
102
delay #(.WID(1),.DEP(2)) u24 (.clk(clk), .ce(ce), .i(under_i), .o(under2));
103
 
104
always @(posedge clk)
105 55 robfinch
        if (ce) incExpByOne2 <= !xInf1a & i1[(N+1)*4*2-4];
106 50 robfinch
 
107
// ----------------------------------------------------------------------------
108
// Clock #3
109
// - increment exponent
110
// - detect a zero mantissa
111
// ----------------------------------------------------------------------------
112
 
113
wire incExpByOne3;
114 55 robfinch
wire [(N+1)*4*2+16+4-1:0] i3;
115 50 robfinch
reg [15:0] xo3;
116
reg zeroMan3;
117
delay #(.WID(1),.DEP(1)) u32 (.clk(clk), .ce(ce), .i(incExpByOne2), .o(incExpByOne3));
118 55 robfinch
delay #(.WID((N+1)*4*2+16+4),.DEP(3)) u33 (.clk(clk), .ce(ce), .i(i[(N+3)*4*2+16+4-1:0]), .o(i3));
119 50 robfinch
 
120
wire [15:0] xo2a;
121
BCDAddN #(.N(4)) ubcdan1
122
(
123
        .ci(1'b0),
124
        .a(xo2),
125
        .b(16'h0001),
126
        .o(xo2a),
127
        .co()
128
);
129
 
130
always @(posedge clk)
131
        if (ce) xo3 <= (incExpByOne2 ? xo2a : xo2);
132
 
133
always @(posedge clk)
134
        if(ce) zeroMan3 <= 1'b0;
135
 
136
// ----------------------------------------------------------------------------
137
// Clock #4
138
// - Shift mantissa left
139
// - If infinity is reached then set the mantissa to zero
140
//   shift mantissa left to reduce to a single whole digit
141
// - create sticky bit
142
// ----------------------------------------------------------------------------
143
 
144 55 robfinch
reg [(N+2)*4-1:0] mo4;
145 50 robfinch
reg inexact4;
146
 
147
always @(posedge clk)
148
if(ce)
149
casez({zeroMan3,incExpByOne3})
150
2'b1?:  mo4 <= 1'd0;
151 55 robfinch
2'b01:  mo4 <= {i3[(N+1)*4*2-1:(N+1)*4],3'b0,|i3[(N+1)*4-1:0]};
152
default:        mo4 <= {i3[(N+1)*4*2-1-4:N*4],3'b0,|i3[N*4-1:0]};
153 50 robfinch
endcase
154
 
155
always @(posedge clk)
156
if(ce)
157
casez({zeroMan3,incExpByOne3})
158
2'b1?:  inexact4 <= 1'd0;
159 55 robfinch
2'b01:  inexact4 <= |i3[(N+1)*4-1:0];
160
default:        inexact4 <= |i3[N*4-1:0];
161 50 robfinch
endcase
162
 
163
// ----------------------------------------------------------------------------
164
// Clock edge #5
165
// - count leading zeros
166
// ----------------------------------------------------------------------------
167
reg [7:0] leadingZeros5;
168
wire [15:0] xo5;
169
wire xInf5;
170
delay #(.WID(16),.DEP(2)) u51 (.clk(clk), .ce(ce), .i(xo3), .o(xo5));
171
delay #(.WID(1),.DEP(3)) u52 (.clk(clk), .ce(ce), .i(xInf2c), .o(xInf5) );
172
 
173
/* Lookup table based leading zero count modules give slightly better
174
   performance but cases must be coded.
175
generate
176
begin
177
if (FPWID <= 32) begin
178
cntlz32Reg clz0 (.clk(clk), .ce(ce), .i({mo4,4'b0}), .o(leadingZeros5) );
179
assign leadingZeros5[7:6] = 2'b00;
180
end
181
else if (FPWID<=64) begin
182
assign leadingZeros5[7] = 1'b0;
183
cntlz64Reg clz0 (.clk(clk), .ce(ce), .i({mo4,7'h0}), .o(leadingZeros5) );
184
end
185
else if (FPWID<=80) begin
186
assign leadingZeros5[7] = 1'b0;
187
cntlz80Reg clz0 (.clk(clk), .ce(ce), .i({mo4,11'b0}), .o(leadingZeros5) );
188
end
189
else if (FPWID<=84) begin
190
assign leadingZeros5[7] = 1'b0;
191
cntlz96Reg clz0 (.clk(clk), .ce(ce), .i({mo4,23'b0}), .o(leadingZeros5) );
192
end
193
else if (FPWID<=96) begin
194
assign leadingZeros5[7] = 1'b0;
195
cntlz96Reg clz0 (.clk(clk), .ce(ce), .i({mo4,11'b0}), .o(leadingZeros5) );
196
end
197
else if (FPWID<=128)
198
cntlz128Reg clz0 (.clk(clk), .ce(ce), .i({mo4,11'b0}), .o(leadingZeros5) );
199
end
200
endgenerate
201
*/
202
 
203
// Sideways add.
204
// Normally there would be only one to two leading zeros. It is tempting then
205
// to check for only one or two. But, denormalized numbers might have more
206
// leading zeros. If denormals were not supported this could be made smaller
207
// and faster.
208
`ifdef SUPPORT_DENORMALS
209
reg [7:0] lzc;
210
reg got_one;
211
always @*
212
begin
213
  got_one = 1'b0;
214
  lzc = 8'h00;
215 55 robfinch
  for (n = (N+2)*4-1; n >= 0; n = n - 4) begin
216 50 robfinch
    if (!got_one) begin
217
      if (mo4[n]|mo4[n-1]|mo4[n-2]|mo4[n-3])
218
        got_one = 1'b1;
219
      else
220
        lzc = lzc + 1'b1;
221
    end
222
  end
223
end
224
always @(posedge clk)
225
  if (ce) leadingZeros5 <= lzc;
226
`else
227
always @(posedge clk)
228
if (ce)
229 55 robfinch
casez(mo4[(N+1)*4-1:(N-1)*4-1])
230 57 robfinch
8'b00000000:    leadingZeros5 <= 8'd2;
231
8'b0000????:    leadingZeros5 <= 8'd1;
232 50 robfinch
default:                        leadingZeros5 <= 8'd0;
233
endcase
234
`endif
235
 
236
 
237
// ----------------------------------------------------------------------------
238
// Clock edge #6
239
// - Compute how much we want to decrement exponent by
240
// - compute amount to shift left and right
241
// - at infinity the exponent can't be incremented, so we can't shift right
242
//   otherwise it was an underflow situation so the exponent was negative
243
//   shift amount needs to be negated for shift register
244
// If the exponent underflowed, then the shift direction must be to the
245
// right regardless of mantissa bits; the number is denormalized.
246
// Otherwise the shift direction must be to the left.
247
// ----------------------------------------------------------------------------
248
reg [7:0] lshiftAmt6;
249
reg [7:0] rshiftAmt6;
250
wire rightOrLeft6;      // 0=left,1=right
251
wire xInf6;
252
wire [15:0] xo6;
253 55 robfinch
wire [(N+2)*4-1:0] mo6;
254 50 robfinch
wire zeroMan6;
255
vtdl #(1) u61 (.clk(clk), .ce(ce), .a(4'd5), .d(under_i), .q(rightOrLeft6) );
256
delay #(.WID(16),.DEP(1)) u62 (.clk(clk), .ce(ce), .i(xo5), .o(xo6));
257 55 robfinch
delay #(.WID((N+2)*4),.DEP(2)) u63 (.clk(clk), .ce(ce), .i(mo4), .o(mo6) );
258 50 robfinch
delay #(.WID(1),.DEP(1)) u64 (.clk(clk), .ce(ce), .i(xInf5), .o(xInf6) );
259
delay #(.WID(1),.DEP(3)) u65 (.clk(clk), .ce(ce),  .i(zeroMan3), .o(zeroMan6));
260
delay #(.WID(1),.DEP(5)) u66 (.clk(clk), .ce(ce), .i(sx0), .o(sx5) );
261
 
262
wire [13:0] xo5d = xo5[3:0] + xo5[7:4] * 10 + xo5[11:8] * 100 + xo5[15:12] * 1000;
263
 
264
always @(posedge clk)
265
        if (ce) lshiftAmt6 <= {leadingZeros5 > xo5d ? xo5d : leadingZeros5,2'b0};
266
 
267
always @(posedge clk)
268 55 robfinch
        if (ce) rshiftAmt6 <= xInf5 ? 1'd0 : sx5 ? 1'd0 : xo5d > N ? N*4 : {xo5d[5:0],2'b00};        // xo2 is negative !
269 50 robfinch
 
270
// ----------------------------------------------------------------------------
271
// Clock edge #7
272
// - figure exponent
273
// - shift mantissa
274
// - figure sticky bit
275
// ----------------------------------------------------------------------------
276
 
277
reg [15:0] xo7;
278
wire rightOrLeft7;
279 55 robfinch
reg [(N+2)*4-1:0] mo7l, mo7r;
280 50 robfinch
reg St6,St7;
281
delay #(.WID(1),.DEP(1)) u71 (.clk(clk), .ce(ce), .i(rightOrLeft6), .o(rightOrLeft7));
282
 
283
wire [11:0] lshftAmtBCD;
284
wire [15:0] xo7d;
285
BinToBCD ubbcd1 (lshiftAmt6, lshftAmtBCD);
286
BCDSubN #(.N(4)) ubcdsn1
287
(
288
        .ci(1'b0),
289
        .a(xo6),
290
        .b({4'h0,lshftAmtBCD}),
291
        .o(xo7d),
292
        .co()
293
);
294
 
295
 
296
always @(posedge clk)
297
if (ce)
298
        xo7 <= zeroMan6 ? xo6 :
299
                xInf6 ? xo6 :                                   // an infinite exponent is either a NaN or infinity; no need to change
300
                rightOrLeft6 ? 1'd0 :   // on a right shift, the exponent was negative, it's being made to zero
301
                xo7d;                   // on a left shift, the exponent can't be decremented below zero
302
 
303
always @(posedge clk)
304
        if (ce) mo7r <= mo6 >> rshiftAmt6;
305
always @(posedge clk)
306
        if (ce) mo7l <= mo6 << lshiftAmt6;
307
 
308
// The sticky bit is set if the bits shifted out on a right shift are set.
309
always @*
310
begin
311
  St6 = 1'b0;
312 55 robfinch
  for (n = 0; n < (N+2)*4; n = n + 1)
313 50 robfinch
    if (n <= rshiftAmt6 + 1) St6 = St6|mo6[n];
314
end
315
always @(posedge clk)
316
  if (ce) St7 <= St6;
317
 
318
// ----------------------------------------------------------------------------
319
// Clock edge #8
320
// - select mantissa
321
// ----------------------------------------------------------------------------
322
 
323
wire so,sxo,nano,info;
324
wire [15:0] xo;
325 55 robfinch
reg [(N+2)*4-1:0] mo;
326 50 robfinch
vtdl #(1) u81 (.clk(clk), .ce(ce), .a(4'd7), .d(so0), .q(so) );
327
delay #(.WID(16),.DEP(1)) u82 (.clk(clk), .ce(ce), .i(xo7), .o(xo));
328
vtdl #(.WID(1)) u83 (.clk(clk), .ce(ce), .a(4'd3), .d(inexact4), .q(inexact_o));
329
delay #(.WID(1),.DEP(1)) u84 (.clk(clk), .ce(ce), .i(rightOrLeft7), .o(under_o));
330
vtdl #(1) u85 (.clk(clk), .ce(ce), .a(4'd7), .d(sx0), .q(sxo) );
331
vtdl #(1) u86 (.clk(clk), .ce(ce), .a(4'd7), .d(nan0), .q(nano) );
332
vtdl #(1) u87 (.clk(clk), .ce(ce), .a(4'd7), .d(inf0), .q(info) );
333
 
334
always @(posedge clk)
335
        if (ce) mo <= rightOrLeft7 ? mo7r|{St7,4'b0} : mo7l;
336
 
337 55 robfinch
assign o = {nano,so,info,sxo,xo,mo[(N+2)*4-1:4]};
338 50 robfinch
 
339
endmodule
340
 

powered by: WebSVN 2.1.0

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