Intel® Quartus® Prime Software
Intel® Quartus® Prime Design Software, Design Entry, Synthesis, Simulation, Verification, Timing Analysis, System Design (Platform Designer, formerly Qsys)
17040 Discussions

Optimization: If vs CASE, which one?

Altera_Forum
Honored Contributor II
1,148 Views

Hello people, 

 

I was recently reading the ALTERA synthesis manual and I saw that the IF statement is more resource demanding than the CASE statement. In cases where we use a lone IF as in the following: (reseting a counter) 

 

........... SIGNAL DATAREF : SIGNED(15 DOWNTO 0); ......... IF(DATAREF >= TO_SIGNED(1000,16)) THEN DATAREF<= TO_SIGNED(0,16); END IF;  

 

Then, does it matter if we use an IF or a CASE statement? 

 

Thanks in advance
0 Kudos
4 Replies
Altera_Forum
Honored Contributor II
428 Views

I would challenge you to use a case statement in that code.  

It really referes to muxes. an if/elsif/else tree builds a priority encoder, whereas a similar casestatement will build a simple mux. But synthesisors are getting better now, so they can work out when a load of if/elseifs are mutually exclusive and therefore build a mux instead of a priority encoder. 

 

But unless you really care about logic usage (you probably dont with modern devices - ram and DSP usages are usually the problem areas) you should write readible code, rather than code that is really bady written but saves you a couple of LUTs.
0 Kudos
Altera_Forum
Honored Contributor II
428 Views

Hey! Thanks for the answer! 

 

The thing is that this block of code is not the only one. For example I have a protection block that takes some 32 bit values from an ADC and checks each one individually for values over a limit. So I use maybe 10 or 12 IF statements with comparison for 32 bit values. How do I optimize that? 

 

IF(VOLTAGE >= TO_SIGNED(2400,32)) THEN 

ERROR<= ERROR OR X"0A" 

END IF; 

 

IF(CURRENT >= TO_SIGNED(1400,32)) THEN 

ERROR<= ERROR OR X"10" 

END IF; 

 

and so on.. 

 

Thanks
0 Kudos
Altera_Forum
Honored Contributor II
428 Views

I have seen somewhere that I should save the result of the comparison to a signal and then make the IF or CASE check. So as pipeling the comparison with the decision. Is it the way to go?

0 Kudos
Altera_Forum
Honored Contributor II
428 Views

Thess are parallel statements, no priority involved if the results are ORed. A case construct won't represent the function.  

If pipelining is necessary depends on the timing constraints, you should try.
0 Kudos
Reply