swapping vars

cottrell at nbs-vms.ARPA cottrell at nbs-vms.ARPA
Wed Feb 6 06:31:24 AEST 1985


/*
> > > How often have you written:
> > > 
> > > 	{	register type	t;
> > > 
> > > 		t = a;
> > > 		a = b;
> > > 		b = t;
> > > 	}
> > Rarely. I use:	`a ^= b; b ^= a; a ^= b;' Only worx for integer types.
> 
> Also takez more cycles on most machines.
> 
> An example: on the 68000, case 1 is
> 
> 	move.l	a,t		# 32-bit ints
> 	move.l	b,a
> 	move.l	t,a
> 
> case 2 is
> 
> 	move.l	b,d0
> 	eor.l	d0,a
> 	move.l	a,d0
> 	eor.l	d0,b
> 	move.l	b,d0
> 	eor.l	d0,a
> 
> Cute, but not worth it.
> 
> 	Guy Harris
> 	{seismo,ihnp4,allegra}!rlgvax!guy

If a & b are in registers it only takes 3 eor's. I don't know if eor
is slower than move. If all regs are being used, the var `t' may
have to be a memory location. Why did Motorola choose `eor' instead
of `xor'? Are they Winnie the Pooh freaks :-?
*/



More information about the Comp.lang.c mailing list