URL
https://opencores.org/ocsvn/ft816float/ft816float/trunk
Subversion Repositories ft816float
[/] [ft816float/] [trunk/] [rtl/] [verilog2/] [fpNormalize32combo.sv] - Rev 74
Compare with Previous | Blame | View Log
// ============================================================================// __// \\__/ o\ (C) 2006-2022 Robert Finch, Waterloo// \ __ / All rights reserved.// \/_// robfinch<remove>@finitron.ca// ||//// fpNormalize32combo.sv// - floating point normalization unit// - combinational logic only// - IEEE 754 representation////// BSD 3-Clause License// Redistribution and use in source and binary forms, with or without// modification, are permitted provided that the following conditions are met://// 1. Redistributions of source code must retain the above copyright notice, this// list of conditions and the following disclaimer.//// 2. Redistributions in binary form must reproduce the above copyright notice,// this list of conditions and the following disclaimer in the documentation// and/or other materials provided with the distribution.//// 3. Neither the name of the copyright holder nor the names of its// contributors may be used to endorse or promote products derived from// this software without specific prior written permission.//// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.//// This unit takes a floating point number in an intermediate// format and normalizes it. No normalization occurs// for NaN's or infinities. The unit has a two cycle latency.//// The mantissa is assumed to start with two whole bits on// the left. The remaining bits are fractional.//// The width of the incoming format is reduced via a generation// of sticky bit in place of the low order fractional bits.//// On an underflowed input, the incoming exponent is assumed// to be negative. A right shift is needed.// ============================================================================import fp32Pkg::*;module fpNormalize32combo(i, o, under_i, under_o, inexact_o);input FP32X i; // expanded format inputoutput FP32N o; // normalized output + guard, sticky and round bits, + 1 whole digitinput under_i;output reg under_o;output reg inexact_o;integer n;// ----------------------------------------------------------------------------// No Clock required// ----------------------------------------------------------------------------reg [fp32Pkg::EMSB+1:0] xo0;reg so0;always_combxo0 <= {under_i,i.exp};always_combso0 <= i.sign; // sign doesn't change// ----------------------------------------------------------------------------// Clock #1// - Capture exponent information// ----------------------------------------------------------------------------reg xInf1a, xInf1b, xInf1c;FP32X i1;always_combi1 <= i;always_combxInf1a <= &xo0 & !under_i;always_combxInf1b <= &xo0[fp32Pkg::EMSB:1] & !under_i;always_combxInf1c <= &xo0[fp32Pkg::EMSB:0] & !under_i;// ----------------------------------------------------------------------------// Clock #2// - determine exponent increment// Since the there are *three* whole digits in the incoming format// the number of whole digits needs to be reduced. If the MSB is// set, then increment the exponent and no shift is needed.// ----------------------------------------------------------------------------reg xInf2c, xInf2b;reg [fp32Pkg::EMSB:0] xo2;reg incExpByOne2, incExpByTwo2;reg under2;always_combxInf2c <= xInf1c;always_combxInf2b <= xInf1b;always_combxo2 <= xo0;always_combunder2 <= under_i;always_combincExpByTwo2 <= !xInf1b & i1[fp32Pkg::FX];always_combincExpByOne2 <= !xInf1a & i1[fp32Pkg::FX-1];// ----------------------------------------------------------------------------// Clock #3// - increment exponent// - detect a zero mantissa// ----------------------------------------------------------------------------reg incExpByTwo3;reg incExpByOne3;FP32X i3;reg [fp32Pkg::EMSB+1:0] xo3;reg zeroMan3;always_combincExpByTwo3 <= incExpByTwo2;always_combincExpByOne3 <= incExpByOne2;always_combi3 <= i;wire [fp32Pkg::EMSB+1:0] xv3a = xo2 + {incExpByTwo2,1'b0};wire [fp32Pkg::EMSB+1:0] xv3b = xo2 + incExpByOne2;always_combxo3 <= xo2 + (incExpByTwo2 ? 2'd2 : incExpByOne2 ? 2'd1 : 2'd0);always_combzeroMan3 <= ((xv3b[fp32Pkg::EMSB+1]|| &xv3b[fp32Pkg::EMSB:0])||(xv3a[fp32Pkg::EMSB+1]| &xv3a[fp32Pkg::EMSB:0]))&& !under2 && !xInf2c;// ----------------------------------------------------------------------------// Clock #4// - Shift mantissa left// - If infinity is reached then set the mantissa to zero// shift mantissa left to reduce to a single whole digit// - create sticky bit// ----------------------------------------------------------------------------reg [fp32Pkg::FMSB+5:0] mo4;reg inexact4;always_combcasez({zeroMan3,incExpByTwo3,incExpByOne3})3'b1??: mo4 <= 1'd0;3'b01?: mo4 <= {i3[fp32Pkg::FX:fp32Pkg::FMSB],|i3[fp32Pkg::FMSB-1:0]};3'b001: mo4 <= {i3[fp32Pkg::FX-1:fp32Pkg::FMSB-1],|i3[fp32Pkg::FMSB-2:0]};default: mo4 <= {i3[fp32Pkg::FX-2:fp32Pkg::FMSB-2],|i3[fp32Pkg::FMSB-3:0]};endcasealways_combcasez({zeroMan3,incExpByTwo3,incExpByOne3})3'b1??: inexact4 <= 1'd0;3'b01?: inexact4 <= |i3[fp32Pkg::FMSB+1:0];3'b001: inexact4 <= |i3[fp32Pkg::FMSB:0];default: inexact4 <= |i3[fp32Pkg::FMSB-1:0];endcase// ----------------------------------------------------------------------------// Clock edge #5// - count leading zeros// ----------------------------------------------------------------------------reg [7:0] leadingZeros5;reg [fp32Pkg::EMSB+1:0] xo5;reg xInf5;always_combxo5 <= xo3;always_combxInf5 <= xInf2c;/* Lookup table based leading zero count modules give slightly betterperformance but cases must be coded.generatebeginif (FPWID <= 32) begincntlz32Reg clz0 (.clk(clk), .ce(ce), .i({mo4,4'b0}), .o(leadingZeros5) );assign leadingZeros5[7:6] = 2'b00;endelse if (FPWID<=32) beginassign leadingZeros5[7] = 1'b0;cntlz32Reg clz0 (.clk(clk), .ce(ce), .i({mo4,7'h0}), .o(leadingZeros5) );endelse if (FPWID<=80) beginassign leadingZeros5[7] = 1'b0;cntlz80Reg clz0 (.clk(clk), .ce(ce), .i({mo4,11'b0}), .o(leadingZeros5) );endelse if (FPWID<=84) beginassign leadingZeros5[7] = 1'b0;cntlz96Reg clz0 (.clk(clk), .ce(ce), .i({mo4,23'b0}), .o(leadingZeros5) );endelse if (FPWID<=96) beginassign leadingZeros5[7] = 1'b0;cntlz96Reg clz0 (.clk(clk), .ce(ce), .i({mo4,11'b0}), .o(leadingZeros5) );endelse if (FPWID<=128)cntlz128Reg clz0 (.clk(clk), .ce(ce), .i({mo4,11'b0}), .o(leadingZeros5) );endendgenerate*/// Sideways add.// Normally there would be only one to two leading zeros. It is tempting then// to check for only one or two. But, denormalized numbers might have more// leading zeros. If denormals were not supported this could be made smaller// and faster.`ifdef SUPPORT_DENORMALSreg [7:0] lzc;reg got_one;always_combbegingot_one = 1'b0;lzc = 8'h00;for (n = fp32Pkg::FMSB+5; n >= 0; n = n - 1) beginif (!got_one) beginif (mo4[n])got_one = 1'b1;elselzc = lzc + 1'b1;endendendalways_combleadingZeros5 <= lzc;`elsealways_combcasez(mo4[fp32Pkg::FMSB+5:fp32Pkg::FMSB+4])2'b1?: leadingZeros5 <= 8'd0;2'b01: leadingZeros5 <= 8'd1;2'b00: leadingZeros5 <= 8'd2;endcase`endif// ----------------------------------------------------------------------------// Clock edge #6// - Compute how much we want to decrement exponent by// - compute amount to shift left and right// - at infinity the exponent can't be incremented, so we can't shift right// otherwise it was an underflow situation so the exponent was negative// shift amount needs to be negated for shift register// If the exponent underflowed, then the shift direction must be to the// right regardless of mantissa bits; the number is denormalized.// Otherwise the shift direction must be to the left.// ----------------------------------------------------------------------------reg [7:0] lshiftAmt6;reg [7:0] rshiftAmt6;reg rightOrLeft6; // 0=left,1=rightreg xInf6;reg [fp32Pkg::EMSB+1:0] xo6;reg [fp32Pkg::FMSB+5:0] mo6;reg zeroMan6;always_combrightOrLeft6 <= under_i;always_combxo6 <= xo5;always_combmo6 <= mo4;always_combxInf6 <= xInf5;always_combzeroMan6 <= zeroMan3;always_comblshiftAmt6 <= leadingZeros5 > xo5 ? xo5 : leadingZeros5;always_combrshiftAmt6 <= xInf5 ? 1'd0 : $signed(xo5) > 1'd0 ? 1'd0 : ~xo5+2'd1; // xo2 is negative !// ----------------------------------------------------------------------------// Clock edge #7// - figure exponent// - shift mantissa// - figure sticky bit// ----------------------------------------------------------------------------reg [fp32Pkg::EMSB:0] xo7;reg rightOrLeft7;reg [fp32Pkg::FMSB+5:0] mo7l, mo7r;reg St6,St7;always_combrightOrLeft7 <= rightOrLeft6;always_combxo7 <= zeroMan6 ? xo6 :xInf6 ? xo6 : // an infinite exponent is either a NaN or infinity; no need to changerightOrLeft6 ? 1'd0 : // on a right shift, the exponent was negative, it's being made to zeroxo6 - lshiftAmt6; // on a left shift, the exponent can't be decremented below zeroalways_combmo7r <= mo6 >> rshiftAmt6;always_combmo7l <= mo6 << lshiftAmt6;// The sticky bit is set if the bits shifted out on a right shift are set.always_combbeginSt6 = 1'b0;for (n = 0; n < FMSB+5; n = n + 1)if (n <= rshiftAmt6 + 1) St6 = St6|mo6[n];endalways_combSt7 <= St6;// ----------------------------------------------------------------------------// Clock edge #8// - select mantissa// ----------------------------------------------------------------------------reg so;reg [fp32Pkg::EMSB:0] xo;reg [fp32Pkg::FMSB+5:0] mo;always_combso <= so0;always_combxo <= xo7;always_combinexact_o <= inexact4;always_combunder_o <= rightOrLeft7;always_combmo <= rightOrLeft7 ? mo7r|{St7,2'b0} : mo7l;assign o.sign = so;assign o.exp = xo;assign o.sig = mo[FMSB+5:2];endmodule
