From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 24411 invoked by alias); 25 Apr 2013 17:59:34 -0000 Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org Received: (qmail 24376 invoked by uid 48); 25 Apr 2013 17:59:31 -0000 From: "burnus at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug fortran/57071] New: Optimize (-1)**k to 1 - 2 * mod(K, 2) Date: Thu, 25 Apr 2013 17:59:00 -0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: fortran X-Bugzilla-Keywords: missed-optimization X-Bugzilla-Severity: normal X-Bugzilla-Who: burnus at gcc dot gnu.org X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Changed-Fields: Message-ID: X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated Content-Type: text/plain; charset="UTF-8" MIME-Version: 1.0 X-SW-Source: 2013-04/txt/msg02115.txt.bz2 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57071 Bug #: 57071 Summary: Optimize (-1)**k to 1 - 2 * mod(K, 2) Classification: Unclassified Product: gcc Version: 4.9.0 Status: UNCONFIRMED Keywords: missed-optimization Severity: normal Priority: P3 Component: fortran AssignedTo: unassigned@gcc.gnu.org ReportedBy: burnus@gcc.gnu.org CC: tkoenig@gcc.gnu.org Motivated by https://groups.google.com/forum/?fromgroups=#!topic/comp.lang.fortran/60jYF5XEY3c The code (k is an integer): (-1)**k and (-1.0)**k can be rather common in numeric code. The former is converted into _gfortran_pow_i4_i4 (-1, *k); the latter is __builtin_powif (-1.0e+0, *k); However, for (-1)**k, the result is simply 1 is k is even and -1 if it is odd, or in other words: 1 - 2 * mod(K, 2) integer function f(k) f = (-1)**k ! Or: (-1.0)**k end