Posted Apr 7, 2013 18:11 UTC (Sun) by mathstuf (subscriber, #69389)
[Link]
GCC does have a pure attribute, but it's not verified that you're using it properly and no one uses it anyways…
Rust
Posted Apr 8, 2013 8:03 UTC (Mon) by nix (subscriber, #2304)
[Link]
It does get used for functions often used in very tight inner loops. Just looking at glibc, strcmp() is declared pure, for example, as are many related functions.
Rust
Posted Apr 7, 2013 21:50 UTC (Sun) by HelloWorld (guest, #56129)
[Link]
The restrict keyword, by itself, does not add some opportunities for loop unrollings where aliased things are used inside the loops.
I don't understand that, can you give an example?
The lack of defining pure functions, without side-effects, also elide some opportunities for repeated code removal.
Modern optimising compilers try to figure out whether a function is pure and optimise accordingly. Example:
#include <stdio.h>
int sq(int i) {
return i*i;
}
int main(int argc, char **argv) {
int i = strtol(argv[1], 0, 0);
printf("%d\n", sq(i) * sq(i));
return 0;
}
Compiled with gcc -O3 -fno-inline-small-functions -S -masm=intel (gcc 4.7.2) this yields
So gcc sees that sq is a pure function and only calls it once, even though it's called twice in the source code. Granted, this isn't quite the same as in Fortran as the compiler needs to be able to see the definition of the relevant function for this to work. But due to link-time optimisation this isn't as big a limitation as it used to be. I'm not convinced the Fortran way offers significant benefits in the real world.