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

Subversion Repositories ft816float

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

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 26 robfinch
`include "fpSize.sv"
33 6 robfinch
 
34
input [2:0] rm;                  // rounding mode
35 8 robfinch
input [MSB+3:0] i;               // intermediate format input
36 6 robfinch
output [WID-1:0] o;              // rounded output
37
 
38
//------------------------------------------------------------
39
// variables
40
wire so;
41
wire [EMSB:0] xo;
42
reg  [FMSB:0] mo;
43 8 robfinch
wire [EMSB:0] xo1 = i[MSB+2:FMSB+4];
44
wire [FMSB+3:0] mo1 = i[FMSB+3:0];
45 6 robfinch
wire xInf = &xo1;
46
wire dn = !(|xo1);                      // denormalized input
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
// Compute the round bit
55
// Infinities and NaNs are not rounded!
56
always @(xInf,rm,g,r,s,so)
57 13 robfinch
        casez ({xInf,rm})
58
        4'b0000:        rnd = (g & r) | (r & s);        // round to nearest even
59
        4'b0001:        rnd = 0;                                 // round to zero (truncate)
60
        4'b0010:        rnd = (r | s) & !so;            // round towards +infinity
61
        4'b0011:        rnd = (r | s) & so;                     // round towards -infinity
62
        4'b1???:    rnd = (r | s);
63 6 robfinch
        default:        rnd = 0;                         // no rounding if exponent indicates infinite or NaN
64
        endcase
65
 
66
// round the number, check for carry
67
// note: inf. exponent checked above (if the exponent was infinite already, then no rounding occurs as rnd = 0)
68
// note: exponent increments if there is a carry (can only increment to infinity)
69
// performance note: use the carry chain to increment the exponent
70 8 robfinch
wire [MSB:0] rounded = {xo1,mo1[FMSB+3:2]} + rnd;
71
wire carry = mo1[FMSB+3] & !rounded[FMSB+1];
72 6 robfinch
 
73 8 robfinch
assign so = i[MSB+3];
74
assign xo = rounded[MSB:FMSB+2];
75 6 robfinch
 
76
always @(rnd or xo or carry or dn or rounded or mo1)
77 10 robfinch
        casez({rnd,&xo,carry,dn})
78
        4'b0??0: mo = mo1[FMSB+2:2];             // not rounding, not denormalized, => hide MSB
79
        4'b0??1:        mo = mo1[FMSB+3:3];             // not rounding, denormalized
80 8 robfinch
        4'b1000:        mo = rounded[FMSB  :0];  // exponent didn't change, number was normalized, => hide MSB
81
        4'b1001:        mo = rounded[FMSB+1:1]; // exponent didn't change, but number was denormalized, => retain MSB
82
        4'b1010:        mo = rounded[FMSB+1:1]; // exponent incremented (new MSB generated), number was normalized, => hide 'extra (FMSB+2)' MSB
83
        4'b1011:        mo = rounded[FMSB+1:1]; // exponent incremented (new MSB generated), number was denormalized, number became normalized, => hide 'extra (FMSB+2)' MSB
84 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
85 6 robfinch
        endcase
86
 
87
endmodule
88
 
89
 
90
// Round and register the output
91
 
92
module fpRoundReg(clk, ce, rm, i, o);
93 8 robfinch
parameter WID = 128;
94 26 robfinch
`include "fpSize.sv"
95 6 robfinch
 
96
input clk;
97
input ce;
98 8 robfinch
input [2:0] rm;                  // rounding mode
99
input [MSB+3:0] i;               // expanded format input
100 6 robfinch
output reg [WID-1:0] o;          // rounded output
101
 
102
wire [WID-1:0] o1;
103
fpRound #(WID) u1 (.rm(rm), .i(i), .o(o1) );
104
 
105
always @(posedge clk)
106
        if (ce)
107
                o <= o1;
108
 
109
endmodule

powered by: WebSVN 2.1.0

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