# How does .sum() operate in a constraint

**URL:** <https://verificationacademy.com/forums/t/how-does-sum-operate-in-a-constraint/47559>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog, systemverilog-constraint\
**Created:** [June 7, 2024, 7:05pm UTC](https://verificationacademy.com/forums/t/how-does-sum-operate-in-a-constraint/47559 "2024-06-07T19:05:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lmadem](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/l/46a35a/32.png) [@lmadem](https://verificationacademy.com/forums/u/lmadem)\
**Post date:** [June 7, 2024, 7:05pm UTC](https://verificationacademy.com/forums/t/how-does-sum-operate-in-a-constraint/47559/1 "2024-06-07T19:05:53Z")

</div>

Goal : To generate a array with sum of all the elements is less than or equal to N(array-size)

```auto
class data;
  rand bit [5:0] val[]; //a dynamic array of unsigned 6-bit elements
  
  //constraint to generate the sum of all the elements in the array is less than or equal to N(array size)
  constraint c1 {
    //fixing the size of the array(which is N)
    val.size() == 8;
    val.sum() with (int'(item)) <= 8; //working
    val.sum() <= 8; //not-working
  }
  
  function void display();
    foreach(val[i])
      $display("Index[%0d]:%0d",i,val[i]);
  endfunction
endclass

module test;
  data d;
  initial 
    begin
      d = new();
      d.randomize();
      d.display();
    end
endmodule

```

I would like to understand how does the .sum() operator work in a constraint. For the above code, with this line: val.sum() with (int’(item)) \<= 8; I was able to generate an array with sum of all the elements less than or equal to array-size but when I use this line val.sum() \<= 8; the array elements are generated randomly based on the datatype range. Could someone please help me to understand this behaviour.

val.sum() with (int’(item)) \<= 8;

 ![image](https://verificationacademy.com/forums/uploads/default/original/2X/e/ed1edafc66452d9fd34588fe81a1f7f8e9527ad3.png)

val.sum() \<= 8

 ![image](https://verificationacademy.com/forums/uploads/default/original/2X/1/102693bb13c9c720c453e22927919a290fe6960a.png)

---

<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:** [June 7, 2024, 9:19pm UTC](https://verificationacademy.com/forums/t/how-does-sum-operate-in-a-constraint/47559/2 "2024-06-07T21:19:43Z")

</div>

The `sum() array reduction operator works the same in a constraint as it does anywhere else.

See my DVCon paper: [SystemVerilog Constraints: Appreciating What You Forgot in School to Get Better Results](https://dvcon-proceedings.org/wp-content/uploads/systemverilog-constraints-appreciating-what-you-forgot-in-school-to-get-better-results.pdf)

---

<div class="post-metadata">

**Author:** ![lmadem](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/l/46a35a/32.png) [@lmadem](https://verificationacademy.com/forums/u/lmadem)\
**Post date:** [June 7, 2024, 9:48pm UTC](https://verificationacademy.com/forums/t/how-does-sum-operate-in-a-constraint/47559/3 "2024-06-07T21:48:41Z")

</div>

Thank you for pointing me to the right direction!
