Newsgroups: comp.lang.c Path: utzoo!henry From: henry@utzoo.uucp (Henry Spencer) Subject: Re: Using distributive property to optimize. Message-ID: <1990Feb5.165748.7869@utzoo.uucp> Organization: U of Toronto Zoology References: <229@altos86.Altos.COM> Date: Mon, 5 Feb 90 16:57:48 GMT In article <229@altos86.Altos.COM> clp@altos86.Altos.COM (Chuck L. Peterson) writes: >We just noticed that this old 286 XENIX compiler we have floating >around changes the statement: > n = a - (b + c); >To this: > n = a - b - c; > >Anyone know if it is valid for a C Compiler to do this? >The two will most certainly give different results when >dealing with overflow/underflow conditions. The slightly fuzzy definition of overflow behavior in K&R1 could be read to permit this. Old C compilers usually just ignored the possibility of overflow entirely, and rearranged expressions whenever they felt like it. They got away with this, usually, because most C implementations didn't do anything about overflow anyway, and 2's-complement arithmetic gives the same answer either way. (I assume we are talking about integers, not floating point!) ANSI C has tightened the rules a little. Overflow behavior must not change as the result of optimization. Mind you, if the code you generate ignores overflows, and if your machine's arithmetic is such that the result is the same, then that isn't an issue and the rearrangement is still kosher. (Note a very subtle fine point: compilers that ignore the possibility of overflow do not necessarily generate code that ignores overflows! If the machine remembers overflows (e.g. with an overflow condition-code bit), tests on computed values can be sensitive to whether an overflow occurred in the computation. I found an example that did this with the original pdp11 C compiler. Generating code that truly ignores overflows on such a machine requires that the compiler be aware of the issue.) -- SVR4: every feature you ever | Henry Spencer at U of Toronto Zoology wanted, and plenty you didn't.| uunet!attcan!utzoo!henry henry@zoo.toronto.edu