From owner-freebsd-questions@FreeBSD.ORG Tue Apr 11 23:35:16 2006 Return-Path: X-Original-To: freebsd-questions@freebsd.org Delivered-To: freebsd-questions@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 798E116A400; Tue, 11 Apr 2006 23:35:16 +0000 (UTC) (envelope-from nb_root@videotron.ca) Received: from relais.videotron.ca (relais.videotron.ca [24.201.245.36]) by mx1.FreeBSD.org (Postfix) with ESMTP id 1E14743D45; Tue, 11 Apr 2006 23:35:16 +0000 (GMT) (envelope-from nb_root@videotron.ca) Received: from clk01a ([66.130.198.54]) by VL-MO-MR002.ip.videotron.ca (Sun Java System Messaging Server 6.2-2.05 (built Apr 28 2005)) with ESMTP id <0IXL00E4Z06R4Y00@VL-MO-MR002.ip.videotron.ca>; Tue, 11 Apr 2006 19:35:15 -0400 (EDT) Date: Tue, 11 Apr 2006 19:35:13 -0400 From: Nicolas Blais In-reply-to: <443C3AA7.4020500@freebsd.org> To: freebsd-questions@freebsd.org Message-id: <200604111935.14023.nb_root@videotron.ca> MIME-version: 1.0 Content-type: multipart/signed; boundary=nextPart3634917.4F0UrlmBaY; protocol="application/pgp-signature"; micalg=pgp-sha1 Content-transfer-encoding: 7bit References: <6a56d69c0604111554o587ce2c5ha1ff4ea20bbab0a4@mail.gmail.com> <443C3AA7.4020500@freebsd.org> User-Agent: KMail/1.9.1 Cc: Andy Reitz , Jonathan Herriott , Colin Percival Subject: Re: pow function working unexpectedly X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 11 Apr 2006 23:35:16 -0000 --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--