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

Subversion Repositories fpu

[/] [fpu/] [trunk/] [verilog/] [pre_norm.v] - Blame information for rev 11

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 rudi
/////////////////////////////////////////////////////////////////////
2
////                                                             ////
3
////  Pre Normalize                                              ////
4
////  Pre Normalization Unit for Add/Sub Operations              ////
5
////                                                             ////
6
////  Author: Rudolf Usselmann                                   ////
7
////          rudi@asics.ws                                      ////
8
////                                                             ////
9
/////////////////////////////////////////////////////////////////////
10
////                                                             ////
11
//// Copyright (C) 2000 Rudolf Usselmann                         ////
12
////                    rudi@asics.ws                            ////
13
////                                                             ////
14
//// This source file may be used and distributed without        ////
15
//// restriction provided that this copyright statement is not   ////
16
//// removed from the file and that any derivative work contains ////
17
//// the original copyright notice and the associated disclaimer.////
18
////                                                             ////
19
////     THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY     ////
20
//// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED   ////
21
//// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS   ////
22
//// FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL THE AUTHOR      ////
23
//// OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,         ////
24
//// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES    ////
25
//// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE   ////
26
//// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR        ////
27
//// BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF  ////
28
//// LIABILITY, WHETHER IN  CONTRACT, STRICT LIABILITY, OR TORT  ////
29
//// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT  ////
30
//// OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE         ////
31
//// POSSIBILITY OF SUCH DAMAGE.                                 ////
32
////                                                             ////
33
/////////////////////////////////////////////////////////////////////
34
 
