This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: swap (a, b) suggestion
At 8:22 AM +0100 1/4/98, Benny Amorsen wrote:
* >>>>> "RH" == Richard Henderson <rth@cygnus.com> writes:
*
* RH> On Sat, Jan 03, 1998 at 06:11:23PM -0500, Eric Buddington wrote:
* >> For integer values, the following works, and is significantly
* >> faster:
* >>
* >> template <class T>
* >> inline void swap(T& a, T& b) {
* >> a ^= b;
* >> b ^= a;
* >> a ^= b;
* >> }
*
* RH> If it is faster at all I'll warrent it is only so for depressingly
* RH> register starved machines.
*
* Perhaps the optimization should be made in a different place -- Make
* the optimizer recognize the sequence "tmp = a; a = b; b = tmp" and do
* it the most efficient way. The instruction combiner or the peephole
* optimizer could be made to catch this.
*
*
* Benny
That's the way I did this in my enhancements to the SHARC DSP port
of GCC. It has an instruction that allows parallel register
assignments, so I can do
ra=pass rb, rb=ra;
which in one instruction does the swap. I suppose for completeness sake, I
ought to add a pattern for the three XORs and reduce them to the single
instruction too :-)
-- Al Lehotsky
------------------------------------------------------------------------
Quality Software Management
http://www.tiac.net/users/lehotsky
lehotsky@tiac.net
(617)862-5418 Voice
(617)674-1096 FAX
Software Process Improvement and Management Consulting
Language Design and Compiler Implementation