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

Subversion Repositories ft816float

[/] [ft816float/] [trunk/] [rtl/] [verilog2/] [fpRound.sv] - Blame information for rev 48

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

Line No. Rev Author Line
1 48 robfinch
// ============================================================================
2
//        __
3
//   \\__/ o\    (C) 2006-2020  Robert Finch, Waterloo
4
//    \  __ /    All rights reserved.
5
//     \/_//     robfinch@finitron.ca
6
//       ||
7
//
8
//      fpRound.sv
9
//    - floating point rounding unit
10
//    - parameterized width
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 .
26
//
27
// ============================================================================
28
 
29
import fp::*;
30
 
31
module fpRound(clk, ce, rm, i, o);
32
input clk;
33
input ce;
34
input [2:0] rm;                 // rounding mode
35
input [MSB+3:0] i;              // intermediate format input
36
output [MSB:0] o;               // rounded output
37
 
38
//------------------------------------------------------------
39
// variables
40
wire so;
41
wire [EMSB:0] xo;
42
reg  [FMSB:0] mo;
43
reg [EMSB:0] xo1;
44
reg [FMSB+3:0] mo1;
45
wire xInf = &i[MSB+2:FMSB+4];
46
wire so0 = i[MSB+3];
47
assign o = {so,xo,mo};
48
 
49
wire g = i[2];  // guard bit: always the same bit for all operations
50
wire r = i[1];  // rounding bit
51
wire s = i[0];  // sticky bit
52
reg rnd;
53
 
54
//------------------------------------------------------------
55
// Clock #1
56
// - determine round amount (add 1 or 0)
57
//------------------------------------------------------------
58
 
59
`ifdef MIN_LATENCY
60
always @*
61
`else
62
always @(posedge clk)
63
`endif
64
if (ce) xo1 <= i[MSB+2:FMSB+4];
65
`ifdef MIN_LATENCY
66
always @*
67
`else
68
always @(posedge clk)
69
`endif
70
if (ce) mo1 <= i[FMSB+3:0];
71
 
72
// Compute the round bit
73
// Infinities and NaNs are not rounded!
74
`ifdef MIN_LATENCY
75
always @*
76
`else
77
always @(posedge clk)
78
`endif
79
if (ce)
80
        casez ({xInf,rm})
81
        4'b0000:        rnd <= (g & r) | (r & s);     // round to nearest even
82
        4'b0001:        rnd <= 1'd0;                                                    // round to zero (truncate)
83
        4'b0010:        rnd <= (r | s) & !so0;                // round towards +infinity
84
        4'b0011:        rnd <= (r | s) & so0;                        // round towards -infinity
85
        4'b0100:  rnd <= (r | s);                                       // round to nearest away from zero
86
        4'b1???:        rnd <= 1'd0;    // no rounding if exponent indicates infinite or NaN
87
        default:        rnd <= 0;
88
        endcase
89
 
90
//------------------------------------------------------------
91
// Clock #2
92
// round the number, check for carry
93
// note: inf. exponent checked above (if the exponent was infinite already, then no rounding occurs as rnd = 0)
94
// note: exponent increments if there is a carry (can only increment to infinity)
95
//------------------------------------------------------------
96
 
97
reg [MSB:0] rounded2;
98
reg carry2;
99
reg rnd2;
100
reg dn2;
101
wire [EMSB:0] xo2;
102
wire [MSB:0] rounded1 = {xo1,mo1[FMSB+3:2]} + rnd;
103
`ifdef MIN_LATENCY
104
always @*
105
`else
106
always @(posedge clk)
107
`endif
108
        if (ce) rounded2 <= rounded1;
109
`ifdef MIN_LATENCY
110
always @*
111
`else
112
always @(posedge clk)
113
`endif
114
        if (ce) carry2 <= mo1[FMSB+3] & !rounded1[FMSB+1];
115
`ifdef MIN_LATENCY
116
always @*
117
`else
118
always @(posedge clk)
119
`endif
120
        if (ce) rnd2 <= rnd;
121
`ifdef MIN_LATENCY
122
always @*
123
`else
124
always @(posedge clk)
125
`endif
126
        if (ce) dn2 <= !(|xo1);
127
assign xo2 = rounded2[MSB:FMSB+2];
128
 
129
//------------------------------------------------------------
130
// Clock #3
131
// - shift mantissa if required.
132
//------------------------------------------------------------
133
`ifdef MIN_LATENCY
134
assign so = i[MSB+3];
135
assign xo = xo2;
136
`else
137
delay3 #(1) u21 (.clk(clk), .ce(ce), .i(i[MSB+3]), .o(so));
138
delay1 #(EMSB+1) u22 (.clk(clk), .ce(ce), .i(xo2), .o(xo));
139
`endif
140
 
141
`ifdef MIN_LATENCY
142
always @*
143
`else
144
always @(posedge clk)
145
`endif
146
        casez({rnd2,&xo2,carry2,dn2})
147
        4'b0??0:        mo <= mo1[FMSB+2:2];		// not rounding, not denormalized, => hide MSB
148
        4'b0??1:        mo <= mo1[FMSB+3:3];            // not rounding, denormalized
149
        4'b1000:        mo <= rounded2[FMSB  :0];	// exponent didn't change, number was normalized, => hide MSB,
150
        4'b1001:        mo <= rounded2[FMSB+1:1];	// exponent didn't change, but number was denormalized, => retain MSB
151
        4'b1010:        mo <= rounded2[FMSB+1:1];	// exponent incremented (new MSB generated), number was normalized, => hide 'extra (FMSB+2)' MSB
152
        4'b1011:        mo <= rounded2[FMSB+1:1];	// exponent incremented (new MSB generated), number was denormalized, number became normalized, => hide 'extra (FMSB+2)' MSB
153
        4'b11??:        mo <= 1'd0;                                             // number became infinite, no need to check carry etc., rnd would be zero if input was NaN or infinite
154
        endcase
155
 
156
endmodule
157
 
158
 
159
// Round and register the output
160
/*
161
module fpRoundReg(clk, ce, rm, i, o);
162
parameter WID = 128;
163
`include "fpSize.sv"
164
 
165
input clk;
166
input ce;
167
input [2:0] rm;                 // rounding mode
168
input [MSB+3:0] i;              // expanded format input
169
output reg [WID-1:0] o;         // rounded output
170
 
171
wire [WID-1:0] o1;
172
fpRound #(WID) u1 (.rm(rm), .i(i), .o(o1) );
173
 
174
always @(posedge clk)
175
        if (ce)
176
                o <= o1;
177
 
178
endmodule
179
*/

powered by: WebSVN 2.1.0

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