Floating point operation result is (very) incorrect
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
GNU Arm Embedded Toolchain |
New
|
Undecided
|
Unassigned |
Bug Description
We have a function that boils down to:
int Test01( int n )
{
float x = n;
x *= 0.0000953674f;
return (int) x;
}
When compiled with -O3 option, such function return results that are thousand times larger and often have wrong sign. For example, when I pass 14096 as an input argument, the output suppose to be 1, but instead the function returns 43770.
If, however, I replace it with
volatile float g = 0.0000953674f;
int Test02( int n )
{
float x = n;
x *= g;
return (int) x;
}
or remove optimization, the results become correct.
The bug happens only with more sophisticated function than Test01() example above, but it is unrealistic to provide our entire source tree to reproduce it outside of our company. But we can do various tests that would be required to fix it.
Compiler Version:
.ident "GCC: (GNU Arm Embedded Toolchain 10.3-2021.10) 10.3.1 20210824 (release)"
Downloaded as binary from developer.arm.com. Host computer: Windows 10.
Options:
\ARMTools\
-fno-exceptions
-fno-rtti
-fno-threadsafe
-fno-use-cxa-atexit
-fno-math-errno,
-mcpu=cortex-
-mno-unaligned-
-O3
Target MCU: STM32L496 (Cortex M4)