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

Subversion Repositories ft816float

[/] [ft816float/] [trunk/] [rtl/] [verilog2/] [fpRound.v] - Blame information for rev 29

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

Line No. Rev Author Line
1 29 robfinch
// ============================================================================
2
//        __
3
//   \\__/ o\    (C) 2006-2019  Robert Finch, Waterloo
4
//    \  __ /    All rights reserved.
5
//     \/_//     robfinch<remove>@finitron.ca
6
//       ||
7
//
8
//      fpRound.v
9
//    - floating point rounding unit
10
//    - parameterized FPWIDth
11
//    - IEEE 754 representation
12
//
13
//
14
// This source file is free software: you can redistribute it and/or modify 
15
// it under the terms of the GNU Lesser General Public License as published 
16
// by the Free Software Foundation, either version 3 of the License, or     
17
// (at your option) any later version.                                      
18
//                                                                          
19
// This source file is distributed in the hope that it will be useful,      
20
// but WITHOUT ANY WARRANTY; without even the implied warranty of           
21
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            
22
// GNU General Public License for more details.                             
23
//                                                                          
24
// You should have received a copy of the GNU General Public License        
25
// along with this program.  If not, see <http://www.gnu.org/licenses/>.    
26
//                                                                          
27
// ============================================================================
28
 
29
`include "fpConfig.sv"
30
 
31
module fpRound(clk, ce, rm, i, o);
32
parameter FPWID = 128;
33
`include "fpSize.sv"
34
input clk;
35
input ce;
36
input [2:0] rm;                  // rounding mode
37
input [MSB+3:0] i;               // intermediate format input
38
output [MSB:0] o;                // rounded output
39
 
40
//------------------------------------------------------------
41
// variables
42
wire so;
43
wire [EMSB:0] xo;
44
reg  [FMSB:0] mo;
45
reg [EMSB:0] xo1;
46
reg [FMSB+3:0] mo1;
47
wire xInf = &i[MSB+2:FMSB+4];
48
wire so0 = i[MSB+3];
49
assign o = {so,xo,mo};
50
 
51
wire g = i[2];  // guard bit: always the same bit for all operations
52
wire r = i[1];  // rounding bit
53
wire s = i[0];   // sticky bit
54
reg rnd;
55
 
56
//------------------------------------------------------------
57
// Clock #1
58
// - determine round amount (add 1 or 0)
59
//------------------------------------------------------------
60
 
61
`ifdef MIN_LATENCY
62
always @*
63
`else
64
always @(posedge clk)
65
`endif
66
if (ce) xo1 <= i[MSB+2:FMSB+4];
67
`ifdef MIN_LATENCY
68
always @*
69
`else
70
always @(posedge clk)
71
`endif
72
if (ce) mo1 <= i[FMSB+3:0];
73
 
74
// Compute the round bit
75
// Infinities and NaNs are not rounded!
76
`ifdef MIN_LATENCY
77
always @*
78
`else
79
always @(posedge clk)
80
`endif
81
if (ce)
82
        casez ({xInf,rm})
83
        4'b0000:        rnd <= (g & r) | (r & s);       // round to nearest even
84
        4'b0001:        rnd <= 1'd0;                                                    // round to zero (truncate)
85
        4'b0010:        rnd <= (r | s) & !so0;          // round towards +infinity
86
        4'b0011:        rnd <= (r | s) & so0;                   // round towards -infinity
87
        4'b0100:  rnd <= (r | s);                                       // round to nearest away from zero
88
        4'b1???:        rnd <= 1'd0;    // no rounding if exponent indicates infinite or NaN
89
        default:        rnd <= 0;
90
        endcase
91
 
92
//------------------------------------------------------------
93
// Clock #2
94
// round the number, check for carry
95
// note: inf. exponent checked above (if the exponent was infinite already, then no rounding occurs as rnd = 0)
96
// note: exponent increments if there is a carry (can only increment to infinity)
97
//------------------------------------------------------------
98
 
99
reg [MSB:0] rounded2;
100
reg carry2;
101
reg rnd2;
102
reg dn2;
103
wire [EMSB:0] xo2;
104
wire [MSB:0] rounded1 = {xo1,mo1[FMSB+3:2]} + rnd;
105
`ifdef MIN_LATENCY
106
always @*
107
`else
108
always @(posedge clk)
109
`endif
110
        if (ce) rounded2 <= rounded1;
111
`ifdef MIN_LATENCY
112
always @*
113
`else
114
always @(posedge clk)
115
`endif
116
        if (ce) carry2 <= mo1[FMSB+3] & !rounded1[FMSB+1];
117
`ifdef MIN_LATENCY
118
always @*
119
`else
120
always @(posedge clk)
121
`endif
122
        if (ce) rnd2 <= rnd;
123
`ifdef MIN_LATENCY
124
always @*
125
`else
126
always @(posedge clk)
127
`endif
128
        if (ce) dn2 <= !(|xo1);
129
assign xo2 = rounded2[MSB:FMSB+2];
130
 
131
//------------------------------------------------------------
132
// Clock #3
133
// - shift mantissa if required.
134
//------------------------------------------------------------
135
`ifdef MIN_LATENCY
136
assign so = i[MSB+3];
137
assign xo = xo2;
138
`else
139
delay3 #(1) u21 (.clk(clk), .ce(ce), .i(i[MSB+3]), .o(so));
140
delay1 #(EMSB+1) u22 (.clk(clk), .ce(ce), .i(xo2), .o(xo));
141
`endif
142
 
143
`ifdef MIN_LATENCY
144
always @*
145
`else
146
always @(posedge clk)
147
`endif
148
        casez({rnd2,&xo2,carry2,dn2})
149
        4'b0??0: mo <= mo1[FMSB+2:2];            // not rounding, not denormalized, => hide MSB
150
        4'b0??1:        mo <= mo1[FMSB+3:3];            // not rounding, denormalized
151
        4'b1000:        mo <= rounded2[FMSB  :0];        // exponent didn't change, number was normalized, => hide MSB,
152
        4'b1001:        mo <= rounded2[FMSB+1:1];       // exponent didn't change, but number was denormalized, => retain MSB
153
        4'b1010:        mo <= rounded2[FMSB+1:1];       // exponent incremented (new MSB generated), number was normalized, => hide 'extra (FMSB+2)' MSB
154
        4'b1011:        mo <= rounded2[FMSB+1:1];       // exponent incremented (new MSB generated), number was denormalized, number became normalized, => hide 'extra (FMSB+2)' MSB
155
        4'b11??:        mo <= 1'd0;                                             // number became infinite, no need to check carry etc., rnd would be zero if input was NaN or infinite
156
        endcase
157
 
158
endmodule
159
 
160
 
161
// Round and register the output
162
/*
163
module fpRoundReg(clk, ce, rm, i, o);
164
parameter FPWID = 128;
165
`include "fpSize.sv"
166
 
167
input clk;
168
input ce;
169
input [2:0] rm;                 // rounding mode
170
input [MSB+3:0] i;              // expanded format input
171
output reg [FPWID-1:0] o;               // rounded output
172
 
173
wire [FPWID-1:0] o1;
174
fpRound #(FPWID) u1 (.rm(rm), .i(i), .o(o1) );
175
 
176
always @(posedge clk)
177
        if (ce)
178
                o <= o1;
179
 
180
endmodule
181
*/

powered by: WebSVN 2.1.0

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