Problem
I am working with flash memory optimization of STM32F051. It's revealed, that conversion between float
and int
types consumes a lot of flash.
Digging into this, it turned out that the conversion to int
takes around 200 bytes of flash memory; while the conversion to unsigned int
takes around 1500 bytes!
It’s known, that both int
and unsigned int
differ only by the interpretation of the ‘sign’ bit, so such behavior – is a great mystery for me.
Note: Performing the 2-stage conversion float
-> int
-> unsigned int
also consumes only around 200 bytes.
Questions
Analyzing that, I have such questions:
1) What is a mechanism of the conversion of float
to unsigned int
. Why it takes so many memory space, when in the same time conversion float
->int
->unsigned int
takes so little memory? Maybe it’s connected with IEEE 754 standard?
2) Are there any problems expected when the conversion float
->int
->unsigned int
is used instead of a direct float
->int
?
3) Are there any methods to wrap float
-> unsigned int
conversion keeping the low memory footprint?
Note: The familiar question has been already asked here (Trying to understand how the casting/conversion is done by compiler,e.g., when cast from float to int), but still there is no clear answer and my question is about the memory usage.
Technical data
- Compiler: ARM-NONE-EABI-GCC (gcc version 4.9.3 20141119 (release))
- MCU: STM32F051
- MCU's core: 32 bit ARM CORTEX-M0
Code example
float
->int
(~200 bytes of flash)int main() { volatile float f; volatile int i; i = f; return 0; }
float
->unsigned int
(~1500 bytes! of flash)int main() { volatile float f; volatile unsigned int ui; ui = f; return 0; }
float
->int
->unsigned int
(~200 bytes of flash)int main() { volatile float f; volatile int i; volatile unsigned int ui; i = f; ui = i; return 0; }