Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!columbia!rutgers!clyde!ima!mirror!xanth!kent From: kent@xanth.UUCP (Kent Paul Dolan) Newsgroups: comp.lang.c Subject: Re: C and overflow anomolies Message-ID: <827@xanth.UUCP> Date: Mon, 13-Apr-87 04:50:20 EST Article-I.D.: xanth.827 Posted: Mon Apr 13 04:50:20 1987 Date-Received: Sat, 18-Apr-87 19:26:14 EST References: <15958@sun.uucp> <5716@brl-smoke.ARPA> Reply-To: kent@xanth.UUCP (Kent Paul Dolan) Distribution: world Organization: Old Dominion University, Norfolk Va. Lines: 166 Summary: answer/tutorial In article <820@xanth.UUCP> kent@xanth.UUCP I wrote: [edited, paraphrased, etc. to save space; we've seen it twice already] ...a compiler writer...feels free to [...muck about like mad several ways...], and recent publications suggest that separate compilation may not be good enough, either, to prevent unexpected interactions of two pieces of code which were certainly separate in the programmer's mind. Second, this may provide insufficient or inappropriate granularity of control against the optimizing compiler. [...] of execution of a series of statements which the compiler may decide to rearrange. The unary plus doesn't cut it here, but the compiler author has promised to look everywhere for optimizations. How do I stop the [compiler] from exercising optimization where it is dangerous to the success of the program, without turning it off where it is harmless or beneficial? I think the standard should include a better, prettier method of control of optimization scope and degree. Comments? In article <568@csun.UUCP> aeusemrs@csun.UUCP (Mike Stump) writes: >{just a couple questions for now} > >|Copyright 1987 Kent Paul Dolan. All Rights Reserved. Author grants >|retransmission rights recursively only. > [I guess I must include his copyright message...] > I think under the fair use doctrine, you could have omitted the copyright, but I am no lawyer. This was yet another in a small groundswell of attempts to persuade the Stargate project not to do damage to the nature of USENet in the (laudable) process of attempting to save sites money on phone bills. At present, USENet postings may be freely passed from site to site; we would lose this ability for news passed via Stargate as presently envisioned. The Stargate project's present plan proposes to copyright all the USENet material it broadcasts as a collective work, and to insist on recompense from Stargate subscribers who pass the material on over phone lines to additional sites, beyond the normal Stargate subscription fees. This problem is being adequately flamed in the Stargate newsgroup, please read/respond there, not here, on this topic. > Does optimization mean the changing of the internal workings of something, >such that the inputs, and the results or outputs of the ``black box'' are the >SAME, and of course, the process is less consuming in some way, typically >time? Is this definition use only in theory, or is it used in practice too? Optimization in non-pathological cases should be of the black-box type; if a=2, b=3, and c=4, then d = a-b+c has the same result as d = a+c-b. The problems being addressed in this (very, very long) discussion have to do with behavior in the pathological cases. As previous postings have shown, these are probably the majority, certainly in the case of floating point numbers. For example, if the implementation of floating point numbers in a particular hardware implementation is a signed, 4 bit exponent, and a signed, 12 bit mantissa, then the most positive representable number (in binary) is 11111111.1111. This being the case, then given (all binary): a = 10000000.0000; b = 00000001.0000; c = 10000000.0000; then the evaluation order (a - b) + c procedes with no problem, while the evaluation order (a + c) - b produces an overflow. If there were a hardware implementation which rewarded doing the add before the subtract, than an optimizing compiler might do the evaluation the second way, even though the programmer, knowing the possible data values for a, b, and c, had written the code using the (a - b) + c format, and expected never to see an overflow from this piece of code. The point of course, is that the compiler does not know what values will be assigned to a, b, and c at run time, and so does not know if that optimization is safe or not. The reason that discussion has been so prolonged, is that the programmer might know, and (some of us) want a nice way to tell the compiler not to muck about with that expression. The compiler writer, lacking guidance to the contrary, wants to muck about, because then the code will run faster, benchmark better, and the compiler will compete better in the marketplace (all else being equal). > Can you please give me an example of the case you alluded to, in your last >paragraph above? I have never felt the need to have anything but, the full >unrepressed optimizations, global or local, of the the compiler. With that I >have one comment, I can see the use of storing values into areas of memory (or >was that i/o, :-)) declared as volatile in C. The volatile declaration (I have no access to the dpANS, thank heaven, or I'd read that, too) certainly takes care of part of the problem, but it gives compiler writers license to break TONS of existing code, which depended on K&R's statement that splitting expressions into separate statements was enough to guarantee the evaluation order. If I wrote (all "float" variables): temp = a - b; d = temp + c; in a piece of existing code to avoid the above overflow; the compiler writer is now free to elide temp from the code, then go back and rearrange to achieve: d = (a + c) - b; exactly what I was trying to avoid, unless I declare temp to be volatile. Would _you_ like to go back through your site's existing C code and find all instances where that is why temp was put in the code, and add the appropriate volitile declaration? ;-) How about if a little maintenance activity has produced: temp = a - b; /* 2000 lines of code not involving temp, a, b, c, or d */ d = temp + c; which the ambitious compiler writer is STILL free to optimize into: /* 2000 lines of code not involving temp, a, b, c, or d */ d = (a + c) - b; Aaaaarargh! ;-) > What type of ``unexpected interactions of two pieces of code which were >certainly separate in the programmer's mind'' where you referring to in your >first paragraph? main(){... /* evreybody floats */ external float foo(); temp = a - b; d = foo(temp,c); /* only call of foo in any code in the known universe ;-) */ ... } /* 2,000,000 lines of kernal code and several hundred separate source files later, we finally find foo: ;-) */ foo(temp,c) ...{ temp2 = temp + c; return temp2; } Which the ambitious PhD student compiler writer doing a thesis on "AI Applications in Optimizing Compilers and Linkers in Spite of all Reasonable Attempts to Prevent it" is perfectly free to change, by putting the call to foo inline, and then converting to: d = (a + c) - b; (aw, you guessed ;-). >-- >Mike Stump, Cal State Univ, Northridge Comp Sci Department >uucp: {sdcrdcf, ihnp4, hplabs, ttidca, psivax, csustan}!csun!aeusemrs It's almost daylight, did any of that make more (any) sense? Kent. -- The Contradictor Member HUP (Happily Unemployed Programmers) // Also // A Back at ODU to learn how to program better (after 25 years!) \\ // Happy \// Amigan! UUCP : kent@xanth.UUCP or ...{sun,cbosgd,harvard}!xanth!kent CSNET : kent@odu.csnet ARPA : kent@xanth.cs.odu.edu Voice : (804) 587-7760 USnail: P.O. Box 1559, Norfolk, Va 23501-1559 Copyright 1987 Kent Paul Dolan. How about if we keep the human All Rights Reserved. Author grants race around long enough to see retransmission rights recursively only. a bit more of the universe?