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

Subversion Repositories ft816float

[/] [ft816float/] [trunk/] [rtl/] [verilog2/] [fpRound32combo.sv] - Blame information for rev 84

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

Line No. Rev Author Line
1 74 robfinch
// ============================================================================
2
//        __
3
//   \\__/ o\    (C) 2006-2022  Robert Finch, Waterloo
4
//    \  __ /    All rights reserved.
5
//     \/_//     robfinch@finitron.ca
6
//       ||
7
//
8
//      fpRound32combo.sv
9
//    - floating point rounding unit
10
//    - IEEE 754 representation
11
//              - combinational logic only
12
//
13
//
14
// 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
//
40
// ============================================================================
41
 
42
import fp32Pkg::*;
43
 
44
module fpRound32combo(rm, i, o);
45
input [2:0] rm;                 // rounding mode
46
input FP32N i;          // intermediate format input
47
output FP32 o;          // rounded output
48
 
49
//------------------------------------------------------------
50
// variables
51
wire so;
52
wire [fp32Pkg::EMSB:0] xo;
53
reg  [fp32Pkg::FMSB:0] mo;
54
reg [fp32Pkg::EMSB:0] xo1;
55
reg [fp32Pkg::FMSB+3:0] mo1;
56
wire xInf = &i[fp32Pkg::MSB+2:fp32Pkg::FMSB+4];
57
wire so0 = i[fp32Pkg::MSB+3];
58
 
59
assign o.sign = so;
60
assign o.exp = xo;
61
assign o.sig = mo;
62
 
63
 
64
wire l = i[3];
65
wire g = i[2];  // guard bit: always the same bit for all operations
66
wire r = i[1];  // rounding bit
67
wire s = i[0];  // sticky bit
68
reg rnd;
69
 
70
//------------------------------------------------------------
71
// Clock #1
72
// - determine round amount (add 1 or 0)
73
//------------------------------------------------------------
74
 
75
always_comb
76
        xo1 <= i[fp32Pkg::MSB+2:fp32Pkg::FMSB+4];
77
always_comb
78
        mo1 <= i[fp32Pkg::FMSB+3:0];
79
 
80
wire tie = g & ~(r|s);
81
// Compute the round bit
82
// Infinities and NaNs are not rounded!
83
always_comb
84
        casez ({xInf,rm})
85
        4'b0000:  rnd <= (g & (r|s)) | (l & tie); // round to nearest ties to even
86
        4'b0001:        rnd <= 1'd0;                            // round to zero (truncate)
87
        4'b0010:        rnd <= g & !so0;              // round towards +infinity
88
        4'b0011:        rnd <= g & so0;                      // round towards -infinity
89
        4'b0100:  rnd <= (g & (r|s)) | tie; // round to nearest ties away from zero
90
        4'b1???:        rnd <= 1'd0;    // no rounding if exponent indicates infinite or NaN
91
        default:        rnd <= 0;
92
        endcase
93
 
94
//------------------------------------------------------------
95
// Clock #2
96
// round the number, check for carry
97
// note: inf. exponent checked above (if the exponent was infinite already, then no rounding occurs as rnd = 0)
98
// note: exponent increments if there is a carry (can only increment to infinity)
99
//------------------------------------------------------------
100
 
101
reg [fp32Pkg::MSB:0] rounded2;
102
reg carry2;
103
reg rnd2;
104
reg dn2;
105
wire [fp32Pkg::EMSB:0] xo2;
106
wire [fp32Pkg::MSB:0] rounded1 = {xo1,mo1[fp32Pkg::FMSB+3:3],1'b0} + {rnd,1'b0};        // Add onto LSB, GRS=0
107
always_comb
108
        rounded2 <= rounded1;
109
always_comb
110
        carry2 <= mo1[fp32Pkg::FMSB+3] & !rounded1[fp32Pkg::FMSB+1];
111
always_comb
112
        rnd2 <= rnd;
113
always_comb
114
        dn2 <= !(|xo1);
115
assign xo2 = rounded2[fp32Pkg::MSB:fp32Pkg::FMSB+2];
116
 
117
//------------------------------------------------------------
118
// Clock #3
119
// - shift mantissa if required.
120
//------------------------------------------------------------
121
assign so = i[fp32Pkg::MSB+3];
122
assign xo = xo2;
123
 
124
always_comb
125
        casez({rnd2,&xo2,carry2,dn2})
126
        4'b0??0:        mo <= mo1[fp32Pkg::FMSB+2:2];		// not rounding, not denormalized, => hide MSB
127
        4'b0??1:        mo <= mo1[fp32Pkg::FMSB+3:3];           // not rounding, denormalized
128
        4'b1000:        mo <= rounded2[fp32Pkg::FMSB  :0];	// exponent didn't change, number was normalized, => hide MSB,
129
        4'b1001:        mo <= rounded2[fp32Pkg::FMSB+1:1];	// exponent didn't change, but number was denormalized, => retain MSB
130
        4'b1010:        mo <= rounded2[fp32Pkg::FMSB+1:1];	// exponent incremented (new MSB generated), number was normalized, => hide 'extra (FMSB+2)' MSB
131
        4'b1011:        mo <= rounded2[fp32Pkg::FMSB+1:1];	// exponent incremented (new MSB generated), number was denormalized, number became normalized, => hide 'extra (FMSB+2)' MSB
132
        4'b11??:        mo <= 1'd0;                                             // number became infinite, no need to check carry etc., rnd would be zero if input was NaN or infinite
133
        endcase
134
 
135
endmodule

powered by: WebSVN 2.1.0

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