Line 16... |
Line 16... |
// Revision:
|
// Revision:
|
// Revision 0.01 - File Created
|
// Revision 0.01 - File Created
|
// Additional Comments:
|
// Additional Comments:
|
//
|
//
|
//////////////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////////////
|
module Multiply_Accumulate #( parameter size_exponent = 8, //exponent bits
|
module Multiply_Accumulate #( parameter size_mantissa = 24, //mantissa bits(1.M)
|
parameter size_mantissa = 24, //mantissa bits
|
parameter size_exponent = 8, //exponent bits
|
parameter size_counter = 5, //log2(size_mantissa) + 1 = 5
|
parameter size_counter = 5, //log2(size_mantissa) + 1 = 5
|
parameter size_exception_field = 2, // zero/normal numbers/infinity/NaN
|
parameter size_exception_field = 2, // zero/normal numbers/infinity/NaN
|
parameter zero = 00, //00
|
parameter zero = 00, //00
|
parameter normal_number = 01, //01
|
parameter normal_number = 01, //01
|
parameter infinity = 10, //10
|
parameter infinity = 10, //10
|
Line 38... |
Line 38... |
input [size - 1:0] b_number_i,
|
input [size - 1:0] b_number_i,
|
input [size - 1:0] c_number_i,
|
input [size - 1:0] c_number_i,
|
input sub,
|
input sub,
|
output[size - 1:0] resulting_number_o);
|
output[size - 1:0] resulting_number_o);
|
|
|
|
parameter bias_0_bits = size_exponent - 1;
|
|
parameter shift_mantissa_0_bits = size_mantissa-1'b1;
|
|
|
|
wire [size_exception_field - 1 : 0] sp_case_a_number, sp_case_b_number, sp_case_c_number;
|
wire [size_mantissa - 1 : 0] m_a_number, m_b_number, m_c_number;
|
wire [size_mantissa - 1 : 0] m_a_number, m_b_number, m_c_number;
|
wire [size_exponent - 1 : 0] e_a_number, e_b_number, e_c_number;
|
wire [size_exponent - 1 : 0] e_a_number, e_b_number, e_c_number;
|
wire s_a_number, s_b_number, s_c_number;
|
wire s_a_number, s_b_number, s_c_number;
|
wire [size_exception_field - 1 : 0] sp_case_a_number, sp_case_b_number, sp_case_c_number;
|
|
//---------------------------------------------------------------------------------------
|
|
|
|
|
wire [size_exponent : 0] ab_greater_exponent, c_greater_exponent;
|
|
|
|
wire [size_exponent - 1 : 0] exp_difference;
|
|
wire [size_exponent - 1 : 0] unadjusted_exponent;
|
|
wire [size_exponent : 0] exp_inter;
|
|
|
wire [size_mul_mantissa-1:0] mul_mantissa, c_mantissa;
|
wire [size_mul_mantissa - 1 : 0] m_ab_mantissa, c_mantissa;
|
wire [size_mul_mantissa :0] acc_resulting_number;
|
wire [size_exponent : 0] e_ab_number_inter, e_ab_number;
|
wire [size_mul_mantissa :0] ab_shifted_mul_mantissa, c_shifted_mantissa;
|
|
wire [size_exponent : 0] exp_ab;
|
|
wire [size_exponent-1:0] modify_exp_ab, modify_exp_c;
|
|
wire [size_mul_counter-1: 0] lz_mul;
|
wire [size_mul_counter-1: 0] lz_mul;
|
|
|
|
wire zero_flag;
|
wire sign_res;
|
wire sign_res;
|
wire eff_sub;
|
wire eff_op;
|
wire ovf;
|
|
wire comp_exp;
|
wire [size_mantissa - 1 : 0] initial_rounding_bits, inter_rounding_bits, final_rounding_bits;
|
wire [size_mul_mantissa+1:0] normalized_mantissa;
|
wire [size_mul_mantissa + 1 : 0] normalized_mantissa, adder_mantissa;
|
wire [size_mantissa - 1 : 0] rounded_mantissa;
|
wire [size_mul_mantissa : 0] unnormalized_mantissa;
|
wire [size_exponent :0] unnormalized_exp;
|
wire [size_mul_mantissa - 1 : 0] shifted_m_ab;
|
|
wire [size_mul_mantissa - 1 : 0] m_c, m_ab;
|
|
|
|
wire [size_exception_field - 1 : 0] sp_case_result_o;
|
wire [size_mantissa-2:0] final_mantissa;
|
wire [size_mantissa-2:0] final_mantissa;
|
wire [size_exponent-1:0] final_exponent;
|
wire [size_exponent-1:0] final_exponent;
|
wire [size_exception_field - 1 : 0] sp_case_result_o;
|
wire [size_mantissa : 0] rounded_mantissa;
|
|
|
|
|
assign m_a_number = {1'b1, a_number_i[size_mantissa - 2 :0]};
|
assign m_a_number = {1'b1, a_number_i[size_mantissa - 2 :0]};
|
assign m_b_number = {1'b1, b_number_i[size_mantissa - 2 :0]};
|
assign m_b_number = {1'b1, b_number_i[size_mantissa - 2 :0]};
|
assign m_c_number = {1'b1, c_number_i[size_mantissa - 2 :0]};
|
assign m_c_number = {1'b1, c_number_i[size_mantissa - 2 :0]};
|
assign e_a_number = a_number_i[size_mantissa + size_exponent - 1 : size_mantissa - 1];
|
assign e_a_number = a_number_i[size_mantissa + size_exponent - 1 : size_mantissa - 1];
|
Line 83... |
Line 92... |
multiply #( .size_mantissa(size_mantissa),
|
multiply #( .size_mantissa(size_mantissa),
|
.size_counter(size_counter),
|
.size_counter(size_counter),
|
.size_mul_mantissa(size_mul_mantissa))
|
.size_mul_mantissa(size_mul_mantissa))
|
multiply_instance ( .a_mantissa_i(m_a_number),
|
multiply_instance ( .a_mantissa_i(m_a_number),
|
.b_mantissa_i(m_b_number),
|
.b_mantissa_i(m_b_number),
|
.mul_mantissa(mul_mantissa));
|
.mul_mantissa(m_ab_mantissa));
|
|
|
|
assign c_mantissa = {1'b0,m_c_number, {(shift_mantissa_0_bits){1'b0}}};
|
|
assign e_ab_number_inter = e_a_number + e_b_number;
|
|
assign e_ab_number = e_ab_number_inter - ({1'b1,{(bias_0_bits){1'b0}}} - 1'b1);
|
|
|
assign c_mantissa = {1'b0,m_c_number, {(size_mantissa-1'b1){1'b0}}};
|
//find the greater exponent
|
assign exp_ab = e_a_number + e_b_number - ({1'b1,{(size_exponent-1'b1){1'b0}}} - 1'b1);
|
assign ab_greater_exponent = e_ab_number - e_c_number;
|
assign {modify_exp_ab, modify_exp_c, unnormalized_exp} = (exp_ab >= e_c_number)? {8'd0,(exp_ab - e_c_number), exp_ab} : {(e_c_number - exp_ab), 8'd0, {1'b0,e_c_number}};
|
assign c_greater_exponent = e_c_number - e_ab_number;
|
|
|
|
//find the difference between exponents
|
|
assign exp_difference = (ab_greater_exponent[size_exponent])? c_greater_exponent[size_exponent - 1 : 0] : ab_greater_exponent[size_exponent - 1 : 0];
|
|
assign exp_inter = (c_greater_exponent[size_exponent])? {1'b0, e_ab_number} : {1'b0, e_c_number};
|
|
|
//instantiate shifter component for mul_mantissa shift, mul_mantissa <=> ab_mantissa
|
//set shifter always on m_ab_number
|
shifter #( .INPUT_SIZE(size_mul_mantissa),
|
assign {m_c, m_ab} = (ab_greater_exponent[size_exponent])? {c_mantissa, m_ab_mantissa} :
|
.SHIFT_SIZE(size_exponent),
|
{m_ab_mantissa, c_mantissa};
|
.OUTPUT_SIZE(size_mul_mantissa + 1'b1),
|
|
.DIRECTION(1'b0),
|
|
.PIPELINE(pipeline),
|
|
.POSITION(pipeline_pos))
|
|
shifter_ab_instance( .a(mul_mantissa),
|
|
.arith(1'b0),
|
|
.shft(modify_exp_ab),
|
|
.shifted_a(ab_shifted_mul_mantissa));
|
|
|
|
|
|
//instantiate shifter component for c_mantissa shift
|
//shift m_ab_number
|
shifter #( .INPUT_SIZE(size_mul_mantissa),
|
shifter #( .INPUT_SIZE(size_mul_mantissa),
|
.SHIFT_SIZE(size_exponent),
|
.SHIFT_SIZE(size_exponent),
|
.OUTPUT_SIZE(size_mul_mantissa + 1'b1),
|
.OUTPUT_SIZE(size_mul_mantissa + size_mantissa),
|
.DIRECTION(1'b0),
|
.DIRECTION(1'b0), //0=right, 1=left
|
.PIPELINE(pipeline),
|
.PIPELINE(pipeline),
|
.POSITION(pipeline_pos))
|
.POSITION(pipeline_pos))
|
shifter_c_instance( .a(c_mantissa),
|
m_b_shifter_instance( .a(m_ab),//mantissa
|
.arith(1'b0),
|
.arith(1'b0),//logical shift
|
.shft(modify_exp_c),
|
.shft(exp_difference),
|
.shifted_a(c_shifted_mantissa));
|
.shifted_a({shifted_m_ab, initial_rounding_bits}));
|
|
|
|
|
//instantiate effective_op component
|
//instantiate effective_op component
|
effective_op effective_op_instance( .sign_a(s_a_number),
|
effective_op effective_op_instance( .sign_a(s_a_number),
|
.sign_b(s_b_number),
|
.sign_b(s_b_number),
|
.sign_c(s_c_number),
|
.sign_c(s_c_number),
|
.sub(sub),
|
.sub(sub),
|
.eff_sub(eff_sub));
|
.eff_sub(eff_op));
|
|
|
|
|
//instantiate accumulate component
|
//instantiate accumulate component
|
accumulate #( .size_mantissa(size_mantissa),
|
accumulate #(.size_mul_mantissa(size_mul_mantissa))
|
.size_counter(size_counter),
|
accumulate_instance ( .m_a(m_c),
|
.size_mul_mantissa(size_mul_mantissa))
|
.m_b(shifted_m_ab),
|
accumulate_instance ( .ab_number_i(ab_shifted_mul_mantissa[size_mul_mantissa:1]),
|
.eff_op(eff_op),
|
.c_number_i(c_shifted_mantissa[size_mul_mantissa:1]),
|
.adder_mantissa(adder_mantissa));
|
.sub(eff_sub),
|
|
.ovf(ovf),
|
//compute unnormalized_mantissa
|
.acc_resulting_number_o(acc_resulting_number));
|
assign {unnormalized_mantissa, inter_rounding_bits} =
|
|
(adder_mantissa[size_mul_mantissa + 1])? ({~adder_mantissa[size_mul_mantissa : 0], ~initial_rounding_bits}) :
|
|
({adder_mantissa[size_mul_mantissa : 0], initial_rounding_bits});
|
|
|
//instantiate leading_zeros component
|
//instantiate leading_zeros component
|
leading_zeros #( .SIZE_INT(size_mul_mantissa + 1'b1),
|
leading_zeros #( .SIZE_INT(size_mul_mantissa + 1'b1),
|
.SIZE_COUNTER(size_mul_counter),
|
.SIZE_COUNTER(size_mul_counter),
|
.PIPELINE(pipeline))
|
.PIPELINE(pipeline))
|
leading_zeros_instance( .a(acc_resulting_number),
|
leading_zeros_instance( .a(unnormalized_mantissa[size_mul_mantissa : 0]),
|
.ovf(ovf),
|
.ovf(unnormalized_mantissa[size_mul_mantissa]),
|
.lz(lz_mul));
|
.lz(lz_mul));
|
|
|
|
|
//instantiate shifter component
|
//instantiate shifter component
|
shifter #( .INPUT_SIZE(size_mul_mantissa + 1'b1),
|
shifter #( .INPUT_SIZE(size_mul_mantissa + size_mantissa + 1),
|
.SHIFT_SIZE(size_mul_counter),
|
.SHIFT_SIZE(size_mul_counter),
|
.OUTPUT_SIZE(size_mul_mantissa + 2'd2),
|
.OUTPUT_SIZE(size_mul_mantissa + size_mantissa + 2),
|
.DIRECTION(1'b1),
|
.DIRECTION(1'b1),
|
.PIPELINE(pipeline),
|
.PIPELINE(pipeline),
|
.POSITION(pipeline_pos))
|
.POSITION(pipeline_pos))
|
shifter_instance( .a(acc_resulting_number),
|
shifter_instance( .a({unnormalized_mantissa, inter_rounding_bits}),
|
.arith(1'b0),
|
.arith(1'b0),
|
.shft(lz_mul),
|
.shft(lz_mul),
|
.shifted_a(normalized_mantissa));
|
.shifted_a({normalized_mantissa, final_rounding_bits}));
|
|
|
|
//instantiate rounding_component
|
//instantiate rounding component
|
rounding #( .SIZE_MOST_S_MANTISSA(size_mantissa+1),
|
rounding #( .SIZE_MOST_S_MANTISSA(size_mantissa),
|
.SIZE_LEAST_S_MANTISSA(size_mul_mantissa+2))
|
.SIZE_LEAST_S_MANTISSA(size_mul_mantissa-size_mantissa+2))
|
rounding_instance( .unrounded_mantissa({1'b0, normalized_mantissa[size_mul_mantissa+1 : size_mantissa + 2]}),
|
rounding_instance ( .unrounded_mantissa(normalized_mantissa[size_mul_mantissa+1 : size_mul_mantissa+2-size_mantissa]),
|
.dummy_bits({normalized_mantissa[size_mantissa + 1 : 0],final_rounding_bits}),
|
.dummy_bits(normalized_mantissa[size_mul_mantissa+1-size_mantissa : 0]),
|
|
.rounded_mantissa(rounded_mantissa));
|
.rounded_mantissa(rounded_mantissa));
|
|
|
|
|
//instantiate special_cases_mul_acc component
|
//instantiate special_cases_mul_acc component
|
special_cases_mul_acc #( .size_exception_field(size_exception_field),
|
special_cases_mul_acc #( .size_exception_field(size_exception_field),
|
.zero(zero),
|
.zero(zero),
|
.normal_number(normal_number),
|
.normal_number(normal_number),
|
.infinity(infinity),
|
.infinity(infinity),
|
Line 177... |
Line 180... |
special_cases_mul_acc_instance ( .sp_case_a_number(sp_case_a_number),
|
special_cases_mul_acc_instance ( .sp_case_a_number(sp_case_a_number),
|
.sp_case_b_number(sp_case_b_number),
|
.sp_case_b_number(sp_case_b_number),
|
.sp_case_c_number(sp_case_c_number),
|
.sp_case_c_number(sp_case_c_number),
|
.sp_case_result_o(sp_case_result_o));
|
.sp_case_result_o(sp_case_result_o));
|
|
|
|
//set zero_flag in case of equal numbers
|
|
assign zero_flag = ~(|(rounded_mantissa));
|
|
|
//compute resulted_sign
|
//compute resulted_sign
|
assign sign_res = (eff_sub)? ((c_shifted_mantissa > ab_shifted_mul_mantissa)? s_c_number : ~s_c_number) : s_c_number;
|
assign sign_res = (eff_op)? (!c_greater_exponent[size_exponent]?
|
|
(!ab_greater_exponent[size_exponent]? ~adder_mantissa[size_mul_mantissa+1] : s_c_number) : ~(s_b_number^s_a_number)) : s_c_number;
|
|
|
assign final_exponent = unnormalized_exp - lz_mul + 2'd2;
|
assign final_mantissa = (rounded_mantissa[size_mantissa])?
|
assign final_mantissa = rounded_mantissa[size_mantissa-2 : 0];
|
(rounded_mantissa[size_mantissa : 1]) :
|
assign resulting_number_o = {sp_case_result_o, sign_res, final_exponent, final_mantissa};
|
(rounded_mantissa[size_mantissa-1: 0]);
|
|
|
|
assign unadjusted_exponent = exp_inter - lz_mul;
|
|
assign final_exponent = unadjusted_exponent + 2'd2;
|
|
assign resulting_number_o = (zero_flag)? {size{1'b0}} :{sp_case_result_o, sign_res, final_exponent, final_mantissa};
|
endmodule
|
endmodule
|
|
|
No newline at end of file
|
No newline at end of file
|