Answer by Charles for Why doesn't GCC optimize a*a*a*a*a*a to (a*a*a)*(a*a*a)?
gcc actually can do this optimization, even for floating-point numbers. For example, double foo(double a) { return a*a*a*a*a*a; } becomes foo(double): mulsd %xmm0, %xmm0 movapd %xmm0, %xmm1 mulsd...
View ArticleAnswer by GameDeveloper for Why doesn't GCC optimize a*a*a*a*a*a to...
Library functions like "pow" are usually carefully crafted to yield the minimum possible error (in generic case). This is usually achieved approximating functions with splines (according to Pascal's...
View ArticleAnswer by vinc17 for Why doesn't GCC optimize a*a*a*a*a*a to (a*a*a)*(a*a*a)?
No posters have mentioned the contraction of floating expressions yet (ISO C standard, 6.5p8 and 7.12.2). If the FP_CONTRACT pragma is set to ON, the compiler is allowed to regard an expression such as...
View ArticleAnswer by picomancer for Why doesn't GCC optimize a*a*a*a*a*a to...
GCC does actually optimize a*a*a*a*a*a to (a*a*a)*(a*a*a) when a is an integer. I tried with this command: $ echo 'int f(int x) { return x*x*x*x*x*x; }' | gcc -o - -O2 -S -masm=intel -x c - There are a...
View ArticleAnswer by Rastaban for Why doesn't GCC optimize a*a*a*a*a*a to (a*a*a)*(a*a*a)?
There are already a few good answers to this question, but for the sake of completeness I wanted to point out that the applicable section of the C standard is 5.1.2.2.3/15 (which is the same as section...
View ArticleAnswer by Bjorn for Why doesn't GCC optimize a*a*a*a*a*a to (a*a*a)*(a*a*a)?
As Lambdageek pointed out float multiplication is not associative and you can get less accuracy, but also when get better accuracy you can argue against optimisation, because you want a deterministic...
View ArticleAnswer by Szabolcs for Why doesn't GCC optimize a*a*a*a*a*a to (a*a*a)*(a*a*a)?
Fortran (designed for scientific computing) has a built-in power operator, and as far as I know Fortran compilers will commonly optimize raising to integer powers in a similar fashion to what you...
View ArticleAnswer by user811773 for Why doesn't GCC optimize a*a*a*a*a*a to...
Because a 32-bit floating-point number - such as 1.024 - is not 1.024. In a computer, 1.024 is an interval: from (1.024-e) to (1.024+e), where "e" represents an error. Some people fail to realize this...
View ArticleAnswer by sanjoyd for Why doesn't GCC optimize a*a*a*a*a*a to (a*a*a)*(a*a*a)?
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...
View ArticleAnswer by Stephen Canon for Why doesn't GCC optimize a*a*a*a*a*a to...
Lambdageek correctly points out that because associativity does not hold for floating-point numbers, the "optimization" of a*a*a*a*a*a to (a*a*a)*(a*a*a) may change the value. This is why it is...
View ArticleAnswer by Lambdageek for Why doesn't GCC optimize a*a*a*a*a*a to...
Because Floating Point Math is not Associative. The way you group the operands in floating point multiplication has an effect on the numerical accuracy of the answer. As a result, most compilers are...
View ArticleAnswer by Mark Ransom for Why doesn't GCC optimize a*a*a*a*a*a to...
I would not have expected this case to be optimized at all. It can't be very often where an expression contains subexpressions that can be regrouped to remove entire operations. I would expect compiler...
View ArticleWhy doesn't GCC optimize a*a*a*a*a*a to (a*a*a)*(a*a*a)?
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*a, but the call pow(a,6) is not optimized and...
View Article