-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
sum.c is erroneous in the presence of compiler optimization #2
Comments
I see two possible clean solutions:
|
Thanks for reporting this! This was originally tested with |
On my machine (OSX 10.11)
I would suggest to use |
radarek wrote:
I disagree. At -O0, gcc tends to generate hugely inefficient code. Benchmarking that tells you nothing about the speed you can expect from a C program. I suggest the following: #include <stdlib.h>
#include <stdio.h>
// Number to guess: How many iterations of
// this loop can we go through in a second?
int main(int argc, char **argv) {
unsigned int NUMBER, i, s;
NUMBER = atoi(argv[1]);
for (s = i = 0; i < NUMBER; ++i) {
s += i;
}
printf("%u\n", s);
return 0;
} Notice three changes:
|
There are two problems with sum.c as written: since the sum,
s
, is not used, any optimizing compiler will remove the loop entirely; and even if it is used, at least GCC and Clang are able to determine that the loop will result ins == NUMBER
and remove it anyway.It's hard to come up with a simple alternate solution because compilers are pretty tricky with these kinds of simple loops. One possibility is to make
s
volatile, but this will somewhat artificially constrain performance by making the CPU store each iteration to memory rather than keeping it in a register. Another is to addin the loop, to force the compiler to put the sum in some (single) register before executing an empty block of assembly.
The text was updated successfully, but these errors were encountered: