Floating point operation result is (very) incorrect

Bug #2066549 reported by Ivan Stewart
6
This bug affects 1 person
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\bin\arm-none-eabi-gcc.exe,
-fno-exceptions,-Wfatal-errors,
-fno-rtti
-fno-threadsafe-statics
-fno-use-cxa-atexit
-fno-math-errno,
-mcpu=cortex-m4,-mfpu=fpv4-sp-d16,-mfloat-abi=hard,-mthumb,--specs=nano.specs,
-mno-unaligned-access,
-O3

Target MCU: STM32L496 (Cortex M4)

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.