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/TMS320F28377S: Optimizer behavior on ternary operator and left shift

Part Number: TMS320F28377S


Tool/software: TI C/C++ Compiler

Hi TI

I recently wanted to assign some bits to a 32 bit unsigned long value this way and saw a strange behavior of the optimizer.

This code is reduced to show what I mean:

volatile char myGlobalVolatileCharIsOne = 1;

unsigned long myFunction(void)
{
  unsigned long Result = 0UL;
  myGlobalVolatileCharIsOne = 1; 
  Result |= ((myGlobalVolatileCharIsOne ? 1UL : 0UL) << 1);
  Result |= ((myGlobalVolatileCharIsOne ? 1UL : 0UL) << 16);
  return Result;
}

I did expect Result = 0x00010002.

With -Ooff I get Result = 0x00010002

With -O3 I get Result = 0x00000002

It seems somewhere to be related to the left shift. If I write the expression this way is works as expected:

  Result |= (myGlobalVolatileCharIsOne ? 0x00010000UL : 0UL);

This way is also ok:

  Result |= (myGlobalVolatileCharIsOne ? (1UL<<16)  : 0UL);

CGT 16.9.8 and CGT 18.1.3

Best regards

Roger