The iC960 R3.0 and R3.5 compilers generate code for switch statements in one of two ways. Depending on how the switch statement is designed, the compiler will either generate a series of compares with a corresponding branch or it will generate a jump table through which it will branch. The compare-with-branch approach is optimal for application size when the switch statement either has only a few cases or has a very wide range of cases compared to the quantity of cases. The jump table mechanism provides for faster execution and application space savings when numerous cases are involved and the range of cases is narrow.
The algorithm
if (( #cases > 10 ) && ( range / #cases <= 3 )) generate_jump_table();
describes the decision process of the compiler as to whether a jump table should be generated for a given switch statement, where "range" is defined as the delta between the minimum and maximum case values.
For example, if a given switch statement had 12 cases with a minimum case value of 2 and a maximum case value of 20, the range would be 18. Therefore the calculation "range / #cases" yields a "sparsity" value of 1-1/2, which is less than or equal to 3 so a jump table would be generated for this switch statement.
However, if the switch statement still had 12 cases but a maximum case value of 50, the sparsity would then be 4 and a jump table would not be generated. A jump table must contain the same number of elements as the range; for this example it would contain 48 elements had it been generated, with only 12 elements used. The size of the application would have been larger than with 12 compares-with-branches.
In some instances, especially when your application contains many switch statements where each switch statement has numerous (but <= 10) cases and the sparsity is <= 3, the compare-with-branch code is not optimal for execution performance.
When trying to get your application to execute a little faster, you should experiment with adding dummy cases to selected switch statements to force the compiler to generate a jump table.
Legal Stuff © 1997 Intel Corporation