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 12:10:00 -0700 Organization: None to speak of Lines: 44 Message-ID: References: <11ea6682-cdee-4513-97c2-6c90b585716a@googlegroups.com> <307ebff5-b74f-46ab-beb7-7e2e4ff494fb@googlegroups.com> <6f83a74b-776e-4941-882a-20b8c7620ce4@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="f92ede553c9c24f94ddbe371a05a728b"; logging-data="10722"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18cpinOUeA4Uh3ZrBFqDgIC" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:V0gx0KRsq5fW+8BqV/JTUBYjI+4= sha1:obQ8Z7ZNiv0gx6RhZEzEwcwcSPA= Xref: csiph.com comp.lang.c:108616 supercat@casperkitty.com writes: > On Thursday, April 27, 2017 at 11:39:02 AM UTC-5, Keith Thompson wrote: >> 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. > > Such behavior wouldn't be applicable on all machines, and the Standard > seeks to avoid showing favoritism to particular architectures. > > On the other hand, I'm curious what evidence you could find from before > 2000 that would suggest that left-shifting of negative numbers would have > been expected to have any adverse implications *other than incompatibility > with non-two's-complement hardware, or with configuration options that > would explicitly trap it*. I haven't said or implied that there is any such evidence, and I'm not sufficiently interested in the question to go looking. I find the wording of the standard clear enough. Left-shifting of negative numbers is not an operation that's supported by C, so I'm not going to try to use it. >> 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. > > Doing so would not make a compiler non-conforming. It would merely limit > the range of purposes for which the compiler was suitable. It would be more useful for the purpose of detecting code whose behavior is undefined. [...] -- 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"