Skip to main content
gamedev.net
🔒 Locked

i^=i

Started by diddly-yo 4 replies 200+ views
Original Post
diddly-yo
diddly-yo
Is i^=i faster than i = 0? Thanks, diddly
Kippesoep
Kippesoep
Not usually. I think you got this from the old asm trick that "xor ax, ax" was more efficient than "mov ax, 0". That was simply because the code size for the former was less than the latter.

Kippesoep
diddly-yo
diddly-yo
I was curious, because for people who don''t have asm experience (read: me), i^=i took some time to figure out. i = 0 seemed a little more straightforward. Anyway, thanks.

diddly
Fruny
Fruny
If you have a post Stone-Age compiler,
it will already optimize i=0 into i^=i;

Of course, on µPs with a zero register,
loading from that register is as fast,
and doesn''t hog the ALU.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
IndirectX
IndirectX
i^=i requires a read and write, and if i is a variable that is actually stored in memory I suspect that mov will be faster, since it''s just a write. For registers, i^=i should be faster but compilers should optimize it anyway.
---visit #directxdev on afternet <- not just for directx, despite the name

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.