home *** CD-ROM | disk | FTP | other *** search
- Path: nntp.teleport.com!sschaem
- From: sschaem@teleport.com (Stephan Schaem)
- Newsgroups: comp.sys.amiga.programmer
- Subject: Re: x ^= y ^= x ^= y;
- Date: 24 Feb 1996 15:47:17 GMT
- Organization: Teleport - Portland's Public Access (503) 220-1016
- Message-ID: <4gnbu5$3n2@maureen.teleport.com>
- References: <1286.6624T1439T237@cs.ruu.nl>
- NNTP-Posting-Host: kelly.teleport.com
- X-Newsreader: TIN [version 1.2 PL2]
-
- Wessel Dankers (wsldanke@cs.ruu.nl) wrote:
- : In one of my college-books I found this transition:
-
- : <x := x+y ; y := x-y ; x := x-y, s> -> s{s(y)/x}{s(x)/y}
-
- : proven correct.
-
- : In plain C:
-
- : x = x + y;
- : y = x - y;
- : x = x - y;
-
- : will swap the integers x and y. Of course I immediately started `hacking'
- : on it, resulting in:
-
- : x = x ^ y;
- : y = x ^ y;
- : x = x ^ y;
-
- : with the C bitwise-or operator.
-
- : Which can of course be rewritten as:
-
- : x ^= y;
- : y ^= x;
- : x ^= y;
-
- : Note that these algorithms do not use temporary storage and can be
- : generalized for large memory areas with a for-loop.
-
- : My questions are:
-
- : a) are these routines efficient for integers? (probably not)
-
- It should translate to (If the variable are in register)
-
- eor.? dx,dy
- eor.? dy,dx
- eor.? dx,dy
-
- But in C you shoulnd't care, actually it shouldn't be a concern
- on how its implemented. you can only think on the methode, if one
- particular C compiler screwup you have to accept the fact.
-
- : b) is this x ^= y ^= x ^= y; method usable with the blitter?
- : It would be possible to swap two memory locations with no
- : extra memory required in three Blit calls. Is the blitter
- : equally fast with copying as with XOR'ing?
-
- I personaly never cared to swap chipmem, and on my
- system I would use a generalized CPU function.(32bit fastmem)
-
- move.l (a0),d1
- move.l (a1),(a0)+
- move.l d1,(a1)+
-
- equal to 4 bus access to swap 4 byte. VS 3*3*2 : 18 bus access with
- the blitter... >4 time slower on large exange, ouch. (On small its
- alot worse because of blitter setup)
-
- Stephan
-