bergo
bergo

Reputation: 163

switch case with two variables

I'm seeing in a sample code of TI the following switch case, I was wondering what is the meaning of the second variable that the switch argument receives,

__interrupt void Timer_A(void)
{
  switch (TAIV, 10)        // Efficient switch-implementation
  {
    case  2:  break;                        // TACCR1 not used
    case  4:  break;                        // TACCR2 not used
    case 10:  P1OUT ^= 0x01;                // overflow
              break;
  }
}

my guess is that there is a priority to first check the case value of "10" but I'm not really sure.

Upvotes: 3

Views: 1841

Answers (3)

ouah
ouah

Reputation: 145829

I think there is an intrinsic call missing:

switch (__even_in_range(TAIV, 10))
{

__even_in_range is an intrinsic used for MSP-430 mcu. It is provided by both TI compiler cl430 for MSP-430 and IAR compiler for MSP-430. It requires two arguments, the interrupt vector register and the last value in the allowed range, which in this example is 10. The intrinsic is used to help the compiler to generate efficient code.

See IAR for MSP-430 compiler documentation which gives this example in page 25:

#pragma vector=TIMERA1_VECTOR
__interrupt void Timer_A1_ISR(void)
{
    switch (__even_in_range(TAIV, 10))
    {
    case 2: P1POUT ˆ= 0x04;
    break;
    case 4: P1POUT ˆ= 0x02;
    break;
    case 10: P1POUT ˆ= 0x01;
    break;
    }
}

and says:

The effect of the intrinsic function is that the generated code can only handle even values within the given range, which is exactly what is required in this case as the interrupt vector register for Timer A can only be 0, 2, 4, 6, 8, or 10.

The description of __even_in_range in page 237 says:

Instructs the compiler to rely on the specified value being even and within the specified range. The code will be generated accordingly and will only work if the requirement is fulfilled

Upvotes: 8

Bathsheba
Bathsheba

Reputation: 234655

There is no multi-argument switch in C. An errant refactorer has used the comma operator, which, given its left to right associativity, yields an expression equal to 10.

Your code reduces to switch (10), notwithstanding that TAIV is evaluated and might be doing something useful (a macro perhaps).

Upvotes: 2

devnull
devnull

Reputation: 123458

Comma operator back in action.

It boils down to case 10.

Upvotes: 1

Related Questions