Path: csiph.com!news.swapon.de!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: in place swap of float types Date: Wed, 28 Aug 2019 13:01:25 -0700 Organization: None to speak of Lines: 35 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader02.eternal-september.org; posting-host="43c6504da4c711ed8a19ef2a7c0d5196"; logging-data="10453"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/rkb+X7XrjhCN4NfwTkpl2" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:vlxZlxXcWGTC0UTeLzuLks2SD2g= sha1:4rYn9eAzbUs5S+ZLnNM55GYpqXY= Xref: csiph.com comp.lang.c:145829 James Kuyper writes: > On Wednesday, August 28, 2019 at 7:03:05 AM UTC-4, Bart wrote: >> On 28/08/2019 08:36, David Brown wrote: > ... >> > No, such tricks are not useful - if space is really tight, write clear >> > sensible code and work /with/ your compiler, rather than trying to >> > outsmart it. >> >> I don't understand why anyone would think this xor trick was any faster >> than using the conventional swap idiom using a temporary. > > The xor trick has always been about minimizing memory usage (including > registers) - not minimizing execution time. > > And as David pointed out, it's of doubtful usefulness, even for that > purpose, in high-level languages like C. The compiler generally knows > better than the programmer how to best use the available registers, and > languages like C don't provide the level of detailed control needed to > do a significantly better job. Only the best assembly language > programmers can do a significantly better job than the best compilers - > because the expertise of the best assembly language programmers has, to > the extent possible, been built into the best compilers. Another problem that I don't think anyone has pointed out in this thread is that it fails if you try to swap an object with itself -- for example swapping *p and *q, where p and q are pointers to the same object, or swapping arr[i] and arr[j] where i==j. Most algorithms, if implemented correctly, won't try to swap an object with itself, but it's something to consider. -- Keith Thompson (The_Other_Keith) kst-u@mib.org Will write code for food. void Void(void) { Void(); } /* The recursive call of the void */