Skip to content

Instantly share code, notes, and snippets.

@jepler
Last active December 15, 2015 18:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jepler/5306888 to your computer and use it in GitHub Desktop.
Save jepler/5306888 to your computer and use it in GitHub Desktop.
surprising optimization difference (I'm not quite courageous enough to call it a compiler bug yet). Additionally compiling it with -ffast-math gives the same result via the incomprehensible sequence below gcc version info: Target: x86_64-linux-gnu gcc version 4.7.2 (Debian 4.7.2-5) The wrong result turns out to be dot / (sqrt(radius) * radius * …
jepler@babs:~$ gcc -Os rsq.c -lm && ./a.out
0.993271
jepler@babs:~$ gcc -Os -funsafe-math-optimizations rsq.c -lm && ./a.out
1.365475
000000000040057c <fn>:
40057c: f2 0f 51 d1 sqrtsd %xmm1,%xmm2
400580: f2 0f 59 c9 mulsd %xmm1,%xmm1
400584: f2 0f 59 ca mulsd %xmm2,%xmm1
400588: f2 0f 5e c1 divsd %xmm1,%xmm0
40058c: f2 0f 11 05 34 04 20 movsd %xmm0,0x200434(%rip) # 6009c8 <val>
400593: 00
400594: c3 retq
double val;
void fn(double dot, double radius) {
val = dot / (radius * radius);
}
#include <stdio.h>
int main() {
fn(.278102, 0.529137);
printf("%f\n", val);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment