DevMaster.net Forums
[[ Home | Forums | 3D Engines Database | Wiki | Articles/Tutorials | Game Dev Jobs | IRC Chat Network | Contact Us ]]

Go Back   DevMaster.net Forums > Site Discussions > Code & Snapshot Discussion
User Name
Password
Register FAQ Members List Search Today's Posts Mark Forums Read

Reply
 
Thread Tools Search this Thread Display Modes
Old 07-31-2003, 09:59 AM   #1
Dia Kharrat
DevMaster Staff
 
Join Date: Jan 2003
Posts: 1,201
Default

Code:
void swap(int &x, int &y) { x -= y; y += x; // y gets the original value of x x = (y - x); // x gets the original value of y }
Enjoy testing it out!
Dia Kharrat is offline   Reply With Quote
Old 08-01-2003, 12:22 AM   #2
DrunkenCoder
Member
 
Join Date: Jul 2003
Posts: 97
Talking

and don't try swapping a variable with itself...

the code below, have the exact same behaviour.
Code:
void swap2(int &x, int &y) { x ^= y ^= x ^= y; }
DrunkenCoder is offline   Reply With Quote
Old 08-03-2003, 02:13 PM   #3
davepermen
Senior Member
 
davepermen's Avatar
 
Join Date: Jan 2003
Location: Switzerland
Posts: 1,333
Default

hehe, the much beloved xor..

haven't seen the +- above yet..

but fastest is still with a temp variable.. register actually..

Code:
__asm { mov eax,a mov ebx,b mov b,eax mov a,ebx }

or possibly there is even some way with mmx?

similar there is something for the fu**ing fpu..

but its always fun to swap "mathematically".. without tempvar..

with that idea, people where able to define the radix sort, the only sort faster than the minimum O(n*lg2(n)).. hehe
___________________________________________
davepermen.net
-Loving a Person is having the wish to see this Person happy, no matter what that means to yourself.
-No matter what it means to myself....
davepermen is offline   Reply With Quote
Old 08-04-2003, 12:14 AM   #4
DrunkenCoder
Member
 
Join Date: Jul 2003
Posts: 97
Talking

smaller (but definatly not faster)

Code:
_asm { xchg [a], eax xchg [b], eax xchg [a], eax }

but if you're resorting to assembly swaping of variables you're probably
already writing the loop in assembly and trying to hold everything in
registers.
DrunkenCoder is offline   Reply With Quote
Old 08-04-2003, 12:17 AM   #5
davepermen
Senior Member
 
davepermen's Avatar
 
Join Date: Jan 2003
Location: Switzerland
Posts: 1,333
Default

i never got how xchg really works, but it looks similar to the xor-snippet above

any other swap-codes?
___________________________________________
davepermen.net
-Loving a Person is having the wish to see this Person happy, no matter what that means to yourself.
-No matter what it means to myself....
davepermen is offline   Reply With Quote
Old 08-04-2003, 02:22 AM   #6
DrunkenCoder
Member
 
Join Date: Jul 2003
Posts: 97
Talking

well, the ones I can think of right now are
Code:
push [a] push [b] pop [a] pop [b]

not very effective and even worse for register only swaps where xchg should
be used, but it's diffrent...

for floats you could use:
Code:
fldp [a] fldp [b] fxch fstp [a] fstp [b]

using mmx you could use the pshufw instruction family.
DrunkenCoder is offline   Reply With Quote
Old 09-22-2003, 02:58 AM   #7
bladder
DevMaster Staff
 
bladder's Avatar
 
Join Date: Sep 2003
Location: Hell
Posts: 1,109
Default

Quote:
Originally Posted by davepermen
any other swap-codes?
Code:
void swap(int& a, int&b ) { int c = a; a = b; b = c; }

sorry. couldnt resist.
___________________________________________
- TripleBuffer
- Me blog
bladder is offline   Reply With Quote
Old 10-20-2003, 03:31 AM   #8
Mihail121
Senior Member
 
Mihail121's Avatar
 
Join Date: Jan 2003
Posts: 868
Talking

Here is a swap performed without a second variable. My creation so hands off!!!!

Code:
void swap(int *x,int *y) { int second_varible; int third_variable; // See?! I'm not using the second!!! third_variable = *y; *y = *x; *x = third_variable; }
Mihail121 is offline   Reply With Quote
Old 10-20-2003, 05:13 AM   #9
anubis
DevMaster Staff
 
anubis's Avatar
 
Join Date: Apr 2003
Location: Germany
Posts: 2,328
Default



no wait you are serious about your post ?
___________________________________________
If Prolog is the answer, what is the question ?
anubis is offline   Reply With Quote
Old 10-20-2003, 07:42 AM   #10
davepermen
Senior Member
 
davepermen's Avatar
 
Join Date: Jan 2003
Location: Switzerland
Posts: 1,333
Default

hehe
___________________________________________
davepermen.net
-Loving a Person is having the wish to see this Person happy, no matter what that means to yourself.
-No matter what it means to myself....
davepermen is offline   Reply With Quote
Old 10-20-2003, 07:22 PM   #11
Smokey97
New Member
 
Join Date: Jan 2003
Location: Australia
Posts: 12
Default

lol, i saw this post a few mins after he posted it... and i thought it must have been a joke, so i didnt say anything... but i guess ti isint a joke.
___________________________________________
There's only one thing we're incapable of doing; thats being incapable of doing something.
Smokey97 is offline   Reply With Quote
Old 10-21-2003, 03:24 AM   #12
Mihail121
Senior Member
 
Mihail121's Avatar
 
Join Date: Jan 2003
Posts: 868
Arrow

What's wrong with you people? Why do u laugh? Of course i'm serious. Don't u see it works perfecly fine?! I use this one a lot in my projects. Stable and fast and everything. Yep it's cool
Mihail121 is offline   Reply With Quote
Old 10-21-2003, 05:11 AM   #13
anubis
DevMaster Staff
 
anubis's Avatar
 
Join Date: Apr 2003
Location: Germany
Posts: 2,328
Default

hospital or police ???
___________________________________________
If Prolog is the answer, what is the question ?
anubis is offline   Reply With Quote
Old 10-13-2004, 03:09 PM   #14
FarooqMela
New Member
 
Join Date: Sep 2004
Posts: 3
Default

I had something similar sitting around in a header file...
Mine is one line of code though ;-)

Code:
inline void UselessSwap(int& a, int &b) { a -= b += a -= b = -b; }
FarooqMela is offline   Reply With Quote
Old 10-10-2006, 05:29 PM   #15
momotaro
New Member
 
Join Date: Oct 2006
Location: morocco
Posts: 1
Default Re: swapping two variables without using a temp var

where the hell did u find this one! it is pretty cool!
momotaro is offline   Reply With Quote
Old 10-10-2006, 07:19 PM   #16
Nils Pipenbrinck
Senior Member
 
Nils Pipenbrinck's Avatar
 
Join Date: Sep 2005
Location: Hamburg / Germany
Posts: 597
Default Re: swapping two variables without using a temp var

Haha..

What a great thread.. I like the last one.. Never thought about this solution (did anyone tried if it really works?). I also like the function name as it really sais what it's all about.

These "optimizations" are nowadays slower than a temporary variable. But they re cool nevertheless.


I like bit-tricks like these alot.
Nils Pipenbrinck is offline   Reply With Quote
Old 10-11-2006, 03:26 AM   #17
roel
Senior Member
 
roel's Avatar
 
Join Date: Sep 2005
Location: .nl
Posts: 505
Default Re: swapping two variables without using a temp var

Funny thread indeed, resurrecting old threads isn't always that bad
roel is offline   Reply With Quote
Old 10-11-2006, 05:20 PM   #18
bladder
DevMaster Staff
 
bladder's Avatar
 
Join Date: Sep 2003
Location: Hell
Posts: 1,109
Default Re: swapping two variables without using a temp var

Quote:
Originally Posted by FarooqMela
I had something similar sitting around in a header file...
Mine is one line of code though ;-)

