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 non-negative integer.
  • Size must be non-zero integer.
  • Can be nested by any collection types (e.g., array, list, map or set).

Declarations

v2.2.0

Not support yet

PSSGen: Not support float32, float64, and chandle as data_type.

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: {  "",   "",   ""}
// (1)!
  1. square format NOT support for declare nested array
Note

Array is a fixed-size collection, which's size must be assigned by a non-zero integer, and cannot be changed after declared.

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: Support bit, int, and string as element in initialization assignment.

Not support yet

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

Array can be assigned at declaration; otherwise, each elements will be initialized to 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"}
// (1)!
  1. square format NOT support for declare nested array

Array Operators

Operator Description
[] Used to access a specific element of an array by 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 whether both sizes are not equal or if any element with corresponding index is not equal.
in Evaluates to true if element on LHS of in is 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 data_type of element is bit or int.
float64 sum()
Not support yet v2.1
Returns the sum of all elements in the array, when data_type of element is float32 or float64.
list<data_type> to_list() Returns all elements to a list-type.
set<data_type> to_set(): Returns all elements to a set-type.

Index operator []

v2.2.0

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

Not support yet

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

Used to access a specific element of an array by 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
1
2
3
int intArray [3] = {1, 2, 3};

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

Assignment operator =

v2.2.0

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

Not support yet

PSSGen: 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}
1
2
3
int intArray [3] = {1, 2, 3};

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

Operator that modify contents can only be used within exec block or 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. Equalize size and all elements with corresponding indexes.
  2. Inequalize size.
  3. Different data_type between arrays are incomparable.
1
2
3
4
5
6
7
8
int    intArray_0  [2] = {  1 ,  2      };
int    intArray_1  [3] = {  1 ,  2 ,  3 };
string stringArray [2] = { "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. Equalize size and all elements with corresponding indexes.
  2. Inequalize size.
  3. Different data_type between arrays are incomparable.

Warning

Different data_type of element of two arrays should NOT be compared.

Inequality operator !=

v2.2.0

Evaluates to true whether both 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. Equalize size and all elements with corresponding indexes.
  2. Inequalize size.
  3. Different data_type between arrays are incomparable.
1
2
3
4
5
6
7
8
int    intArray_0  [2] = {  1 ,  2      };
int    intArray_1  [3] = {  1 ,  2 ,  3 };
string stringArray [2] = { "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. Equalize size and all elements with corresponding indexes.
  2. Inequalize size.
  3. Different data_type between arrays are incomparable.

Warning

Different data_type of element of two arrays should NOT be compared.

Set membership operator in

v2.2.0

Evaluates to true if element on LHS of in is 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. Different data_type between LHS of in and the array's element on RHS of in.
1
2
3
4
5
6
int intArray [3] = {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. Different data_type between LHS of in and the array's element on RHS of in.

Warning

Data_type of element on LHS of in should be SAME as the array's element on RHS of in.

foreach statement

v2.2.0

Iterates over the array's elements.

Look at 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
}
int intArray [3] = {1, 2, 3};
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
}
Usage: Constraint random array by using foreach()

For a random array, the foreach() method can be used to constraint each element's possible value.

1
2
3
4
5
6
7
8
9
rand array<bit [4], 4> nibbleArray;
constraint {
    nibbleArray[0] in [2..3];
    nibbleArray[1] in [2..3];
    nibbleArray[2] in [2..3];
    nibbleArray[3] in [2..3];
}
...
//  do something with solved nibbleArray
1
2
3
4
5
6
rand array<bit [4], 4> nibbleArray;
constraint {
    foreach(nibbleArray[i]) nibbleArray[i] in [2..3];   // (1)!
}
...
//  do something with solved nibbleArray
  1. Note that, using iterator_identifier format (e.g., foreach(i : nibbleArray)) can't constraint the array's elements.

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
1
2
3
int intArray [2];

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

Considered as a constant expression.

function int sum()

v2.2.0

Returns the sum of all elements in the array, when data_type of 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. string can't be calculated
1
2
3
4
5
6
7
int     intArray    [2] = {  1   ,  2    };
bit [2] bitArray    [2] = { 2'b01, 2'b10 };
string  stringArray [2] = { "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. string can't be calculated

Warning

The data-type of element should be integer types (e.g., int or bit).

Usage: use sum() to constrain a random array
1
2
3
4
5
6
rand array<int, 2> intArray;

constraint {
    intArray.sum() == 3;
    intArray[0] == 2;   //  intArray: {0, 0} -> {2, 1}
}
1
2
3
4
5
6
rand int intArray [2];

constraint {
    intArray.sum() == 3;
    intArray[0] == 2;   //  intArray: {0, 0} -> {2, 1}
}

function float64 sum()

Not support yet v2.1

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

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

flaot64 fVal_0 = intArray.sum()   ; //  fVal_0: 0.0 -> 3.0
flaot64 fVal_1 = bitArray.sum()   ; //  fVal_1: 0.0 -> 3.0
flaot64 fVal_2 = stringArray.sum(); //  ILLEGAL (1)
  1. string can't be calculated
1
2
3
4
5
6
7
float32 float32Array [2] = {1.0, 2.0};
flaot64 float64Array [2] = {1.0, 2.0};
string  stringArray  [2] = {"1", "2"};

flaot64 fVal_0 = intArray.sum()   ; //  fVal_0: 0.0 -> 3.0
flaot64 fVal_1 = bitArray.sum()   ; //  fVal_1: 0.0 -> 3.0
flaot64 fVal_2 = stringArray.sum(); //  ILLEGAL (1)
  1. string can't be calculated

Warning

The data-type of element should be floating-point types (e.g., float32 or float64).

function list<data_type> to_list()

v2.2.0

Returns all elements to a list-type.

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"};
1
2
3
4
5
int    intArray    [3] = {  2 ,  1 ,  2  };
string stringArray [3] = { "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 to a set-type.

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"}
1
2
3
4
5
int    intArray    [3] = {  2 ,  1 ,  2  };
string stringArray [3] = { "2", "1", "2" };

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

Last update: 2023-10-26
Created: 2023-10-04