This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Incorrect compile for convert float to short/int16

Hi,
the following code for STM32F4 (Cortex-M4):

float fZero= 0.;
float fInfinity;
short sTestPlus, sTestNeg;
int main( void){
  fInfinity= 1.f/fZero;
  sTestPlus= fInfinity;
  fInfinity= -1.f/fZero;
  sTestNeg= fInfinity;
  while(1);
}

should result in the values sTestPlus= 0x7FFF and sTestNeg= 0x8000. Instead it produces 0xFFFF and 0x0000.

The reason is, that the compiler uses the signed 32bit convert (leading to the correct result 0x7FFFFFFF and 0x80000000), and then just cuts out the lower short, which is really poor.

As I understand, it is no problem with Cortex M4 to do float to signed16 using the VCVT command in a more sophisticated way (specifying 16bit).

Is there a way to get this done?

Parents
  • The infinite value is not the crucial thing here

    What gave you the impression that anybody thought it was?

    This is neither correct nor makes much sense.

    For what feels like the 100th time: you're wrong. It is correct, because your code is causing undefined behaviour. Which means that every possible outcome is correct. It's impossible to be incorrect here.

    The thing that makes no sense here is your insistence to rate your personal opinion above everything else, including the definition of the programming language. And this is not the first time you behaved like that. Are you really that incapable of gathering insight from earlier experience?

Reply
  • The infinite value is not the crucial thing here

    What gave you the impression that anybody thought it was?

    This is neither correct nor makes much sense.

    For what feels like the 100th time: you're wrong. It is correct, because your code is causing undefined behaviour. Which means that every possible outcome is correct. It's impossible to be incorrect here.

    The thing that makes no sense here is your insistence to rate your personal opinion above everything else, including the definition of the programming language. And this is not the first time you behaved like that. Are you really that incapable of gathering insight from earlier experience?

Children
No data