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/] [GeneralPrecMAFMappedConversions/] [Multiply_AccumulateConversion.v] - Blame information for rev 19

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 16 constantin
`timescale 1ns / 1ps
2
//////////////////////////////////////////////////////////////////////////////////
3
// Company: 
4
// Engineer: 
5
// 
6
// Create Date:    17:53:05 10/15/2013 
7
// Design Name: 
8
// Module Name:    Multiply_AccumulateConversion
9
// Project Name: 
10
// Target Devices: 
11
// Tool versions: 
12
// Description: C ± A*B with mapped conversions, conversion applies to C number
13
//
14
// Dependencies: 
15
//
16
// Revision: 
17
// Revision 0.01 - File Created
18
// Additional Comments: 
19
//
20
//////////////////////////////////////////////////////////////////////////////////
21
module Multiply_AccumulateConversion #( parameter size_mantissa = 24,   //mantissa bits(1.M)
22
                                                                                parameter size_exponent = 8,    //exponent bits
23
                                                                                parameter size_counter  = 5,    //log2(size_mantissa) + 1 = 5
24
                                                                                parameter size_exception_field = 2,     // zero/normal numbers/infinity/NaN
25 19 constantin
                                                                                parameter [size_exception_field - 1 : 0] zero                    = 00, //00
26
                                                                                parameter [size_exception_field - 1 : 0] normal_number   = 01, //01
27
                                                                                parameter [size_exception_field - 1 : 0] infinity                = 10, //10
28
                                                                                parameter [size_exception_field - 1 : 0] NaN                     = 11, //11
29 16 constantin
                                                                                parameter size_integer                  = 32,
30
                                                                                parameter counter_integer               = 6, //log2(size_integer) + 1 = 6)
31
                                                                                parameter [1 : 0] FP_operation   = 0, //00 
32
                                                                                parameter [1 : 0] FP_to_int              = 1, //01 
33
                                                                                parameter [1 : 0] int_to_FP              = 2, //10 
34
                                                                                parameter pipeline              = 0,
35
                                                                                parameter pipeline_pos  = 0,  //8 bits
36
 
37
                                                                                parameter size = size_exponent + size_mantissa + size_exception_field)
38 19 constantin
                                                                        (       input [1 : 0] conversion,
39 16 constantin
                                                                                input [size - 1:0] c_number_i,
40
                                                                                input [size - 1:0] a_number_i,
41
                                                                                input [size - 1:0] b_number_i,
42
                                                                                input sub,
43
                                                                                output[size - 1:0] resulting_number_o);
44
 
45
        parameter size_mul_mantissa = size_mantissa + size_mantissa;
46
        parameter size_mul_counter      = size_counter + 1;
47
        parameter max_size                      = (size_integer > size_mantissa)? size_integer : size_mantissa;
48
        parameter max_counter           = (counter_integer > size_counter)? counter_integer : size_counter;
49
        parameter size_diff_i_m         = (size_integer > size_mantissa)? (size_integer - size_mantissa) : (size_mantissa - size_integer);
50
        parameter bias                          = {1'b0,{(size_exponent-1){1'b1}}};
51
        parameter exp_biased            = bias + size_mantissa;
52
        parameter exponent                      = (size_mul_mantissa - max_size) + exp_biased;
53
        parameter subtr                         = max_size -2'd2;
54
 
55
        parameter bias_0_bits           = size_exponent - 1;
56
        parameter shift_mantissa_0_bits = size_mantissa-1'b1;
57
 
58
 
59
        wire [size_exception_field - 1 : 0] sp_case_a_number, sp_case_b_number, sp_case_c_number;
60
        wire [size_mantissa - 1 : 0] m_a_number, m_b_number, m_c_number;
61
        wire [size_exponent - 1 : 0] e_a_number, e_b_number, e_c_number;
62
        wire s_a_number, s_b_number, s_c_number;
63
 
64
        wire [size_exponent     : 0] ab_greater_exponent, c_greater_exponent;
65
 
66
        wire [size_exponent - 1 : 0] exp_difference;
67
        wire [size_exponent     : 0] exp_inter;
68
 
69 19 constantin
        wire [size_mantissa - 2 : 0] mul_mantissa;
70 16 constantin
        wire [size_mul_mantissa - 1     : 0] m_ab_mantissa, c_mantissa;
71
        wire [size_exponent                     : 0] e_ab_number_inter, e_ab_number;
72
        wire [size_mul_counter - 1      : 0] lz_mul;
73
 
74
        wire zero_flag;
75 19 constantin
        wire sign_res, sign_inter;
76 16 constantin
        wire eff_op;
77
 
78 19 constantin
        wire [size_mantissa - 1         : 0] initial_rounding_bits, inter_rounding_bits, final_rounding_bits, max_inter_rounding_bits;
79 16 constantin
        wire [size_mul_mantissa + 1 : 0] normalized_mantissa, adder_mantissa;
80
        wire [size_mul_mantissa         : 0] unnormalized_mantissa;
81
        wire [size_mul_mantissa - 1 : 0] shifted_m_ab, convert_neg_mantissa, mantissa_to_shift;
82
        wire [size_mul_mantissa - 1 : 0] m_c, m_ab;
83
 
84 19 constantin
        wire [size_exception_field - 1 : 0] sp_case_mul_result_o;
85
 
86 16 constantin
        wire [size_exception_field - 1 : 0] sp_case_o, sp_case_result_o;
87
        wire [size_mantissa - 2 : 0] final_mantissa;
88
        wire [size_exponent - 1 : 0] final_exponent;
89
        wire [size_mantissa : 0] rounded_mantissa;
90
 
91 19 constantin
        wire [max_size - 1 : 0]  entity_to_round;
92
        wire [size_mul_mantissa + 1 : 0] dummy_to_round, inter_dummy_to_round;
93
        wire [max_size - size_mantissa - 2 : 0] dummy_out;
94 16 constantin
 
95
        wire [size_mantissa - 1 : 0] resulted_mantissa;
96
        wire [size_exponent - 1 : 0] resulted_exponent;
97
 
98
        wire [size_exponent  : 0] subtracter;
99
 
100
        wire [size_mul_mantissa-max_size : 0] max_entityINT_FP_msb;
101
        wire [size_exponent     : 0] shift_value_when_positive_exponent, shift_value_when_negative_exponent;
102
        wire [size_exponent - 1 : 0] shift_value, shft_val;
103
        wire [size_exponent - 1 : 0] max_unadjusted_exponent, max_adjust_exponent, adjust;
104
        wire [size_exponent - 1 : 0] max_exp_selection;
105
        wire [size_exponent - 1 : 0] max_resulted_e_o;
106
        wire [max_size - 1 : 0] max_entityINT_FP, max_entityFP_INT;
107
        wire lsb_shft_bit;
108
        wire arith_shift;
109
        wire max_ovf;
110
 
111
        wire do_conversion;
112
 
113
        assign do_conversion = |conversion; //let me know if there is a conversion
114
 
115
        assign m_a_number                       = {1'b1, a_number_i[size_mantissa - 2 :0]};
116
        assign m_b_number                       = {1'b1, b_number_i[size_mantissa - 2 :0]};
117
        assign m_c_number                       = {1'b1, c_number_i[size_mantissa - 2 :0]};
118
        assign e_a_number                       = a_number_i[size_mantissa + size_exponent - 1 : size_mantissa - 1];
119
        assign e_b_number                       = b_number_i[size_mantissa + size_exponent - 1 : size_mantissa - 1];
120
        assign e_c_number                       = c_number_i[size_mantissa + size_exponent - 1 : size_mantissa - 1];
121
        assign s_a_number                       = a_number_i[size - size_exception_field - 1];
122
        assign s_b_number                       = b_number_i[size - size_exception_field - 1];
123
        assign s_c_number                       = c_number_i[size - size_exception_field - 1];
124
        assign sp_case_a_number = a_number_i[size - 1 : size - size_exception_field];
125
        assign sp_case_b_number = b_number_i[size - 1 : size - size_exception_field];
126
        assign sp_case_c_number = c_number_i[size - 1 : size - size_exception_field];
127
 
128
 
129
        //instantiate multiply component
130
        multiply #(     .size_mantissa(size_mantissa),
131
                                        .size_counter(size_counter),
132
                                        .size_mul_mantissa(size_mul_mantissa))
133
                multiply_instance (     .a_mantissa_i(m_a_number),
134
                                                                        .b_mantissa_i(m_b_number),
135
                                                                        .mul_mantissa(m_ab_mantissa));
136
 
137 19 constantin
        assign mul_mantissa = m_ab_mantissa[size_mul_mantissa-1]?       m_ab_mantissa[size_mul_mantissa-2 : size_mul_mantissa - size_mantissa] :
138
                                                                                                                                m_ab_mantissa[size_mul_mantissa-3 : size_mul_mantissa - size_mantissa - 1];
139
 
140 16 constantin
        assign c_mantissa       = {1'b0,m_c_number, {(shift_mantissa_0_bits){1'b0}}};
141
        assign e_ab_number_inter = e_a_number + e_b_number;
142
        assign e_ab_number = e_ab_number_inter  - {(bias_0_bits){1'b1}};
143
 
144
        //find the greater exponent
145
        assign ab_greater_exponent = e_ab_number - e_c_number;
146
        assign c_greater_exponent = e_c_number - e_ab_number;
147
 
148
        //find the difference between exponents
149
        assign exp_difference   = (ab_greater_exponent[size_exponent])? c_greater_exponent[size_exponent - 1 : 0] : ab_greater_exponent[size_exponent - 1 : 0];
150
        assign exp_inter                = (c_greater_exponent[size_exponent])? {1'b0, e_ab_number} : {1'b0, e_c_number};
151
 
152
        //set shifter always on m_ab_number
153
        assign {m_c, m_ab} = (ab_greater_exponent[size_exponent])? {c_mantissa, m_ab_mantissa} :
154
                                                        {m_ab_mantissa, c_mantissa};
155
 
156
        assign subtracter =  e_c_number - bias;
157
        assign shift_value_when_positive_exponent = subtr - subtracter[size_exponent-1 : 0];
158
        assign shift_value_when_negative_exponent = max_size + (~subtracter[size_exponent-1 : 0]);
159
        assign shift_value = (subtracter[size_exponent])? shift_value_when_negative_exponent[size_exponent - 1 : 0] :
160
                             (shift_value_when_positive_exponent[size_exponent])? (~shift_value_when_positive_exponent[size_exponent - 1 : 0]):
161
                                                                                   shift_value_when_positive_exponent[size_exponent - 1 : 0];
162
        assign shft_val = do_conversion? shift_value : exp_difference;
163 19 constantin
        assign convert_neg_mantissa = {{(size_mantissa){1'b1}}, 1'b0, ~c_number_i[size_mantissa-2 : 0]};
164
        assign mantissa_to_shift = conversion[0]?        (s_c_number? {{size_mantissa{1'b1}}, convert_neg_mantissa + 1'b1} : {{size_mantissa{1'b0}}, 1'b1, c_number_i[size_mantissa-2 : 0]}) :
165
                                                                m_ab;
166 16 constantin
        assign arith_shift = conversion[0]? s_c_number : 1'b0;
167
 
168
        //shift m_ab_number                             
169
        shifter #(      .INPUT_SIZE(size_mul_mantissa),
170
                                .SHIFT_SIZE(size_exponent),
171
                                .OUTPUT_SIZE(size_mul_mantissa + size_mantissa),
172
                                .DIRECTION(1'b0), //0=right, 1=left
173
                                .PIPELINE(pipeline),
174
                                .POSITION(pipeline_pos))
175
                m_b_shifter_instance(   .a(mantissa_to_shift),//mantissa
176
                                                                .arith(arith_shift),//logical shift
177
                                                                .shft(shft_val),
178
                                                                .shifted_a({shifted_m_ab, initial_rounding_bits}));
179
 
180
        assign max_entityFP_INT = {s_c_number, shifted_m_ab[max_size - size_diff_i_m - 1 : 0], initial_rounding_bits[size_mantissa - 1 : size_mantissa - size_diff_i_m + 1]};
181
 
182
 
183
        //instantiate effective_op component
184
        effective_op effective_op_instance(     .sign_a(s_a_number),
185
                                                                                .sign_b(s_b_number),
186
                                                                                .sign_c(s_c_number),
187
                                                                                .sub(sub),
188
                                                                                .eff_sub(eff_op));
189
 
190
        //instantiate accumulate component
191
        accumulate #(.size_mul_mantissa(size_mul_mantissa))
192
                accumulate_instance (   .m_a(m_c),
193
                                                                .m_b(shifted_m_ab),
194
                                                                .eff_op(eff_op),
195
                                                                .adder_mantissa(adder_mantissa));
196
 
197
        //compute unnormalized_mantissa
198
        assign  unnormalized_mantissa =
199
                                (adder_mantissa[size_mul_mantissa + 1])?        (~adder_mantissa[size_mul_mantissa : 0]) : adder_mantissa[size_mul_mantissa      : 0];
200 19 constantin
        assign  inter_rounding_bits = conversion[0]? {initial_rounding_bits[size_mantissa - size_diff_i_m : 0], {(size_diff_i_m - 1){initial_rounding_bits[0]}}} :
201
                                                                        conversion[1]?  {size_mantissa{1'b0}} :
202 16 constantin
                                                                                                        ((adder_mantissa[size_mul_mantissa + 1])? ~initial_rounding_bits : initial_rounding_bits);
203
 
204 19 constantin
        assign max_entityINT_FP = do_conversion? (c_number_i[size_integer - 1]? (~c_number_i[max_size-1 : 0]) :  c_number_i[max_size-1 : 0]) :
205 16 constantin
                                                                                                        unnormalized_mantissa[max_size-1 : 0];
206
        assign max_entityINT_FP_msb = do_conversion? {(size_mul_mantissa-max_size+1){1'b0}} : unnormalized_mantissa[size_mul_mantissa : max_size];
207
 
208 19 constantin
        assign lsb_shft_bit = do_conversion?    conversion[0]? s_c_number : c_number_i[size_integer-1] : max_entityINT_FP[0];
209 16 constantin
 
210
        assign max_ovf = do_conversion? 1'b0 : unnormalized_mantissa[size_mul_mantissa];
211
 
212
        //instantiate leading_zeros component
213
        leading_zeros #(.SIZE_INT(size_mul_mantissa + 1'b1),
214
                                        .SIZE_COUNTER(size_mul_counter),
215
                                        .PIPELINE(pipeline))
216
                leading_zeros_instance( .a({max_entityINT_FP_msb, max_entityINT_FP}),
217
                                                                .ovf(max_ovf),
218
                                                                .lz(lz_mul));
219
 
220 19 constantin
        assign max_inter_rounding_bits = conversion[1]? {size_mantissa{c_number_i[size_integer-1]}} : {inter_rounding_bits, inter_rounding_bits[0]};
221
 
222 16 constantin
        //instantiate shifter component
223
        shifter #(      .INPUT_SIZE(size_mul_mantissa + size_mantissa + 1),
224
                                .SHIFT_SIZE(size_mul_counter),
225
                                .OUTPUT_SIZE(size_mul_mantissa + size_mantissa + 2),
226
                                .DIRECTION(1'b1),
227
                                .PIPELINE(pipeline),
228
                                .POSITION(pipeline_pos))
229 19 constantin
                shifter_instance(       .a( {max_entityINT_FP_msb, max_entityINT_FP, max_inter_rounding_bits}),
230 16 constantin
                                                        .arith(lsb_shft_bit),
231
                                                        .shft(lz_mul),
232
                                                        .shifted_a({normalized_mantissa, final_rounding_bits}));
233 19 constantin
 
234
        assign inter_dummy_to_round = {normalized_mantissa[size_mantissa + 1 : 0], final_rounding_bits};
235
 
236
        assign entity_to_round  = conversion[0]? max_entityFP_INT : {{(max_size - size_mantissa){1'b0}}, normalized_mantissa[size_mul_mantissa+1 : size_mantissa + 2]};
237
        assign dummy_to_round   = conversion[0]? {inter_rounding_bits, {(size_mantissa + 2){1'b0}}} :
238
                                                                (conversion[1] & (&{normalized_mantissa[size_mantissa : 0], final_rounding_bits}) & (~normalized_mantissa[size_mantissa+1]))?
239
                                                                        (c_number_i[size_integer-1]?    ~inter_dummy_to_round : inter_dummy_to_round) :
240
                                                                {normalized_mantissa[size_mantissa + 1 : 0], final_rounding_bits};
241
 
242 16 constantin
        //instantiate rounding_component
243 19 constantin
        rounding #(     .SIZE_MOST_S_MANTISSA(max_size),
244 16 constantin
                                .SIZE_LEAST_S_MANTISSA(size_mul_mantissa+2))
245 19 constantin
                rounding_instance(      .unrounded_mantissa(entity_to_round ),
246
                                    .dummy_bits(dummy_to_round),
247
                                    .rounded_mantissa({dummy_out, rounded_mantissa}));
248 16 constantin
 
249
 
250
        assign max_exp_selection = do_conversion? exponent : exp_inter;
251
        assign max_adjust_exponent = max_exp_selection - lz_mul;
252
        assign adjust = do_conversion? size_diff_i_m : 2'd2;
253
        assign max_unadjusted_exponent = max_adjust_exponent + adjust;
254
        assign max_resulted_e_o = (do_conversion & ~(|{max_entityINT_FP_msb, max_entityINT_FP}))? bias : max_unadjusted_exponent + rounded_mantissa[size_mantissa];
255
 
256
        assign resulted_exponent = conversion[0]?        max_entityFP_INT[size_mantissa+size_exponent-2 : size_mantissa-1] : max_resulted_e_o;
257 19 constantin
        assign resulted_mantissa = conversion[0]?        rounded_mantissa/*max_entityFP_INT[size_mantissa-1 : 0]*/ :
258 16 constantin
                                                                                                (rounded_mantissa[size_mantissa])?      (rounded_mantissa[size_mantissa : 1]) :
259
                                                                                                                                                                                (rounded_mantissa[size_mantissa-1 : 0]);
260
 
261
        //instantiate special_cases_mul_acc component
262
        special_cases_mul_acc   #(      .size_exception_field(size_exception_field),
263
                                                                .zero(zero),
264
                                                                .normal_number(normal_number),
265
                                                                .infinity(infinity),
266
                                                                .NaN(NaN))
267
                special_cases_mul_acc_instance  (       .sp_case_a_number(sp_case_a_number),
268
                                                                                        .sp_case_b_number(sp_case_b_number),
269
                                                                                        .sp_case_c_number(sp_case_c_number),
270
                                                                                        .sp_case_result_o(sp_case_o));
271 19 constantin
 
272
        special_cases_mul       #(      .size_exception_field(size_exception_field),
273
                                                        .zero(zero),
274
                                                        .normal_number(normal_number),
275
                                                        .infinity(infinity),
276
                                                        .NaN(NaN))
277
                special_cases_mul_instance(     .sp_case_a_number(sp_case_a_number),
278
                                                                        .sp_case_b_number(sp_case_b_number),
279
                                                                        .sp_case_result_o(sp_case_mul_result_o));
280 16 constantin
 
281 19 constantin
        assign sp_case_result_o = conversion[0]? 2'd0 :
282
                                                                conversion[1]? normal_number : sp_case_o;
283 16 constantin
 
284
        //set zero_flag in case of equal numbers
285 19 constantin
        assign zero_flag = ~(|(rounded_mantissa));
286 16 constantin
 
287
        //compute resulted_sign
288 19 constantin
        sign_computation sign_computation_instance(     .eff_op                                 (eff_op),
289
                                                                                                .s_a_number                             (s_c_number),
290
                                                                                                .s_b_number                             (s_a_number ^ s_b_number),
291
                                                                                                .a_greater_exponent             (c_greater_exponent[size_exponent]),
292
                                                                                                .b_greater_exponent             (ab_greater_exponent[size_exponent]),
293
                                                                                                .adder_mantissa_ovf             (adder_mantissa[size_mul_mantissa]),
294
                                                                                                .sign                                   (sign_inter));
295
 
296
        assign sign_res =       conversion[0]? 1'b0 :
297
                                                conversion[1]? c_number_i[size_integer-1] :
298
                                                sign_inter;
299
                                                //((eff_op)?    (!c_greater_exponent[size_exponent]? 
300
                                                //                              (!ab_greater_exponent[size_exponent]? ~adder_mantissa[size_mul_mantissa+1] : s_c_number) : ~(s_b_number^s_a_number)) : s_c_number);
301 16 constantin
 
302
        assign final_mantissa = resulted_mantissa;
303
 
304
        assign final_exponent = resulted_exponent;
305 19 constantin
        assign resulting_number_o = (zero_flag)? {size{1'b0}} :
306
                                                                ((!(|sp_case_a_number) || !(|sp_case_b_number)) & (~do_conversion))? {c_number_i[size-1 : size-size_exception_field], s_c_number, c_number_i[size-1-size_exception_field-1 : 0]} :
307
                                                                        ((!(|sp_case_c_number)) & (~do_conversion) )?
308
                                                                                (sub?
309
                                                                                        {sp_case_mul_result_o, ~(s_a_number^s_b_number), e_ab_number[size_exponent-1 : 0], mul_mantissa} :
310
                                                                                        {sp_case_mul_result_o, s_a_number^s_b_number, e_ab_number[size_exponent-1 : 0], mul_mantissa}) :
311
                                                                                                {sp_case_result_o, sign_res, final_exponent, final_mantissa};
312 16 constantin
endmodule

powered by: WebSVN 2.1.0

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