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