swap(x,y)

Rob McMahon cudcv at warwick.ac.uk
Sat Aug 26 05:30:05 AEST 1989


Re: x += y; y = x - y; x -= y; 
vs. x ^= y; y ^= x; x ^= y; 
vs. tmp = x; x = y; y = tmp;

Surely this is another of these things that should be coded legibly and left
to the compiler to do the best way, rather than trying to guess what
operations the current machine has, and whether the compiler has put variables
in memory or registers etc.  Lets talk about how to improve the compilers
first.  On our Gould PN6000 with GCC 1.34

	{
	  int tmp;
	  tmp = x;
	  x = y;
	  y = tmp;
	}

compiles to

	xchg	r5,r4

Rob
-- 
UUCP:   ...!mcvax!ukc!warwick!cudcv	PHONE:  +44 203 523037
JANET:  cudcv at uk.ac.warwick             ARPA:   cudcv at warwick.ac.uk
Rob McMahon, Computing Services, Warwick University, Coventry CV4 7AL, England



More information about the Comp.lang.c mailing list