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

Subversion Repositories xilinx_virtex_fp_library

[/] [xilinx_virtex_fp_library/] [trunk/] [SinglePathFPAdderMappedConversions/] [SinglePathAdderConversion.v] - Blame information for rev 19

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 18 constantin
`timescale 1ns / 1ps
2
//////////////////////////////////////////////////////////////////////////////////
3
// Company:     UPT
4
// Engineer:    Constantina-Elena Gavriliu
5
// 
6
// Create Date:    16:09:49 11/04/2013 
7
// Design Name: 
8
 
9
// Module Name:    SinglePathAdderConversion 
10
// Project Name: 
11
// Target Devices: 
12
// Tool versions: 
13
// Description: A ± B with mapped conversions
14
//                              //do not take into consideration cases for which the operation generates a NaN or Infinity exception (with corresponding sign) when initial "special cases" are not such exceptions
15
//
16
// Dependencies:        effective_op.v
17
//                                      leading_zeros.v
18
//                                      rounding.v
19
//                                      shifter.v
20
//                                      special_cases.v: 
21
//
22
// Revision: 
23
// Revision 0.01 - File Created
24
// Additional Comments: 
25
//
26
//////////////////////////////////////////////////////////////////////////////////
27
module SinglePathAdderConversion #(     parameter size_mantissa         = 24, //calculate the size containing the hiden bit 1.M
28
                                                        parameter size_exponent                         = 8,
29
                                                        parameter size_exception_field          = 2,
30
                                                        parameter size_counter                          = 5,    //log2(size_mantissa) + 1 = 5)
31
                                                        parameter [size_exception_field - 1 : 0] zero                    = 0, //00
32
                                                        parameter [size_exception_field - 1 : 0] normal_number   = 1, //01
33
                                                        parameter [size_exception_field - 1 : 0] infinity                = 2, //10
34
                                                        parameter [size_exception_field - 1 : 0] NaN                     = 3, //11
35
                                                        parameter size_integer                  = 32,
36
                                                        parameter counter_integer               = 6,//log2(size_integer) + 1 = 6)
37
                                                        parameter [1 : 0] FP_operation   = 0, //00 
38
                                                        parameter [1 : 0] FP_to_int              = 1, //01 
39
                                                        parameter [1 : 0] int_to_FP              = 2, //10 
40
                                                        parameter pipeline                              = 0,
41
                                                        parameter pipeline_pos                  = 0,     // 8 bits
42
                                                        parameter size  = size_mantissa + size_exponent + size_exception_field
43
                                                        )
44
                                                        (       input [1:0] conversion,
45
                                                                input sub,
46
                                                                input [size - 1 : 0] a_number_i,
47
                                                                input [size - 1 : 0] b_number_i,
48
                                                                output[size - 1 : 0] resulted_number_o);
49
 
50
        parameter double_size_mantissa  = size_mantissa + size_mantissa;
51
        parameter double_size_counter   = size_counter + 1;
52
        parameter max_size                              = (size_integer > size_mantissa)? size_integer : size_mantissa;
53
        parameter max_counter                   = (counter_integer > size_counter)? counter_integer : size_counter;
54
        parameter size_diff_i_m                 = (size_integer > size_mantissa)? (size_integer - size_mantissa) : (size_mantissa - size_integer);
55
        parameter bias                                  = {1'b0,{(size_exponent-1){1'b1}}};
56
        parameter exp_biased                    = bias + size_mantissa;
57
        parameter exponent                              = exp_biased - 1'b1;
58
        parameter subtr                                 = max_size -2'd2;
59
 
60
 
61
        wire [size_exception_field - 1 : 0] sp_case_a_number, sp_case_b_number;
62
        wire [size_mantissa - 1 : 0] m_a_number, m_b_number;
63
        wire [size_exponent - 1 : 0] e_a_number, e_b_number;
64
        wire s_a_number, s_b_number;
65
 
66
        wire [size_exponent     : 0] a_greater_exponent, b_greater_exponent;
67
 
68
        wire [size_exponent - 1 : 0] exp_difference;
69
        wire [size_exponent     : 0] exp_inter;
70
        wire [size_mantissa - 1 : 0] shifted_m_b, convert_neg_mantissa, mantissa_to_shift;
71
 
72
        wire [size_mantissa - 1 : 0] initial_rounding_bits, final_rounding_bits;
73
        wire [size_mantissa - 2 : 0] inter_rounding_bits;
74
        wire eff_op;
75
 
76
        wire [size_mantissa + 2 : 0] adder_mantissa;
77
        wire [size_mantissa + 1 : 0] unnormalized_mantissa;
78
 
79
        wire [size_exception_field - 1 : 0] sp_case_o, resulted_exception_field;
80
        wire [size_mantissa - 1 : 0] resulted_mantissa;
81
        wire [size_exponent - 1 : 0] resulted_exponent;
82
        wire resulted_sign;
83
 
84
        wire zero_flag;
85
 
86
        wire [size_exponent  : 0] subtracter;
87
 
88
        wire [max_size  : 0] dummy_bits;
89
        wire [size_exponent     : 0] shift_value_when_positive_exponent, shift_value_when_negative_exponent;
90
        wire [size_exponent - 1 : 0] shift_value, shft_val;
91
        wire lsb_shft_bit;
92
 
93
        wire [size_exponent - 1 : 0] max_resulted_e_o;
94
        wire [size_exponent - 1 : 0] max_unadjusted_exponent, max_adjust_exponent;
95
        wire [size_exponent - 1 : 0] max_exp_selection;
96
        wire [size_mantissa - 1 : 0] r_mantissa;
97
        wire [max_size  : 0] max_rounded_mantissa;
98
        wire [max_counter - 1 : 0] max_lzs;
99
        wire [max_size - 1 : 0] max_entityINT_FP;
100
        wire [max_size - 1 : 0] init_entityFP_INT, max_entityFP_INT;
101
        wire arith_shift;
102
        wire max_ovf;
103
 
104
        reg intermediar_sign;
105
        wire [4:0] sign_cases;
106
        wire do_conversion;
107
 
108
        wire dummy_ovf, correction, negation_cond;
109
 
110
        assign do_conversion = |conversion; //let me know if there is a conversion
111
 
112
        assign e_a_number       = a_number_i[size_mantissa + size_exponent - 1 : size_mantissa - 1];
113
        assign e_b_number = b_number_i[size_mantissa + size_exponent - 1 : size_mantissa - 1];
114
        assign s_a_number = a_number_i[size - size_exception_field - 1];
115
        assign s_b_number = b_number_i[size - size_exception_field - 1];
116
        assign sp_case_a_number = a_number_i[size - 1 : size - size_exception_field];
117
        assign sp_case_b_number = b_number_i[size - 1 : size - size_exception_field];
118
 
119
 
120
        //find the greater exponent
121
        assign a_greater_exponent = e_a_number - e_b_number;
122
        assign b_greater_exponent = e_b_number - e_a_number;
123
 
124
        //find the difference between exponents
125
        assign exp_difference   = (a_greater_exponent[size_exponent])? b_greater_exponent[size_exponent - 1 : 0] : a_greater_exponent[size_exponent - 1 : 0];
126
        assign exp_inter                = (b_greater_exponent[size_exponent])? {1'b0, e_a_number} : {1'b0, e_b_number};
127
 
128
        //set shifter always on m_b_number
129
        assign {m_a_number, m_b_number} = (b_greater_exponent[size_exponent])?
130
                                                                                                        {{1'b1, a_number_i[size_mantissa - 2 :0]}, {1'b1, b_number_i[size_mantissa - 2 :0]}} :
131
                                                                                                        {{1'b1, b_number_i[size_mantissa - 2 :0]}, {1'b1, a_number_i[size_mantissa - 2 :0]}};
132
 
133
        assign subtracter =  e_a_number - bias;
134
        assign shift_value_when_positive_exponent = subtr - subtracter[size_exponent-1 : 0];
135
        assign shift_value_when_negative_exponent = max_size + (~subtracter[size_exponent-1 : 0]);
136
        assign shift_value = (subtracter[size_exponent])? shift_value_when_negative_exponent[size_exponent - 1 : 0] :
137
                             (shift_value_when_positive_exponent[size_exponent])? (~shift_value_when_positive_exponent[size_exponent - 1 : 0]):
138
                                                                                   shift_value_when_positive_exponent[size_exponent - 1 : 0];
139
        assign shft_val = do_conversion? shift_value : exp_difference;
140
 
141
        assign convert_neg_mantissa = {1'b0, ~a_number_i[size_mantissa-2 : 0]};
142
 
143
        assign mantissa_to_shift = conversion[0]? (s_a_number? convert_neg_mantissa + 1'b1 : {1'b1, a_number_i[size_mantissa-2 : 0]}) : m_b_number;
144
        assign arith_shift = conversion[0]? s_a_number : 1'b0;
145
 
146
        //shift m_b_number                              
147
        shifter #(      .INPUT_SIZE(size_mantissa),
148
                                .SHIFT_SIZE(size_exponent),
149
                                .OUTPUT_SIZE(double_size_mantissa),
150
                                .DIRECTION(1'b0), //0=right, 1=left
151
                                .PIPELINE(pipeline),
152
                                .POSITION(pipeline_pos))
153
                m_b_shifter_instance(   .a(mantissa_to_shift),//mantissa
154
                                                                .arith(arith_shift),//logical shift
155
                                                                .shft(shft_val),
156
                                                                .shifted_a({shifted_m_b, initial_rounding_bits}));
157
 
158
        //istantiate effective_operation_component
159
        effective_op effective_op_instance( .a_sign(s_a_number), .b_sign(s_b_number), .sub(sub), .eff_op(eff_op));
160
 
161
        ///compute addition
162
        assign adder_mantissa = (eff_op)?       ({1'b0, m_a_number, 1'b0} - {1'b0, shifted_m_b, initial_rounding_bits[size_mantissa - 1]}) :
163
 
164
 
165
                                                                                ({1'b0, m_a_number, 1'b0} + {1'b0, shifted_m_b, initial_rounding_bits[size_mantissa - 1]});
166
 
167
        //compute unnormalized_mantissa
168
        assign unnormalized_mantissa = (adder_mantissa[size_mantissa + 2])? ~adder_mantissa[size_mantissa + 1 : 0] : adder_mantissa[size_mantissa + 1 : 0];
169
        assign inter_rounding_bits = (~(|exp_difference[size_exponent - 1 : 1]))?
170
                                                                                        ((adder_mantissa[size_mantissa + 2]? ~initial_rounding_bits[size_mantissa - 2 : 0] : initial_rounding_bits[size_mantissa - 2 : 0])) :
171
                                                                                        ((eff_op)? ((|initial_rounding_bits[size_mantissa - 2 : 0])?~initial_rounding_bits[size_mantissa - 2 : 0] : initial_rounding_bits[size_mantissa - 2 : 0]) : initial_rounding_bits[size_mantissa - 2 : 0]);
172
 
173
        assign max_entityINT_FP = do_conversion? (a_number_i[size_integer-1]? (~a_number_i[max_size-1 : 0]) : a_number_i[max_size-1 : 0]) :
174
                                                                                                                                        {{(max_size-size_mantissa-2){1'b0}}, unnormalized_mantissa[size_mantissa + 1 : 0]};
175
        assign lsb_shft_bit = (do_conversion)? (conversion[0]? s_a_number : a_number_i[size_integer-1]) : inter_rounding_bits[0];
176
 
177
 
178
        //compute leading_zeros over unnormalized mantissa
179
        leading_zeros #(        .SIZE_INT(max_size), .SIZE_COUNTER(max_counter), .PIPELINE(pipeline))
180
                leading_zeros_instance (.a(max_entityINT_FP),
181
                                                                .ovf(1'b0),
182
                                                                .lz(max_lzs));
183
 
184
        assign final_rounding_bits = conversion[1]? {size_mantissa{a_number_i[size_integer-1]}} : {inter_rounding_bits, inter_rounding_bits[0]};
185
 
186
        //compute shifting over unnormalized_mantissa
187
        shifter #(      .INPUT_SIZE(max_size + size_mantissa),
188
                                .SHIFT_SIZE(max_counter),
189
                                .OUTPUT_SIZE(max_size + size_mantissa + 1),
190
                                .DIRECTION(1'b1), //0=right, 1=left
191
                                .PIPELINE(pipeline),
192
                                .POSITION(pipeline_pos))
193
                shifter_instance(       .a({max_entityINT_FP, final_rounding_bits}),//mantissa
194
                                                        .arith(lsb_shft_bit),//logical shift
195
                                                        .shft(max_lzs),
196
                                                        .shifted_a({r_mantissa, dummy_bits}));
197
 
198
        wire [max_size - 1 : 0] entity_to_shift;
199
        wire [max_size : 0] dummy_entity;
200
        assign entity_to_shift = conversion[0]? {shifted_m_b, initial_rounding_bits[size_mantissa-1 : size_mantissa - size_diff_i_m + 1]} : {{size_diff_i_m{1'b0}},r_mantissa};
201
        assign dummy_entity = conversion[0]? {initial_rounding_bits[size_mantissa - size_diff_i_m : 0], {(max_size + size_diff_i_m - size_mantissa){1'b0}}} :
202
                                                                                                ((conversion[1] & (&dummy_bits[max_size-1:0]) & (~dummy_bits[max_size]))? (a_number_i[size_integer-1]? ~dummy_bits : dummy_bits) : dummy_bits);
203
 
204
        assign correction = ~(|exp_difference[size_exponent - 1 : 1])?  1'b0 :
205
                                                        (eff_op? ((|initial_rounding_bits[size_mantissa - 2 : 0])?
206
                                                                ((adder_mantissa[0] | ((~adder_mantissa[0]) & (~adder_mantissa[size_mantissa]) & (~initial_rounding_bits[size_mantissa - 1])
207
                                                                                & (~(&final_rounding_bits[size_mantissa-2 : 0]))))? 1'b1 : 1'b0) : 1'b0) : 1'b0);
208
 
209
        //instantiate rounding_component
210
        rounding #(     .SIZE_MOST_S_MANTISSA(max_size + 1),
211
                                .SIZE_LEAST_S_MANTISSA(max_size + 1))
212
                rounding_instance(      .unrounded_mantissa({1'b0,entity_to_shift}),
213
                                    .dummy_bits(dummy_entity),
214
                                                        .correction(correction),
215
                                    .rounded_mantissa(max_rounded_mantissa));
216
 
217
        assign max_entityFP_INT = {s_a_number, max_rounded_mantissa[max_size - 2 : 0]};
218
 
219
        assign max_exp_selection = do_conversion? exponent : exp_inter-1'b1;
220
        assign max_adjust_exponent = max_exp_selection - max_lzs;
221
        assign max_unadjusted_exponent = max_adjust_exponent + size_diff_i_m;
222
        assign max_resulted_e_o = (do_conversion & ~(|max_entityINT_FP))? bias : max_unadjusted_exponent + max_rounded_mantissa[size_mantissa];
223
 
224
        assign resulted_exponent = conversion[0]?        max_entityFP_INT[size_mantissa+size_exponent-2 : size_mantissa-1] : max_resulted_e_o;
225
        assign resulted_mantissa = conversion[0]?        max_entityFP_INT[size_mantissa-1 : 0] :
226
                                                                                                (max_rounded_mantissa[size_mantissa])?  (max_rounded_mantissa[size_mantissa : 1]) :
227
                                                                                                                                                                                (max_rounded_mantissa[size_mantissa-1 : 0]);
228
 
229
 
230
        //compute exception_field
231
        special_cases   #(      .size_exception_field(size_exception_field),
232
                                                .zero(zero),
233
                                                .normal_number(normal_number),
234
                                                .infinity(infinity),
235
                                                .NaN(NaN))
236
                special_cases_instance( .sp_case_a_number(sp_case_a_number),
237
                                                                .sp_case_b_number(sp_case_b_number),
238
                                                                .sp_case_result_o(sp_case_o));
239
 
240
        //compute special case
241 19 constantin
        assign resulted_exception_field = conversion[0]? 2'd0:
242
                                                                                conversion[1]? normal_number : sp_case_o;
243 18 constantin
 
244
        //set zero_flag in case of equal numbers
245
        assign zero_flag = ~((|{resulted_mantissa,sp_case_o[1]}) & (|sp_case_o));
246
 
247
        assign sign_cases = {eff_op, s_a_number, s_b_number, a_greater_exponent[size_exponent], b_greater_exponent[size_exponent]};
248
 
249
        always
250
                @(*)
251
        begin
252
                case (sign_cases)
253
                        5'b00000:       intermediar_sign = 1'b0;
254
                        5'b00001:       intermediar_sign = 1'b0;
255
                        5'b00010:       intermediar_sign = 1'b0;
256
 
257
                        5'b10000:       intermediar_sign = ~adder_mantissa[size_mantissa+1];
258
                        5'b10001:       intermediar_sign = 1'b0;
259
                        5'b10010:       intermediar_sign = 1'b1;
260
 
261
                        5'b10100:       intermediar_sign = ~adder_mantissa[size_mantissa+1];
262
                        5'b10101:       intermediar_sign = 1'b0;
263
                        5'b10110:       intermediar_sign = 1'b1;
264
 
265
                        5'b00100:       intermediar_sign = 1'b0;
266
                        5'b00101:       intermediar_sign = 1'b0;
267
                        5'b00110:       intermediar_sign = 1'b0;
268
 
269
                        5'b11000:       intermediar_sign = adder_mantissa[size_mantissa+1];
270
                        5'b11001:       intermediar_sign = 1'b1;
271
                        5'b11010:       intermediar_sign = 1'b0;
272
 
273
                        5'b01000:       intermediar_sign = 1'b1;
274
                        5'b01001:       intermediar_sign = 1'b1;
275
                        5'b01010:       intermediar_sign = 1'b1;
276
 
277
                        5'b01100:       intermediar_sign = 1'b1;
278
                        5'b01101:       intermediar_sign = 1'b1;
279
                        5'b01110:       intermediar_sign = 1'b1;
280
 
281
                        5'b11100:       intermediar_sign = adder_mantissa[size_mantissa+1];
282
                        5'b11101:       intermediar_sign = 1'b1;
283
                        5'b11110:       intermediar_sign = 1'b0;
284
 
285
                        default: intermediar_sign = 1'b1;
286
                endcase
287
        end
288
 
289 19 constantin
        assign resulted_sign = conversion[0]? 1'b0 :
290
                                                                conversion[1]? a_number_i[size_integer-1] : intermediar_sign;
291 18 constantin
 
292
        assign resulted_number_o =  do_conversion? {resulted_exception_field, resulted_sign, resulted_exponent, resulted_mantissa[size_mantissa - 2 : 0]} :
293
                                                                (zero_flag | (~(|resulted_exception_field)))? {size{1'b0}} :
294
                                                                        (&(resulted_exception_field))? {resulted_exception_field, resulted_sign,{(size-1-size_exception_field){1'b0}}} :
295
                                                                        (resulted_exception_field[1])? {resulted_exception_field, {(size-size_exception_field){1'b0}}} :
296
                                                                        (!sp_case_a_number)? {b_number_i[size-1 : size-size_exception_field], resulted_sign, b_number_i[size-1-size_exception_field-1 : 0]} :
297
                                                                        (!sp_case_b_number)? {a_number_i[size-1 : size-size_exception_field], resulted_sign, a_number_i[size-1-size_exception_field-1 : 0]} :
298
                                                                        {resulted_exception_field, resulted_sign, resulted_exponent, resulted_mantissa[size_mantissa - 2 : 0]};
299
 
300
endmodule

powered by: WebSVN 2.1.0

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