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

Subversion Repositories ft816float

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

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

powered by: WebSVN 2.1.0

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