The Designer's Guide Community
Forum
Welcome, Guest. Please Login or Register. Please follow the Forum guidelines.
Jul 17th, 2024, 2:29am
Pages: 1
Send Topic Print
Ternary operator (that is, ?:) versus if/else advantage? (Read 4298 times)
Peruzzi
Community Member
***
Offline



Posts: 71

Ternary operator (that is, ?:) versus if/else advantage?
Mar 12th, 2009, 12:18pm
 
For behavioral modeling as opposed to RTL code for synthesis, I wonder if there's a simulation execution speed advantage to using the ternary operator rather than the more readable if/else construct.  

Here's an example of a digital mux, but my question applies to both digital and analog behavioral models:


// If/Else approach

reg t;

always @ (a or b or select)
if (select)
t = a;
else
t = b;

// Ternary approach:
wire t = (select ? a : b);

If the goal were digital synthesis, there's a difference: if/else results in a register with multiplexed input.  Ternary results in just the mux, but that's not the point.

The two approaches give identical behavioral results.

My instinct tells me the ternary approach takes the same number of cycles to execute as the if/else code, even though it looks more compact and clever, but does anybody know if this is in fact true?

I tend to use the if/else approach to make the code more readable to others (as well as to myself months later).  But I would change to the ternary approach if it offered even a small speed advantage.

Thanks,

Bob P.

Back to top
 
 
View Profile   IP Logged
boe
Community Fellow
*****
Offline



Posts: 615

Re: Ternary operator (that is, ?:) versus if/else advantage?
Reply #1 - Mar 12th, 2009, 1:21pm
 
Peruzzi wrote on Mar 12th, 2009, 12:18pm:
... If the goal were digital synthesis, there's a difference: if/else results in a register with multiplexed input.  Ternary results in just the mux, but that's not the point. ...
Your code fragments should both synthesize to just a MUX.

Quote:
... My instinct tells me the ternary approach takes the same number of cycles to execute as the if/else code, even though it looks more compact and clever, but does anybody know if this is in fact true? ...
It  might depend on the simulator (and, maybe even its version)...
BOE
Back to top
 
 
View Profile   IP Logged
jbdavid
Community Fellow
*****
Offline



Posts: 378
Silicon Valley
Re: Ternary operator (that is, ?:) versus if/else advantage?
Reply #2 - Mar 12th, 2009, 1:42pm
 
I find something described on one line is EASIER to read, than something that takes 4 or 5 lines..

A master coder could probably figure out a way to measure the speed difference between simulators..  but I suspect its LOW on the list of ways to speed up model execution.  Profilers can help you identify where your model is spending its time.
It also may Vary between different vendors.

I would NOT suggest forbiding the use of ternary operators in  behavioral modelng.. as to RTL, idk.

Jonathan
Back to top
 
 

jbdavid
Mixed Signal Design Verification
View Profile WWW   IP Logged
Peruzzi
Community Member
***
Offline



Posts: 71

Re: Ternary operator (that is, ?:) versus if/else advantage?
Reply #3 - Mar 13th, 2009, 11:28am
 
BOE, Jonathan,

Both interesting responses and useful information.  I guess readability is in the eye of...

Thanks for responding.

Bob P.
Back to top
 
 
View Profile   IP Logged
Pages: 1
Send Topic Print
Copyright 2002-2024 Designer’s Guide Consulting, Inc. Designer’s Guide® is a registered trademark of Designer’s Guide Consulting, Inc. All rights reserved. Send comments or questions to editor@designers-guide.org. Consider submitting a paper or model.