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

Subversion Repositories ft816float

[/] [ft816float/] [trunk/] [rtl/] [verilog2/] [fpNormalize.sv] - Blame information for rev 49

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 48 robfinch
// ============================================================================
2
//        __
3
//   \\__/ o\    (C) 2006-2020  Robert Finch, Waterloo
4
//    \  __ /    All rights reserved.
5
//     \/_//     robfinch@finitron.ca
6
//       ||
7
//
8
//      fpNormalize.sv
9
//    - floating point normalization unit
10
//    - eight cycle latency
11
//    - parameterized width
12
//    - IEEE 754 representation
13
//
14
//
15
// This source file is free software: you can redistribute it and/or modify
16
// it under the terms of the GNU Lesser General Public License as published
17
// by the Free Software Foundation, either version 3 of the License, or
18
// (at your option) any later version.
19
//
20
// This source file is distributed in the hope that it will be useful,
21
// but WITHOUT ANY WARRANTY; without even the implied warranty of
22
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
// GNU General Public License for more details.
24
//
25
// You should have received a copy of the GNU General Public License
26
// along with this program.  If not, see .
27
//
28
//      This unit takes a floating point number in an intermediate
29
// format and normalizes it. No normalization occurs
30
// for NaN's or infinities. The unit has a two cycle latency.
31
//
32
// The mantissa is assumed to start with two whole bits on
33
// the left. The remaining bits are fractional.
34
//
35
// The width of the incoming format is reduced via a generation
36
// of sticky bit in place of the low order fractional bits.
37
//
38
// On an underflowed input, the incoming exponent is assumed
39
// to be negative. A right shift is needed.
40
// ============================================================================
41
 
42
import fp::*;
43
 
44
module fpNormalize(clk, ce, i, o, under_i, under_o, inexact_o);
45
input clk;
46
input ce;
47
input [EX:0] i;         // expanded format input
48
output [MSB+3:0] 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 49 robfinch
integer n;
54 48 robfinch
// ----------------------------------------------------------------------------
55
// No Clock required
56
// ----------------------------------------------------------------------------
57
reg [EMSB:0] xo0;
58
reg so0;
59
 
60
always @*
61
        xo0 <= i[EX-1:FX+1];
62
always @*
63
        so0 <= i[EX];           // sign doesn't change
64
 
65
// ----------------------------------------------------------------------------
66
// Clock #1
67
// - Capture exponent information
68
// ----------------------------------------------------------------------------
69
reg xInf1a, xInf1b, xInf1c;
70
wire [FX:0] i1;
71
delay1 #(FX+1) u11 (.clk(clk), .ce(ce), .i(i), .o(i1));
72
 
73
always @(posedge clk)
74
        if (ce) xInf1a <= &xo0 & !under_i;
75
always @(posedge clk)
76
        if (ce) xInf1b <= &xo0[EMSB:1] & !under_i;
77
always @(posedge clk)
78
        if (ce) xInf1c = &xo0;
79
 
80
// ----------------------------------------------------------------------------
81
// Clock #2
82
// - determine exponent increment
83
// Since the there are *three* whole digits in the incoming format
84
// the number of whole digits needs to be reduced. If the MSB is
85
// set, then increment the exponent and no shift is needed.
86
// ----------------------------------------------------------------------------
87
wire xInf2c, xInf2b;
88
wire [EMSB:0] xo2;
89
reg incExpByOne2, incExpByTwo2;
90
delay1 u21 (.clk(clk), .ce(ce), .i(xInf1c), .o(xInf2c));
91
delay1 u22 (.clk(clk), .ce(ce), .i(xInf1b), .o(xInf2b));
92
delay2 #(EMSB+1) u23 (.clk(clk), .ce(ce), .i(xo0), .o(xo2));
93
delay2 u24 (.clk(clk), .ce(ce), .i(under_i), .o(under2));
94
 
95
always @(posedge clk)
96
        if (ce) incExpByTwo2 <= !xInf1b & i1[FX];
97
always @(posedge clk)
98
        if (ce) incExpByOne2 <= !xInf1a & i1[FX-1];
99
 
100
// ----------------------------------------------------------------------------
101
// Clock #3
102
// - increment exponent
103
// - detect a zero mantissa
104
// ----------------------------------------------------------------------------
105
 