Code:
inline void UselessSwap(int& a, int &b) { a -= b += a -= b = -b; }

Not that I'm whining or anything, but if my understanding is correct about certain aspects of c++, the above code is going straight for the realm of "undefined behavior" in c++.

Someone correct me if I'm wrong.
___________________________________________
- TripleBuffer
- Me blog
bladder is offline   Reply With Quote
Old 10-11-2006, 05:26 PM   #19
.oisyn
DevMaster Staff
 
.oisyn's Avatar
 
Join Date: Sep 2005
Location: The Netherlands
Posts: 1,442
Default Re: swapping two variables without using a temp var

You're wrong
___________________________________________
C++ addict
-
Currently working on: the 3D engine for Tomb Raider: Underworld and Deus Ex 3.
.oisyn is offline   Reply With Quote
Old 10-11-2006, 05:39 PM   #20
.oisyn
DevMaster Staff
 
.oisyn's Avatar
 
Join Date: Sep 2005
Location: The Netherlands
Posts: 1,442
Default Re: swapping two variables without using a temp var

Sorry, I was being a bitch
The reason you're wrong is because the associativity of these expressions is well defined. Just as a + b + c + d is parsed as ((a + b) + c) + d and std::cout << "hello" << 34 << std::endl is equivalent to ((std::cout << "hello") << 34) << std::endl, this expression becomes a -= (b += (a -= (b = -b)))

