OpenCores
URL https://opencores.org/ocsvn/ft816float/ft816float/trunk

Subversion Repositories ft816float

[/] [ft816float/] [trunk/] [rtl/] [verilog/] [fpNormalize.v] - Diff between revs 6 and 8

Go to most recent revision | Show entire file | Details | Blame | View Log

Rev 6 Rev 8
Line 1... Line 1...
 
`timescale 1ns / 1ps
// ============================================================================
// ============================================================================
//        __
//        __
//   \\__/ o\    (C) 2006-2016  Robert Finch, Stratford
//   \\__/ o\    (C) 2006-2016  Robert Finch, Waterloo
//    \  __ /    All rights reserved.
//    \  __ /    All rights reserved.
//     \/_//     robfinch<remove>@finitron.ca
//     \/_//     robfinch<remove>@finitron.ca
//       ||
//       ||
//
//
 
//      fpNormalize.v
 
//    - floating point normalization unit
 
//    - two cycle latency
 
//    - parameterized width
 
//    - IEEE 754 representation
 
//
 
//
// This source file is free software: you can redistribute it and/or modify 
// This source file is free software: you can redistribute it and/or modify 
// it under the terms of the GNU Lesser General Public License as published 
// it under the terms of the GNU Lesser General Public License as published 
// by the Free Software Foundation, either version 3 of the License, or     
// by the Free Software Foundation, either version 3 of the License, or     
// (at your option) any later version.                                      
// (at your option) any later version.                                      
//                                                                          
//                                                                          
Line 16... Line 24...
// GNU General Public License for more details.                             
// GNU General Public License for more details.                             
//                                                                          
//                                                                          
// You should have received a copy of the GNU General Public License        
// You should have received a copy of the GNU General Public License        
// along with this program.  If not, see <http://www.gnu.org/licenses/>.    
// along with this program.  If not, see <http://www.gnu.org/licenses/>.    
//
//
//      fpNormalize.v
 
//  - floating point normalization unit
 
//  - two cycle latency
 
//  - parameterized width
 
//
 
//      This unit takes a floating point number in an intermediate
//      This unit takes a floating point number in an intermediate
// format and normalizes it. No normalization occurs
// format and normalizes it. No normalization occurs
// for NaN's or infinities. The unit has a two cycle latency.
// for NaN's or infinities. The unit has a two cycle latency.
//
//
// The mantissa is assumed to start with three whole bits on
// The mantissa is assumed to start with two whole bits on
// the left. The remaining bits are fractional. The three whole bits
// the left. The remaining bits are fractional.
// result from a MAC (multiply accumulate) operation. The result from
 
// a MAC can vary from 0 to 8 which requires three whole digits.
 
//
//
// The width of the incoming format is reduced via a generation
// The width of the incoming format is reduced via a generation
// of sticky bit in place of the low order fractional bits.
// of sticky bit in place of the low order fractional bits.
//
//
// On an underflowed input, the incoming exponent is assumed
// On an underflowed input, the incoming exponent is assumed
// 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 = 32;
parameter WID = 128;
localparam MSB = WID-1;
localparam MSB = WID-1;
localparam EMSB =
localparam EMSB = WID==128 ? 14 :
 
                  WID==96 ? 14 :
          WID==80 ? 14 :
          WID==80 ? 14 :
          WID==64 ? 10 :
          WID==64 ? 10 :
                                  WID==52 ? 10 :
                                  WID==52 ? 10 :
                                  WID==48 ? 10 :
                                  WID==48 ? 10 :
                                  WID==44 ? 10 :
                                  WID==44 ? 10 :
                                  WID==42 ? 10 :
                                  WID==42 ? 10 :
                                  WID==40 ?  9 :
                                  WID==40 ?  9 :
                                  WID==32 ?  7 :
                                  WID==32 ?  7 :
                                  WID==24 ?  6 : 4;
                                  WID==24 ?  6 : 4;
localparam FMSB =
localparam FMSB = WID==128 ? 111 :
 
                  WID==96 ? 79 :
          WID==80 ? 63 :
          WID==80 ? 63 :
          WID==64 ? 51 :
          WID==64 ? 51 :
                                  WID==52 ? 39 :
                                  WID==52 ? 39 :
                                  WID==48 ? 35 :
                                  WID==48 ? 35 :
                                  WID==44 ? 31 :
                                  WID==44 ? 31 :
                                  WID==42 ? 29 :
                                  WID==42 ? 29 :
                                  WID==40 ? 28 :
                                  WID==40 ? 28 :
                                  WID==32 ? 22 :
                                  WID==32 ? 22 :
                                  WID==24 ? 15 : 9;
                                  WID==24 ? 15 : 9;
 
 
localparam WX = 3;            // Three whole digits
localparam FX = (FMSB+2)*2-1;   // the MSB of the expanded fraction
localparam FX = (FMSB+1)*2-1;   // the MSB of the expanded fraction
localparam EX = FX + 1 + EMSB + 1 + 1 - 1;
// Fraction + Three whole bits 
 
localparam EX = FX + WX + EMSB + 1; // The MSB of the exponent
 
 
 
input clk;
input clk;
input ce;
input ce;
input under;
input under;
input [EX+1:0] i;                 // expanded format input
input [EX:0] i;          // expanded format input
output [WID+3:0] o;              // normalized output + guard, sticky and round bits, + 1 whole digit
output [WID+2:0] o;              // normalized output + guard, sticky and round bits, + 1 whole digit
 
 
wire [EMSB:0] infXp = {EMSB+1{1'b1}};    // simple constant - value of exp for inifinity
 
 
 
// variables
// variables
wire so;
wire so;
 
 
wire so1 = i[EX+1];             // sign doesn't change
wire so1 = i[EX];               // sign doesn't change
 
 
// Since the there are *three* whole digits in the incoming format
// Since the there are *two* 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 by two and no shift is needed.
// set, then increment the exponent and no shift is needed.
// Otherwise if the next MSB is set, increment the exponent by one,
 
// and shift left once.
 
wire [EMSB:0] xo;
wire [EMSB:0] xo;
wire [EMSB:0] xo1a = i[EX:FX+WX+1];
wire [EMSB:0] xo1a = i[EX-1:FX+1];
 
wire xInf = &xo1a & !under;
wire incExp2 = i[FX+WX-1]|i[FX+WX-2];
wire incExp1 = !xInf & i[FX];
// Allow an extra bit for exponent overflow
wire [EMSB:0] xo1 = xo1a + incExp1;
// Add two to exponent to shift the decimal place left twice.
 
// (Gives 1 leading whole digit).
 
wire [EMSB+1:0] xo1b = xo1a + 2;
 
wire [EMSB:0] xo1;
 
wire [EMSB:0] xo2;
wire [EMSB:0] xo2;
wire xInf1a = &xo1a[EMSB:0];
wire xInf1 = &xo1;
 
 
// If there was a carry from the addition and we were in the underflow
 
// state, then the number became normal again. Clear the carry bit.
 
// Otherwise if the exponent overflowed and it's not the underflow
 
// state, then set the exponent to infinity. Othwerise just keep the
 
// remaining exponent bits - the result is still underflowed.
 
assign xo1 = (under & xo1b[EMSB+1]) ? xo1b[EMSB:0] :
 
              (xInf1a & !under) ? infXp : xo1b[EMSB+1] ? infXp : xo1b;
 
wire xInf = &xo1 & !under;
 
wire under1 = under & !xo1b[EMSB+1];  // keep trakc of renormallzation
 
 
 
 
// If infinity is reached then set the mantissa to zero
 
wire gbit =  i[FMSB];
 
wire rbit =  i[FMSB-1];
 
wire sbit = |i[FMSB-2:0];
// 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+1+3:0] mo; //GRS+1whole digit
wire [FMSB+4:0] mo;
wire [FX+WX:0] mo1 = xInf & incExp2 ? 0 :  // set mantissa to zero for infinity
wire [FMSB+4:0] mo1 = xInf1 & incExp1 ? 0 :
           i[FX+WX:0];
        incExp1 ? {i[FX:FMSB+2],gbit,rbit,sbit} :               // reduce mantissa size
wire [FX+WX:0] mo2;
                         {i[FX-1:FMSB+1],gbit,rbit,sbit};       // reduce mantissa size
 
wire [FMSB+3:0] mo2;
wire [7:0] leadingZeros2;
wire [7:0] leadingZeros2;
 
 
// Adjust the operand to the leading zero counter by left aligning it
 
// by padding trailing zeros. This is a constant shift that doesn't take
 
// any hardware.
 
generate
generate
begin
begin
if (WID==64) begin
if (WID==32)
wire [127:0] mo1a = {mo1,{127-(FX+3){1'b0}}};
cntlz32Reg clz0 (.clk(clk), .ce(ce), .i({mo1,5'b0}), .o(leadingZeros2) );
cntlz128Reg clz0 (.clk(clk), .ce(ce), .i(mo1a), .o(leadingZeros2) );
else if (WID==128)
end
cntlz128Reg clz0 (.clk(clk), .ce(ce), .i({mo1,12'b0}), .o(leadingZeros2) );
else begin  // 32 bits
else if (WID==96)
wire [63:0] mo1a = {mo1,{63-(FX+3){1'b0}}};
cntlz96Reg clz0 (.clk(clk), .ce(ce), .i({mo1,12'b0}), .o(leadingZeros2) );
cntlz64Reg clz0 (.clk(clk), .ce(ce), .i(mo1a), .o(leadingZeros2) );
else if (WID==80)
assign leadingZeros2[7] = 1'b0;
cntlz80Reg clz0 (.clk(clk), .ce(ce), .i({mo1,12'b0}), .o(leadingZeros2) );
end
else if (WID==64)
 
cntlz64Reg clz0 (.clk(clk), .ce(ce), .i({mo1,8'h0}), .o(leadingZeros2) );
end
end
endgenerate
endgenerate
 
 
// compensate for leadingZeros delay
// compensate for leadingZeros delay
wire xInf2;
wire xInf2;
delay1 #(EMSB+1) d2(.clk(clk), .ce(ce), .i(xo1), .o(xo2) );
delay1 #(EMSB+1) d2(.clk(clk), .ce(ce), .i(xo1), .o(xo2) );
delay1 #(1)      d3(.clk(clk), .ce(ce), .i(xInf), .o(xInf2) );
delay1 #(1)      d3(.clk(clk), .ce(ce), .i(xInf1), .o(xInf2) );
 
 
// If the exponent underflowed, then the shift direction must be to the
// If the exponent underflowed, then the shift direction must be to the
// right regardless of mantissa bits; the number is denormalized.
// right regardless of mantissa bits; the number is denormalized.
// Otherwise the shift direction must be to the left.
// Otherwise the shift direction must be to the left.
wire rightOrLeft2;      // 0=left,1=right
wire rightOrLeft2;      // 0=left,1=right
delay1 #(1) d8(.clk(clk), .ce(ce), .i(under1), .o(rightOrLeft2) );
delay1 #(1) d8(.clk(clk), .ce(ce), .i(under), .o(rightOrLeft2) );
 
 
// Compute how much we want to decrement by. We can't decrement by
// Compute how much we want to decrement by
// more than the exponent as the number becomes denormal when the
 
// exponent reaches zero.
 
wire [7:0] lshiftAmt2 = leadingZeros2 > xo2 ? xo2 : leadingZeros2;
wire [7:0] lshiftAmt2 = leadingZeros2 > xo2 ? xo2 : leadingZeros2;
 
 
// compute amount to shift right
// compute amount to shift right
// at infinity the exponent can't be incremented, so we can't shift 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
// otherwise it was an underflow situation so the exponent was negative
// shift amount needs to be negated for shift register
// shift amount needs to be negated for shift register
wire [EMSB:0] nxo2 = -xo2;
wire [7:0] rshiftAmt2 = xInf2 ? 0 : -xo2 > FMSB+3 ? FMSB+4 : FMSB+4+xo2;  // xo2 is negative !
wire [7:0] rshiftAmt2 = xInf2 ? 0 : nxo2 > FMSB+WX ? FMSB+WX+1 : nxo2;    // xo2 is negative !
 
 
 
 
 
// sign
// sign
// the output sign is the same as the input sign
// the output sign is the same as the input sign
delay1 #(1)      d7(.clk(clk), .ce(ce), .i(so1), .o(so) );
delay1 #(1)      d7(.clk(clk), .ce(ce), .i(so1), .o(so) );
Line 168... Line 151...
                xInf2 ? xo2 :           // an infinite exponent is either a NaN or infinity; no need to change
                xInf2 ? xo2 :           // an infinite exponent is either a NaN or infinity; no need to change
                rightOrLeft2 ? 0 :       // on a right shift, the exponent was negative, it's being made to zero
                rightOrLeft2 ? 0 :       // on a right shift, the exponent was negative, it's being made to zero
                xo2 - lshiftAmt2;       // on a left shift, the exponent can't be decremented below zero
                xo2 - lshiftAmt2;       // on a left shift, the exponent can't be decremented below zero
 
 
// mantissa
// mantissa
delay1 #(FX+WX+1) d4(.clk(clk), .ce(ce), .i(mo1), .o(mo2) );
delay1 #(FMSB+5) d4(.clk(clk), .ce(ce), .i(mo1), .o(mo2) );
 
 
wire [FX+WX:0] mo2a;
wire [FMSB+3:0] mo2a;
// Now do the shifting
//shiftAndMask #(FMSB+4) u1 (.op({rightOrLeft2,1'b0}), .a(mo2), .b(rightOrLeft2 ? lshiftAmt2 : rshiftAmt2), .mb(6'd0), .me(FMSB+3), .o(mo2a) );
assign mo2a = rightOrLeft2 ? mo2 >> rshiftAmt2 : mo2 << lshiftAmt2;
 
 
 
//      always @(posedge clk)
//      always @(posedge clk)
//              if (ce)
//              if (ce)
// If infinity is reached then set the mantissa to zero
assign mo = rightOrLeft2 ? mo2 >> rshiftAmt2 : mo2 << lshiftAmt2;
wire gbit =  mo2a[FMSB+3];
 
wire rbit =  mo2a[FMSB+2];
 
wire sbit = |mo2a[FMSB+1:0];
 
assign mo = {mo2a[FX+WX:FMSB+3],gbit,rbit,sbit};
 
 
 
assign o = {so,xo,mo};
assign o = {so,xo,mo[FMSB+4:1]};
 
 
endmodule
endmodule
 
 
module fpNormalize_tb();
 
reg clk;
 
wire [35:0] o1,o2,o3,o4,o5,o6;
 
initial begin
 
  clk = 0;
 
end
 
 
 
always #10 clk = ~clk;
 
// input =
 
// 23*2 + 3 + 8 + 1 = 58 bits
 
fpNormalize #(32) u1 (clk, 1'b1, 1'b0, 58'h0, o1);  // zeor should result in a zero
 
fpNormalize #(32) u2 (clk, 1'b1, 1'b0, 58'h1FE123456781234, o2);  // Nan should be a Nan
 
fpNormalize #(32) u3 (clk, 1'b1, 1'b1, 58'h000001234567890, o3);  // denomral should be denormal
 
fpNormalize #(32) u4 (clk, 1'b1, 1'b1, 58'h1F0001234567890, o4);  // denomral should be denormal (underflow exp is neg)
 
fpNormalize #(32) u5 (clk, 1'b1, 1'b0, 58'h0FF000000000000, o5);  // the value 4
 
fpNormalize #(32) u6 (clk, 1'b1, 1'b0, 58'h104900000000000, o6);  // the value 100
 
 
 
endmodule
 
 
 
 No newline at end of file
 No newline at end of file

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.