We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
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?
This would be the optimum disassembly - using the VCVT command with S16 ... VCVT.S16.F32 S0, S0
The manual says there is no such instruction. See for yourself:
infocenter.arm.com/.../index.jsp
However, the manual does say that there is an instruction to convert from F32 to fixed-point S16:
Perhaps, this instruction could be used to convert to an S16 integer?
VCVT.S16.F32 S0, S0, #0
I'm not sure since I haven't done much fixed-point math recently.
Yes, I assume the 2nd version of VCVT should work.
(But I did not test in assembly myself).
further of course I would suggest to fix the problem, that "i=f" produces a wrong conversion for int8/int16/...
So how many more times will you have to be told that that is no problem, because there is no wrong conversion in those cases you tested, before you finally get it?
The only wrong thing there is your expectation about what that code should do.