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

Subversion Repositories ft816float

[/] [ft816float/] [trunk/] [rtl/] [verilog/] [fpNormalize.v] - Blame information for rev 26

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 8 robfinch
`timescale 1ns / 1ps
2 6 robfinch
// ============================================================================
3
//        __
4 26 robfinch
//   \\__/ o\    (C) 2006-2019  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 14 robfinch
//    - one cycle latency
12 8 robfinch
//    - 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 26 robfinch
`include "fpSize.sv"
46 6 robfinch
 
47
input clk;
48
input ce;
49
input under;
50 8 robfinch
input [EX:0] i;          // expanded format input
51
output [WID+2:0] o;              // normalized output + guard, sticky and round bits, + 1 whole digit
52 6 robfinch
 
53
// variables
54
wire so;
55
 
56 8 robfinch
wire so1 = i[EX];               // sign doesn't change
57 6 robfinch
 
58 26 robfinch
// Since the there are *three* whole digits in the incoming format
59 6 robfinch
// the number of whole digits needs to be reduced. If the MSB is
60 8 robfinch
// set, then increment the exponent and no shift is needed.
61 6 robfinch
wire [EMSB:0] xo;
62 8 robfinch
wire [EMSB:0] xo1a = i[EX-1:FX+1];
63
wire xInf = &xo1a & !under;
64 26 robfinch
wire xInf3 = &xo1a[EMSB:1] & !under;
65
wire incExp2 = !xInf3 & i[FX];
66
wire incExp1 = !xInf & i[FX-1];
67
wire [EMSB:0] xo1 = xo1a + (incExp2 ? 2'd2 : incExp1 ? 2'd1 : 2'd0);
68 6 robfinch
wire [EMSB:0] xo2;
69 8 robfinch
wire xInf1 = &xo1;
70 6 robfinch
 
71 8 robfinch
// If infinity is reached then set the mantissa to zero
72 6 robfinch
// shift mantissa left by one to reduce to a single whole digit
73
// if there is no exponent increment
74 8 robfinch
wire [FMSB+4:0] mo;
75 26 robfinch
wire [FMSB+4:0] mo1 = ((xInf1 & (incExp1|incExp2))|(xInf3 & incExp2)) ? 0 :
76
        incExp2 ? {i[FX:FMSB+1],|i[FMSB:0]} :
77
        incExp1 ? {i[FX-1:FMSB],|i[FMSB-1:0]} :  // reduce mantissa size
78
                         {i[FX-2:FMSB-1],|i[FMSB-2:0]};          // reduce mantissa size
79 13 robfinch
wire [FMSB+4:0] mo2;
80 6 robfinch
wire [7:0] leadingZeros2;
81
 
82
generate
83
begin
84 12 robfinch
if (WID <= 32) begin
85 8 robfinch
cntlz32Reg clz0 (.clk(clk), .ce(ce), .i({mo1,5'b0}), .o(leadingZeros2) );
86 10 robfinch
assign leadingZeros2[7:6] = 2'b00;
87
end
88 13 robfinch
else if (WID<=64) begin
89 11 robfinch
assign leadingZeros2[7] = 1'b0;
90 13 robfinch
cntlz64Reg clz0 (.clk(clk), .ce(ce), .i({mo1,8'h0}), .o(leadingZeros2) );
91 11 robfinch
end
92 13 robfinch
else if (WID<=80) begin
93 11 robfinch
assign leadingZeros2[7] = 1'b0;
94 8 robfinch
cntlz80Reg clz0 (.clk(clk), .ce(ce), .i({mo1,12'b0}), .o(leadingZeros2) );
95 11 robfinch
end
96 26 robfinch
else if (WID<=84) begin
97
assign leadingZeros2[7] = 1'b0;
98
cntlz96Reg clz0 (.clk(clk), .ce(ce), .i({mo1,24'b0}), .o(leadingZeros2) );
99
end
100 13 robfinch
else if (WID<=96) begin
101 11 robfinch
assign leadingZeros2[7] = 1'b0;
102 13 robfinch
cntlz96Reg clz0 (.clk(clk), .ce(ce), .i({mo1,12'b0}), .o(leadingZeros2) );
103 6 robfinch
end
104 13 robfinch
else if (WID<=128)
105
cntlz128Reg clz0 (.clk(clk), .ce(ce), .i({mo1,12'b0}), .o(leadingZeros2) );
106 11 robfinch
end
107 6 robfinch
endgenerate
108
 
109
// compensate for leadingZeros delay
110
wire xInf2;
111
delay1 #(EMSB+1) d2(.clk(clk), .ce(ce), .i(xo1), .o(xo2) );
112 8 robfinch
delay1 #(1)      d3(.clk(clk), .ce(ce), .i(xInf1), .o(xInf2) );
113 6 robfinch
 
114 10 robfinch
 
115 6 robfinch
// If the exponent underflowed, then the shift direction must be to the
116
// right regardless of mantissa bits; the number is denormalized.
117
// Otherwise the shift direction must be to the left.
118
wire rightOrLeft2;      // 0=left,1=right
119 8 robfinch
delay1 #(1) d8(.clk(clk), .ce(ce), .i(under), .o(rightOrLeft2) );
120 6 robfinch
 
121 8 robfinch
// Compute how much we want to decrement by
122 6 robfinch
wire [7:0] lshiftAmt2 = leadingZeros2 > xo2 ? xo2 : leadingZeros2;
123
 
124
// compute amount to shift right
125
// at infinity the exponent can't be incremented, so we can't shift right
126
// otherwise it was an underflow situation so the exponent was negative
127
// shift amount needs to be negated for shift register
128 12 robfinch
wire [7:0] rshiftAmt2 = xInf2 ? 0 : $signed(xo2) > 0 ? 0 : ~xo2+1;//FMSB+4+xo2;     // xo2 is negative !
129 6 robfinch
 
130
 
131
// sign
132
// the output sign is the same as the input sign
133
delay1 #(1)      d7(.clk(clk), .ce(ce), .i(so1), .o(so) );
134
 
135
// exponent
136
//      always @(posedge clk)
137
//              if (ce)
138
assign xo =
139
                xInf2 ? xo2 :           // an infinite exponent is either a NaN or infinity; no need to change
140
                rightOrLeft2 ? 0 :       // on a right shift, the exponent was negative, it's being made to zero
141
                xo2 - lshiftAmt2;       // on a left shift, the exponent can't be decremented below zero
142
 
143
// mantissa
144 8 robfinch
delay1 #(FMSB+5) d4(.clk(clk), .ce(ce), .i(mo1), .o(mo2) );
145 6 robfinch
 
146 8 robfinch
wire [FMSB+3:0] mo2a;
147
//shiftAndMask #(FMSB+4) u1 (.op({rightOrLeft2,1'b0}), .a(mo2), .b(rightOrLeft2 ? lshiftAmt2 : rshiftAmt2), .mb(6'd0), .me(FMSB+3), .o(mo2a) );
148 6 robfinch
 
149
//      always @(posedge clk)
150
//              if (ce)
151 10 robfinch
assign mo = rightOrLeft2 ? (mo2 >> rshiftAmt2) : (mo2 << lshiftAmt2);
152
//always @(posedge clk)
153
//      $display("%c xo2=%d -xo2=%d rshift=%d >%d %d", rightOrLeft2 ? "r" : "l",xo2, -xo2, rshiftAmt2,($unsigned(-xo2) > $unsigned(FMSB+3)),FMSB+3);
154 8 robfinch
assign o = {so,xo,mo[FMSB+4:1]};
155 6 robfinch
 
156
endmodule
157 8 robfinch
 

powered by: WebSVN 2.1.0

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