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

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 5 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_Accumulate 
9
// Project Name: 
10
// Target Devices: 
11
// Tool versions: 
12
// Description: C  A*B
13
//
14
// Dependencies: 
15
//
16
// Revision: 
17
// Revision 0.01 - File Created
18
// Additional Comments: 
19
//
20
//////////////////////////////////////////////////////////////////////////////////
21 19 constantin
module Multiply_Accumulate #(   parameter size_mantissa = 24,                   //mantissa bits(1.M)
22 9 constantin
                                                                                parameter size_exponent = 8,    //exponent bits
23 3 constantin
                                                                                parameter size_counter  = 5,    //log2(size_mantissa) + 1 = 5
24 19 constantin
                                                                                parameter size_exception_field  = 2,    // zero/normal numbers/infinity/NaN
25
                                                                                parameter zero                                  = 00,   //00
26
                                                                                parameter normal_number                 = 01,   //01
27
                                                                                parameter infinity                              = 10,   //10
28
                                                                                parameter NaN                                   = 11,   //11
29 5 constantin
                                                                                parameter pipeline              = 0,
30
                                                                                parameter pipeline_pos  = 0,  //8 bits
31 3 constantin
 
32 5 constantin
                                                                                parameter size = size_exponent + size_mantissa + size_exception_field,
33
                                                                                parameter size_mul_mantissa = size_mantissa + size_mantissa,
34
                                                                                parameter size_mul_counter = size_counter + 1)
35 19 constantin
                                                                        (       input [size - 1:0] a_number_i,
36 5 constantin
                                                                                input [size - 1:0] b_number_i,
37
                                                                                input [size - 1:0] c_number_i,
38
                                                                                input sub,
39
                                                                                output[size - 1:0] resulting_number_o);
40
 
41 9 constantin
        parameter bias_0_bits = size_exponent - 1;
42
        parameter shift_mantissa_0_bits = size_mantissa-1'b1;
43 5 constantin
 
44 9 constantin
        wire [size_exception_field - 1 : 0] sp_case_a_number, sp_case_b_number, sp_case_c_number;
45 5 constantin
        wire [size_mantissa - 1 : 0] m_a_number, m_b_number, m_c_number;
46
        wire [size_exponent - 1 : 0] e_a_number, e_b_number, e_c_number;
47
        wire s_a_number, s_b_number, s_c_number;
48
 
49 9 constantin
        wire [size_exponent     : 0] ab_greater_exponent, c_greater_exponent;
50 5 constantin
 
51 9 constantin
        wire [size_exponent - 1 : 0] exp_difference;
52
        wire [size_exponent - 1 : 0] unadjusted_exponent;
53
        wire [size_exponent     : 0] exp_inter;
54
 
55 19 constantin
        wire [size_mantissa - 2 : 0] mul_mantissa;
56
 
57 9 constantin
        wire [size_mul_mantissa - 1     : 0] m_ab_mantissa, c_mantissa;
58
        wire [size_exponent                     : 0] e_ab_number_inter, e_ab_number;
59
        wire [size_mul_counter - 1      : 0] lz_mul;
60
 
61
        wire zero_flag;
62 5 constantin
        wire sign_res;
63 9 constantin
        wire eff_op;
64
 
65
        wire [size_mantissa - 1         : 0] initial_rounding_bits, inter_rounding_bits, final_rounding_bits;
66
        wire [size_mul_mantissa + 1 : 0] normalized_mantissa, adder_mantissa;
67
        wire [size_mul_mantissa         : 0] unnormalized_mantissa;
68
        wire [size_mul_mantissa - 1 : 0] shifted_m_ab;
69
        wire [size_mul_mantissa - 1 : 0] m_c, m_ab;
70
 
71 19 constantin
        wire [size_exception_field - 1 : 0] sp_case_mul_result_o;
72
 
73
        wire [size_exception_field - 1 : 0] sp_case_result_o;
74 9 constantin
        wire [size_mantissa - 2 : 0] final_mantissa;
75
        wire [size_exponent - 1 : 0] final_exponent;
76
        wire [size_mantissa : 0] rounded_mantissa;
77
 
78 3 constantin
 
