Will It Optimize? See how well you can anticipate gcc's optimizer (2010)
ridiculousfish.com
Will It Optimize? See how well you can anticipate gcc's optimizer (2010)
1–10 of 34 posts
Re: Will It Optimize? See how well you can anticipate gcc's optimizer (2010)
#2"Once, I was interested in finding out how a C compiler would handle the div instruction. So, I opened up my editor, and wrote this C program:
void main () {
printf("%f", 14.0 / 3.0);
}
I compiled it, and took a look at the generated assembly. How many div instructions do you think it used?[Various guesses, mostly "one."]
The correct answer is "zero." The compiler figured it out at compile time, and put a constant in instead. So, I decided to put it in a function:
float divide (float a, float b) {
return a / b;
}
void main () {
printf("%f", divide(a, b));
}
Guess what happened next.[Various guesses.]
The f--king compiler optimized it out again!"
Re: Will It Optimize? See how well you can anticipate gcc's optimizer (2010)
#3 uint32_t roll(uint32_t const x) {
return (x > 17);
}
I haven't managed to make GCC generate a non-constant roll (i.e. (x > (32-c)) ), probably because it couldn't infer that c is in the [0,31] range.Re: Will It Optimize? See how well you can anticipate gcc's optimizer (2010)
#4This reminds me of something my assembly instructor said in class once (paraphrased): "Once, I was interested in finding out how a C compiler would handle the div instruction. So, I opened up my editor, and wrote this C program: void main () { printf("%f", 14.0 / 3.0); } I compiled it, and took a look at the generated assembly. How many div instructions do you think it used? [Various guesses, mostly "one."] The corre…
Re: Will It Optimize? See how well you can anticipate gcc's optimizer (2010)
#5This reminds me of something my assembly instructor said in class once (paraphrased): "Once, I was interested in finding out how a C compiler would handle the div instruction. So, I opened up my editor, and wrote this C program: void main () { printf("%f", 14.0 / 3.0); } I compiled it, and took a look at the generated assembly. How many div instructions do you think it used? [Various guesses, mostly "one."] The corre…
I don't spend a lot of time looking at complier outputs, but the optimization is quite obvious in the first case. I don't have a clue how it would optimize it out in the second case though...
Re: Will It Optimize? See how well you can anticipate gcc's optimizer (2010)
#6This reminds me of something my assembly instructor said in class once (paraphrased): "Once, I was interested in finding out how a C compiler would handle the div instruction. So, I opened up my editor, and wrote this C program: void main () { printf("%f", 14.0 / 3.0); } I compiled it, and took a look at the generated assembly. How many div instructions do you think it used? [Various guesses, mostly "one."] The corre…
I don't spend a lot of time looking at complier outputs, but the optimization is quite obvious in the first case. I don't have a clue how it would optimize it out in the second case though...
Re: Will It Optimize? See how well you can anticipate gcc's optimizer (2010)
#7Re: Will It Optimize? See how well you can anticipate gcc's optimizer (2010)
#8I am surprised at number 3, I would have thought that it would be optimised to x Edit: No. 5 explains why.
Re: Will It Optimize? See how well you can anticipate gcc's optimizer (2010)
#9Re: Will It Optimize? See how well you can anticipate gcc's optimizer (2010)
#10I did some similar experiments myself a long time ago. Something mildly surprising for me at that time was that this got optimized to a single roll instruction: uint32_t roll(uint32_t const x) { return (x > 17); } I haven't managed to make GCC generate a non-constant roll (i.e. (x > (32-c)) ), probably because it couldn't infer that c is in the [0,31] range.