Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 11 Apr 2006 16:24:23 -0700
From:      Colin Percival <cperciva@freebsd.org>
To:        Andy Reitz <reitz@eecs.cwru.edu>
Cc:        Jonathan Herriott <herriojr@gmail.com>, freebsd-questions@freebsd.org, Nicolas Blais <nb_root@videotron.ca>
Subject:   Re: pow function working unexpectedly
Message-ID:  <443C3AA7.4020500@freebsd.org>
In-Reply-To: <Pine.SOL.4.53.0604111911260.3684@bender>
References:  <6a56d69c0604111554o587ce2c5ha1ff4ea20bbab0a4@mail.gmail.com> <200604111857.43171.nb_root@videotron.ca> <6a56d69c0604111607l5fba5939pfc6461a99a2ceab3@mail.gmail.com> <Pine.SOL.4.53.0604111911260.3684@bender>

next in thread | previous in thread | raw e-mail | index | archive | help
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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?443C3AA7.4020500>