# How is this both constraint working same?

**URL:** <https://verificationacademy.com/forums/t/how-is-this-both-constraint-working-same/45755>\
**Category:** SystemVerilog\
**Tags:** constraint-randomization, constraints, slove-before-constraint\
**Created:** [January 12, 2024, 5:22pm UTC](https://verificationacademy.com/forums/t/how-is-this-both-constraint-working-same/45755 "2024-01-12T17:22:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amruthabmanigar1](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/a/5daacb/32.png) [@Amruthabmanigar1](https://verificationacademy.com/forums/u/Amruthabmanigar1)\
**Post date:** [January 12, 2024, 5:22pm UTC](https://verificationacademy.com/forums/t/how-is-this-both-constraint-working-same/45755/1 "2024-01-12T17:22:09Z")

</div>

Hi, team.

```auto
constraint en_c { solve en before val; 
                   if(en == 1) { val inside {[0:100]} } ;}
  constraint en_c1 { solve en before val1;
               !(en == 1) || { val1 inside {[0:100]} } ;}

```

I found that these two constraints working same, can anyone explain me how’s it working.

Thank you

---

<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 12, 2024, 5:44pm UTC](https://verificationacademy.com/forums/t/how-is-this-both-constraint-working-same/45755/2 "2024-01-12T17:44:05Z")

</div>

All constraints get broken down into a set of Boolean equations that must evaluate to a non-zero result (that’s how SystemVerilog defines _true_). Although the `if` constraint seems like a procedural construct, it actually gets converted into the boolean equation you see in `en_c1`

---

<div class="post-metadata">

**Author:** ![Amruthabmanigar1](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/a/5daacb/32.png) [@Amruthabmanigar1](https://verificationacademy.com/forums/u/Amruthabmanigar1)\
**Post date:** [January 13, 2024, 4:42am UTC](https://verificationacademy.com/forums/t/how-is-this-both-constraint-working-same/45755/3 "2024-01-13T04:42:16Z")

</div>

Thanks Dave, Explanation indeed helped me to understand the logic how constraints work.

but, i have this doubt. Please check with this.

case1:  
constraint en\_c { solve en before val;  
if(en == 1) { val inside {[0:100]} } ;}  
but in this case only if my en==1, constraint is applied to the val, if en==0 it will not work.

case2:  
constraint en\_c1 { solve en before val1;  
!(en == 1) || { val1 inside {[0:100]} } ;}  
here  
1)if en==1  
!(en==1), which it evaluates to 0, which means constraint shouldn’t work right?  
2) if en==0  
!(en==1) evaluates to 1, which means now constraint should be applicable right?

Is it like this **|| { val1 inside {[0:100]} } ;}** part of the constraint always be True ?

Thank you

---

<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 13, 2024, 7:45am UTC](https://verificationacademy.com/forums/t/how-is-this-both-constraint-working-same/45755/4 "2024-01-13T07:45:20Z")

</div>

It might help if you built a truth table for each constraint, and shorten `val` to 2-bits and the range to `[0:1]`. Then you will see that the valid solutions (marked with a T) are the same for both constraints.

```v
en_c { if(en == 1) { val inside {[0:1]} } ;}
en_c1 { !(en == 1) || val inside {[0:1]} } ;}

```

| en | val | en\_c | en\_c1 |
| --- | --- | --- | --- |
| 0 | 0 | T | T |
| 0 | 1 | T | T |
| 0 | 2 | T | T |
| 0 | 3 | T | T |
| 1 | 0 | T | T |
| 1 | 1 | T | T |
| 1 | 2 | F | F |
| 1 | 3 | F | F |

---

<div class="post-metadata">

**Author:** ![Amruthabmanigar1](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/a/5daacb/32.png) [@Amruthabmanigar1](https://verificationacademy.com/forums/u/Amruthabmanigar1)\
**Post date:** [January 13, 2024, 8:06am UTC](https://verificationacademy.com/forums/t/how-is-this-both-constraint-working-same/45755/5 "2024-01-13T08:06:00Z")

</div>

Thank you Dave. Truth table made it simple.
