Path: utzoo!mnetor!uunet!husc6!bbn!rochester!cornell!oravax!clay From: clay@oravax.UUCP (McFarland) Newsgroups: comp.lang.c Subject: Re: Bad optimizations (was Re: comma operator) Message-ID: <179@oravax.UUCP> Date: 22 Jan 88 00:15:31 GMT References: <3819@sigi.Colorado.EDU> <5080013@hpfcdc.HP.COM> <7120@brl-smoke.ARPA> <3887@sigi.Colorado.EDU> <269@vsi1.UUCP> Reply-To: clay@oravax.odyssey.UUCP (Clay Brooke-McFarland) Organization: Odyssey Research Associates, Ithaca, New York Lines: 77 In article <269@vsi1.UUCP> steve@vsi1.UUCP (Steve Maurer) writes: >SWARBRICK FRANCIS JOHN writes: >> >> #define swap(a,b) ((a) = ((b) = ((a) = (a) ^ (b)) ^ (b)) ^ (a)) >> >> He says it will work for anything (int, char *, etc.). But it >> sure boggles my mind... >> > It boggles mine as well. Just from looking at it, it looks like >the resulting Assembly would be a mess, even in the simplistic case of >integers. However, just to try this out, I decided to compile the >following section of code on our Sun3 (without optimization). > (Code deleted) >The resulting code is approximately 3 times longer! This is >partly due to the superflous storing of intermediate results that >the compiler makes, because it isn't sure in that portion of the >parse tree whether the new result is going to be used. > > In other words, "optimal moves" don't necessarily work in HLL's. Or anyplace else where they don't fit. I first invented this chestnut in 1966. I'm sure there were many prior inventors; it's obvious once you think " wouldn't it be nice to store both a and b in the same place for a while". I was using it to swap registers on the IBM 360 ( it didn't need a register swap, it had General Registers :-). In that context it worked perfectly but, as Maurer showed, it's ridiculous in C. I think the definition of googol (googolplex??:-) is "the number of computing constructs that were great for what they were designed for, but are used for 20 years where they are kluges". Clay Brooke-McFarland Odyssey Research Associates --------------------------------------------------------------------- "Wait! There's something we're all overlooking!" Chorus: "What is it,| Doctor?" "I don't know. I'm overlooking it myself." | ---------------------------------------------------------------------- >> >> #define swap(a,b) ((a) = ((b) = ((a) = (a) ^ (b)) ^ (b)) ^ (a)) >> >> He says it will work for anything (int, char *, etc.). But it >> sure boggles my mind... >> > It boggles mine as well. Just from looking at it, it looks like >the resulting Assembly would be a mess, even in the simplistic case of >integers. However, just to try this out, I decided to compile the >following section of code on our Sun3 (without optimization). > (Code deleted) >The resulting code is approximately 3 times longer! This is >partly due to the superflous storing of intermediate results that >the compiler makes, because it isn't sure in that portion of the >parse tree whether the new result is going to be used. > > In other words, "optimal moves" don't necessarily work in HLL's. > Or anyplace else where they don't fit. I first invented this chestnut in 1966. I'm sure there were many prior inventors; it's obvious once you think " wouldn't it be nice to store both a and b in the same place for a while". I was using it to swap registers on the IBM 360 ( it didn't need a register swap, it had General Registers :-). In that context it worked perfectly but, as Maurer showed, it's ridiculous in C. I think the definition of googol (googolplex??:-) is "the number of computing constructs that were great for what they were designed for, but are used for 20 years where they are kluges". Clay Brooke-McFarland Odyssey Research Associates "Wait! There's something we're all overlooking!" Chorus: "What is it, Doctor?" "I don't know. I'm overlooking it myself."