View Single Post
  #5  
Old 10-18-2012, 02:55 AM
KLS
Administrator
 
Join Date: Sep 2006
Posts: 1,348
Default

lerxst's short answer is going to be right 99% of the time. Just cause I needed a break from hammering out these templates I compiled them both for you and generated the assembly.

Optimizations off MSVC10
Code:
	; x += y;
	; x /= 10;
	mov	edx, DWORD PTR _x$[ebp]
	add	edx, DWORD PTR _y$[ebp]
	mov	DWORD PTR _x$[ebp], edx
	mov	eax, DWORD PTR _x$[ebp]
	cdq
	mov	ecx, 10
	idiv	ecx
	mov	DWORD PTR _x$[ebp], eax

	; x = (x + y) / 10;
	mov	eax, DWORD PTR _x$[ebp]
	add	eax, DWORD PTR _y$[ebp]
	cdq
	mov	ecx, 10
	idiv	ecx
	mov	DWORD PTR _x$[ebp], eax
Optimizations on MSVC10(/O2)
Code:
	; x += y;
	; x /= 10;
	mov	eax, 1717986919
	lea	ecx, DWORD PTR [edx+edi]
	imul	ecx
	sar	edx, 2
	mov	eax, edx
	shr	eax, 31
	add	eax, edx  

	; x = (x + y) / 10;
	mov	eax, 1717986919
	lea	ecx, DWORD PTR [edx+edi]
	imul	ecx
	sar	edx, 2
	mov	eax, edx
	shr	eax, 31
	add	eax, edx
With optimizations on they both generate the same code, with them off it's slightly different.
Reply With Quote