79 9 constantin
        assign m_a_number                       = {1'b1, a_number_i[size_mantissa - 2 :0]};
80 5 constantin
        assign m_b_number                       = {1'b1, b_number_i[size_mantissa - 2 :0]};
81
        assign m_c_number                       = {1'b1, c_number_i[size_mantissa - 2 :0]};
82
        assign e_a_number                       = a_number_i[size_mantissa + size_exponent - 1 : size_mantissa - 1];
83
        assign e_b_number                       = b_number_i[size_mantissa + size_exponent - 1 : size_mantissa - 1];
84
        assign e_c_number                       = c_number_i[size_mantissa + size_exponent - 1 : size_mantissa - 1];
85
        assign s_a_number                       = a_number_i[size - size_exception_field - 1];
86
        assign s_b_number                       = b_number_i[size - size_exception_field - 1];
87
        assign s_c_number                       = c_number_i[size - size_exception_field - 1];
88
        assign sp_case_a_number = a_number_i[size - 1 : size - size_exception_field];
89
        assign sp_case_b_number = b_number_i[size - 1 : size - size_exception_field];
90
        assign sp_case_c_number = c_number_i[size - 1 : size - size_exception_field];
91 3 constantin
 
92 5 constantin
 
93
        //instantiate multiply component
94
        multiply #(     .size_mantissa(size_mantissa),
95
                                        .size_counter(size_counter),
96
                                        .size_mul_mantissa(size_mul_mantissa))
97
                multiply_instance (     .a_mantissa_i(m_a_number),
98
                                                                        .b_mantissa_i(m_b_number),
99 9 constantin
                                                                        .mul_mantissa(m_ab_mantissa));
100 19 constantin
 
101
        assign mul_mantissa = m_ab_mantissa[size_mul_mantissa-1]?       m_ab_mantissa[size_mul_mantissa-2 : size_mul_mantissa - size_mantissa] :
102
                                                                                                                                m_ab_mantissa[size_mul_mantissa-3 : size_mul_mantissa - size_mantissa - 1];
103 5 constantin
 
104 9 constantin
        assign c_mantissa       = {1'b0,m_c_number, {(shift_mantissa_0_bits){1'b0}}};
105
        assign e_ab_number_inter = e_a_number + e_b_number;
106 12 constantin
        assign e_ab_number = e_ab_number_inter  - {(bias_0_bits){1'b1}};
107 5 constantin
 
108 9 constantin
        //find the greater exponent
109
        assign ab_greater_exponent = e_ab_number - e_c_number;
110
        assign c_greater_exponent = e_c_number - e_ab_number;
111 5 constantin
 
112 9 constantin
        //find the difference between exponents
113
        assign exp_difference   = (ab_greater_exponent[size_exponent])? c_greater_exponent[size_exponent - 1 : 0] : ab_greater_exponent[size_exponent - 1 : 0];
114
        assign exp_inter                = (c_greater_exponent[size_exponent])? {1'b0, e_ab_number} : {1'b0, e_c_number};
115 5 constantin
 
116 9 constantin
        //set shifter always on m_ab_number
117
        assign {m_c, m_ab} = (ab_greater_exponent[size_exponent])? {c_mantissa, m_ab_mantissa} :
118
                                                        {m_ab_mantissa, c_mantissa};
119 5 constantin
 
120 9 constantin
        //shift m_ab_number                             
121 5 constantin
        shifter #(      .INPUT_SIZE(size_mul_mantissa),
122 9 constantin
                                .SHIFT_SIZE(size_exponent),
123
                                .OUTPUT_SIZE(size_mul_mantissa + size_mantissa),
124
                                .DIRECTION(1'b0), //0=right, 1=left
125
                                .PIPELINE(pipeline),
126
                                .POSITION(pipeline_pos))
127
                m_b_shifter_instance(   .a(m_ab),//mantissa
128
                                                                .arith(1'b0),//logical shift
129
                                                                .shft(exp_difference),
130
                                                                .shifted_a({shifted_m_ab, initial_rounding_bits}));
131 5 constantin
 
132
 
133
        //instantiate effective_op component
134
        effective_op effective_op_instance(     .sign_a(s_a_number),
135
                                                                                                        .sign_b(s_b_number),
136
                                                                                                        .sign_c(s_c_number),
137
                                                                                                        .sub(sub),
138 9 constantin
                                                                                                        .eff_sub(eff_op));
139
 
140 5 constantin
        //instantiate accumulate component
141 9 constantin
        accumulate #(.size_mul_mantissa(size_mul_mantissa))
142
                accumulate_instance (   .m_a(m_c),
143
                                                                .m_b(shifted_m_ab),
144
                                                                .eff_op(eff_op),
145
                                                                .adder_mantissa(adder_mantissa));
146
 
147
        //compute unnormalized_mantissa
148
        assign {unnormalized_mantissa, inter_rounding_bits} =
149
                                (adder_mantissa[size_mul_mantissa + 1])?        ({~adder_mantissa[size_mul_mantissa : 0], ~initial_rounding_bits}) :
150
                                                                                                                        ({adder_mantissa[size_mul_mantissa      : 0], initial_rounding_bits});
151
 
152 5 constantin
        //instantiate leading_zeros component
153
        leading_zeros #(        .SIZE_INT(size_mul_mantissa + 1'b1),
154
                                                        .SIZE_COUNTER(size_mul_counter),
155
                                                        .PIPELINE(pipeline))
156 9 constantin
                leading_zeros_instance( .a(unnormalized_mantissa[size_mul_mantissa : 0]),
157
                                                                                .ovf(unnormalized_mantissa[size_mul_mantissa]),
158 5 constantin
                                                                                .lz(lz_mul));
159
 
160
        //instantiate shifter component
161 9 constantin
        shifter #(      .INPUT_SIZE(size_mul_mantissa + size_mantissa + 1),
162 5 constantin
                                        .SHIFT_SIZE(size_mul_counter),
163 9 constantin
                                        .OUTPUT_SIZE(size_mul_mantissa + size_mantissa + 2),
164 5 constantin
                                        .DIRECTION(1'b1),
165
                                        .PIPELINE(pipeline),
166
                                        .POSITION(pipeline_pos))
167 9 constantin
                shifter_instance(       .a({unnormalized_mantissa, inter_rounding_bits}),
168 5 constantin
                                                                .arith(1'b0),
169
                                                                .shft(lz_mul),
170 9 constantin
                                                                .shifted_a({normalized_mantissa, final_rounding_bits}));
171
 
172
        //instantiate rounding_component
173
        rounding #(     .SIZE_MOST_S_MANTISSA(size_mantissa+1),
174
                                .SIZE_LEAST_S_MANTISSA(size_mul_mantissa+2))
175
                rounding_instance(      .unrounded_mantissa({1'b0, normalized_mantissa[size_mul_mantissa+1 : size_mantissa + 2]}),
176
                                    .dummy_bits({normalized_mantissa[size_mantissa + 1 : 0],final_rounding_bits}),
177
                                    .rounded_mantissa(rounded_mantissa));
178
 
179 19 constantin
        //instantiate special_cases_mul and special_cases_mul_acc components
180 5 constantin
        special_cases_mul_acc   #(      .size_exception_field(size_exception_field),
181 19 constantin
                                                                .zero(zero),
182
                                                                .normal_number(normal_number),
183
                                                                .infinity(infinity),
184
                                                                .NaN(NaN))
185 5 constantin
                special_cases_mul_acc_instance  (       .sp_case_a_number(sp_case_a_number),
186 19 constantin
                                                                                        .sp_case_b_number(sp_case_b_number),
187
                                                                                        .sp_case_c_number(sp_case_c_number),
188
                                                                                        .sp_case_result_o(sp_case_result_o));
189
 
190
        special_cases_mul       #(      .size_exception_field(size_exception_field),
191
                                                        .zero(zero),
192
                                                        .normal_number(normal_number),
193
                                                        .infinity(infinity),
194
                                                        .NaN(NaN))
195
                special_cases_mul_instance(     .sp_case_a_number(sp_case_a_number),
196
                                                                        .sp_case_b_number(sp_case_b_number),
197
                                                                        .sp_case_result_o(sp_case_mul_result_o));
198 9 constantin
 
199
        //set zero_flag in case of equal numbers
200 19 constantin
        assign zero_flag = ~(|(rounded_mantissa));
201 5 constantin
 
202
        //compute resulted_sign
203 9 constantin
        assign sign_res =       (eff_op)?       (!c_greater_exponent[size_exponent]?
204
                                                                                (!ab_greater_exponent[size_exponent]? ~adder_mantissa[size_mul_mantissa+1] : s_c_number) : ~(s_b_number^s_a_number)) : s_c_number;
205 5 constantin
 
206 9 constantin
        assign final_mantissa = (rounded_mantissa[size_mantissa])?
207
                                                                        (rounded_mantissa[size_mantissa : 1]) :
208
                                                                        (rounded_mantissa[size_mantissa-1: 0]);
209
 
210
        assign unadjusted_exponent = exp_inter - lz_mul;
211
        assign final_exponent = unadjusted_exponent + 2'd2;
212 19 constantin
        assign resulting_number_o = (zero_flag)? {size{1'b0}} :
213
                                                                (!sp_case_a_number || !sp_case_b_number)? {c_number_i[size-1 : size-size_exception_field], s_c_number, c_number_i[size-1-size_exception_field-1 : 0]} :
214
                                                                (!sp_case_c_number)?
215
                                                                        (eff_op?
216
                                                                                {sp_case_mul_result_o, ~(s_a_number^s_b_number), e_ab_number[size_exponent-1 : 0], mul_mantissa} :
217
                                                                                {sp_case_mul_result_o, s_a_number^s_b_number, e_ab_number[size_exponent-1 : 0], mul_mantissa}) :
218
                                                                                        {sp_case_result_o, sign_res, final_exponent, final_mantissa};
219 5 constantin
endmodule

powered by: WebSVN 2.1.0

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