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

Subversion Repositories thor

[/] [thor/] [trunk/] [FT64v5/] [rtl/] [fpUnit/] [fpRound.v] - Blame information for rev 51

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 51 robfinch
`timescale 1ns / 1ps
2
// ============================================================================
3
//        __
4
//   \\__/ o\    (C) 2006-2018  Robert Finch, Waterloo
5
//    \  __ /    All rights reserved.
6
//     \/_//     robfinch<remove>@finitron.ca
7
//       ||
8
//
9
//      fpRound.v
10
//    - floating point rounding unit
11
//    - parameterized width
12
//    - IEEE 754 representation
13
//
14
//
15
// 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
//                                                                          
28
// ============================================================================
29
 
30
module fpRound(rm, i, o);
31
parameter WID = 128;
32
localparam MSB = WID-1;
33
localparam EMSB = WID==128 ? 14 :
34
                  WID==96 ? 14 :
35
                  WID==80 ? 14 :
36
                  WID==64 ? 10 :
37
                                  WID==52 ? 10 :
38
                                  WID==48 ? 11 :
39
                                  WID==44 ? 10 :
40
                                  WID==42 ? 10 :
41
                                  WID==40 ?  9 :
42
                                  WID==32 ?  7 :
43
                                  WID==24 ?  6 : 4;
44
localparam FMSB = WID==128 ? 111 :
45
                  WID==96 ? 79 :
46
                  WID==80 ? 63 :
47
                  WID==64 ? 51 :
48
                                  WID==52 ? 39 :
49
                                  WID==48 ? 34 :
50
                                  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
input [MSB+3:0] i;               // intermediate format input
58
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
wire [EMSB:0] xo1 = i[MSB+2:FMSB+4];
66
wire [FMSB+3:0] mo1 = i[FMSB+3:0];
67
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
        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
        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
wire [MSB:0] rounded = {xo1,mo1[FMSB+3:2]} + rnd;
93
wire carry = mo1[FMSB+3] & !rounded[FMSB+1];
94
 
95
assign so = i[MSB+3];
96
assign xo = rounded[MSB:FMSB+2];
97
 
98
always @(rnd or xo or carry or dn or rounded or mo1)
99
        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
        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
        4'b11??:        mo = 0;                                  // number became infinite, no need to check carry etc., rnd would be zero if input was NaN or infinite
107
        endcase
108
 
109
endmodule
110
 
111
 
112
// Round and register the output
113
 
114
module fpRoundReg(clk, ce, rm, i, o);
115
parameter WID = 128;
116
localparam MSB = WID-1;
117
localparam EMSB = WID==128 ? 14 :
118
                  WID==96 ? 14 :
119
                  WID==80 ? 14 :
120
                  WID==64 ? 10 :
121
                                  WID==52 ? 10 :
122
                                  WID==48 ? 11 :
123
                                  WID==44 ? 10 :
124
                                  WID==42 ? 10 :
125
                                  WID==40 ?  9 :
126
                                  WID==32 ?  7 :
127
                                  WID==24 ?  6 : 4;
128
localparam FMSB = WID==128 ? 111 :
129
                  WID==96 ? 79 :
130
                  WID==80 ? 63 :
131
                  WID==64 ? 51 :
132
                                  WID==52 ? 39 :
133
                                  WID==48 ? 34 :
134
                                  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
input [2:0] rm;                  // rounding mode
143
input [MSB+3:0] i;               // expanded format input
144
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.