Path: utzoo!utgpu!watmath!att!tut.cis.ohio-state.edu!gem.mps.ohio-state.edu!uwm.edu!uakari.primate.wisc.edu!dogie.macc.wisc.edu!uwvax!rang From: rang@cs.wisc.edu (Anton Rang) Newsgroups: comp.lang.c Subject: Re: swap(x,y) Message-ID: Date: 21 Sep 89 15:49:57 GMT References: <8350@boring.cwi.nl> <14706@bfmny0.UU.NET> <433@crdos1.crd.ge.COM> Sender: news@spool.cs.wisc.edu Organization: UW-Madison CS department Lines: 35 In-reply-to: davidsen@crdos1.crd.ge.COM's message of 21 Sep 89 12:54:59 GMT In article <433@crdos1.crd.ge.COM> davidsen@crdos1.crd.ge.COM (Wm E Davidsen Jr) writes: > I would be willing to bet that there are more uses of swap than shift >in applications programs, and yet we have shift. Shift can't be portably represented in a language without it; there is no (simple) sequence of statements which will do a shift. The simple sequence: t = x; x = y; y = t; will do a swap. A reasonable compiler (on a reasonable machine) ought to discover that T is only used in these three statements, and put it in a register. At this point, a peephole optimizer ought to discover that there is a swap instruction applicable. MOVE X, R0 MOVE Y, X MOVE R0, Y can be transformed into SWAP X, Y if the value in R0 is not used past this point. Anton +----------------------------------+------------------+ | Anton Rang (grad student) | rang@cs.wisc.edu | | University of Wisconsin--Madison | | +----------------------------------+------------------+ "You are in a twisty little maze of Unix versions, all different."