Constrain sum of elements in an array

In reply to georgean:

Hi georgean,
I would like to share my knowledge when you use a constraint on sum().
For ex:
rand bit [2:0] a;
Constraint on the sum of a is less than or equal to 7 and size of a is 3.
now there is no wonder if you see the following values of a:
a[0] = 'h7, a[1] = 'h7, a[2] = 'h7
Reason is the lower 3 bits of a.sum() is 5 and the constraint is still valid!

Solution would be to use one extra bit when declaring “a” i.e. declare it as, “rand bit [3:0] a” and constrain the MSB of a i.e. a[3] = 0. Now you can see valid values on a.

Regards,
Naveen

1 Like