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

next in thread | previous in thread | raw e-mail | index | archive | help
--nextPart3634917.4F0UrlmBaY
Content-Type: text/plain;
  charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

On Tuesday 11 April 2006 19:24, Colin Percival wrote:
> 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

+1. The compiler sees constants differently from variables (which reside in=
 a=20
memory space). With the constant, the optimizer can rewrite the equation wi=
th=20
its answer.

Anyhow, it can't be seen as a bug because you should include the math libra=
ry=20
whenever you're using a function from it (see man pow). Therefore, if you=20
would have included the library in the first place, you would have never=20
found this 'oddity'.

Nicolas.

=2D-=20
=46reeBSD 7.0-CURRENT #1: Sat Apr  8 21:33:25 EDT 2006    =20
root@clk01a:/usr/obj/usr/src/sys/CLK01A=20
PGP? : http://www.clkroot.net/security/nb_root.asc

--nextPart3634917.4F0UrlmBaY
Content-Type: application/pgp-signature

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (FreeBSD)

iD8DBQBEPD0y4wTBlvcsbJURAnPgAJ9t6qoGsuSZQrmtVquKvBUf0C8t1QCgoc5x
PSSmS21cCmq02IVCjLdnRJM=
=/Zz/
-----END PGP SIGNATURE-----

--nextPart3634917.4F0UrlmBaY--



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