Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!cwjcc!gatech!purdue!mentor.cc.purdue.edu!l.cc.purdue.edu!cik From: cik@l.cc.purdue.edu (Herman Rubin) Newsgroups: comp.lang.c Subject: Re: swap(x,y) Summary: "Use" is not *(int *) Message-ID: <1560@l.cc.purdue.edu> Date: 4 Sep 89 21:25:39 GMT References: <8350@boring.cwi.nl> <10790@riks.csl.sony.co.jp> Organization: Purdue University Statistics Department Lines: 44 In article <10790@riks.csl.sony.co.jp>, diamond@csl.sony.co.jp (Norman Diamond) writes: > In article <1545@l.cc.purdue.edu> cik@l.cc.purdue.edu (Herman Rubin) writes: > > >If one adds a pseudo-op, say use (I will not object to a more appropriate > >term) so that (use int)x means treat x as type int no matter what it was, > >if possible, then it > [the XOR trick] > >will work on float and pointer, and even swap between, > >say, float and pointer. If this is modified to allow multiple-word types, > >it could handle double and struct. > >This is another example of keeping the tools from the programmer. > > The way to write this pseudo-op in C is: > > *(int*)&x What if x is in a register? Much of the time things will be in registers. Indeed, on some machines, arithmetic operations can only be done using registers. One might not even have memory to memory moves. On such machines, there is no point in attempting to swap two items in memory by other than moving both to registers and storing the registers, or memory and register by other than using a spare register. But the situation with few spare registers and a register-register swap wanted is much more common. > It still doesn't work if x is longer than an int. Most hardware does > not supply tools for performing XOR on floating-point registers. When > Mr. Rubin designs his language, he will have to design a machine to > run it on too. Quite a few machines do not have separate floating-point registers. On those, the swap is appropriate for XOR-sized items. And on a double length operand, two XORs might be the best way to do it, etc. This is again especially important if registers are scarce. This problem is a good example of the need to consider that the way to perform operations on quantities may depend on not only the types of the arguments, but also upon where they are stored. The machine language programmer has no problems with this. The hardware limitations are machine dependent. -- Herman Rubin, Dept. of Statistics, Purdue Univ., West Lafayette IN47907 Phone: (317)494-6054 hrubin@l.cc.purdue.edu (Internet, bitnet, UUCP)