Why doesn't GCC optimize a*a*a*a*a*a to (a*a*a)*(a*a*a)?

前端 未结 12 1682
孤街浪徒
孤街浪徒 2020-11-22 08:41

I am doing some numerical optimization on a scientific application. One thing I noticed is that GCC will optimize the call pow(a,2) by compiling it into a

12条回答
  •  温柔的废话
    2020-11-22 09:15

    Another similar case: most compilers won't optimize a + b + c + d to (a + b) + (c + d) (this is an optimization since the second expression can be pipelined better) and evaluate it as given (i.e. as (((a + b) + c) + d)). This too is because of corner cases:

    float a = 1e35, b = 1e-5, c = -1e35, d = 1e-5;
    printf("%e %e\n", a + b + c + d, (a + b) + (c + d));
    

    This outputs 1.000000e-05 0.000000e+00

提交回复
热议问题