[Bug middle-end/57073] __builtin_powif (-1.0, k) should be optimized to "1.0 - 2.0 * (K%2)"

tkoenig at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Sun Apr 28 09:05:00 GMT 2013


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57073

--- Comment #3 from Thomas Koenig <tkoenig at gcc dot gnu.org> 2013-04-28 09:05:31 UTC ---
(In reply to comment #1)
> Modulo is an integer only operation, so I'd say you want
> (k & 1) ? -1.0 : 1.0
> instead, converting (k & 1) into floating point and multiplying and subtracting
> it or (k & 1) << 1 into floating point and subtracting it is is likely going to
> be more expensive, though of course it should be benchmarked.

You're right, it is much faster:

g25@linux-fd1f:~/Krempel/P2> cat a.c
#include <stdio.h>

volatile float b;

int main()
{
  int i;
  for (i=0; i<1000000000; i++)
    b = (i & 1) ? -1.0 : 1.0;

  printf("%f\n",b);
}
ig25@linux-fd1f:~/Krempel/P2> gcc -O a.c && time ./a.out
-1.000000

real    0m1.948s
user    0m1.946s
sys     0m0.000s
ig25@linux-fd1f:~/Krempel/P2> cat b.c
#include <stdio.h>

volatile float b;

int main()
{
  int i;
  for (i=0; i<1000000000; i++)
    b = 1.0 - ((i&1)<<1);

  printf("%f\n",b);
}
ig25@linux-fd1f:~/Krempel/P2> gcc -O b.c && time ./a.out
-1.000000

real    0m7.059s
user    0m7.053s
sys     0m0.001s



More information about the Gcc-bugs mailing list