35
`timescale 1ns / 100ps
36
 
37
 
38
module pre_norm(clk, rmode, add, opa, opb, opa_nan, opb_nan, fracta_out,
39
                fractb_out, exp_dn_out, sign, nan_sign, result_zero_sign,
40
                fasu_op);
41
input           clk;
42
input   [1:0]    rmode;
43
input           add;
44
input   [31:0]   opa, opb;
45
input           opa_nan, opb_nan;
46
output  [26:0]   fracta_out, fractb_out;
47
output  [7:0]    exp_dn_out;
48
output          sign;
49
output          nan_sign, result_zero_sign;
50
output          fasu_op;                        // Operation Output
51
 
52
////////////////////////////////////////////////////////////////////////
53
//
54
// Local Wires and registers
55
//
56
 
57
wire            signa, signb;           // alias to opX sign
58
wire    [7:0]    expa, expb;             // alias to opX exponent
59
wire    [22:0]   fracta, fractb;         // alias to opX fraction
60
wire            expa_lt_expb;           // expa is larger than expb indicator
61
wire            fractb_lt_fracta;       // fractb is larger than fracta indicator
62
reg     [7:0]    exp_dn_out;             // de normalized exponent output
63
wire    [7:0]    exp_small, exp_large;
64
wire    [7:0]    exp_diff;               // Numeric difference of the two exponents
65
wire    [22:0]   adj_op;                 // Fraction adjustment: input
66
wire    [26:0]   adj_op_tmp;
67
wire    [26:0]   adj_op_out;             // Fraction adjustment: output
68
wire    [26:0]   fracta_n, fractb_n;     // Fraction selection after normalizing
69
wire    [26:0]   fracta_s, fractb_s;     // Fraction Sorting out
70
reg     [26:0]   fracta_out, fractb_out; // Fraction Output
71
reg             sign, sign_d;           // Sign Output
72
reg             add_d;                  // operation (add/sub)
73
reg             fasu_op;                // operation (add/sub) register
74
wire            expa_dn, expb_dn;
75
reg             sticky;
76
reg             result_zero_sign;
77
reg             add_r, signa_r, signb_r;
78
wire    [4:0]    exp_diff_sft;
79
wire            exp_lt_27;
80
wire            op_dn;
81
wire    [26:0]   adj_op_out_sft;
82
reg             fracta_lt_fractb, fracta_eq_fractb;
83
wire            nan_sign1;
84
reg             nan_sign;
85
 
86
////////////////////////////////////////////////////////////////////////
87
//
88
// Aliases
89
//
90
 
91
assign  signa = opa[31];
92
assign  signb = opb[31];
93
assign   expa = opa[30:23];
94
assign   expb = opb[30:23];
95
assign fracta = opa[22:0];
96
assign fractb = opb[22:0];
97
 
98
////////////////////////////////////////////////////////////////////////
99
//
100
// Pre-Normalize exponents (and fractions)
101
//
102
 
103
assign expa_lt_expb = expa > expb;              // expa is larger than expb
104
 
105
// ---------------------------------------------------------------------
106
// Normalize
107
 
108
assign expa_dn = !(|expa);                      // opa denormalized
109
assign expb_dn = !(|expb);                      // opb denormalized
110
 
111
// ---------------------------------------------------------------------
112
// Calculate the difference between the smaller and larger exponent
113
 
114
wire    [7:0]    exp_diff1, exp_diff1a, exp_diff2;
115
 
116
assign exp_small  = expa_lt_expb ? expb : expa;
117
assign exp_large  = expa_lt_expb ? expa : expb;
118
assign exp_diff1  = exp_large - exp_small;
119
assign exp_diff1a = exp_diff1-1;
120
assign exp_diff2  = (expa_dn | expb_dn) ? exp_diff1a : exp_diff1;
121
assign  exp_diff  = (expa_dn & expb_dn) ? 8'h0 : exp_diff2;
122
 
123
always @(posedge clk)   // If numbers are equal we should return zero
124
        exp_dn_out <= #1 (!add_d & expa==expb & fracta==fractb) ? 8'h0 : exp_large;
125
 
126
// ---------------------------------------------------------------------
127
// Adjust the smaller fraction
128
 
129
 
130
assign op_dn      = expa_lt_expb ? expb_dn : expa_dn;
131
assign adj_op     = expa_lt_expb ? fractb : fracta;
132
assign adj_op_tmp = { ~op_dn, adj_op, 3'b0 };   // recover hidden bit (op_dn) 
133
 
134
// adj_op_out is 27 bits wide, so can only be shifted 27 bits to the right
135
assign exp_lt_27        = exp_diff  > 8'd27;
136
assign exp_diff_sft     = exp_lt_27 ? 5'd27 : exp_diff[4:0];
137
assign adj_op_out_sft   = adj_op_tmp >> exp_diff_sft;
138
assign adj_op_out       = {adj_op_out_sft[26:1], adj_op_out_sft[0] | sticky };
139
 
140
// ---------------------------------------------------------------------
141
// Get truncated portion (sticky bit)
142
 
143
always @(exp_diff_sft or adj_op_tmp)
144
   case(exp_diff_sft)           // synopsys full_case parallel_case
145
        00: sticky = 1'h0;
146
        01: sticky =  adj_op_tmp[0];
147
        02: sticky = |adj_op_tmp[01:0];
148
        03: sticky = |adj_op_tmp[02:0];
149
        04: sticky = |adj_op_tmp[03:0];
150
        05: sticky = |adj_op_tmp[04:0];
151
        06: sticky = |adj_op_tmp[05:0];
152
        07: sticky = |adj_op_tmp[06:0];
153
        08: sticky = |adj_op_tmp[07:0];
154
        09: sticky = |adj_op_tmp[08:0];
155
        10: sticky = |adj_op_tmp[09:0];
156
        11: sticky = |adj_op_tmp[10:0];
157
        12: sticky = |adj_op_tmp[11:0];
158
        13: sticky = |adj_op_tmp[12:0];
159
        14: sticky = |adj_op_tmp[13:0];
160
        15: sticky = |adj_op_tmp[14:0];
161
        16: sticky = |adj_op_tmp[15:0];
162
        17: sticky = |adj_op_tmp[16:0];
163
        18: sticky = |adj_op_tmp[17:0];
164
        19: sticky = |adj_op_tmp[18:0];
165
        20: sticky = |adj_op_tmp[19:0];
166
        21: sticky = |adj_op_tmp[20:0];
167
        22: sticky = |adj_op_tmp[21:0];
168
        23: sticky = |adj_op_tmp[22:0];
169
        24: sticky = |adj_op_tmp[23:0];
170
        25: sticky = |adj_op_tmp[24:0];
171
        26: sticky = |adj_op_tmp[25:0];
172
        27: sticky = |adj_op_tmp[26:0];
173
   endcase
174
 
175
// ---------------------------------------------------------------------
176
// Select operands for add/sub (recover hidden bit)
177
 
178
assign fracta_n = expa_lt_expb ? {~expa_dn, fracta, 3'b0} : adj_op_out;
179
assign fractb_n = expa_lt_expb ? adj_op_out : {~expb_dn, fractb, 3'b0};
180
 
181
// ---------------------------------------------------------------------
182
// Sort operands (for sub only)
183
 
184
assign fractb_lt_fracta = fractb_n > fracta_n;  // fractb is larger than fracta
185
assign fracta_s = fractb_lt_fracta ? fractb_n : fracta_n;
186
assign fractb_s = fractb_lt_fracta ? fracta_n : fractb_n;
187
 
188
always @(posedge clk)
189
        fracta_out <= #1 fracta_s;
190
 
191
always @(posedge clk)
192
        fractb_out <= #1 fractb_s;
193
 
194
// ---------------------------------------------------------------------
195
// Determine sign for the output
196
 
197
// sign: 0=Positive Number; 1=Negative Number
198
always @(signa or signb or add or fractb_lt_fracta)
199
   case({signa, signb, add})            // synopsys full_case parallel_case
200
 
201
        // Add
202
        3'b0_0_1: sign_d = 0;
203
        3'b0_1_1: sign_d = fractb_lt_fracta;
204
        3'b1_0_1: sign_d = !fractb_lt_fracta;
205
        3'b1_1_1: sign_d = 1;
206
 
207
        // Sub
208
        3'b0_0_0: sign_d = fractb_lt_fracta;
209
        3'b0_1_0: sign_d = 0;
210
        3'b1_0_0: sign_d = 1;
211
        3'b1_1_0: sign_d = !fractb_lt_fracta;
212
   endcase
213
 
214
always @(posedge clk)
215
        sign <= #1 sign_d;
216
 
217
// Fix sign for ZERO result
218
always @(posedge clk)
219
        signa_r <= #1 signa;
220
 
221
always @(posedge clk)
222
        signb_r <= #1 signb;
223
 
224
always @(posedge clk)
225
        add_r <= #1 add;
226
 
227
always @(posedge clk)
228
        result_zero_sign <= #1  ( add_r &  signa_r &  signb_r) |
229
                                (!add_r &  signa_r & !signb_r) |
230
                                ( add_r & (signa_r |  signb_r) & (rmode==3)) |
231
                                (!add_r & (signa_r == signb_r) & (rmode==3));
232
 
233
// Fix sign for NAN result
234
always @(posedge clk)
235
        fracta_lt_fractb <= #1 fracta < fractb;
236
 
237
always @(posedge clk)
238
        fracta_eq_fractb <= #1 fracta == fractb;
239
 
240
assign nan_sign1 = fracta_eq_fractb ? (signa_r & signb_r) : fracta_lt_fractb ? signb_r : signa_r;
241
 
242
always @(posedge clk)
243
        nan_sign <= #1 (opa_nan & opb_nan) ? nan_sign1 : opb_nan ? signb_r : signa_r;
244
 
245
////////////////////////////////////////////////////////////////////////
246
//
247
// Decode Add/Sub operation
248
//
249
 
250
// add: 1=Add; 0=Subtract
251
always @(signa or signb or add)
252
   case({signa, signb, add})            // synopsys full_case parallel_case
253
 
254
        // Add
255
        3'b0_0_1: add_d = 1;
256
        3'b0_1_1: add_d = 0;
257
        3'b1_0_1: add_d = 0;
258
        3'b1_1_1: add_d = 1;
259
 
260
        // Sub
261
        3'b0_0_0: add_d = 0;
262
        3'b0_1_0: add_d = 1;
263
        3'b1_0_0: add_d = 1;
264
        3'b1_1_0: add_d = 0;
265
   endcase
266
 
267
always @(posedge clk)
268
        fasu_op <= #1 add_d;
269
 
270
endmodule

powered by: WebSVN 2.1.0

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