1 |
6 |
robfinch |
// ============================================================================
|
2 |
|
|
// __
|
3 |
|
|
// \\__/ o\ (C) 2006-2016 Robert Finch, Stratford
|
4 |
|
|
// \ __ / All rights reserved.
|
5 |
|
|
// \/_// robfinch<remove>@finitron.ca
|
6 |
|
|
// ||
|
7 |
|
|
//
|
8 |
|
|
// This source file is free software: you can redistribute it and/or modify
|
9 |
|
|
// it under the terms of the GNU Lesser General Public License as published
|
10 |
|
|
// by the Free Software Foundation, either version 3 of the License, or
|
11 |
|
|
// (at your option) any later version.
|
12 |
|
|
//
|
13 |
|
|
// This source file is distributed in the hope that it will be useful,
|
14 |
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15 |
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
16 |
|
|
// GNU General Public License for more details.
|
17 |
|
|
//
|
18 |
|
|
// You should have received a copy of the GNU General Public License
|
19 |
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
20 |
|
|
//
|
21 |
|
|
// fpNormalize.v
|
22 |
|
|
// - floating point normalization unit
|
23 |
|
|
// - two cycle latency
|
24 |
|
|
// - parameterized width
|
25 |
|
|
//
|
26 |
|
|
// This unit takes a floating point number in an intermediate
|
27 |
|
|
// format and normalizes it. No normalization occurs
|
28 |
|
|
// for NaN's or infinities. The unit has a two cycle latency.
|
29 |
|
|
//
|
30 |
|
|
// The mantissa is assumed to start with three whole bits on
|
31 |
|
|
// the left. The remaining bits are fractional. The three whole bits
|
32 |
|
|
// result from a MAC (multiply accumulate) operation. The result from
|
33 |
|
|
// a MAC can vary from 0 to 8 which requires three whole digits.
|
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 |
|
|
module fpNormalize(clk, ce, under, i, o);
|
43 |
|
|
parameter WID = 32;
|
44 |
|
|
localparam MSB = WID-1;
|
45 |
|
|
localparam EMSB =
|
46 |
|
|
WID==80 ? 14 :
|
47 |
|
|
WID==64 ? 10 :
|
48 |
|
|
WID==52 ? 10 :
|
49 |
|
|
WID==48 ? 10 :
|
50 |
|
|
WID==44 ? 10 :
|
51 |
|
|
WID==42 ? 10 :
|
52 |
|
|
WID==40 ? 9 :
|
53 |
|
|
WID==32 ? 7 :
|
54 |
|
|
WID==24 ? 6 : 4;
|
55 |
|
|
localparam FMSB =
|
56 |
|
|
WID==80 ? 63 :
|
57 |
|
|
WID==64 ? 51 :
|
58 |
|
|
WID==52 ? 39 :
|
59 |
|
|
WID==48 ? 35 :
|
60 |
|
|
WID==44 ? 31 :
|
61 |
|
|
WID==42 ? 29 :
|
62 |
|
|
WID==40 ? 28 :
|
63 |
|
|
WID==32 ? 22 :
|
64 |
|
|
WID==24 ? 15 : 9;
|
65 |
|
|
|
66 |
|
|
localparam WX = 3; // Three whole digits
|
67 |
|
|
localparam FX = (FMSB+1)*2-1; // the MSB of the expanded fraction
|
68 |
|
|
// Fraction + Three whole bits
|
69 |
|
|
localparam EX = FX + WX + EMSB + 1; // The MSB of the exponent
|
70 |
|
|
|
71 |
|
|
input clk;
|
72 |
|
|
input ce;
|
73 |
|
|
input under;
|
74 |
|
|
input [EX+1:0] i; // expanded format input
|
75 |
|
|
output [WID+3:0] o; // normalized output + guard, sticky and round bits, + 1 whole digit
|
76 |
|
|
|
77 |
|
|
wire [EMSB:0] infXp = {EMSB+1{1'b1}}; // simple constant - value of exp for inifinity
|
78 |
|
|
|
79 |
|
|
// variables
|
80 |
|
|
wire so;
|
81 |
|
|
|
82 |
|
|
wire so1 = i[EX+1]; // sign doesn't change
|
83 |
|
|
|
84 |
|
|
// Since the there are *three* whole digits in the incoming format
|
85 |
|
|
// the number of whole digits needs to be reduced. If the MSB is
|
86 |
|
|
// set, then increment the exponent by two and no shift is needed.
|
87 |
|
|
// Otherwise if the next MSB is set, increment the exponent by one,
|
88 |
|
|
// and shift left once.
|
89 |
|
|
wire [EMSB:0] xo;
|
90 |
|
|
wire [EMSB:0] xo1a = i[EX:FX+WX+1];
|
91 |
|
|
|
92 |
|
|
wire incExp2 = i[FX+WX-1]|i[FX+WX-2];
|
93 |
|
|
// Allow an extra bit for exponent overflow
|
94 |
|
|
// Add two to exponent to shift the decimal place left twice.
|
95 |
|
|
// (Gives 1 leading whole digit).
|
96 |
|
|
wire [EMSB+1:0] xo1b = xo1a + 2;
|
97 |
|
|
wire [EMSB:0] xo1;
|
98 |
|
|
wire [EMSB:0] xo2;
|
99 |
|
|
wire xInf1a = &xo1a[EMSB:0];
|
100 |
|
|
|
101 |
|
|
// If there was a carry from the addition and we were in the underflow
|
102 |
|
|
// state, then the number became normal again. Clear the carry bit.
|
103 |
|
|
// Otherwise if the exponent overflowed and it's not the underflow
|
104 |
|
|
// state, then set the exponent to infinity. Othwerise just keep the
|
105 |
|
|
// remaining exponent bits - the result is still underflowed.
|
106 |
|
|
assign xo1 = (under & xo1b[EMSB+1]) ? xo1b[EMSB:0] :
|
107 |
|
|
(xInf1a & !under) ? infXp : xo1b[EMSB+1] ? infXp : xo1b;
|
108 |
|
|
wire xInf = &xo1 & !under;
|
109 |
|
|
wire under1 = under & !xo1b[EMSB+1]; // keep trakc of renormallzation
|
110 |
|
|
|
111 |
|
|
// shift mantissa left by one to reduce to a single whole digit
|
112 |
|
|
// if there is no exponent increment
|
113 |
|
|
wire [FMSB+1+3:0] mo; //GRS+1whole digit
|
114 |
|
|
wire [FX+WX:0] mo1 = xInf & incExp2 ? 0 : // set mantissa to zero for infinity
|
115 |
|
|
i[FX+WX:0];
|
116 |
|
|
wire [FX+WX:0] mo2;
|
117 |
|
|
wire [7:0] leadingZeros2;
|
118 |
|
|
|
119 |
|
|
// Adjust the operand to the leading zero counter by left aligning it
|
120 |
|
|
// by padding trailing zeros. This is a constant shift that doesn't take
|
121 |
|
|
// any hardware.
|
122 |
|
|
generate
|
123 |
|
|
begin
|
124 |
|
|
if (WID==64) begin
|
125 |
|
|
wire [127:0] mo1a = {mo1,{127-(FX+3){1'b0}}};
|
126 |
|
|
cntlz128Reg clz0 (.clk(clk), .ce(ce), .i(mo1a), .o(leadingZeros2) );
|
127 |
|
|
end
|
128 |
|
|
else begin // 32 bits
|
129 |
|
|
wire [63:0] mo1a = {mo1,{63-(FX+3){1'b0}}};
|
130 |
|
|
cntlz64Reg clz0 (.clk(clk), .ce(ce), .i(mo1a), .o(leadingZeros2) );
|
131 |
|
|
assign leadingZeros2[7] = 1'b0;
|
132 |
|
|
end
|
133 |
|
|
end
|
134 |
|
|
endgenerate
|
135 |
|
|
|
136 |
|
|
// compensate for leadingZeros delay
|
137 |
|
|
wire xInf2;
|
138 |
|
|
delay1 #(EMSB+1) d2(.clk(clk), .ce(ce), .i(xo1), .o(xo2) );
|
139 |
|
|
delay1 #(1) d3(.clk(clk), .ce(ce), .i(xInf), .o(xInf2) );
|
140 |
|
|
|
141 |
|
|
// If the exponent underflowed, then the shift direction must be to the
|
142 |
|
|
// right regardless of mantissa bits; the number is denormalized.
|
143 |
|
|
// Otherwise the shift direction must be to the left.
|
144 |
|
|
wire rightOrLeft2; // 0=left,1=right
|
145 |
|
|
delay1 #(1) d8(.clk(clk), .ce(ce), .i(under1), .o(rightOrLeft2) );
|
146 |
|
|
|
147 |
|
|
// Compute how much we want to decrement by. We can't decrement by
|
148 |
|
|
// more than the exponent as the number becomes denormal when the
|
149 |
|
|
// exponent reaches zero.
|
150 |
|
|
wire [7:0] lshiftAmt2 = leadingZeros2 > xo2 ? xo2 : leadingZeros2;
|
151 |
|
|
|
152 |
|
|
// compute amount to shift right
|
153 |
|
|
// at infinity the exponent can't be incremented, so we can't shift right
|
154 |
|
|
// otherwise it was an underflow situation so the exponent was negative
|
155 |
|
|
// shift amount needs to be negated for shift register
|
156 |
|
|
wire [EMSB:0] nxo2 = -xo2;
|
157 |
|
|
wire [7:0] rshiftAmt2 = xInf2 ? 0 : nxo2 > FMSB+WX ? FMSB+WX+1 : nxo2; // xo2 is negative !
|
158 |
|
|
|
159 |
|
|
|
160 |
|
|
// sign
|
161 |
|
|
// the output sign is the same as the input sign
|
162 |
|
|
delay1 #(1) d7(.clk(clk), .ce(ce), .i(so1), .o(so) );
|
163 |
|
|
|
164 |
|
|
// exponent
|
165 |
|
|
// always @(posedge clk)
|
166 |
|
|
// if (ce)
|
167 |
|
|
assign xo =
|
168 |
|
|
xInf2 ? xo2 : // an infinite exponent is either a NaN or infinity; no need to change
|
169 |
|
|
rightOrLeft2 ? 0 : // on a right shift, the exponent was negative, it's being made to zero
|
170 |
|
|
xo2 - lshiftAmt2; // on a left shift, the exponent can't be decremented below zero
|
171 |
|
|
|
172 |
|
|
// mantissa
|
173 |
|
|
delay1 #(FX+WX+1) d4(.clk(clk), .ce(ce), .i(mo1), .o(mo2) );
|
174 |
|
|
|
175 |
|
|
wire [FX+WX:0] mo2a;
|
176 |
|
|
// Now do the shifting
|
177 |
|
|
assign mo2a = rightOrLeft2 ? mo2 >> rshiftAmt2 : mo2 << lshiftAmt2;
|
178 |
|
|
|
179 |
|
|
// always @(posedge clk)
|
180 |
|
|
// if (ce)
|
181 |
|
|
// If infinity is reached then set the mantissa to zero
|
182 |
|
|
wire gbit = mo2a[FMSB+3];
|
183 |
|
|
wire rbit = mo2a[FMSB+2];
|
184 |
|
|
wire sbit = |mo2a[FMSB+1:0];
|
185 |
|
|
assign mo = {mo2a[FX+WX:FMSB+3],gbit,rbit,sbit};
|
186 |
|
|
|
187 |
|
|
assign o = {so,xo,mo};
|
188 |
|
|
|
189 |
|
|
endmodule
|
190 |
|
|
|
191 |
|
|
module fpNormalize_tb();
|
192 |
|
|
reg clk;
|
193 |
|
|
wire [35:0] o1,o2,o3,o4,o5,o6;
|
194 |
|
|
initial begin
|
195 |
|
|
clk = 0;
|
196 |
|
|
end
|
197 |
|
|
|
198 |
|
|
always #10 clk = ~clk;
|
199 |
|
|
// input =
|
200 |
|
|
// 23*2 + 3 + 8 + 1 = 58 bits
|
201 |
|
|
fpNormalize #(32) u1 (clk, 1'b1, 1'b0, 58'h0, o1); // zeor should result in a zero
|
202 |
|
|
fpNormalize #(32) u2 (clk, 1'b1, 1'b0, 58'h1FE123456781234, o2); // Nan should be a Nan
|
203 |
|
|
fpNormalize #(32) u3 (clk, 1'b1, 1'b1, 58'h000001234567890, o3); // denomral should be denormal
|
204 |
|
|
fpNormalize #(32) u4 (clk, 1'b1, 1'b1, 58'h1F0001234567890, o4); // denomral should be denormal (underflow exp is neg)
|
205 |
|
|
fpNormalize #(32) u5 (clk, 1'b1, 1'b0, 58'h0FF000000000000, o5); // the value 4
|
206 |
|
|
fpNormalize #(32) u6 (clk, 1'b1, 1'b0, 58'h104900000000000, o6); // the value 100
|
207 |
|
|
|
208 |
|
|
endmodule
|