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.

__even_in_range

Where can I find definition and explanation of what __even_in_range() does?  I looked all over the place, I use CCS5.

  • TIMOFEY SITNIKOV said:

    Where can I find definition and explanation of what __even_in_range() does?  I looked all over the place, I use CCS5.

    Timofey,

    You can find this definition in the MSP430 Compiler User's Guide.  In version 4.0 of the guide it is described in Section 5.9.22.  When running CCS5 go to the Help menu and select Help Contents and do a search.

    Here is a quote from the user guide "The __even_in_range intrinsic provides a hint to the compiler when generating switch statements for interrupt vector routines."

    Cheers,

    Darren

  • I tried searching, it says "Nothing found".  Frustrating...

  • Timofey,

    I copied section 5.9.22 from the Compiler User's Guide for you.  See below.

    Cheers,

    Darren

    5.9.22. The vector Pragma

    The vector pragma indicates that the function that follows is to be used as the interrupt vector routine for the listed vectors. The syntax of the pragma is:

    #pragma vector =vec1[,vec2,vec3, ...]

    The vector pragma requires linker command file support. The command file must specify output sections for each interrupt vector of the form .intxx where xx is the number of the interrupt vector. The output sections mut map to the physical memory location of the appropriate interrupt vector. The standard linker command files are set up to handle the vector pragma.

    The __even_in_range intrinsic provides a hint to the compiler when generating switch statements for interrupt vector routines. The intrinsic is usually used as follows:

    switch (__even_in_range(x, NUM)){ ... }

    The __even_in_range intrinsic returns the value x to control the switch statement, but also tells the compiler that x must be an even value in the range of 0 to NUM, inclusive.

  • Hi Timofey,

    the full title of that document is "MSP430 Optimizing C/C++ Compiler v 4.1 User's Guide (Rev. G)" for MSP430 compiler released at revision level 4.1.x. You can find it at TI's webside searching for literature number SLAU132G or here. The vector Pragma chapter noted by Darren is moved to 5.10.22 in that document.

    Best regards,
    Christian

  • In addition to the explanation found in the noted documents, it might be interesting to know what this intrinsic really does.

    Normally, a switch statement generates a structure like

    if (case1) [...]
    if (case2) [...]
    if (case3) [...]

    This means that he last case may have n comparisons and branches before it is handled.

    The __even_in_range() intrinsic tells the compiler that the expected argument is 1) always an even number and 2) in the range of x..n.
    So what?
    It allows the compiler to use a way more efficient method to branch to the different cases.

    Instead if chained comparisons, the compiler just adds teh argument to the program counter. And follows this instruction with a table of jump instrucitons (which take 2 bytes each). As a result of this add, the program counter directly lands on the jump instruciton that jumps to the case code.
    For the first case, this is as fast (and big) as comparing and branching, but all other cases take exactly the same time instead of an increasing compare/branch time for each case. With one exception: the last case (highest value) is even shorter, as no jump instruction is needed.

    This intrinsic works best in conjunction with the IV registers, as these registers always return an even value in a known range. And, (surprise!) the highest priority interrupt has the largest value in the IV registers, so it is executed fastest.

    BTW: it is not necessary to write an explicit case entry for each of the possible cases (if they aren't needed or handled). A single default case will catch them all. However, the code will fail (likely crash or at least malfunciton) at runtime if the argument is for some reason larger than the specified maximum.

  • Christian

    Thank you so much, this document answers lots of my questions.

**Attention** This is a public forum