This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Compiler/TMS320F280049: Switch tables generation

Part Number: TMS320F280049


Tool/software: TI C/C++ Compiler

Hi all,

According to Compiler User’s Guide, the –unified memory(-mt) option allows more efficient data memory instructions to be used to access switch tables, I tried but seems that Compiler doesn’t generate tables.

If we do a switch-case C statement, enable –unified memory and compile the C code, is there anything I should take care to generate switch tables? Is there any use case limitation of this? If Compiler generates switch table, we should be able to see .switch section in .map file, is this correct?

Regards,

Luke

  • Whether switch tables, which appear in the .switch section, get used is not influenced by the option --unified_memory.  It is influenced by the density of the case values in the switch statement.  Here is an example of a switch statement with high density ...

       switch (arg)
       {
          case 0 : call0();
                   break;
    
          case 1 : call1();
                   break;
    
          case 2 : call2();
                   break;
    
          /* and so on */
    
          case 25 : call25();
                    break;
       }

    Here is an example of a switch statement with low density ...

       switch (arg)
       {
          case 0 :    call0();
                      break;
    
          case 100 :  call100();
                      break;
    
          case 200 :  call200();
                      break;
    
          /* and so on */
    
          case 2500 : call2500();
                    break;
       }

    A switch statement with a high density of case values is usually implemented with table lookup, and the table is in the .switch section.  If you also use --unified_memory, then the instructions which reference the table are more efficient.

    Thanks and regards,

    -George

  • George,

    Do you mean customers should use lookup table for switch statement so that compiler builds the table in the .switch section?

    Could you advise an example on this use case please?

    Regards,

    Luke

  • Luke Chen said:
    Do you mean customers should use lookup table for switch statement so that compiler builds the table in the .switch section?

    No.  Write the switch statement however you want.  But understand that when the compiler generates the code for that switch statement, a table may or may not be used.  If a table is used, it appears in the .switch section.

    Thanks and regards,

    -George

  • George,

    I am looking for the coding style or method so that compiler will always using lookup tables for switch statement, is it possible?

    Thanks and regards,

    Luke

  • Luke Chen said:
    I am looking for the coding style or method so that compiler will always using lookup tables for switch statement, is it possible?

    Always?  No.  Sometimes?  Yes.

    As for an example ... Please see the C code in my first  post.  The first switch statement is implemented with a lookup table.  The second is not.  The difference is due to the case values inside the switch statement.  If they are dense, which means the values are all close to each other, then a table is used.  If they are not dense, which means the values are far apart, then a table is not used.

    In practice, it is rare to be able to choose the case values inside the switch statement so as to make them dense, and thus increase the likelihood a lookup table is used.   But, if you can influence the values of the case statements, then try to make them dense.

    To be clear, there is no method by which you can force a lookup table to be used.

    Thanks and regards,

    -George