Path: utzoo!attcan!uunet!husc6!spdcc!ima!haddock!karl From: karl@haddock.ima.isc.com (Karl Heuer) Newsgroups: comp.lang.c Subject: Re: Using distributive property to optimize. Message-ID: <15904@haddock.ima.isc.com> Date: 7 Feb 90 20:25:41 GMT References: <229@altos86.Altos.COM> <1020.nlhp3@oracle.nl> <94348@linus.UUCP> Reply-To: karl@haddock.ima.isc.com (Karl Heuer) Organization: Interactive Systems, Cambridge, MA 02138-5302 Lines: 20 In article <94348@linus.UUCP> bs@linus.UUCP writes: >[is it valid to change "n = a - (b + c);" to "n = a - b - c;"] > >Note also that even with integer arithmetic, the rearrangement is not >safe. If (say) b is a large negative number and c a large positive >number a - (b+c) may not overflow. However, a-b most certainly can. But if, as is common, the implementation chooses to handle signed integer overflow by producing the unique representable value which is congruent modulo UINT_MAX+1 to the true answer, then (a-b)-c will yield the same value as a-(b+c). (And if the variables are unsigned integers, then the rewrite is valid on *all* implementations, because that's how unsigned arithmetic is defined in C.) >Compilers should NEVER rearrange computations. As long as they get the right answer, they may; as long as it's a desirable performance improvement, they should. Karl W. Z. Heuer (karl@ima.ima.isc.com or harvard!ima!karl), The Walking Lint