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.

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.

Sini Balakrishnan

Sini Balakrishnan

Staff Engineer at Intel
Sini has spent more than a dozen years in the semiconductor industry, focusing mostly on verification. She is an expert on Formal Verification and has written international papers and articles on related topics.
Sini Balakrishnan

Latest posts by Sini Balakrishnan (see all)

3 Comments

      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

Leave a Reply

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