swap (a, b) suggestion

Benny Amorsen amorsen@sscnet.com
Sat Jan 3 23:22:00 GMT 1998


>>>>> "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



More information about the Gcc mailing list