Comments on: Is Swap Without Temporary a Good Idea? http://www.codingaway.com/2007/01/31/is-swap-without-temporary-a-good-idea/ Philosophy for Programmers: Code, Technology & Stuff. Sun, 06 Jul 2008 19:37:14 +0000 http://wordpress.org/?v=2.1.2 By: Alex http://www.codingaway.com/2007/01/31/is-swap-without-temporary-a-good-idea/#comment-9 Alex Thu, 01 Feb 2007 09:32:08 +0000 http://www.codingaway.com/2007/01/31/is-swap-without-temporary-a-good-idea/#comment-9 Consider instead the more general case, not using memory storage in a properly optimized piece of code: where eax contains A, ebx contains B //Assuming ecx isn't in use mov ecx,eax mov ebx,eax mov eax,ecx Now consider this using the "xor" method xor eax,ebx xor ebx,eax xor eax,ebx Speedwise these to small pieces of code perform quite similar, readability isn't damaged, the advantage is saving a register. Ofcourse register interdependencies in the pipeline might vary depending on processor, but it's hard not to justify using the xor method, due to the fact that it frees up a register to be used for something else. Consider instead the more general case, not using memory storage in a properly optimized piece of code:
where eax contains A, ebx contains B
//Assuming ecx isn’t in use
mov ecx,eax
mov ebx,eax
mov eax,ecx

Now consider this using the “xor” method
xor eax,ebx
xor ebx,eax
xor eax,ebx

Speedwise these to small pieces of code perform quite similar, readability isn’t damaged, the advantage is saving a register. Ofcourse register interdependencies in the pipeline might vary depending on processor, but it’s hard not to justify using the xor method, due to the fact that it frees up a register to be used for something else.

]]>
By: Adrian Boeing http://www.codingaway.com/2007/01/31/is-swap-without-temporary-a-good-idea/#comment-11 Adrian Boeing Thu, 01 Feb 2007 16:29:47 +0000 http://www.codingaway.com/2007/01/31/is-swap-without-temporary-a-good-idea/#comment-11 Not that I advocate the XOR swap trick, but you might find it gets closer to what its meant to do if you switch to release mode: swap1: ; 16 : int c=a; mov eax, DWORD PTR [ecx] push esi ; 17 : a = b; mov esi, DWORD PTR [edx] mov DWORD PTR [ecx], esi ; 18 : b = c; mov DWORD PTR [edx], eax pop esi ; 19 : } swap2: ; 23 : a ^= b; mov eax, DWORD PTR [edx] xor DWORD PTR [ecx], eax mov eax, DWORD PTR [ecx] ; 24 : b ^= a; xor DWORD PTR [edx], eax mov edx, DWORD PTR [edx] ; 25 : a ^= b; xor DWORD PTR [ecx], edx I'm sure if you flicked a few more optimization options you would end up with some sensible assembler code. Not that I advocate the XOR swap trick, but you might find it gets closer to what its meant to do if you switch to release mode:

swap1:

; 16 : int c=a;

mov eax, DWORD PTR [ecx]
push esi

; 17 : a = b;

mov esi, DWORD PTR [edx]
mov DWORD PTR [ecx], esi

; 18 : b = c;

mov DWORD PTR [edx], eax
pop esi

; 19 : }

swap2:
; 23 : a ^= b;

mov eax, DWORD PTR [edx]
xor DWORD PTR [ecx], eax
mov eax, DWORD PTR [ecx]

; 24 : b ^= a;

xor DWORD PTR [edx], eax
mov edx, DWORD PTR [edx]

; 25 : a ^= b;

xor DWORD PTR [ecx], edx

I’m sure if you flicked a few more optimization options you would end up with some sensible assembler code.

]]>
By: Lorenzo http://www.codingaway.com/2007/01/31/is-swap-without-temporary-a-good-idea/#comment-12 Lorenzo Thu, 01 Feb 2007 17:26:26 +0000 http://www.codingaway.com/2007/01/31/is-swap-without-temporary-a-good-idea/#comment-12 Adrian: Looks pretty close, but still slower, and the source is still odd to look at. Alex: I was mostly interested in how a C/C++ compiler can optimize such code, and the advantage to a C/C++ programmer. In the case you suggest, if you were to write the code by hand in assembly, I agree it is not a bad choice. However, in my opinion, it is still unreadable in the sense that if you don't know the "trick", it is going to take a few minutes to find out how that works. Adrian: Looks pretty close, but still slower, and the source is still odd to look at.

Alex: I was mostly interested in how a C/C++ compiler can optimize such code, and the advantage to a C/C++ programmer. In the case you suggest, if you were to write the code by hand in assembly, I agree it is not a bad choice. However, in my opinion, it is still unreadable in the sense that if you don’t know the “trick”, it is going to take a few minutes to find out how that works.

]]>