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: [email protected]
ReportedBy: [email protected]
CC: [email protected]
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