概述
Array Reduction methods
SystemVerilog Array Reduction methods operate on an unpacked array to reduce the array to a single value.built-in array reduction methods are,
- sum() returns the sum of all the array elements
- product() returns the product of all the array elements
- and() returns the bit-wise AND ( & ) of all the array elements
- or() returns the bit-wise OR ( | ) of all the array elements
- xor() returns the logical XOR ( ^ ) of all the array elements
‘with’ clause is allowed for sort and rsort methods.About ‘with’:expression specified in “with” clause will be evaluated for each array element and performs the operation on an array.
一、Array reduction methods SUM and PRODUCT
On calling sum() method sum of array_1 elements (1,2,3,4) will be returned to variable t_sum.
On calling product() method product of array_1 elements (1,2,3,4) will be returned to variable t_product.
module fixedsize_array;
//declaration of array’s
int array_1[4];
int t_sum,t_product;
initial begin
//array initialization
array_1 = '{1,2,3,4};
t_sum = array_1.sum(); //t_sum = 1+2+3+4
$display("Sum of array_1 is t%0d",t_sum);
t_product = array_1.product(); //t_product = 1*2*3*4
$display("product of array_1 is t%0d",t_product);
end
endmodule
Simulator Output
Sum of array_1 is 10
product of array_1 is 24
二、Array reduction method AND
On calling and() method, bit-wise and (&) will be performed on all the array elements and returned.Let’s consider an example of an array with 2, 3 and 4 elements.
module fixedsize_array;
//declaration of array’s
int array_1[2];
int array_2[2];
int array_3[3];
int array_4[4];
int b_and;
initial begin
//array initialization
array_1 = '{2,3};
array_2 = '{2,1};
array_3 = '{10,9,8};
array_4 = '{3,5,7,9};
b_and = array_1.and();
$display("Bit-wise AND of array_1 is t%0d",b_and);
b_and = array_2.and();
$display("Bit-wise AND of array_2 is t%0d",b_and);
b_and = array_3.and();
$display("Bit-wise AND of array_3 is t%0d",b_and);
b_and = array_4.and();
$display("Bit-wise AND of array_4 is t%0d",b_and);
end
endmodule
Simulator Output
Bit-wise AND of array_1 is 2
Bit-wise AND of array_2 is 0
Bit-wise AND of array_3 is 8
Bit-wise AND of array_4 is 1
Execute the above code on
三、Array reduction method OR
On calling or() method, bit-wise or (|) will be performed on all the array elements and returned.Let’s consider an example of an array with 2, 3 and 4 elements.
module fixedsize_array;
//declaration of array’s
int array_1[2];
int array_2[2];
int array_3[3];
int array_4[4];
int b_or;
initial begin
//array initialization
array_1 = '{2,3};
array_2 = '{2,1};
array_3 = '{10,9,8};
array_4 = '{3,5,7,9};
b_or = array_1.or();
$display("bit-wise OR of array_1 is t%0d",b_or);
b_or = array_2.or();
$display("bit-wise OR of array_2 is t%0d",b_or);
b_or = array_3.or();
$display("bit-wise OR of array_3 is t%0d",b_or);
b_or = array_4.or();
$display("bit-wise OR of array_4 is t%0d",b_or);
end
endmodule
Simulator Output
bit-wise OR of array_1 is 3
bit-wise OR of array_2 is 3
bit-wise OR of array_3 is 11
bit-wise OR of array_4 is 15
Execute the above code on
最后
以上就是认真帽子为你收集整理的[SV]SystemVerilog数组缩减法(Array Reduction methods) Array Reduction methods的全部内容,希望文章能够帮你解决[SV]SystemVerilog数组缩减法(Array Reduction methods) Array Reduction methods所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复