Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: Is this code considered to transgress the signed overflow purism? Date: Thu, 27 Apr 2017 09:38:51 -0700 Organization: None to speak of Lines: 34 Message-ID: References: <11ea6682-cdee-4513-97c2-6c90b585716a@googlegroups.com> <307ebff5-b74f-46ab-beb7-7e2e4ff494fb@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="f92ede553c9c24f94ddbe371a05a728b"; logging-data="30627"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19nyJWF4E0MHvksT2rgF6gW" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:kYy/tGBv7NJn6pmlERb3ShH72B4= sha1:a3u852a51Bnn+74xAJZAFpMf7bg= Xref: csiph.com comp.lang.c:108611 supercat@casperkitty.com writes: [...] > The clear intention of the Standard is that on a sign-magnitude platform > whose hardware may randomly catch fire if the left-shift instruction is > fed a negative number, a compiler for such a platform would have no > obligation to prevent that from happening. I see no evidence that the > authors of the Standard expected or intended that implementations > targeting two's-complement hardware would should not opt to behave "in a > documented manner characteristic of the environment". The authors of C89 > deliberately avoided defining behaviors that would be applicable to some > machines but not others, figuring that compiler writers and programmers > alike should be able to recognize the obvious useful cases. The authors of the C89 standard deliberately stated that a left shift on a negative value has undefined behavior. "behaving during translation or program execution in a documented manner characteristic of the environment" (that's quoted from a note on the definition of "undefined behavior") is just one of the possible behaviors, and it is not emphasized over any of the others. An optimizing compiler may assume that the left operand of a left shift operator is non-negative. If that assumption is violated, the resulting code may behave differently than if the compiler generated a shift instruction. A compiler might also generate code to check that the left operand is non-negative and trap if it isn't. Don't you get tired of making the same point again and again and again? -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for JetHead Development, Inc. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"