Array reduction methods
The array reduction methods are used to reduce the array to a single value with the optional use of the ‘with’ clause.
The reduction methods can be applied on any unpacked array. For a ‘with’ clause, boolean or arithmetic reduction operation must be specified.
Methods |
Description |
and |
Returns bitwise AND (&) of all elements of the array. |
or |
Returns bitwise OR (|) of all elements of the array. |
xor |
Returns bitwise XOR (^) of all elements of the array. |
sum |
Returns the sum of all elements of the array. |
product |
Returns product of all elements of the array. |
Note: If the ‘with’ clause is specified, the above reduction methods return value based on evaluating the expression for each array element.
Array reduction methods Example
module arr_ordering_ex;
int arr[8] = '{5,6,9,2,3,4,6,10};
int que[$];
initial begin
que.push_back(45);
que.push_back(30);
que.push_back(99);
que.push_back(51);
que.push_back(85);
$display("-------------------------------------");
$display("------ Printing array and queue -----");
$display("-------------------------------------");
print_array_queue();
$display("-------------------------------------");
$display("------------ Methods ----------------");
$display("-------------------------------------");
$display("and for: arr = %0h and queue = %0h", arr.and(), que.and());
$display("or for : arr = %0h and queue = %0h", arr.or(), que.or());
$display("xor for: arr = %0h and queue = %0h", arr.xor(), que.xor());
$display("sum for: arr = %0d and queue = %0d", arr.sum(), que.sum());
$display("product for: arr = %0d and queue = %0d", arr.product(), que.product());
end
//-------------------------
// print array and queue
//-------------------------
function void print_array_queue();
$display("Array = %p", arr);
$display("Queue = %p", que);
endfunction
endmodule
Output:
-------------------------------------
------ Printing array and queue -----
-------------------------------------
Array = '{5, 6, 9, 2, 3, 4, 6, 10}
Queue = '{45, 30, 99, 51, 85}
-------------------------------------
------------ Methods ----------------
-------------------------------------
and for: arr = 0 and queue = 0
or for : arr = f and queue = 7f
xor for: arr = 3 and queue = 36
sum for: arr = 45 and queue = 310
product for: arr = 388800 and queue = 579372750
System Verilog Tutorials