Skip to content

Instantly share code, notes, and snippets.

@zed
Created March 5, 2011 20:02
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 zed/856669 to your computer and use it in GitHub Desktop.
Save zed/856669 to your computer and use it in GitHub Desktop.
what code gcc generates for `x/2` and `x*.5`
/*
http://stackoverflow.com/questions/895574/what-are-some-good-code-optimization-methods/1354688#1354688
USAGE:
$ gcc -O3 -msse3 -fomit-frame-pointer -c *.c && objdump -Mintel -S *.o
*/
int posx(unsigned int screen_width, unsigned int window_width) {
return (screen_width / 2) - (window_width / 2);
}
int posx05(unsigned int screen_width, unsigned int window_width) {
return (screen_width * 0.5) - (window_width * 0.5);
}
gcc -O3 -msse3 -fomit-frame-pointer -c *.c && objdump -Mintel -S *.o
posx.o: file format elf64-x86-64
Disassembly of section .text:
0000000000000000 <posx>:
0: 89 f8 mov eax,edi
2: d1 ee shr esi,1
4: d1 e8 shr eax,1
6: 29 f0 sub eax,esi
8: c3 ret
9: eb 05 jmp 10 <posx05>
b: 90 nop
c: 90 nop
d: 90 nop
e: 90 nop
f: 90 nop
0000000000000010 <posx05>:
10: 89 ff mov edi,edi
12: 89 f6 mov esi,esi
14: f2 48 0f 2a c7 cvtsi2sd xmm0,rdi
19: f2 48 0f 2a ce cvtsi2sd xmm1,rsi
1e: f2 0f 59 05 00 00 00 mulsd xmm0,QWORD PTR [rip+0x0] # 26 <posx05+0x16>
25: 00
26: f2 0f 59 0d 00 00 00 mulsd xmm1,QWORD PTR [rip+0x0] # 2e <posx05+0x1e>
2d: 00
2e: f2 0f 58 c1 addsd xmm0,xmm1
32: f2 0f 2c c0 cvttsd2si eax,xmm0
36: c3 ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment