user529758
user529758

Reputation:

GCC compile-time floating point optimization

I'm developing for the AVR platform and I have a question. I don't want the floating point library to be linked with my code, but I like the concept of having analog values of the range 0.0 ... 1.0 instead of 0...255 and 0...1023, depending on even whether I'm using a port as an input or as an output.

So I decided to multiply the input/output functions' arguments by 1023.0 and 255.0, respecively. Now, my question is: if I implement the division like this:

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

will GCC (with the -O3 flag turned on) optimize the compile-time floating point multiplications, known at compile time and cast to an integral type, into integer operations? (I know that when using these macros with non-constant arguments, the optimization is not possible; I just want to know if it will be done in the other case.)

Upvotes: 5

Views: 2227

Answers (2)

fnl
fnl

Reputation: 2239

GCC should always do constant folding if you supply bit as a numeric literal. If you want the compiler enforce the constness, you could get away with 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)))

Upvotes: 3

ugoren
ugoren

Reputation: 16441

Generally, I think gcc -O2 will do all arithmetic on constants at compile time.
It won't convert it to integer arithmetic - just to a constant integer.

It may be dangerous to rely on, especially if other people maintain the code. A situation where passing a non-constant parameter to a macro results in an error isn't good.

Upvotes: 2

Related Questions