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

Subversion Repositories ft816float

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

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 49 robfinch
// BSD 3-Clause License
15
// Redistribution and use in source and binary forms, with or without
16
// modification, are permitted provided that the following conditions are met:
17
//
18
// 1. Redistributions of source code must retain the above copyright notice, this
19
//    list of conditions and the following disclaimer.
20
//
21
// 2. Redistributions in binary form must reproduce the above copyright notice,
22
//    this list of conditions and the following disclaimer in the documentation
23
//    and/or other materials provided with the distribution.
24
//
25
// 3. Neither the name of the copyright holder nor the names of its
26
//    contributors may be used to endorse or promote products derived from
27
//    this software without specific prior written permission.
28
//
29
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
30
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
32
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
33
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
36
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
37
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 48 robfinch
//
40
// ============================================================================
41
 
42
import fp::*;
43
 
44 49 robfinch
`ifdef MIN_LATENCY
45
`define PIPE_ADV  *
46
`else
47
`define PIPE_ADV  (posedge clk)
48
`endif
49
 
50 48 robfinch
module fpRound(clk, ce, rm, i, o);
51
input clk;
52
input ce;
53
input [2:0] rm;                 // rounding mode
54
input [MSB+3:0] i;              // intermediate format input
55
output [MSB:0] o;               // rounded output
56
 
57
//------------------------------------------------------------
58
// variables
59
wire so;
60
wire [EMSB:0] xo;
61
reg  [FMSB:0] mo;
62
reg [EMSB:0] xo1;
63
reg [FMSB+3:0] mo1;
64
wire xInf = &i[MSB+2:FMSB+4];
65
wire so0 = i[MSB+3];
66
assign o = {so,xo,mo};
67
 
68 49 robfinch
wire l = i[3];
69 48 robfinch
wire g = i[2];  // guard bit: always the same bit for all operations
70
wire r = i[1];  // rounding bit
71
wire s = i[0];  // sticky bit
72
reg rnd;
73
 
74
//------------------------------------------------------------
75
// Clock #1
76
// - determine round amount (add 1 or 0)
77
//------------------------------------------------------------
78
 
79 49 robfinch
always @`PIPE_ADV
80 48 robfinch
if (ce) xo1 <= i[MSB+2:FMSB+4];
81 49 robfinch
always @`PIPE_ADV
82 48 robfinch
if (ce) mo1 <= i[FMSB+3:0];
83
 
84 49 robfinch
wire tie = g & ~(r|s);
85 48 robfinch
// Compute the round bit
86
// Infinities and NaNs are not rounded!
87 49 robfinch
always @`PIPE_ADV
88 48 robfinch
if (ce)
89
        casez ({xInf,rm})
90 49 robfinch
        4'b0000:  rnd <= (g & (r|s)) | (l & tie); // round to nearest ties to even
91
        4'b0001:        rnd <= 1'd0;                            // round to zero (truncate)
92
        4'b0010:        rnd <= g & !so0;              // round towards +infinity
93
        4'b0011:        rnd <= g & so0;                      // round towards -infinity
94
        4'b0100:  rnd <= (g & (r|s)) | tie; // round to nearest ties away from zero
95 48 robfinch
        4'b1???:        rnd <= 1'd0;    // no rounding if exponent indicates infinite or NaN
96
        default:        rnd <= 0;
97
        endcase
98
 
99
//------------------------------------------------------------
100
// Clock #2
101
// round the number, check for carry
102
// note: inf. exponent checked above (if the exponent was infinite already, then no rounding occurs as rnd = 0)
103
// note: exponent increments if there is a carry (can only increment to infinity)
104
//------------------------------------------------------------
105
 
106
reg [MSB:0] rounded2;
107
reg carry2;
108
reg rnd2;
109
reg dn2;
110
wire [EMSB:0] xo2;
111 49 robfinch
wire [MSB:0] rounded1 = {xo1,mo1[FMSB+3:3],1'b0} + {rnd,1'b0};  // Add onto LSB, GRS=0
112
always @`PIPE_ADV
113 48 robfinch
        if (ce) rounded2 <= rounded1;
114 49 robfinch
always @`PIPE_ADV
115 48 robfinch
        if (ce) carry2 <= mo1[FMSB+3] & !rounded1[FMSB+1];
116 49 robfinch
always @`PIPE_ADV
117 48 robfinch
        if (ce) rnd2 <= rnd;
118 49 robfinch
always @`PIPE_ADV
119 48 robfinch
        if (ce) dn2 <= !(|xo1);
120
assign xo2 = rounded2[MSB:FMSB+2];
121
 
122
//------------------------------------------------------------
123
// Clock #3
124
// - shift mantissa if required.
125
//------------------------------------------------------------
126
`ifdef MIN_LATENCY
127
assign so = i[MSB+3];
128
assign xo = xo2;
129
`else
130
delay3 #(1) u21 (.clk(clk), .ce(ce), .i(i[MSB+3]), .o(so));
131
delay1 #(EMSB+1) u22 (.clk(clk), .ce(ce), .i(xo2), .o(xo));
132
`endif
133
 
134 49 robfinch
always @`PIPE_ADV
135
if (ce)
136 48 robfinch
        casez({rnd2,&xo2,carry2,dn2})
137
        4'b0??0:        mo <= mo1[FMSB+2:2];		// not rounding, not denormalized, => hide MSB
138
        4'b0??1:        mo <= mo1[FMSB+3:3];            // not rounding, denormalized
139
        4'b1000:        mo <= rounded2[FMSB  :0];	// exponent didn't change, number was normalized, => hide MSB,
140
        4'b1001:        mo <= rounded2[FMSB+1:1];	// exponent didn't change, but number was denormalized, => retain MSB
141
        4'b1010:        mo <= rounded2[FMSB+1:1];	// exponent incremented (new MSB generated), number was normalized, => hide 'extra (FMSB+2)' MSB
142
        4'b1011:        mo <= rounded2[FMSB+1:1];	// exponent incremented (new MSB generated), number was denormalized, number became normalized, => hide 'extra (FMSB+2)' MSB
143
        4'b11??:        mo <= 1'd0;                                             // number became infinite, no need to check carry etc., rnd would be zero if input was NaN or infinite
144
        endcase
145
 
146
endmodule

powered by: WebSVN 2.1.0

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