| 1 |
8 |
robfinch |
`timescale 1ns / 1ps
|
| 2 |
6 |
robfinch |
// ============================================================================
|
| 3 |
|
|
// __
|
| 4 |
10 |
robfinch |
// \\__/ o\ (C) 2006-2018 Robert Finch, Waterloo
|
| 5 |
6 |
robfinch |
// \ __ / All rights reserved.
|
| 6 |
|
|
// \/_// robfinch<remove>@finitron.ca
|
| 7 |
|
|
// ||
|
| 8 |
|
|
//
|
| 9 |
8 |
robfinch |
// fpNormalize.v
|
| 10 |
|
|
// - floating point normalization unit
|
| 11 |
|
|
// - two cycle latency
|
| 12 |
|
|
// - parameterized width
|
| 13 |
|
|
// - IEEE 754 representation
|
| 14 |
|
|
//
|
| 15 |
|
|
//
|
| 16 |
6 |
robfinch |
// This source file is free software: you can redistribute it and/or modify
|
| 17 |
|
|
// it under the terms of the GNU Lesser General Public License as published
|
| 18 |
|
|
// by the Free Software Foundation, either version 3 of the License, or
|
| 19 |
|
|
// (at your option) any later version.
|
| 20 |
|
|
//
|
| 21 |
|
|
// This source file is distributed in the hope that it will be useful,
|
| 22 |
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 23 |
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 24 |
|
|
// GNU General Public License for more details.
|
| 25 |
|
|
//
|
| 26 |
|
|
// You should have received a copy of the GNU General Public License
|
| 27 |
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
| 28 |
8 |
robfinch |
//
|
| 29 |
6 |
robfinch |
// This unit takes a floating point number in an intermediate
|
| 30 |
|
|
// format and normalizes it. No normalization occurs
|
| 31 |
|
|
// for NaN's or infinities. The unit has a two cycle latency.
|
| 32 |
|
|
//
|
| 33 |
8 |
robfinch |
// The mantissa is assumed to start with two whole bits on
|
| 34 |
|
|
// the left. The remaining bits are fractional.
|
| 35 |
6 |
robfinch |
//
|
| 36 |
|
|
// The width of the incoming format is reduced via a generation
|
| 37 |
|
|
// of sticky bit in place of the low order fractional bits.
|
| 38 |
|
|
//
|
| 39 |
|
|
// On an underflowed input, the incoming exponent is assumed
|
| 40 |
|
|
// to be negative. A right shift is needed.
|
| 41 |
|
|
// ============================================================================
|
| 42 |
8 |
robfinch |
|
| 43 |
6 |
robfinch |
module fpNormalize(clk, ce, under, i, o);
|
| 44 |
8 |
robfinch |
parameter WID = 128;
|
| 45 |
6 |
robfinch |
localparam MSB = WID-1;
|
| 46 |
8 |
robfinch |
localparam EMSB = WID==128 ? 14 :
|
| 47 |
|
|
WID==96 ? 14 :
|
| 48 |
|
|
WID==80 ? 14 :
|
| 49 |
|
|
WID==64 ? 10 :
|
| 50 |
6 |
robfinch |
WID==52 ? 10 :
|
| 51 |
10 |
robfinch |
WID==48 ? 11 :
|
| 52 |
6 |
robfinch |
WID==44 ? 10 :
|
| 53 |
|
|
WID==42 ? 10 :
|
| 54 |
|
|
WID==40 ? 9 :
|
| 55 |
|
|
WID==32 ? 7 :
|
| 56 |
|
|
WID==24 ? 6 : 4;
|
| 57 |
8 |
robfinch |
localparam FMSB = WID==128 ? 111 :
|
| 58 |
|
|
WID==96 ? 79 :
|
| 59 |
|
|
WID==80 ? 63 :
|
| 60 |
|
|
WID==64 ? 51 :
|
| 61 |
6 |
robfinch |
WID==52 ? 39 :
|
| 62 |
10 |
robfinch |
WID==48 ? 34 :
|
| 63 |
6 |
robfinch |
WID==44 ? 31 :
|
| 64 |
|
|
WID==42 ? 29 :
|
| 65 |
|
|
WID==40 ? 28 :
|
| 66 |
|
|
WID==32 ? 22 :
|
| 67 |
|
|
WID==24 ? 15 : 9;
|
| 68 |
|
|
|
| 69 |
8 |
robfinch |
localparam FX = (FMSB+2)*2-1; // the MSB of the expanded fraction
|
| 70 |
|
|
localparam EX = FX + 1 + EMSB + 1 + 1 - 1;
|
| 71 |
6 |
robfinch |
|
| 72 |
|
|
input clk;
|
| 73 |
|
|
input ce;
|
| 74 |
|
|
input under;
|
| 75 |
8 |
robfinch |
input [EX:0] i; // expanded format input
|
| 76 |
|
|
output [WID+2:0] o; // normalized output + guard, sticky and round bits, + 1 whole digit
|
| 77 |
6 |
robfinch |
|
| 78 |
|
|
// variables
|
| 79 |
|
|
wire so;
|
| 80 |
|
|
|
| 81 |
8 |
robfinch |
wire so1 = i[EX]; // sign doesn't change
|
| 82 |
6 |
robfinch |
|
| 83 |
8 |
robfinch |
// Since the there are *two* whole digits in the incoming format
|
| 84 |
6 |
robfinch |
// the number of whole digits needs to be reduced. If the MSB is
|
| 85 |
8 |
robfinch |
// set, then increment the exponent and no shift is needed.
|
| 86 |
6 |
robfinch |
wire [EMSB:0] xo;
|
| 87 |
8 |
robfinch |
wire [EMSB:0] xo1a = i[EX-1:FX+1];
|
| 88 |
|
|
wire xInf = &xo1a & !under;
|
| 89 |
|
|
wire incExp1 = !xInf & i[FX];
|
| 90 |
|
|
wire [EMSB:0] xo1 = xo1a + incExp1;
|
| 91 |
6 |
robfinch |
wire [EMSB:0] xo2;
|
| 92 |
8 |
robfinch |
wire xInf1 = &xo1;
|
| 93 |
6 |
robfinch |
|
| 94 |
8 |
robfinch |
// If infinity is reached then set the mantissa to zero
|
| 95 |
|
|
wire gbit = i[FMSB];
|
| 96 |
|
|
wire rbit = i[FMSB-1];
|
| 97 |
|
|
wire sbit = |i[FMSB-2:0];
|
| 98 |
6 |
robfinch |
// shift mantissa left by one to reduce to a single whole digit
|
| 99 |
|
|
// if there is no exponent increment
|
| 100 |
8 |
robfinch |
wire [FMSB+4:0] mo;
|
| 101 |
10 |
robfinch |
wire [FMSB+4:0] mo1 = (xInf1 & incExp1) ? 0 :
|
| 102 |
8 |
robfinch |
incExp1 ? {i[FX:FMSB+2],gbit,rbit,sbit} : // reduce mantissa size
|
| 103 |
|
|
{i[FX-1:FMSB+1],gbit,rbit,sbit}; // reduce mantissa size
|
| 104 |
|
|
wire [FMSB+3:0] mo2;
|
| 105 |
6 |
robfinch |
wire [7:0] leadingZeros2;
|
| 106 |
|
|
|
| 107 |
|
|
generate
|
| 108 |
|
|
begin
|
| 109 |
12 |
robfinch |
if (WID <= 32) begin
|
| 110 |
8 |
robfinch |
cntlz32Reg clz0 (.clk(clk), .ce(ce), .i({mo1,5'b0}), .o(leadingZeros2) );
|
| 111 |
10 |
robfinch |
assign leadingZeros2[7:6] = 2'b00;
|
| 112 |
|
|
end
|
| 113 |
8 |
robfinch |
else if (WID==128)
|
| 114 |
|
|
cntlz128Reg clz0 (.clk(clk), .ce(ce), .i({mo1,12'b0}), .o(leadingZeros2) );
|
| 115 |
11 |
robfinch |
else if (WID==96) begin
|
| 116 |
|
|
assign leadingZeros2[7] = 1'b0;
|
| 117 |
8 |
robfinch |
cntlz96Reg clz0 (.clk(clk), .ce(ce), .i({mo1,12'b0}), .o(leadingZeros2) );
|
| 118 |
11 |
robfinch |
end
|
| 119 |
|
|
else if (WID==80) begin
|
| 120 |
|
|
assign leadingZeros2[7] = 1'b0;
|
| 121 |
8 |
robfinch |
cntlz80Reg clz0 (.clk(clk), .ce(ce), .i({mo1,12'b0}), .o(leadingZeros2) );
|
| 122 |
11 |
robfinch |
end
|
| 123 |
|
|
else if (WID==64) begin
|
| 124 |
|
|
assign leadingZeros2[7] = 1'b0;
|
| 125 |
8 |
robfinch |
cntlz64Reg clz0 (.clk(clk), .ce(ce), .i({mo1,8'h0}), .o(leadingZeros2) );
|
| 126 |
6 |
robfinch |
end
|
| 127 |
11 |
robfinch |
end
|
| 128 |
6 |
robfinch |
endgenerate
|
| 129 |
|
|
|
| 130 |
|
|
// compensate for leadingZeros delay
|
| 131 |
|
|
wire xInf2;
|
| 132 |
|
|
delay1 #(EMSB+1) d2(.clk(clk), .ce(ce), .i(xo1), .o(xo2) );
|
| 133 |
8 |
robfinch |
delay1 #(1) d3(.clk(clk), .ce(ce), .i(xInf1), .o(xInf2) );
|
| 134 |
6 |
robfinch |
|
| 135 |
10 |
robfinch |
|
| 136 |
6 |
robfinch |
// If the exponent underflowed, then the shift direction must be to the
|
| 137 |
|
|
// right regardless of mantissa bits; the number is denormalized.
|
| 138 |
|
|
// Otherwise the shift direction must be to the left.
|
| 139 |
|
|
wire rightOrLeft2; // 0=left,1=right
|
| 140 |
8 |
robfinch |
delay1 #(1) d8(.clk(clk), .ce(ce), .i(under), .o(rightOrLeft2) );
|
| 141 |
6 |
robfinch |
|
| 142 |
8 |
robfinch |
// Compute how much we want to decrement by
|
| 143 |
6 |
robfinch |
wire [7:0] lshiftAmt2 = leadingZeros2 > xo2 ? xo2 : leadingZeros2;
|
| 144 |
|
|
|
| 145 |
|
|
// compute amount to shift right
|
| 146 |
|
|
// at infinity the exponent can't be incremented, so we can't shift right
|
| 147 |
|
|
// otherwise it was an underflow situation so the exponent was negative
|
| 148 |
|
|
// shift amount needs to be negated for shift register
|
| 149 |
12 |
robfinch |
wire [7:0] rshiftAmt2 = xInf2 ? 0 : $signed(xo2) > 0 ? 0 : ~xo2+1;//FMSB+4+xo2; // xo2 is negative !
|
| 150 |
6 |
robfinch |
|
| 151 |
|
|
|
| 152 |
|
|
// sign
|
| 153 |
|
|
// the output sign is the same as the input sign
|
| 154 |
|
|
delay1 #(1) d7(.clk(clk), .ce(ce), .i(so1), .o(so) );
|
| 155 |
|
|
|
| 156 |
|
|
// exponent
|
| 157 |
|
|
// always @(posedge clk)
|
| 158 |
|
|
// if (ce)
|
| 159 |
|
|
assign xo =
|
| 160 |
|
|
xInf2 ? xo2 : // an infinite exponent is either a NaN or infinity; no need to change
|
| 161 |
|
|
rightOrLeft2 ? 0 : // on a right shift, the exponent was negative, it's being made to zero
|
| 162 |
|
|
xo2 - lshiftAmt2; // on a left shift, the exponent can't be decremented below zero
|
| 163 |
|
|
|
| 164 |
|
|
// mantissa
|
| 165 |
8 |
robfinch |
delay1 #(FMSB+5) d4(.clk(clk), .ce(ce), .i(mo1), .o(mo2) );
|
| 166 |
6 |
robfinch |
|
| 167 |
8 |
robfinch |
wire [FMSB+3:0] mo2a;
|
| 168 |
|
|
//shiftAndMask #(FMSB+4) u1 (.op({rightOrLeft2,1'b0}), .a(mo2), .b(rightOrLeft2 ? lshiftAmt2 : rshiftAmt2), .mb(6'd0), .me(FMSB+3), .o(mo2a) );
|
| 169 |
6 |
robfinch |
|
| 170 |
|
|
// always @(posedge clk)
|
| 171 |
|
|
// if (ce)
|
| 172 |
10 |
robfinch |
assign mo = rightOrLeft2 ? (mo2 >> rshiftAmt2) : (mo2 << lshiftAmt2);
|
| 173 |
|
|
//always @(posedge clk)
|
| 174 |
|
|
// $display("%c xo2=%d -xo2=%d rshift=%d >%d %d", rightOrLeft2 ? "r" : "l",xo2, -xo2, rshiftAmt2,($unsigned(-xo2) > $unsigned(FMSB+3)),FMSB+3);
|
| 175 |
8 |
robfinch |
assign o = {so,xo,mo[FMSB+4:1]};
|
| 176 |
6 |
robfinch |
|
| 177 |
|
|
endmodule
|
| 178 |
8 |
robfinch |
|