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

Subversion Repositories ft816float

[/] [ft816float/] [trunk/] [rtl/] [verilog/] [fpRound.v] - Blame information for rev 6

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

Line No. Rev Author Line
1 6 robfinch
// ============================================================================
2
//        __
3
//   \\__/ o\    (C) 2006-2016  Robert Finch, Stratford
4
//    \  __ /    All rights reserved.
5
//     \/_//     robfinch<remove>@finitron.ca
6
//       ||
7
//
8
// This source file is free software: you can redistribute it and/or modify 
9
// it under the terms of the GNU Lesser General Public License as published 
10
// by the Free Software Foundation, either version 3 of the License, or     
11
// (at your option) any later version.                                      
12
//                                                                          
13
// This source file is distributed in the hope that it will be useful,      
14
// but WITHOUT ANY WARRANTY; without even the implied warranty of           
15
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            
16
// GNU General Public License for more details.                             
17
//                                                                          
18
// You should have received a copy of the GNU General Public License        
19
// along with this program.  If not, see <http://www.gnu.org/licenses/>.    
20
//
21
//      fpRound.v
22
//  - floating point rounding unit
23
//  - parameterized width
24
//  - IEEE 754 representation
25
//
26
//      This unit takes a normalized floating point number in an
27
// expanded format and rounds it according to the IEEE-754
28
// standard. NaN's and infinities are not rounded.
29
// This module has a single cycle latency.
30
//
31
// Mode
32
// 0:    round to nearest even
33
// 1:    round to zero (truncate)
34
// 2:    round towards +infinity
35
// 3:    round towards -infinity
36
// ============================================================================
37
//
38
module fpRound(rm, i, o);
39
parameter WID = 32;
40
localparam MSB = WID-1;
41
localparam EMSB = WID==80 ? 14 :
42
                  WID==64 ? 10 :
43
                                  WID==52 ? 10 :
44
                                  WID==48 ? 10 :
45
                                  WID==44 ? 10 :
46
                                  WID==42 ? 10 :
47
                                  WID==40 ?  9 :
48
                                  WID==32 ?  7 :
49
                                  WID==24 ?  6 : 4;
50
localparam FMSB = WID==80 ? 63 :
51
                  WID==64 ? 51 :
52
                                  WID==52 ? 39 :
53
                                  WID==48 ? 35 :
54
                                  WID==44 ? 31 :
55
                                  WID==42 ? 29 :
56
                                  WID==40 ? 28 :
57
                                  WID==32 ? 22 :
58
                                  WID==24 ? 15 : 9;
59
 
60
input [2:0] rm;                  // rounding mode
61
input [WID+3:0] i;               // intermediate format input
62
output [WID-1:0] o;              // rounded output
63
 
64
//------------------------------------------------------------
65
// variables
66
wire so;
67
wire [EMSB:0] xo;
68
reg  [FMSB:0] mo;
69
wire [EMSB:0] xo1 = i[EMSB+FMSB+5:FMSB+5];
70
wire [FMSB+4:0] mo1 = i[FMSB+4:0];
71
wire xInf = &xo1;
72
wire dn = !(|xo1);                      // denormalized input
73
assign o = {so,xo,mo};
74
 
75
wire g = i[2];  // guard bit: always the same bit for all operations
76
wire r = i[1];  // rounding bit
77
wire s = i[0];   // sticky bit
78
reg rnd;
79
 
80
// Compute the round bit
81
// Infinities and NaNs are not rounded!
82
always @(xInf,rm,g,r,s,so)
83
        case ({xInf,rm})
84
        4'd0:   rnd = (g & r) | (r & s);        // round to nearest even
85
        4'd1:   rnd = 0;                                 // round to zero (truncate)
86
        4'd2:   rnd = (r | s) & !so;            // round towards +infinity
87
        4'd3:   rnd = (r | s) & so;                     // round towards -infinity
88
        default:        rnd = 0;                         // no rounding if exponent indicates infinite or NaN
89
        endcase
90
 
91
// round the number, check for carry
92
// note: inf. exponent checked above (if the exponent was infinite already, then no rounding occurs as rnd = 0)
93
// note: exponent increments if there is a carry (can only increment to infinity)
94
// performance note: use the carry chain to increment the exponent
95
wire [MSB+2:0] rounded = {xo1,mo1[FMSB+4:2]} + rnd;
96
wire carry = mo1[FMSB+4] & !rounded[FMSB+2];
97
 
98
assign so = i[WID+3];
99
assign xo = rounded[MSB+2:FMSB+3];
100
 
101
always @(rnd or xo or carry or dn or rounded or mo1)
102
        casex({rnd,&xo,carry,dn})
103
        4'b0xx0:        mo = mo1[FMSB+3:3];             // not rounding, not denormalized, => hide MSB
104
        4'b0xx1:        mo = mo1[FMSB+4:4];             // not rounding, denormalized
105
        4'b1000:        mo = rounded[FMSB+1:1]; // exponent didn't change, number was normalized, => hide MSB
106
        4'b1001:        mo = rounded[FMSB+2:2]; // exponent didn't change, but number was denormalized, => retain MSB
107
        4'b1010:        mo = rounded[FMSB+2:2]; // exponent incremented (new MSB generated), number was normalized, => hide 'extra (FMSB+2)' MSB
108
        4'b1011:        mo = rounded[FMSB+2:2]; // exponent incremented (new MSB generated), number was denormalized, number became normalized, => hide 'extra (FMSB+2)' MSB
109
        4'b11xx:        mo = 0;                                  // number became infinite, no need to check carry etc., rnd would be zero if input was NaN or infinite
110
        endcase
111
 
112
endmodule
113
 
114
 
115
// Round and register the output
116
 
117
module fpRoundReg(clk, ce, rm, i, o);
118
parameter WID = 32;
119
localparam MSB = WID-1;
120
localparam EMSB = WID==80 ? 14 :
121
                  WID==64 ? 10 :
122
                                  WID==52 ? 10 :
123
                                  WID==48 ? 10 :
124
                                  WID==44 ? 10 :
125
                                  WID==42 ? 10 :
126
                                  WID==40 ?  9 :
127
                                  WID==32 ?  7 :
128
                                  WID==24 ?  6 : 4;
129
localparam FMSB = WID==80 ? 63 :
130
                  WID==64 ? 51 :
131
                                  WID==52 ? 39 :
132
                                  WID==48 ? 35 :
133
                                  WID==44 ? 31 :
134
                                  WID==42 ? 29 :
135
                                  WID==40 ? 28 :
136
                                  WID==32 ? 22 :
137
                                  WID==24 ? 15 : 9;
138
 
139
input clk;
140
input ce;
141
input [1:0] rm;                  // rounding mode
142
input [WID+2:0] i;               // expanded format input
143
output reg [WID-1:0] o;          // rounded output
144
 
145
wire [WID-1:0] o1;
146
fpRound #(WID) u1 (.rm(rm), .i(i), .o(o1) );
147
 
148
always @(posedge clk)
149
        if (ce)
150
                o <= o1;
151
 
152
endmodule
153
 
154
module fpRound_tb();
155
 
156
wire [31:0] o1,o2,o3,o4,o5,o6;
157
 
158
fpRound u1 (3'd1, 36'h0, o1);         // zero for zero
159
fpRound u2 (3'd1, 36'h444444444, o2); // 
160
fpRound u3 (3'd1, 36'h444444444, o3); // 
161
 
162
endmodule

powered by: WebSVN 2.1.0

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