Line 1... |
Line 1... |
`timescale 1ns / 1ps
|
`timescale 1ns / 1ps
|
// ============================================================================
|
// ============================================================================
|
// __
|
// __
|
// \\__/ o\ (C) 2006-2018 Robert Finch, Waterloo
|
// \\__/ o\ (C) 2006-2019 Robert Finch, Waterloo
|
// \ __ / All rights reserved.
|
// \ __ / All rights reserved.
|
// \/_// robfinch<remove>@finitron.ca
|
// \/_// robfinch<remove>@finitron.ca
|
// ||
|
// ||
|
//
|
//
|
// fpNormalize.v
|
// fpNormalize.v
|
Line 40... |
Line 40... |
// to be negative. A right shift is needed.
|
// to be negative. A right shift is needed.
|
// ============================================================================
|
// ============================================================================
|
|
|
module fpNormalize(clk, ce, under, i, o);
|
module fpNormalize(clk, ce, under, i, o);
|
parameter WID = 128;
|
parameter WID = 128;
|
localparam MSB = WID-1;
|
`include "fpSize.sv"
|
localparam EMSB = WID==128 ? 14 :
|
|
WID==96 ? 14 :
|
|
WID==80 ? 14 :
|
|
WID==64 ? 10 :
|
|
WID==52 ? 10 :
|
|
WID==48 ? 11 :
|
|
WID==44 ? 10 :
|
|
WID==42 ? 10 :
|
|
WID==40 ? 9 :
|
|
WID==32 ? 7 :
|
|
WID==24 ? 6 : 4;
|
|
localparam FMSB = WID==128 ? 111 :
|
|
WID==96 ? 79 :
|
|
WID==80 ? 63 :
|
|
WID==64 ? 51 :
|
|
WID==52 ? 39 :
|
|
WID==48 ? 34 :
|
|
WID==44 ? 31 :
|
|
WID==42 ? 29 :
|
|
WID==40 ? 28 :
|
|
WID==32 ? 22 :
|
|
WID==24 ? 15 : 9;
|
|
|
|
localparam FX = (FMSB+2)*2-1; // the MSB of the expanded fraction
|
|
localparam EX = FX + 1 + EMSB + 1 + 1 - 1;
|
|
|
|
input clk;
|
input clk;
|
input ce;
|
input ce;
|
input under;
|
input under;
|
input [EX:0] i; // expanded format input
|
input [EX:0] i; // expanded format input
|
Line 78... |
Line 53... |
// variables
|
// variables
|
wire so;
|
wire so;
|
|
|
wire so1 = i[EX]; // sign doesn't change
|
wire so1 = i[EX]; // sign doesn't change
|
|
|
// Since the there are *two* whole digits in the incoming format
|
// Since the there are *three* whole digits in the incoming format
|
// the number of whole digits needs to be reduced. If the MSB is
|
// the number of whole digits needs to be reduced. If the MSB is
|
// set, then increment the exponent and no shift is needed.
|
// set, then increment the exponent and no shift is needed.
|
wire [EMSB:0] xo;
|
wire [EMSB:0] xo;
|
wire [EMSB:0] xo1a = i[EX-1:FX+1];
|
wire [EMSB:0] xo1a = i[EX-1:FX+1];
|
wire xInf = &xo1a & !under;
|
wire xInf = &xo1a & !under;
|
wire incExp1 = !xInf & i[FX];
|
wire xInf3 = &xo1a[EMSB:1] & !under;
|
wire [EMSB:0] xo1 = xo1a + incExp1;
|
wire incExp2 = !xInf3 & i[FX];
|
|
wire incExp1 = !xInf & i[FX-1];
|
|
wire [EMSB:0] xo1 = xo1a + (incExp2 ? 2'd2 : incExp1 ? 2'd1 : 2'd0);
|
wire [EMSB:0] xo2;
|
wire [EMSB:0] xo2;
|
wire xInf1 = &xo1;
|
wire xInf1 = &xo1;
|
|
|
// If infinity is reached then set the mantissa to zero
|
// If infinity is reached then set the mantissa to zero
|
// shift mantissa left by one to reduce to a single whole digit
|
// shift mantissa left by one to reduce to a single whole digit
|
// if there is no exponent increment
|
// if there is no exponent increment
|
wire [FMSB+4:0] mo;
|
wire [FMSB+4:0] mo;
|
wire [FMSB+4:0] mo1 = (xInf1 & incExp1) ? 0 :
|
wire [FMSB+4:0] mo1 = ((xInf1 & (incExp1|incExp2))|(xInf3 & incExp2)) ? 0 :
|
incExp1 ? {i[FX:FMSB+1],|i[FMSB:0],1'b0} : // reduce mantissa size
|
incExp2 ? {i[FX:FMSB+1],|i[FMSB:0]} :
|
{i[FX-1:FMSB],|i[FMSB-1:0],1'b0}; // reduce mantissa size
|
incExp1 ? {i[FX-1:FMSB],|i[FMSB-1:0]} : // reduce mantissa size
|
|
{i[FX-2:FMSB-1],|i[FMSB-2:0]}; // reduce mantissa size
|
wire [FMSB+4:0] mo2;
|
wire [FMSB+4:0] mo2;
|
wire [7:0] leadingZeros2;
|
wire [7:0] leadingZeros2;
|
|
|
generate
|
generate
|
begin
|
begin
|
Line 113... |
Line 91... |
end
|
end
|
else if (WID<=80) begin
|
else if (WID<=80) begin
|
assign leadingZeros2[7] = 1'b0;
|
assign leadingZeros2[7] = 1'b0;
|
cntlz80Reg clz0 (.clk(clk), .ce(ce), .i({mo1,12'b0}), .o(leadingZeros2) );
|
cntlz80Reg clz0 (.clk(clk), .ce(ce), .i({mo1,12'b0}), .o(leadingZeros2) );
|
end
|
end
|
|
else if (WID<=84) begin
|
|
assign leadingZeros2[7] = 1'b0;
|
|
cntlz96Reg clz0 (.clk(clk), .ce(ce), .i({mo1,24'b0}), .o(leadingZeros2) );
|
|
end
|
else if (WID<=96) begin
|
else if (WID<=96) begin
|
assign leadingZeros2[7] = 1'b0;
|
assign leadingZeros2[7] = 1'b0;
|
cntlz96Reg clz0 (.clk(clk), .ce(ce), .i({mo1,12'b0}), .o(leadingZeros2) );
|
cntlz96Reg clz0 (.clk(clk), .ce(ce), .i({mo1,12'b0}), .o(leadingZeros2) );
|
end
|
end
|
else if (WID<=128)
|
else if (WID<=128)
|