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.

Wrong result when casting int16_t to float on TMS320F28069



I'm having a very fundamental problem with casting integer values to floats. The following line of code is failing

f_val = static_cast<float>(i_val);

Where f_val is a float and i_val is a int16_t

The issue is that the result of the cast is always 0.0 (i.e. f_val is always zero regardless of the value of i_val). I have of course also tried old style (float)i casting and just implicitly casting.

To make things more interesting this was working just fine with compiler versions up to 6.4.6. Have not tested all versions of the compiler but it is failing in 15.12.1, 15.12.3, and 16.9.0.

  • If I move this line of code into a function it appears to fix the problem:

    static float Int32ToFloat(int16_t int16) {
      return static_cast<float>(int16);
    }

    ...and the generated assembly seems to be more reasonable:
    Int32ToFloat(int):
    00a008: 3B01 SETC SXM
    46 return static_cast<float>(int16); // result;
    00a009: 85A9 MOV ACC, @AL
    00a00a: BDA90F12 MOV32 R0H, @ACC
    00a00c: 7700 NOP
    00a00d: 7700 NOP
    00a00e: 7700 NOP
    00a00f: 7700 NOP
    00a010: E6890000 I32TOF32 R0H, R0H
    00a012: 0006 LRETR

    When called outside of this function I get the zero result and the assembly is:

    280 tmp = static_cast<float>(raw);
    00a0de: E2030442 MOV32 *-SP[2], R4H

    This seems like very strange behavior to me.

  • I'm sorry, I cannot reproduce this problem. Could you post a compilable test case that demonstrates the problem, along with the compiler options you used?