System Verilog : Array Reduction & Array Ordering Methods

Array reduction methods can be applied to any unpacked array. The “with” clause can be used to specify the item to be used in the reduction.

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.

module array_reduction;

int myarr [8:0];
logic larr [7:0];
int rsum,rprod,rxor,rnd,ror;

initial 
begin 
 myarr = {6,3,1,2,4,8,5,7,9};
 larr = {1,0,1,0,1,1,1,0};
 
 rsum = myarr.sum();
 $display("sum = %0d", rsum);
 
 rprod = myarr.product();
 $display("product = %0d", rprod);
 
 rxor = myarr.xor();
 $display("Xor = %0b", rxor);

 rxor = larr.xor with ( item + 1); //  11 ^  01 ^  11 ^  01 ^  11 ^  11 ^  11 ^ 01
 $display("Xor for larr - with clause = %0d", rxor);
 
 rnd = larr.and();
 $display("And = %0b", rnd);

 ror = larr.or();
 $display("Or = %0b", ror);

end
endmodule
/*
ncsim> run
sum = 45
product = 362880
Xor = 1
Xor for larr - with clause = 3
And = 0
Or = 1
*/

In the above code, sum is the sum of all elements in the array. The execution of xor method has been done on myarr and larr. With larr with clause is used which will add 1 to each element and then do xor.

Array Ordering Methods:
Array ordering methods reorder the elements of any unpacked array (fixed or dynamically sized) except for associative arrays.

reverse() :
It reverses the order of the elements in the array. The `with` clause cannot be specified with this.

sort() :
It sorts the array in ascending order.

rsort() :
It sorts the array in descending order.For both `sort` and `rsort`, the `with` clause (and its expression) is optional when the relational operators (<, >, ==) are defined for the array element type. If a `with` clause is specified, the relational operators(<, >, ==) shall be defined for the type of the expression.

shuffle() :
It randomizes the order of the elements in the array. The `with` clause cannot be specified with this.

module array_order;
int myarr [9:0];

initial 
begin 
 myarr = {60,0,30,10,20,40,80,50,70,90};
 display(myarr,"Array myarr");
 
 myarr.reverse; 
 display(myarr,"myarr.reverse() function");
   
 myarr.sort();
 display(myarr,"myarr.sort() function");
 
 myarr.rsort; 
 display(myarr,"myarr.rsort() function");

 myarr.shuffle; 
 display(myarr,"myarr.shuffle() function");

 myarr.shuffle; 
 display(myarr,"myarr.shuffle() function Again!!");
end 

task display(int arr[9:0],string str);
 $display(str);
 for (int i=0; i<10;i++) $write(" %0d ",myarr[i]); 
 $display("\n");
 return;
endtask
endmodule
/*
ncsim> run
Array myarr
 90  70  50  80  40  20  10  30  0  60 

myarr.reverse() function
 60  0  30  10  20  40  80  50  70  90 

myarr.sort() function
 0  10  20  30  40  50  60  70  80  90 

myarr.rsort() function
 90  80  70  60  50  40  30  20  10  0 

myarr.shuffle() function
 0  10  30  40  20  90  60  70  80  50 

myarr.shuffle() function Again!!
 30  10  40  60  20  80  70  50  0  90 
*/

5 comments on “System Verilog : Array Reduction & Array Ordering Methods

      1. sharan

        Could add differences between like task and function, program and module block?
        What can we use inside module and program block and same way, what not to use ?(for example, can we use always block inside program block ,similar to that ).
        Please email me if you want further any.
        I am fresher so I will encounter these questions. Just sharing

        Reply
  1. PARTH GANDHI

    Doubt in example for Array_reduction:
    Actual : rxor = larr.xor with ( item + 1); // 11 ^ 01 ^ 11 ^ 01 ^ 11 ^ 11 ^ 11 ^ 01;
    Shouldn’t be like : rxor = larr.xor with ( item + 1); // 10 ^ 01 ^ 10 ^ 01 ^ 10 ^ 10 ^ 10 ^ 01;
    Please lemme know if there is any mistake in my understanding ?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *