Path: csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: the language (and the role of Undefined Behavior therein) Date: Mon, 24 Aug 2015 18:21:07 -0700 Organization: None to speak of Lines: 44 Message-ID: References: <5492c298.300765@news.xs4all.nl> <90183436-b839-4ee9-a3ed-a9eab341e847@googlegroups.com> <_MQAx.4834$IB7.2230@fx07.iad> <896d7bb0-72f3-4905-847e-8f6b6246cfc0@googlegroups.com> <55D4E2C9.8030109@verizon.net> <430b14cb-c335-4e54-94c5-d1076a2142fe@googlegroups.com> <55D51DE6.6070606@verizon.net> <75e8b3e2-4171-40bf-9e2d-c3c132272dec@googlegroups.com> <1f86d431-4ab2-447e-830b-5878781225fc@googlegroups.com> <044f1f29-894c-46c1-9ff2-61ba5191c3f5@googlegroups.com> <026de000-b3bd-44b7-914e-0274671a3687@googlegroups.com> <2340e1b4-d142-4a20-986b-089e9f712c9c@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="945944de09706c9b4e29b53c9d2efdc2"; logging-data="16128"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19AVNX/aDfiFxZFXUERhQ94" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:CZ0JpKkAV84tfkk122BpQjTpPkg= sha1:U6L/oDB3QqE437xNx+RqvWJOAKg= Xref: csiph.com comp.lang.c:68285 David Kleinecke writes: > On Sunday, August 23, 2015 at 8:30:00 PM UTC-7, Keith Thompson wrote: >> David Kleinecke writes: [...] >> > Signed arithmetic is also a version of modulo UINT_MAX+1 with a >> > different way of writing some of the integers. >> >> No, it isn't. C doesn't specify wraparound behavior for signed >> integers; signed integer overflow has undefined behavior. > > All the C90 standard says is "The result of the binary + operator is > the sum of the the operands." "Sum" is nowhere defined. I haven't > found any discussion of "overflow". C90 6.3, 5th paragraph (C11 6.5p2 changes "exception" to "exceptional condition"): If an *exception* occurs during the evaluation of an expression (that is, if the result is not mathematically defined or not representable), the behavior is undefined. This applies to signed and floating-point types, but not to unsigned types, since the standard explicitly defines the result of an unsigned operation whose mathematical value is outside the range of the type (C90 6.1.2.5, C11 6.2.5p9). >> If by "signed arithmetic" you mean something other that what's >> specified by C (such as, perhaps, the signed arithmetic operations >> implemented by some particular CPU), you should make that clear. > > I doubt that any CPU does other than arithmetic modulo-two-to-the- > number-of-bits-power. Different CPU's set different sets of flags - > so the interpretations can differ. CPUs that use one's-complement or sign-and-magnitude almost certainly do not operate that way. I believe some CPUs can be configured to trap on overflow, or to do other odd things like saturating. I concede that such behavior is rare, but you did say "any CPU". -- 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"