106
wire incExpByTwo3;
107
wire incExpByOne3;
108
wire [FX:0] i3;
109
reg [EMSB:0] xo3;
110
reg zeroMan3;
111
delay1 u31 (.clk(clk), .ce(ce), .i(incExpByTwo2), .o(incExpByTwo3));
112
delay1 u32 (.clk(clk), .ce(ce), .i(incExpByOne2), .o(incExpByOne3));
113
delay3 #(FX+1) u33 (.clk(clk), .ce(ce), .i(i[FX:0]), .o(i3));
114
wire [EMSB+1:0] xv3a = xo2 + {incExpByTwo2,1'b0};
115
wire [EMSB+1:0] xv3b = xo2 + incExpByOne2;
116
 
117
always @(posedge clk)
118
        if (ce) xo3 <= xo2 + (incExpByTwo2 ? 2'd2 : incExpByOne2 ? 2'd1 : 2'd0);
119
 
120
always @(posedge clk)
121
        if(ce) zeroMan3 <= ((xv3b[EMSB+1]|| &xv3b[EMSB:0])||(xv3a[EMSB+1]| &xv3a[EMSB:0]))
122
                                                                                         && !under2 && !xInf2c;
123
 
124
// ----------------------------------------------------------------------------
125
// Clock #4
126
// - Shift mantissa left
127
// - If infinity is reached then set the mantissa to zero
128
//   shift mantissa left to reduce to a single whole digit
129
// - create sticky bit
130
// ----------------------------------------------------------------------------
131
 
132
reg [FMSB+4:0] mo4;
133
reg inexact4;
134
 
135
always @(posedge clk)
136
if(ce)
137
casez({zeroMan3,incExpByTwo3,incExpByOne3})
138
3'b1??: mo4 <= 1'd0;
139
3'b01?: mo4 <= {i3[FX:FMSB+1],|i3[FMSB:0]};
140
3'b001: mo4 <= {i3[FX-1:FMSB],|i3[FMSB-1:0]};
141
default:        mo4 <= {i3[FX-2:FMSB-1],|i3[FMSB-2:0]};
142
endcase
143
 
144
always @(posedge clk)
145
if(ce)
146
casez({zeroMan3,incExpByTwo3,incExpByOne3})
147
3'b1??: inexact4 <= 1'd0;
148
3'b01?: inexact4 <= |i3[FMSB:0];
149
3'b001: inexact4 <= |i3[FMSB-1:0];
150
default:        inexact4 <= |i3[FMSB-2:0];
151
endcase
152
 
153
// ----------------------------------------------------------------------------
154
// Clock edge #5
155
// - count leading zeros
156
// ----------------------------------------------------------------------------
157
wire [7:0] leadingZeros5;
158
wire [EMSB:0] xo5;
159
wire xInf5;
160
delay2 #(EMSB+1) u51 (.clk(clk), .ce(ce), .i(xo3), .o(xo5));
161
delay3 #(1)      u52 (.clk(clk), .ce(ce), .i(xInf2c), .o(xInf5) );
162
 
163
generate
164
begin
165
if (FPWID <= 32) begin
166
cntlz32Reg clz0 (.clk(clk), .ce(ce), .i({mo4,5'b0}), .o(leadingZeros5) );
167
assign leadingZeros5[7:6] = 2'b00;
168
end
169
else if (FPWID<=64) begin
170
assign leadingZeros5[7] = 1'b0;
171
cntlz64Reg clz0 (.clk(clk), .ce(ce), .i({mo4,8'h0}), .o(leadingZeros5) );
172
end
173
else if (FPWID<=80) begin
174
assign leadingZeros5[7] = 1'b0;
175
cntlz80Reg clz0 (.clk(clk), .ce(ce), .i({mo4,12'b0}), .o(leadingZeros5) );
176
end
177
else if (FPWID<=84) begin
178
assign leadingZeros5[7] = 1'b0;
179
cntlz96Reg clz0 (.clk(clk), .ce(ce), .i({mo4,24'b0}), .o(leadingZeros5) );
180
end
181
else if (FPWID<=96) begin
182
assign leadingZeros5[7] = 1'b0;
183
cntlz96Reg clz0 (.clk(clk), .ce(ce), .i({mo4,12'b0}), .o(leadingZeros5) );
184
end
185
else if (FPWID<=128)
186
cntlz128Reg clz0 (.clk(clk), .ce(ce), .i({mo4,12'b0}), .o(leadingZeros5) );
187
end
188
endgenerate
189
 
190
 
191
// ----------------------------------------------------------------------------
192
// Clock edge #6
193
// - Compute how much we want to decrement exponent by
194
// - compute amount to shift left and right
195
// - at infinity the exponent can't be incremented, so we can't shift right
196
//   otherwise it was an underflow situation so the exponent was negative
197
//   shift amount needs to be negated for shift register
198
// If the exponent underflowed, then the shift direction must be to the
199
// right regardless of mantissa bits; the number is denormalized.
200
// Otherwise the shift direction must be to the left.
201
// ----------------------------------------------------------------------------
202
reg [7:0] lshiftAmt6;
203
reg [7:0] rshiftAmt6;
204
wire rightOrLeft6;      // 0=left,1=right
205
wire xInf6;
206
wire [EMSB:0] xo6;
207
wire [FMSB+4:0] mo6;
208
wire zeroMan6;
209
vtdl #(1) u61 (.clk(clk), .ce(ce), .a(4'd5), .d(under_i), .q(rightOrLeft6) );
210
delay1 #(EMSB+1) u62 (.clk(clk), .ce(ce), .i(xo5), .o(xo6));
211
delay2 #(FMSB+5) u63 (.clk(clk), .ce(ce), .i(mo4), .o(mo6) );
212
delay1 #(1)      u64 (.clk(clk), .ce(ce), .i(xInf5), .o(xInf6) );
213
delay3 u65 (.clk(clk), .ce(ce),  .i(zeroMan3), .o(zeroMan6));
214
 
215
always @(posedge clk)
216
        if (ce) lshiftAmt6 <= leadingZeros5 > xo5 ? xo5 : leadingZeros5;
217
 
218
always @(posedge clk)
219
        if (ce) rshiftAmt6 <= xInf5 ? 1'd0 : $signed(xo5) > 1'd0 ? 1'd0 : ~xo5+2'd1;    // xo2 is negative !
220
 
221
// ----------------------------------------------------------------------------
222
// Clock edge #7
223 49 robfinch
// - figure exponent
224 48 robfinch
// - shift mantissa
225 49 robfinch
// - figure sticky bit
226 48 robfinch
// ----------------------------------------------------------------------------
227
 
228
reg [EMSB:0] xo7;
229
wire rightOrLeft7;
230
reg [FMSB+4:0] mo7l, mo7r;
231 49 robfinch
reg St6,St7;
232 48 robfinch
delay1 u71 (.clk(clk), .ce(ce), .i(rightOrLeft6), .o(rightOrLeft7));
233
 
234
always @(posedge clk)
235
if (ce)
236
        xo7 <= zeroMan6 ? xo6 :
237
                xInf6 ? xo6 :                                   // an infinite exponent is either a NaN or infinity; no need to change
238
                rightOrLeft6 ? 1'd0 :   // on a right shift, the exponent was negative, it's being made to zero
239
                xo6 - lshiftAmt6;                       // on a left shift, the exponent can't be decremented below zero
240
 
241
always @(posedge clk)
242
        if (ce) mo7r <= mo6 >> rshiftAmt6;
243
always @(posedge clk)
244
        if (ce) mo7l <= mo6 << lshiftAmt6;
245
 
246 49 robfinch
// The sticky bit is set if the bits shifted out on a right shift are set.
247
always @*
248
begin
249
  St6 = 1'b0;
250
  for (n = 0; n < FMSB+5; n = n + 1)
251
    if (n <= rshiftAmt6 + 1) St6 = St6|mo6[n];
252
end
253
always @(posedge clk)
254
  if (ce) St7 <= St6;
255 48 robfinch
 
256
// ----------------------------------------------------------------------------
257
// Clock edge #8
258
// - select mantissa
259
// ----------------------------------------------------------------------------
260
 
261
wire so;
262
wire [EMSB:0] xo;
263
reg [FMSB+4:0] mo;
264
vtdl #(1) u81 (.clk(clk), .ce(ce), .a(4'd7), .d(so0), .q(so) );
265
delay1 #(EMSB+1) u82 (.clk(clk), .ce(ce), .i(xo7), .o(xo));
266
vtdl u83 (.clk(clk), .ce(ce), .a(4'd3), .d(inexact4), .q(inexact_o));
267
delay1 u84 (.clk(clk), .ce(ce), .i(rightOrLeft7), .o(under_o));
268
 
269
always @(posedge clk)
270 49 robfinch
        if (ce) mo <= rightOrLeft7 ? mo7r|{St7,1'b0} : mo7l;
271 48 robfinch
 
272
assign o = {so,xo,mo[FMSB+4:1]};
273
 
274
endmodule
275
 

powered by: WebSVN 2.1.0

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