pow function working unexpectedly

Colin Percival cperciva at freebsd.org
Tue Apr 11 23:24:54 UTC 2006


Andy Reitz wrote:
> So, clearly, something is optimizing the pow() function away when the
> arguments are hard-coded lvalues, instead of varibles.
> 
> Now, what that thing *is*, I don't know.

The C compiler precomputes constant expressions; your "pow(2,3)" is
being rewritten to "8" by the compiler.  Similarly, if you write
"1 + 2 / 3 + 4 * 5 - 6", the C compiler will turn this into "15"
rather than producing a series of instructions which computes the
expression.

When you reference variables, this optimization isn't possible, since
those variables might be modified before you reach the line where
they are used.  (Obviously this doesn't happen in your program, but
the compiler isn't smart enough to figure that out.)

Colin Percival


More information about the freebsd-questions mailing list