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

Subversion Repositories ft816float

[/] [ft816float/] [trunk/] [rtl/] [verilog2/] [fpNormalize.v] - Blame information for rev 69

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

Line No. Rev Author Line
1 29 robfinch
// ============================================================================
2
//        __
3
//   \\__/ o\    (C) 2006-2019  Robert Finch, Waterloo
4
//    \  __ /    All rights reserved.
5
//     \/_//     robfinch<remove>@finitron.ca
6
//       ||
7
//
8
//      fpNormalize.v
9
//    - floating point normalization unit
10
//    - eight cycle latency
11
//    - parameterized FPWIDth
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 <http://www.gnu.org/licenses/>.    
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 FPWIDth 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
`include "fpConfig.sv"
43
 
44
module fpNormalize(clk, ce, i, o, under_i, under_o, inexact_o);
45
parameter FPWID = 80;
46
`include "fpSize.sv"
47
 
48
input clk;
49
input ce;
50
input [EX:0] i;          // expanded format input
51
output [MSB+3:0] o;              // normalized output + guard, sticky and round bits, + 1 whole digit
52
input under_i;
53
output under_o;
54
output inexact_o;
55
 
56
 
57
// ----------------------------------------------------------------------------
58
// No Clock required
59
// ----------------------------------------------------------------------------
60
reg [EMSB:0] xo0;
61
reg so0;
62
 
63
always @*
64
        xo0 <= i[EX-1:FX+1];
65
always @*
66
        so0 <= i[EX];           // sign doesn't change
67
 
68
// ----------------------------------------------------------------------------
69
// Clock #1
70
// - Capture exponent information
71
// ----------------------------------------------------------------------------
72
reg xInf1a, xInf1b, xInf1c;
73
wire [FX:0] i1;
74
delay1 #(FX+1) u11 (.clk(clk), .ce(ce), .i(i), .o(i1));
75
 
76
always @(posedge clk)
77
        if (ce) xInf1a <= &xo0 & !under_i;
78
always @(posedge clk)
79
        if (ce) xInf1b <= &xo0[EMSB:1] & !under_i;
80
always @(posedge clk)
81
        if (ce) xInf1c = &xo0;
82
 
83
// ----------------------------------------------------------------------------
84
// Clock #2
85
// - determine exponent increment
86
// Since the there are *three* whole digits in the incoming format
87
// the number of whole digits needs to be reduced. If the MSB is
88
// set, then increment the exponent and no shift is needed.
89
// ----------------------------------------------------------------------------
90
wire xInf2c, xInf2b;
91
wire [EMSB:0] xo2;
92
reg incExpByOne2, incExpByTwo2;
93
delay1 u21 (.clk(clk), .ce(ce), .i(xInf1c), .o(xInf2c));
94
delay1 u22 (.clk(clk), .ce(ce), .i(xInf1b), .o(xInf2b));
95
delay2 #(EMSB+1) u23 (.clk(clk), .ce(ce), .i(xo0), .o(xo2));
96
delay2 u24 (.clk(clk), .ce(ce), .i(under_i), .o(under2));
97
 
98
always @(posedge clk)
99
        if (ce) incExpByTwo2 <= !xInf1b & i1[FX];
100
always @(posedge clk)
101
        if (ce) incExpByOne2 <= !xInf1a & i1[FX-1];
102
 
103
// ----------------------------------------------------------------------------
104
// Clock #3
105
// - increment exponent
106
// - detect a zero mantissa
107
// ----------------------------------------------------------------------------
108
 
