![]() |
| [[ Home | Forums | 3D Engines Database | Wiki | Articles/Tutorials | Game Dev Jobs | IRC Chat Network | Contact Us ]] |
|
|
#1 |
|
DevMaster Staff
Join Date: Jan 2003
Posts: 1,201
|
Code:
|
|
|
|
|
|
#2 |
|
Member
Join Date: Jul 2003
Posts: 97
|
and don't try swapping a variable with itself...
the code below, have the exact same behaviour. Code:
|
|
|
|
|
|
#3 |
|
Senior Member
Join Date: Jan 2003
Location: Switzerland
Posts: 1,333
|
hehe, the much beloved xor..
haven't seen the +- above yet.. but fastest is still with a temp variable.. register actually.. Code:
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.... |
|
|
|
|
|
#4 |
|
Member
Join Date: Jul 2003
Posts: 97
|
smaller (but definatly not faster)
Code:
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. |
|
|
|
|
|
#5 |
|
Senior Member
Join Date: Jan 2003
Location: Switzerland
Posts: 1,333
|
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.... |
|
|
|
|
|
#6 |
|
Member
Join Date: Jul 2003
Posts: 97
|
well, the ones I can think of right now are
Code:
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:
using mmx you could use the pshufw instruction family. |
|
|
|
|
|
#7 | |
|
DevMaster Staff
Join Date: Sep 2003
Location: Hell
Posts: 1,109
|
Quote:
Code:
sorry. couldnt resist. |
|
|
|
|
|
|
#8 |
|
Senior Member
Join Date: Jan 2003
Posts: 868
|
Here is a swap performed without a second variable. My creation so hands off!!!!
Code:
|
|
|
|
|
|
#9 |
|
DevMaster Staff
Join Date: Apr 2003
Location: Germany
Posts: 2,328
|
no wait you are serious about your post ?
___________________________________________
If Prolog is the answer, what is the question ? |
|
|
|
|
|
#10 |
|
Senior Member
Join Date: Jan 2003
Location: Switzerland
Posts: 1,333
|
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.... |
|
|
|
|
|
#11 |
|
New Member
Join Date: Jan 2003
Location: Australia
Posts: 12
|
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. |
|
|
|
|
|
#12 |
|
Senior Member
Join Date: Jan 2003
Posts: 868
|
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
![]() |
|
|
|
|
|
#13 |
|
DevMaster Staff
Join Date: Apr 2003
Location: Germany
Posts: 2,328
|
hospital or police ???
___________________________________________
If Prolog is the answer, what is the question ? |
|
|
|
|
|
#14 |
|
New Member
Join Date: Sep 2004
Posts: 3
|
I had something similar sitting around in a header file...
Mine is one line of code though ;-) Code:
|
|
|
|
|
|
#15 |
|
New Member
Join Date: Oct 2006
Location: morocco
Posts: 1
|
where the hell did u find this one! it is pretty cool!
|
|
|
|
|
|
#16 |
|
Senior Member
Join Date: Sep 2005
Location: Hamburg / Germany
Posts: 597
|
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. |
|
|
|
|
|
#17 |
|
Senior Member
Join Date: Sep 2005
Location: .nl
Posts: 505
|
Funny thread indeed, resurrecting old threads isn't always that bad
![]() |
|
|
|
|
|
#18 | |
|
DevMaster Staff
Join Date: Sep 2003
Location: Hell
Posts: 1,109
|
Quote:
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. |
|
|
|
|
|
|
#19 |
|
DevMaster Staff
Join Date: Sep 2005
Location: The Netherlands
Posts: 1,442
|
You're wrong
![]()
___________________________________________
C++ addict - Currently working on: the 3D engine for Tomb Raider: Underworld and Deus Ex 3. |
|
|
|
|
|
#20 |
|
DevMaster Staff
Join Date: Sep 2005
Location: The Netherlands
Posts: 1,442
|
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. |
|
|
|
|
|
#21 | |
|
DevMaster Staff
Join Date: Sep 2003
Location: Hell
Posts: 1,109
|
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:
ok. Last edited by bladder : 10-11-2006 at 06:55 PM. |
|
|
|
|
|
|
#22 |
|
DevMaster Staff
Join Date: Sep 2003
Location: Hell
Posts: 1,109
|
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] Last edited by bladder : 10-20-2006 at 08:07 PM. |
|
|
|
|
|
#23 | |
|
Senior Member
Join Date: Jan 2003
Location: Switzerland
Posts: 1,333
|
Quote:
___________________________________________
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.... |
|
|
|
|
|
|
#24 |
|
DevMaster Staff
Join Date: Sep 2003
Location: Hell
Posts: 1,109
|
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. |
|
|
|
|
|
#25 |
|
Member
Join Date: Apr 2006
Location: Latvia
Posts: 72
|
C++:
Code:
Python: Code:
|
|
|
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
|