Each = operator returns a reference to *this and is used in the outer expression. The thing that isn't defined is the order of evaluation (which is different from associativity) in the function argument list. ++a = a++ is undefined because that translates to operator=(++a, a++) and the outcome depends on which is evaluated first: ++a or a++.
___________________________________________
C++ addict
-
Currently working on: the 3D engine for Tomb Raider: Underworld and Deus Ex 3.

Last edited by .oisyn : 10-11-2006 at 05:43 PM.
.oisyn is offline   Reply With Quote
Old 10-11-2006, 06:50 PM   #21
bladder
DevMaster Staff
 
bladder's Avatar
 
Join Date: Sep 2003
Location: Hell
Posts: 1,109
Default Re: swapping two variables without using a temp var

argh...

Ok ok, hold on, so the above would translate to:

op-=( a, op+=( b, op-=( a, op=( b, op-( b ) ) ) ) )

mhmm... i see i see...

Man they keep on saying that it's easy to get caught if you don't read the standard specs. But I gotta say, the more I'm reading the specs the more I'm getting confused. All that monkey business about sequence points ->

the following quote+reply is a preemptive strike in-case davperman reads this:

Quote:
Originally Posted by davperman
... some stuff... use c# ...more stuff...

ok.
___________________________________________
- TripleBuffer
- Me blog

Last edited by bladder : 10-11-2006 at 06:55 PM.
bladder is offline   Reply With Quote
Old 10-20-2006, 07:57 PM   #22
bladder
DevMaster Staff
 
bladder's Avatar
 
Join Date: Sep 2003
Location: Hell
Posts: 1,109
Default Re: swapping two variables without using a temp var

Yo! .oisyn!!! You have some explaining to do:

I went back to all this stuff and searched some on undefined behavior and found some... "stuff". Now a statement such as:

i = i++;

is undefined right? because the standard claims that you cannot modify the same value between sequence points. Associativity doesnt come into play in the above.

So looking at that += stuff in the same light as i = i++ we have:

i = i += whatever;

The exact words of the standard are:

"Except where noted, the order of evaluation of operands of individual operators and subexpressions of individual expressions, and the order in which side effects take place, is unspecified. Between the previous and next sequence point a scalar object shall have its stored value modified at most once by the evaluation of an expression." [from 5.0.4]

but in the above expression, it's being modified twice, so it should be undefined. So while it is defined by associativity, the actual evaluation of the internal representation on i is undefined, so the value of i is undefined after the expression (a direct effect of undefined order of evaluation)

if you we're going by associativity, the i = i++ should also be defined that way:

op=(i, op++(i))

or for a = a+= b;

op=(a, op+=(a, b))

[edit]
Another way of putting it, the expr "a -= b += a -= b = -b;" does the following:

A: assign negative b to b
B: subtract b from a
C: add a to b
D: subtract b from a

The sequence points statement says that the order of eval is undefined so it could be ABCD or ACDB or BCDA, etc... I'm not saying the above won't work, but it's still undefined according to the standard (sigh... again if i'm understanding all this correctly)
[/edit]
___________________________________________
- TripleBuffer
- Me blog

Last edited by bladder : 10-20-2006 at 08:07 PM.
bladder is offline   Reply With Quote
Old 10-21-2006, 10:50 AM   #23
davepermen
Senior Member
 
davepermen's Avatar
 
Join Date: Jan 2003
Location: Switzerland
Posts: 1,333
Default Re: swapping two variables without using a temp var

Quote:
Originally Posted by bladder
the following quote+reply is a preemptive strike in-case davperman reads this:
more important: learn my name
___________________________________________
davepermen.net
-Loving a Person is having the wish to see this Person happy, no matter what that means to yourself.
-No matter what it means to myself....
davepermen is offline   Reply With Quote
Old 10-21-2006, 11:32 AM   #24
bladder
DevMaster Staff
 
bladder's Avatar
 
Join Date: Sep 2003
Location: Hell
Posts: 1,109
Default Re: swapping two variables without using a temp var

apologies davepermen - believe it or not, i was expecting such a reply from you . I was unsure when typing the spelling and i recalled you would always grab anyone who didn't spell your name correctly. But I just didn't have the time to search for the correct spelling.
___________________________________________
- TripleBuffer
- Me blog
bladder is offline   Reply With Quote
Old 10-21-2006, 12:21 PM   #25
martinsm
Member
 
Join Date: Apr 2006
Location: Latvia
Posts: 72
Default Re: swapping two variables without using a temp var

C++:
Code:
std::swap(x, y);

Python:
Code:
x, y = y, x
martinsm is offline   Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Forum Jump


All times are GMT -7. The time now is 05:04 AM.


Powered by vBulletin
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.