# SystemVerilog Constraint Help for Negative/Postiive numbers

**URL:** <https://verificationacademy.com/forums/t/systemverilog-constraint-help-for-negative-postiive-numbers/38197>\
**Category:** SystemVerilog\
**Tags:** signed, SystemVerilog, systemverilog-constraint\
**Created:** [October 9, 2020, 1:38am UTC](https://verificationacademy.com/forums/t/systemverilog-constraint-help-for-negative-postiive-numbers/38197 "2020-10-09T01:38:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![kk9110](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/k/d26b3c/32.png) [@kk9110](https://verificationacademy.com/forums/u/kk9110)\
**Post date:** [October 9, 2020, 1:38am UTC](https://verificationacademy.com/forums/t/systemverilog-constraint-help-for-negative-postiive-numbers/38197/1 "2020-10-09T01:38:04Z")

</div>

Hi,

I have three values which are signed logic vectors as shown below. I am trying to build a constraint where the sum of the three numbers, positive and negative included should be less than a constant.

parameter signed LIMIT = 200;  
rand logic signed [15:0] a;  
rand logic signed [15:0] b;  
rand logic signed [15:0] c;

constraint limit {a+b+c \< LIMIT);

I tried the above code snippet but it does not obey the constraint.I would like the value of a ,b and c be negative/and or positive but the sum to be less than the constant.

How can i write this constraint ?

---

<div class="post-metadata">

**Author:** ![ABD\_91](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/a/c77e96/32.png) [@ABD\_91](https://verificationacademy.com/forums/u/ABD_91)\
**Post date:** [October 12, 2020, 6:08pm UTC](https://verificationacademy.com/forums/t/systemverilog-constraint-help-for-negative-postiive-numbers/38197/2 "2020-10-12T18:08:15Z")

</div>

_In reply to [kk9110](https://verificationacademy.com/forums/t/systemverilog-constraint-help-for-negative-postiive-numbers/38197):_

I am not certain how the parameter is interpreted internally  
( **As Signed OR Unsigned ?? .  
I believe LRM isn’t clear about this so the result might vary across Simulators !!** )

**Verilog Basics dictate ::**  
If it’s signed the each Operand in the expression would be treated as Signed ,  
**else** Each Operand would be treated as Unsigned !!

It would be easier to analyze if you had posted the results too

---

<div class="post-metadata">

**Author:** ![kk9110](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/k/d26b3c/32.png) [@kk9110](https://verificationacademy.com/forums/u/kk9110)\
**Post date:** [October 12, 2020, 6:13pm UTC](https://verificationacademy.com/forums/t/systemverilog-constraint-help-for-negative-postiive-numbers/38197/3 "2020-10-12T18:13:45Z")

</div>

_In reply to [ABD\_91](https://verificationacademy.com/forums/t/systemverilog-constraint-help-for-negative-postiive-numbers/38197/2):_

parameter is being treated as signed. I had actually defined it as follows. (Typo in my original post)

parameter signed LIMIT = 200;

I do see that the values for a,b and c are getting randomized between negative and positive numbers. But its just that the sum of the three numbers seems to violate the value of LIMIT.

---

<div class="post-metadata">

**Author:** ![ABD\_91](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/a/c77e96/32.png) [@ABD\_91](https://verificationacademy.com/forums/u/ABD_91)\
**Post date:** [October 12, 2020, 6:17pm UTC](https://verificationacademy.com/forums/t/systemverilog-constraint-help-for-negative-postiive-numbers/38197/4 "2020-10-12T18:17:19Z")

</div>

_In reply to [kk9110](https://verificationacademy.com/forums/t/systemverilog-constraint-help-for-negative-postiive-numbers/38197/3):_

I believe there could be a misinterpretation on your part .  
Realize if the MSb of a , b , c are 1’b1 , they indicate Negative Numbers !!

One could read it as unsigned ( which isn’t true )

What’s the sum you Observe ? Also How are you displaying / Calculating the sum ?

---

<div class="post-metadata">

**Author:** ![kk9110](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/k/d26b3c/32.png) [@kk9110](https://verificationacademy.com/forums/u/kk9110)\
**Post date:** [October 12, 2020, 6:29pm UTC](https://verificationacademy.com/forums/t/systemverilog-constraint-help-for-negative-postiive-numbers/38197/5 "2020-10-12T18:29:33Z")

</div>

_In reply to [ABD\_91](https://verificationacademy.com/forums/t/systemverilog-constraint-help-for-negative-postiive-numbers/38197/4):_

Assume the following

LIMIT - 3H  
A - 020B (Hex), B - FD9C (Hex) and C - 0A44 (Hex)

Sum -09EB (Hex) \> LIMIT (3 Hex)

I am seeing the values as all “Hex” so if I do see a MSB of ‘1’ i know the number is negative.

---

<div class="post-metadata">

**Author:** ![ABD\_91](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/a/c77e96/32.png) [@ABD\_91](https://verificationacademy.com/forums/u/ABD_91)\
**Post date:** [October 12, 2020, 6:35pm UTC](https://verificationacademy.com/forums/t/systemverilog-constraint-help-for-negative-postiive-numbers/38197/6 "2020-10-12T18:35:42Z")

</div>

_In reply to [kk9110](https://verificationacademy.com/forums/t/systemverilog-constraint-help-for-negative-postiive-numbers/38197/5):_

Besides the ‘signed’ keyword I believe **you have changed the Packed Dimensions too in Original Code !!**

[Previously it was [7:0] for all 3 Variables . ]

Anyways with [15:0] there are Chances of Overflow too .

(1) Also LIMIT is 200 which is 32-bit Signed .  
**(200) in Decimal is (C8) in hexadecimal .**

**Why are you expecting it to be 3H ??**

(2) Try to display all 3 Variables in **“%0d”** Format String

---

<div class="post-metadata">

**Author:** ![kk9110](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/k/d26b3c/32.png) [@kk9110](https://verificationacademy.com/forums/u/kk9110)\
**Post date:** [October 12, 2020, 7:45pm UTC](https://verificationacademy.com/forums/t/systemverilog-constraint-help-for-negative-postiive-numbers/38197/7 "2020-10-12T19:45:03Z")

</div>

_In reply to [ABD\_91](https://verificationacademy.com/forums/t/systemverilog-constraint-help-for-negative-postiive-numbers/38197/6):_

Sorry should have been clear. The LIMIT is a parameter and I had it parameterized to 3 hex. I made the following changes and it seems to work well now.

parameter signed LIMIT = 200;  
rand logic signed [15:0] a;  
rand logic signed [15:0] b;  
rand logic signed [15:0] c;

rand logic signed [17:0] sum;

constraint c\_sum {sum \< LIMIT;}  
constraint limit {a+b+c \< sum;
