GCC does not optimize a*a*a*a*a*a to (a*a*a)*(a*a*a).

Asked 1 years ago, Updated 1 years ago, 107 views

As far as I know, the compiler optimizes power(a,2) to a*a pow(a,6) does not optimize, but calls the library function pow().

The C++ Intel Compiler optimizes you to 6 Why does the GCC C compiler optimize 2 and not optimize 6?

I was curious, so when I put a*a*a*a*a in GCC 4.5.1 with the option "-O3 -lm -funroll-loops -msse4" in the power(a,6) place, like this

movapd  %xmm14, %xmm13
mulsd   %xmm14, %xmm13
mulsd   %xmm14, %xmm13
mulsd   %xmm14, %xmm13
mulsd   %xmm14, %xmm13
mulsd   %xmm14, %xmm13

On the other hand, (a*a)*(a*a) ran much faster.

movapd  %xmm14, %xmm13
mulsd   %xmm14, %xmm13
mulsd   %xmm14, %xmm13
mulsd   %xmm13, %xmm13

Why isn't the compiler optimized?

floating-point c optimization gcc assembly

2022-09-22 22:22

1 Answers

Floating point operations do not have a combined law in terms of computer mathematics.

Therefore, the compiler does not optimize because a*a*a*a*a and (a*a)*(a*a) can produce different results.

I tried it on my computer

int main(){
    float a = 3.9989391;
    printf("%f\n", a*a*a*a*a*a);
    printf("%f\n", (a*a*a)*(a*a*a));
}

:

4089.485840
4089.485596

They produce different results.

Option to compiler if slight errors are allowed to speed up You can give -fasciative-math or -fast-math.


2022-09-22 22:22

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.