Floating point optimization during GCC compilation

I am developing an AVR platform and I have a question. I do not want the floating point library to be associated with my code, but I like the concept of having analog values ​​in the range 0.0 ... 1.0 instead of 0 ... 255 and 0 ... 1023, depending on the even, regardless of whether I use whether I port as input or as output.

So, I decided to multiply the arguments of the I / O function by 1023.0 and 255.0, respectively. Now, my question is: if I implement a division like this:

#define analog_out(port, bit) _analog_out(port, ((uint8_t)((bit) * 255.0)))

will GCC (with the -O3 flag enabled) optimize compile-time floating-point multiplication known at compile time and converted to an integral type into whole operations? (I know that when using these macros with inconsistent arguments, optimization is impossible, I just want to know if this will be done in another case.)

+5
source share
2 answers

GCC should always do a permanent fold if you pass a bit as a numeric literal. If you want the compiler to ensure consistency, you could get away from something like this:

#define force_const(x) (__builtin_choose_expr(__builtin_constant_p(x), (x), (void)0))
#define analog_out(port, bit) _analog_out(port, force_const((uint8_t)((bit) * 255.0)))
+3
source

, , gcc -O2 .
- .

, . , , .

+2

All Articles