From owner-freebsd-questions@FreeBSD.ORG Tue Apr 11 23:24:54 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 06B0116A407 for ; Tue, 11 Apr 2006 23:24:54 +0000 (UTC) (envelope-from cperciva@freebsd.org) Received: from pd5mo2so.prod.shaw.ca (shawidc-mo1.cg.shawcable.net [24.71.223.10]) by mx1.FreeBSD.org (Postfix) with ESMTP id 9B36143D67 for ; Tue, 11 Apr 2006 23:24:46 +0000 (GMT) (envelope-from cperciva@freebsd.org) Received: from pd4mr7so.prod.shaw.ca (pd4mr7so-qfe3.prod.shaw.ca [10.0.141.84]) by l-daemon (Sun ONE Messaging Server 6.0 HotFix 1.01 (built Mar 15 2004)) with ESMTP id <0IXK00AFIZOO7I10@l-daemon> for freebsd-questions@freebsd.org; Tue, 11 Apr 2006 17:24:24 -0600 (MDT) Received: from pn2ml2so.prod.shaw.ca ([10.0.121.146]) by pd4mr7so.prod.shaw.ca (Sun ONE Messaging Server 6.0 HotFix 1.01 (built Mar 15 2004)) with ESMTP id <0IXK00K4JZOO6WF0@pd4mr7so.prod.shaw.ca> for freebsd-questions@freebsd.org; Tue, 11 Apr 2006 17:24:24 -0600 (MDT) Received: from [192.168.0.60] ([24.82.18.31]) by l-daemon (Sun ONE Messaging Server 6.0 HotFix 1.01 (built Mar 15 2004)) with ESMTP id <0IXK00JVNZONQW00@l-daemon> for freebsd-questions@freebsd.org; Tue, 11 Apr 2006 17:24:24 -0600 (MDT) Date: Tue, 11 Apr 2006 16:24:23 -0700 From: Colin Percival In-reply-to: To: Andy Reitz Message-id: <443C3AA7.4020500@freebsd.org> MIME-version: 1.0 Content-type: text/plain; charset=ISO-8859-1 Content-transfer-encoding: 7bit X-Enigmail-Version: 0.94.0.0 References: <6a56d69c0604111554o587ce2c5ha1ff4ea20bbab0a4@mail.gmail.com> <200604111857.43171.nb_root@videotron.ca> <6a56d69c0604111607l5fba5939pfc6461a99a2ceab3@mail.gmail.com> User-Agent: Thunderbird 1.5 (X11/20060112) Cc: Jonathan Herriott , freebsd-questions@freebsd.org, Nicolas Blais 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:24:54 -0000 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