public
Created

what code gcc generates for `x/2` and `x*.5`

  • Download Gist
posx.c
C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
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);
}
posx.dis
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.