Skip to content

Array

v1.0.0

Properties

  • Ordered by index.
  • Randomizable when its data_type is randomizable (e.g., randomizable scalar or aggregate of randomizable scalar).
  • Element and data_type can be any scalar or aggregate of scalar.
  • Index must be a non-negative integer.
  • Size must be a non-zero integer.
  • Can be nested within any collection type (e.g., array, list, map or set).

Declarations

v2.2.0

Not supported yet

PSSGen: Does not support float32, float64, and chandle as data_type.

An array has 2 declaration formats:

Format Syntax
Template array<data_type, size> identifier
Square data_type identifier [size]
1
2
3
4
array<int    , 3> intArray   ;          //  intArray   : {   0,    0,    0}
array<bit [8], 3> byteArray  ;          //  byteArray  : {8'b0, 8'b0, 8'b0}
array<string , 3> stringArray;          //  stringArray: {  "",   "",   ""}
array<array<int, 3>, 2> nestedArray;    //  nestedArray: {{0, 0, 0}, {0, 0, 0}}
1
2
3
4
int     intArray    [3];                //  intArray   : {   0,    0,    0}
bit [8] byteArray   [3];                //  byteArray  : {8'b0, 8'b0, 8'b0}
string  stringArray [3];                //  stringArray: {  "",   "",   ""}
int     nestedArray [2][3];             //  nestedArray: {{0, 0, 0}, {0, 0, 0}} (1)
  1. v3.1 Not supported yet A multidimensional square declaration: int nestedArray [2][3] declares the same array as array<array<int, 3>, 2>. Up to PSS 3.0 the square form takes a single dimension, so a nested array needs the template form.
Note

An array is a fixed-size collection whose size must be a non-zero integer and cannot be changed after declaration.

Declare array by rand keyword

v2.2.0

rand array<int, 3> intArray;    //  declare integer array with 3 random elements
rand int intArray [3];          //  declare integer array with 3 random elements

Initialization Assignment

v2.2.0

PSSGen: Supports bit, int, and string as element in initialization assignment.

Not supported yet

PSSGen: Does not support bool, enum, float32, float64, chandle, and struct as element in initialization assignment.

An array can be assigned at declaration; otherwise, each element will be initialized to the default initial value.

1
2
3
4
array<int    , 2> intArray    = {    1,     2};         //  intArray   : {    1,     2}
array<bit [8], 2> byteArray   = {8'b01, 8'b10};         //  byteArray  : {8'b01, 8'b10}
array<string , 2> stringArray = {  "A",   "B"};         //  stringArray: {  "A",   "B"}
array<array<int, 2>, 2> nestedArray = {{1, 2}, {3, 4}}; //  nestedArray: {{1, 2}, {3, 4}}
1
2
3
4
int     intArray    [2] = {    1,     2};               //  intArray   : {    1,     2}
bit [8] byteArray   [2] = {8'b01, 8'b10};               //  byteArray  : {8'b01, 8'b10}
string  stringArray [2] = {  "A",   "B"};               //  stringArray: {  "A",   "B"}
int     nestedArray [2][2] = {{1, 2}, {3, 4}};          //  nestedArray: {{1, 2}, {3, 4}} (1)
  1. v3.1 Not supported yet Multidimensional square declaration; up to PSS 3.0 use the template form.

Declaration form used below

The rest of this page uses the template form (array<data_type, size>). Operators and methods behave the same on arrays declared in square form.

Array Operators

Operator Description
[] Used to access a specific element of an array by a given index, which must be a non-negative integer.
= Creates a copy of the array-type expression on the RHS and assigns it to the array on the LHS.
== Evaluates to true if both sizes are equal and all elements with corresponding indexes are equal.
!= Evaluates to true if the sizes are not equal or if any element with corresponding index is not equal.
in Evaluates to true if the element on the LHS of in exists in the array.
foreach Iterates over the array's elements.

Array Methods

Method Description
int size() Returns the number of elements in the array.
int sum() Returns the sum of all elements in the array, when the data_type of the element is bit or int.
float64 sum()
Not supported yet v2.1
Returns the sum of all elements in the array, when the data_type of the element is float32 or float64.
list<data_type> to_list() Returns all elements as a list-type value.
set<data_type> to_set() Returns all elements as a set-type value.

Index operator []

v2.2.0

PSSGen: Supports bit, int, and string as element for index operator [].

Not supported yet

PSSGen: Does not support bool, enum, float32, float64, chandle, and struct as element for index operator [].

Used to access a specific element of an array by a given index, which must be a non-negative integer.

1
2
3
array<int, 3> intArray = {1, 2, 3};

int intVal = intArray[2];   //  intVal: 0 -> 3

Assignment operator =

v2.2.0

PSSGen: Supports bit, int, and string as element for assignment operator =.

Not supported yet

PSSGen: Does not support bool, enum, float32, float64, chandle, and struct as element for assignment operator =.

Creates a copy of the array-type expression on the RHS and assigns it to the array on the LHS.

1
2
3
array<int, 3> intArray = {1, 2, 3};

intArray = {3, 4, 5};       //  intArray: {1, 2, 3} -> {3, 4, 5}

Note

Operators that modify contents can only be used within an exec block or a native function.

Equality operator ==

v2.2.0

Evaluates to true if both sizes are equal and all elements with corresponding indexes are equal.

1
2
3
4
5
6
7
8
array<int   , 2> intArray_0  = {  1 ,  2      };
array<int   , 3> intArray_1  = {  1 ,  2 ,  3 };
array<string, 2> stringArray = { "1", "2"     };

bit bitVal_0, bitVal_1, bitVal_2;
if (intArray_0 == intArray_0 ) bitVal_0 = 1;    //  bitVal_0: 0 -> 1 (1)
if (intArray_0 == intArray_1 ) bitVal_1 = 1;    //  bitVal_1: 0 -> 0 (2)
if (intArray_0 == stringArray) bitVal_2 = 1;    //  ILLEGAL (3)

  1. Same size, and all elements at corresponding indexes are equal.
  2. Different sizes.
  3. Arrays with different data_type are incomparable.

Warning

Two arrays with different data_type of element should NOT be compared.

Inequality operator !=

v2.2.0

Evaluates to true if the sizes are not equal or if any element with corresponding index is not equal.

1
2
3
4
5
6
7
8
array<int   , 2> intArray_0  = {  1 ,  2      };
array<int   , 3> intArray_1  = {  1 ,  2 ,  3 };
array<string, 2> stringArray = { "1", "2"     };

bit bitVal_0, bitVal_1, bitVal_2;
if (intArray_0 != intArray_0 ) bitVal_0 = 1;    //  bitVal_0: 0 -> 0 (1)
if (intArray_0 != intArray_1 ) bitVal_1 = 1;    //  bitVal_1: 0 -> 1 (2)
if (intArray_0 != stringArray) bitVal_2 = 1;    //  ILLEGAL (3)

  1. Same size, and all elements at corresponding indexes are equal.
  2. Different sizes.
  3. Arrays with different data_type are incomparable.

Warning

Two arrays with different data_type of element should NOT be compared.

Set membership operator in

v2.2.0

Evaluates to true if the element on the LHS of in exists in the array.

1
2
3
4
5
6
array<int, 3> intArray = {1, 2, 3};

bit bitVal_0, bitVal_1, bitVal_2;
if ( 1  in intArray) bitVal_0 = 1;              //  bitVal_0: 0 -> 1
if ( 0  in intArray) bitVal_1 = 1;              //  bitVal_1: 0 -> 0
if ("1" in intArray) bitVal_2 = 1;              //  ILLEGAL (1)

  1. The data_type of the LHS of in differs from that of the array's element on the RHS of in.

Warning

The data_type of the element on the LHS of in should be the SAME as that of the array's element on the RHS of in.

Within a slice

v3.1 Not supported yet

in can test a slice of the array instead of the whole array: [left..right] (both indexes inclusive), [left..] (to the last element) or [..right] (from the first element).

1
2
3
4
5
6
7
array<int, 4> intArray = {1, 2, 3, 4};

bit bitVal_0, bitVal_1, bitVal_2, bitVal_3;
if (2 in intArray[1..2]) bitVal_0 = 1;     //  bitVal_0: 0 -> 1 (slice: {2, 3})
if (1 in intArray[1..] ) bitVal_1 = 1;     //  bitVal_1: 0 -> 0 (slice: {2, 3, 4})
if (1 in intArray[..0] ) bitVal_2 = 1;     //  bitVal_2: 0 -> 1 (slice: {1})
if (1 in intArray[2..1]) bitVal_3 = 1;     //  bitVal_3: 0 -> 0 (empty slice) (1)

  1. The slice is empty when the left index is greater than the right index, the left index is not less than the size, or the right index is negative; in then evaluates to false.

The slice indexes may be random, so a constraint can restrict a value to part of the array, e.g., constraint v in intArray[a..b];. A slice applies only to array and list (not to set, and not to a value list literal such as {1, 2, 3}).

foreach statement

v2.2.0

Iterates over the array's elements.

See Procedural/foreach for more information.

array<int, 3> intArray = {4, 5, 6};
int intVal_0 = 0;
int intVal_1 = 0;
int intVal_2 = 0;

foreach (i : intArray[j]) {
    intVal_0 = i;               //  intVal_0: 0 -> 4 -> 5 -> 6
    intVal_1 = j;               //  intVal_1: 0 -> 0 -> 1 -> 2
    intVal_2 = intArray[j];     //  intVal_2: 0 -> 4 -> 5 -> 6
}


function int size()

v2.2.0

Returns the number of elements in the array.

1
2
3
array<int, 2> intArray;

int intVal = intArray.size();       //  intVal: 0 -> 2

Note

Considered a constant expression.

function int sum()

v2.2.0

Returns the sum of all elements in the array, when the data_type of the element is bit or int.

1
2
3
4
5
6
7
array<int    , 2> intArray    = {  1   ,  2    };
array<bit [2], 2> bitArray    = { 2'b01, 2'b10 };
array<string , 2> stringArray = { "1"  , "2"   };

int intVal_0 = intArray.sum()   ;   //  intVal_0: 0 -> 3
int intVal_1 = bitArray.sum()   ;   //  intVal_1: 0 -> 3
int intVal_2 = stringArray.sum();   //  ILLEGAL (1)

  1. A string cannot be summed.

Warning

The data_type of the element should be an integer type (e.g., int or bit).

function float64 sum()

Not supported yet v2.1

Returns the sum of all elements in the array, when the data_type of the element is float32 or float64.

1
2
3
4
5
6
7
array<float32, 2> float32Array = {1.0, 2.0};
array<float64, 2> float64Array = {1.0, 2.0};
array<string , 2> stringArray  = {"1", "2"};

float64 fVal_0 = float32Array.sum(); //  fVal_0: 0.0 -> 3.0
float64 fVal_1 = float64Array.sum(); //  fVal_1: 0.0 -> 3.0
float64 fVal_2 = stringArray.sum(); //  ILLEGAL (1)

  1. A string cannot be summed.

Warning

The data_type of the element should be a floating-point type (e.g., float32 or float64).

function list<data_type> to_list()

v2.2.0

Returns all elements as a list-type value.

1
2
3
4
5
array<int   , 3> intArray    = {  2 ,  1 ,  2  };
array<string, 3> stringArray = { "2", "1", "2" };

list<int   > intList    = intArray.to_list()   ;    //  intList: {} -> {2, 1, 2}
list<string> stringList = stringArray.to_list();    //  stringList: {} -> {"2", "1", "2"};

function set<data_type> to_set()

v2.2.0

Returns all elements as a set-type value.

1
2
3
4
5
array<int   , 3> intArray    = {  2 ,  1 ,  2  };
array<string, 3> stringArray = { "2", "1", "2" };

set<int   > intSet    = intArray.to_set()   ;       //  intSet: {} -> {2, 1}
set<string> stringSet = stringArray.to_set();       //  stringSet: {} -> {"2", "1"}