what should we use == or === ??

Dear Readers,

I have been using this operator since I have started my career as an ASIC Engineer. So question is what should we use "==" or "===" in if condition.

As per my experience as an ASIC Verification engineer, I would suggest you to use "===". The reason of using "===" is The x and z will be used in comparision and the logical result will be a TRUE and FALSE based on the actual comparision.

NOTE : But please keep in mind "===" is not synthesyzable.

Lets take a simple example :

using "===" operator

if (a === b)
   out1 = a & b ;
else
  out1 = a | b;

In this case a and b are identical, even if they becomes x or z the if clause will be executed and out1 will be driven by AND gate.

But that is not the case if you use "==" for the same logic. In this case, if a or b becomes x or z, else will be executed and out1 will be driven to OR gate.

I hope, this will be useful for you to understand the basic difference between "==" and "===".

Happy Reading,
ASIC With Ankit

5 comments:

Dhaval said...

A = 2'b1X and B = 2'b1X and if use "=="...are you sure it would execute else part?

Dhaval

Ankit Gopani said...

Hi Dhaval,

In this case it wont execute else part as both values are same and simulator will expect both values to be same. It will definitely go in to If condition.

But difference comes in picture when we are trying to display the comparition value using equality operator. You can try it out below example in VCS.

program main ;
reg a_1,a_0,a_x,a_z;
reg b_1,b_0,b_x,b_z;
initial
begin
a_1 = 'b1;a_0 = 'b0;a_x = 'bx;a_z = 'bz;
b_1 = 'b1;b_0 = 'b0;b_x = 'bx;b_z = 'bz;

$display("--------------------------");
$display (" == 0 1 x z ");
$display("--------------------------");
$display (" 0 %b %b %b %b ",a_0 == b_0,a_0 == b_1,a_0 == b_x,a_0 == b_z);
$display (" 1 %b %b %b %b ",a_1 == b_0,a_1 == b_1,a_1 == b_x,a_1 == b_z);
$display (" x %b %b %b %b ",a_x == b_0,a_x == b_1,a_x == b_x,a_x == b_z);
$display (" z %b %b %b %b ",a_z == b_0,a_z == b_1,a_z == b_x,a_z == b_z);
$display("--------------------------");
$display("--------------------------");
$display (" === 0 1 x z ");
$display("--------------------------");
$display (" 0 %b %b %b %b ",a_0 === b_0,a_0 === b_1,a_0 === b_x,a_0 === b_z);
$display (" 1 %b %b %b %b ",a_1 === b_0,a_1 === b_1,a_1 === b_x,a_1 === b_z);
$display (" x %b %b %b %b ",a_x === b_0,a_x === b_1,a_x === b_x,a_x === b_z);
$display (" z %b %b %b %b ",a_z === b_0,a_z === b_1,a_z === b_x,a_z === b_z);

end
endprogram

After running this example you will get the below result:

--------------------------
== 0 1 x z
--------------------------
0 1 0 x x
1 0 1 x x
x x x x x
z x x x x
--------------------------
--------------------------
=== 0 1 x z
--------------------------
0 1 0 0 0
1 0 1 0 0
x 0 0 1 0
z 0 0 0 1
--------------------------
Conclusion:

So from the result we can easily get an idea that while comparision if you use ==, it will give you x if any of the value has an x or z value. (please refere first talbe) But that is not the case if you use === operator x or z as well and gives you 0 or 1 result based on the comparition.

Hope this will answered your quesion.

-Ankit

Anonymous said...

Hello!
You may probably be very interested to know how one can make real money on investments.
There is no need to invest much at first.
You may begin earning with a sum that usually is spent
for daily food, that's 20-100 dollars.
I have been participating in one company's work for several years,
and I'll be glad to share my secrets at my blog.

Please visit blog and send me private message to get the info.

P.S. I earn 1000-2000 per day now.

http://theinvestblog.com [url=http://theinvestblog.com]Online Investment Blog[/url]

Chander Makhija said...

Hi Ankit,
You have nice concern here.
But what I know is when you are dealing with 4 state values/variables then only use "===", as it puts additional task on compiler, that is because compiler will have to seek to 4 state values.

Chander Makhija said...

Hi Ankit, Dhaval,
I just observed in QuestaSim 6.5b that if any variable, compared with "==", has wild card value like X or Z, "==" comparison will result to false. Even I tried comparing a = 2'b1X and b = 2'b1X. It went into else loop.

So Ankit's original prog seems right.

CM