Click here to Skip to main content
15,904,934 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: '^' operator Pin
PravinSingh5-Jul-05 0:04
PravinSingh5-Jul-05 0:04 
GeneralRe: '^' operator Pin
Bob Stanneveld5-Jul-05 0:25
Bob Stanneveld5-Jul-05 0:25 
GeneralRe: '^' operator Pin
LiYS5-Jul-05 0:04
LiYS5-Jul-05 0:04 
GeneralRe: '^' operator Pin
Bob Stanneveld5-Jul-05 0:31
Bob Stanneveld5-Jul-05 0:31 
GeneralRe: '^' operator Pin
Bob Stanneveld5-Jul-05 0:36
Bob Stanneveld5-Jul-05 0:36 
GeneralRe: '^' operator Pin
toxcct5-Jul-05 1:02
toxcct5-Jul-05 1:02 
GeneralRe: '^' operator Pin
Bob Stanneveld5-Jul-05 2:09
Bob Stanneveld5-Jul-05 2:09 
GeneralRe: '^' operator Pin
Toby Opferman5-Jul-05 8:32
Toby Opferman5-Jul-05 8:32 
I think he just wanted the theory[^] behind the XOR swap.

On smaller processors the XOR swap would actually be faster than a temp variable or having to use a 3rd register if registers were already limited. However, you are correct that the optimization of a temp variable can be faster than the generated XOR swap from a C compiler (and using a modern processor).

void SwapXor(UINT *a, UINT *b)
{
	*a = *a ^ *b;
	*b = *a ^ *b;
	*a = *a ^ *b;
}

image00400000+0x1000:
00401000 8b4c2408         mov     ecx,[esp+0x8] ; ecx = b
00401004 8b442404         mov     eax,[esp+0x4] ; eax = a
00401008 56               push    esi
00401009 8b11             mov     edx,[ecx]     ; edx = *b
0040100b 8b30             mov     esi,[eax]     ; esi = *a
0040100d 33f2             xor     esi,edx       ; temp = *a ^ *b
0040100f 8930             mov     [eax],esi     ; *a = esi(temp)
00401011 8bd6             mov     edx,esi       ; edx = *a
00401013 8b31             mov     esi,[ecx]     ; esi = *b
00401015 33f2             xor     esi,edx       ; temp = *b ^ *a
00401017 8931             mov     [ecx],esi     ; *b = esi (temp)
00401019 8b10             mov     edx,[eax]     ; edx = *a
0040101b 8bce             mov     ecx,esi       ; ecx = *b
0040101d 5e               pop     esi
0040101e 33d1             xor     edx,ecx       ; temp = *a ^ *b
00401020 8910             mov     [eax],edx     ; *a = edx(temp)
00401022 c3               ret

void SwapTemp(UINT *a, UINT *b)
{
	UINT Temp;

	Temp = *a;
	*a = *b;
	*b = Temp;
}

00401030 8b542408         mov     edx,[esp+0x8] ; edx = b
00401034 8b442404         mov     eax,[esp+0x4] ; eax = a
00401038 56               push    esi
00401039 8b32             mov     esi,[edx]     ; esi = *b
0040103b 8b08             mov     ecx,[eax]     ; ecx = *a
0040103d 8930             mov     [eax],esi     ; *a  = esi
0040103f 890a             mov     [edx],ecx     ; *b  = ecx
00401041 5e               pop     esi
00401042 c3               ret

1 million XOR Swap = Milliseconds = 10.569012
1 million Temp Swap = Milliseconds = 4.846916

Generally, it's not the number of instructions that make the difference it's how the assembly is structured. This has no bearing in this case though because you get more instructions and you get slower code as well.

You notice that the memory is read multiple times as well as saved multiple times unnesscarily in the code path. You also notice that there was no "true" temp variable ever generated for the other code so even if you eliminate most of the code it's still not an optimal solution.

So, yes while it may look in C that you are being optimal by not using a "temp" variable, if your compiler is smart enough this doesn't matter and does generate faster code as Bob said. If you are working with embedded systems and are coding the direct assembly, an "xor" approach may work better depending on the situation. It could also work if you hand code assembly for different pipelines where 2 instructions may be executed at the same time. This depends though on if a simple "XCHG" instruction would cause a problem with a pipe "stall" or not.

However, in C I think you can rely on the compiler to optimize with the temp variable rather than atempting to do what is considered a lower level optimization in a higher level language (This is not to say that an "xor" swap is always nessecary in smaller CPUs at all, it's just a possible consideration for certain code optimizations which may or may not be useful. Generally XOR is faster to "set a register to 0" as the most common optimization, XOR EAX, EAX back when a "mov" "MOV EAX, 0" was considered a slow operation. I don't think that's the case any more on newer processors).




8bc7c0ec02c0e404c0cc0680f7018827ebee
GeneralRe: '^' operator Pin
David Crow5-Jul-05 5:05
David Crow5-Jul-05 5:05 
GeneralRe: '^' operator Pin
Toby Opferman5-Jul-05 6:45
Toby Opferman5-Jul-05 6:45 
GeneralRe: '^' operator Pin
Toby Opferman5-Jul-05 6:50
Toby Opferman5-Jul-05 6:50 
GeneralRe: '^' operator Pin
PJ Arends5-Jul-05 9:02
professionalPJ Arends5-Jul-05 9:02 
QuestionTo delete (remove) wizard created functions via IDE of VC++ 2003 ? Pin
Maxwell Chen4-Jul-05 22:29
Maxwell Chen4-Jul-05 22:29 
Generalchanging color of my form in vc++ Pin
smartymanav4-Jul-05 21:45
smartymanav4-Jul-05 21:45 
GeneralRe: changing color of my form in vc++ Pin
Alex Korchemniy4-Jul-05 21:54
Alex Korchemniy4-Jul-05 21:54 
GeneralRe: changing color of my form in vc++ Pin
David Spain5-Jul-05 18:43
David Spain5-Jul-05 18:43 
GeneralContext menu on Tree Pin
--------------------------------4-Jul-05 21:21
suss--------------------------------4-Jul-05 21:21 
GeneralRe: Context menu on Tree Pin
Bert [Otherside82] Derijckere5-Jul-05 0:31
Bert [Otherside82] Derijckere5-Jul-05 0:31 
Generalcreating modeless dialog box Pin
sayup4-Jul-05 20:41
sayup4-Jul-05 20:41 
GeneralRe: creating modeless dialog box Pin
2249174-Jul-05 20:57
2249174-Jul-05 20:57 
GeneralRe: creating modeless dialog box Pin
Cedric Moonen4-Jul-05 20:59
Cedric Moonen4-Jul-05 20:59 
GeneralRe: creating modeless dialog box Pin
2249174-Jul-05 21:05
2249174-Jul-05 21:05 
GeneralRe: creating modeless dialog box Pin
Cedric Moonen4-Jul-05 21:24
Cedric Moonen4-Jul-05 21:24 
GeneralRe: creating modeless dialog box Pin
sayup5-Jul-05 2:37
sayup5-Jul-05 2:37 
GeneralSetActivePage of CPropertySheet Pin
GeloSoft4-Jul-05 20:35
GeloSoft4-Jul-05 20:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.