109
wire incExpByTwo3;
110
wire incExpByOne3;
111
wire [FX:0] i3;
112
reg [EMSB:0] xo3;
113
reg zeroMan3;
114
delay1 u31 (.clk(clk), .ce(ce), .i(incExpByTwo2), .o(incExpByTwo3));
115
delay1 u32 (.clk(clk), .ce(ce), .i(incExpByOne2), .o(incExpByOne3));
116
delay3 #(FX+1) u33 (.clk(clk), .ce(ce), .i(i[FX:0]), .o(i3));
117
wire [EMSB+1:0] xv3a = xo2 + {incExpByTwo2,1'b0};
118
wire [EMSB+1:0] xv3b = xo2 + incExpByOne2;
119
 
120
always @(posedge clk)
121
        if (ce) xo3 <= xo2 + (incExpByTwo2 ? 2'd2 : incExpByOne2 ? 2'd1 : 2'd0);
122
 
123
always @(posedge clk)
124
        if(ce) zeroMan3 <= ((xv3b[EMSB+1]|| &xv3b[EMSB:0])||(xv3a[EMSB+1]| &xv3a[EMSB:0]))
125
                                                                                         && !under2 && !xInf2c;
126
 
127
// ----------------------------------------------------------------------------
128
// Clock #4
129
// - Shift mantissa left
130
// - If infinity is reached then set the mantissa to zero
131
//   shift mantissa left to reduce to a single whole digit
132
// - create sticky bit
133
// ----------------------------------------------------------------------------
134
 
135
reg [FMSB+4:0] mo4;
136
reg inexact4;
137
 
138
always @(posedge clk)
139
if(ce)
140
casez({zeroMan3,incExpByTwo3,incExpByOne3})
141
3'b1??: mo4 <= 1'd0;
142
3'b01?: mo4 <= {i3[FX:FMSB+1],|i3[FMSB:0]};
143
3'b001: mo4 <= {i3[FX-1:FMSB],|i3[FMSB-1:0]};
144
default:        mo4 <= {i3[FX-2:FMSB-1],|i3[FMSB-2:0]};
145
endcase
146
 
147
always @(posedge clk)
148
if(ce)
149
casez({zeroMan3,incExpByTwo3,incExpByOne3})
150
3'b1??: inexact4 <= 1'd0;
151
3'b01?: inexact4 <= |i3[FMSB:0];
152
3'b001: inexact4 <= |i3[FMSB-1:0];
153
default:        inexact4 <= |i3[FMSB-2:0];
154
endcase
155
 
156
// ----------------------------------------------------------------------------
157
// Clock edge #5
158
// - count leading zeros
159
// ----------------------------------------------------------------------------
160
wire [7:0] leadingZeros5;
161
wire [EMSB:0] xo5;
162
wire xInf5;
163
delay2 #(EMSB+1) u51 (.clk(clk), .ce(ce), .i(xo3), .o(xo5));
164
delay3 #(1)      u52 (.clk(clk), .ce(ce), .i(xInf2c), .o(xInf5) );
165
 
166
generate
167
begin
168 32 robfinch
if (FPWID <= 32) begin
169 29 robfinch
cntlz32Reg clz0 (.clk(clk), .ce(ce), .i({mo4,5'b0}), .o(leadingZeros5) );
170
assign leadingZeros5[7:6] = 2'b00;
171
end
172 32 robfinch
else if (FPWID<=64) begin
173 29 robfinch
assign leadingZeros5[7] = 1'b0;
174
cntlz64Reg clz0 (.clk(clk), .ce(ce), .i({mo4,8'h0}), .o(leadingZeros5) );
175
end
176 32 robfinch
else if (FPWID<=80) begin
177 29 robfinch
assign leadingZeros5[7] = 1'b0;
178
cntlz80Reg clz0 (.clk(clk), .ce(ce), .i({mo4,12'b0}), .o(leadingZeros5) );
179
end
180 32 robfinch
else if (FPWID<=84) begin
181 29 robfinch
assign leadingZeros5[7] = 1'b0;
182
cntlz96Reg clz0 (.clk(clk), .ce(ce), .i({mo4,24'b0}), .o(leadingZeros5) );
183
end
184 32 robfinch
else if (FPWID<=96) begin
185 29 robfinch
assign leadingZeros5[7] = 1'b0;
186
cntlz96Reg clz0 (.clk(clk), .ce(ce), .i({mo4,12'b0}), .o(leadingZeros5) );
187
end
188 32 robfinch
else if (FPWID<=128)
189 29 robfinch
cntlz128Reg clz0 (.clk(clk), .ce(ce), .i({mo4,12'b0}), .o(leadingZeros5) );
190
end
191
endgenerate
192
 
193
 
194
// ----------------------------------------------------------------------------
195
// Clock edge #6
196
// - Compute how much we want to decrement exponent by
197
// - compute amount to shift left and right
198
// - at infinity the exponent can't be incremented, so we can't shift right
199
//   otherwise it was an underflow situation so the exponent was negative
200
//   shift amount needs to be negated for shift register
201
// If the exponent underflowed, then the shift direction must be to the
202
// right regardless of mantissa bits; the number is denormalized.
203
// Otherwise the shift direction must be to the left.
204
// ----------------------------------------------------------------------------
205
reg [7:0] lshiftAmt6;
206
reg [7:0] rshiftAmt6;
207
wire rightOrLeft6;      // 0=left,1=right
208
wire xInf6;
209
wire [EMSB:0] xo6;
210
wire [FMSB+4:0] mo6;
211
wire zeroMan6;
212
vtdl #(1) u61 (.clk(clk), .ce(ce), .a(4'd5), .d(under_i), .q(rightOrLeft6) );
213
delay1 #(EMSB+1) u62 (.clk(clk), .ce(ce), .i(xo5), .o(xo6));
214
delay2 #(FMSB+5) u63 (.clk(clk), .ce(ce), .i(mo4), .o(mo6) );
215
delay1 #(1)      u64 (.clk(clk), .ce(ce), .i(xInf5), .o(xInf6) );
216
delay3 u65 (.clk(clk), .ce(ce),  .i(zeroMan3), .o(zeroMan6));
217
 
218
always @(posedge clk)
219
        if (ce) lshiftAmt6 <= leadingZeros5 > xo5 ? xo5 : leadingZeros5;
220
 
221
always @(posedge clk)
222
        if (ce) rshiftAmt6 <= xInf5 ? 1'd0 : $signed(xo5) > 1'd0 ? 1'd0 : ~xo5+2'd1;    // xo2 is negative !
223
 
224
// ----------------------------------------------------------------------------
225
// Clock edge #7
226
// - fogure exponent
227
// - shift mantissa
228
// ----------------------------------------------------------------------------
229
 
230
reg [EMSB:0] xo7;
231
wire rightOrLeft7;
232
reg [FMSB+4:0] mo7l, mo7r;
233
delay1 u71 (.clk(clk), .ce(ce), .i(rightOrLeft6), .o(rightOrLeft7));
234
 
235
always @(posedge clk)
236
if (ce)
237
        xo7 <= zeroMan6 ? xo6 :
238
                xInf6 ? xo6 :                                   // an infinite exponent is either a NaN or infinity; no need to change
239
                rightOrLeft6 ? 1'd0 :   // on a right shift, the exponent was negative, it's being made to zero
240
                xo6 - lshiftAmt6;                       // on a left shift, the exponent can't be decremented below zero
241
 
242
always @(posedge clk)
243
        if (ce) mo7r <= mo6 >> rshiftAmt6;
244
always @(posedge clk)
245
        if (ce) mo7l <= mo6 << lshiftAmt6;
246
 
247
 
248
// ----------------------------------------------------------------------------
249
// Clock edge #8
250
// - select mantissa
251
// ----------------------------------------------------------------------------
252
 
253
wire so;
254
wire [EMSB:0] xo;
255
reg [FMSB+4:0] mo;
256
vtdl #(1) u81 (.clk(clk), .ce(ce), .a(4'd7), .d(so0), .q(so) );
257
delay1 #(EMSB+1) u82 (.clk(clk), .ce(ce), .i(xo7), .o(xo));
258
vtdl u83 (.clk(clk), .ce(ce), .a(4'd3), .d(inexact4), .q(inexact_o));
259
delay1 u84 (.clk(clk), .ce(ce), .i(rightOrLeft7), .o(under_o));
260
 
261
always @(posedge clk)
262
        if (ce) mo <= rightOrLeft7 ? mo7r : mo7l;
263
 
264
assign o = {so,xo,mo[FMSB+4:1]};
265
 
266
endmodule
267
 

powered by: WebSVN 2.1.0

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