Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
11 years ago

Optimization: If vs CASE, which one?

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

4 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    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.
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    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
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    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?

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    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.