# Sum of array (array constraints)

**URL:** <https://verificationacademy.com/forums/t/sum-of-array-array-constraints/45481>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog, systemverilog-Arrays\
**Created:** [January 10, 2024, 7:10pm UTC](https://verificationacademy.com/forums/t/sum-of-array-array-constraints/45481 "2024-01-10T19:10:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![VerifEngg](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/v/5e9695/32.png) [@VerifEngg](https://verificationacademy.com/forums/u/VerifEngg)\
**Post date:** [January 10, 2024, 7:10pm UTC](https://verificationacademy.com/forums/t/sum-of-array-array-constraints/45481/1 "2024-01-10T19:10:29Z")

</div>

I have the code for the following constraint:

1. Sum of all array elements must be 17
2. Array must have at least one element which is multiple of 4.
3. Size of an array can be anything

```auto
class Example;
  rand bit [15:0] my_arr[]; //if I change to 31 here, I see an incorrect array 
  rand int inx_num;
   
   constraint c1 {my_arr.size() inside {[10:15]};}
   constraint c1a {inx_num inside {[0:my_arr.size()]};}
   constraint c2 {foreach (my_arr[i]) {
     i == inx_num -> my_arr[i] % 4 == 0 && my_arr[i] > 4;
   }};
                  
   constraint c3 {my_arr.sum() with(int'(item))== 17;}
                  
   function void post_randomize();
     $display("my_arr = %p \n", my_arr);
   endfunction
                  
 endclass
                  
 module TB;
   Example E;
   
   initial begin
     E = new();
     if(!(E.randomize()))
       $display("error");
   end
 endmodule

```

In line #2:  
` rand bit [15:0] my_arr[]` → gives me the correct array; like this:  
my\_arr = '{'h0, 'h1, 'h1, 'h2, 'h1, 'h0, 'h0, 'h1, 'h3, 'h0, 'h8}

`rand bit [31:0] my_arr[]` → gives me an incorrect array (constraint c3 not honored)  
my\_arr = '{'h1420568f, 'h5031fd43, 'h730cfe86, 'h105454e8, 'h4583b052, 'h1c167bce, 'h4b2f6faa, 'h7e643336, 'hd532e50b, 'h3fbca2b6, 'hd82f0210}

Any idea, why?

---

<div class="post-metadata">

**Author:** ![felix.wu](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/f/ce7236/32.png) [@felix.wu](https://verificationacademy.com/forums/u/felix.wu)\
**Post date:** [January 11, 2024, 2:25am UTC](https://verificationacademy.com/forums/t/sum-of-array-array-constraints/45481/2 "2024-01-11T02:25:49Z")

</div>

may the sum of my\_arr is still 17 as 'hd532e50b and 'hd82f0210 are negative values  
what’s the randomized value of inx\_num?  
the constraint of inx\_num should be inside [0:my\_arr.size()-1], right?  
or else, let’s say if inx\_num is randomized to 11 when my\_arr.size == 11, the constraint c2 will never meet the pre-condition of (i==inx\_num), as i is from 0 to 10.

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [January 11, 2024, 5:34am UTC](https://verificationacademy.com/forums/t/sum-of-array-array-constraints/45481/3 "2024-01-11T05:34:05Z")

</div>

You are casting each element in the sum from a 32-bit unsigned type to a 32-bit signed type. So some element may be treated as negative numbers and still have your sum be 17. It’s not a problem when the element size is 16 bits. All 16-bit values will be cast to a 32-bit signed number and still be considered positive values.

Also, for every 2_N_ elements you want to add, you need _N_ extra bits in the sum result to prevent overflow. For 16 elements, you need 4 extra bits.

```v
constraint c3 {my_arr.sum() with(36'(item))== 17;}

```
