Path: csiph.com!weretis.net!feeder6.news.weretis.net!news.misty.com!news.iecc.com!.POSTED.news.iecc.com!nerds-end From: Keith Thompson Newsgroups: comp.compilers Subject: Re: Undefined Behavior Optimizations in C Date: Fri, 20 Jan 2023 13:54:36 -0800 Organization: None to speak of Sender: johnl@iecc.com Approved: comp.compilers@iecc.com Message-ID: <23-01-068@comp.compilers> References: <23-01-027@comp.compilers> <23-01-031@comp.compilers> <23-01-041@comp.compilers> <23-01-062@comp.compilers> <23-01-063@comp.compilers> <23-01-066@comp.compilers> MIME-Version: 1.0 Content-Type: text/plain Injection-Info: gal.iecc.com; posting-host="news.iecc.com:2001:470:1f07:1126:0:676f:7373:6970"; logging-data="25720"; mail-complaints-to="abuse@iecc.com" Keywords: C, arithmetic Posted-Date: 20 Jan 2023 22:09:05 EST X-submission-address: compilers@iecc.com X-moderator-address: compilers-request@iecc.com X-FAQ-and-archives: http://compilers.iecc.com Xref: csiph.com comp.compilers:3336 gah4 writes: [snip] > For many years, C allowed for sign-magnitude and ones' complement > representation. It still does. The upcoming 2023 ISO C standard mandates 2's-complement for signed integer types, but it hasn't been published yet. [...] > Now, C has unsigned int which you can use when you need specific > overflow behavior (except on some Unisys machines). If a Unisys C implementation doesn't behave as the standard requires with respect to unsigned overflow, then it's a non-conforming implementation. (Non-conforming implementations can still be useful.) [...] > As C allows for both UB and system-dependent behavior, and it is > hard for people to remember every case of each one, it is unreasonable > to me, to assume fixed point overflow is UB. C has: - Undefined behavior (the standard imposes no requirements); - Unspecified behavior (the standard provides 2 or more possibilities and implementations can choose arbitrarily; an example is the order of evaluation of function arguments); and - Implementation-defined behavior (unspecified behavior where the implementation must document its choice). Signed integer overflow has undefined behavior -- not because it is or isn't reasonable, but because the standard says so. Even in C23, the mandate for 2's-complement representation doesn't imply a requirement for 2's-complement behavior on overflow. [...] -- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com Working, but not speaking, for XCOM Labs void Void(void) { Void(); } /* The recursive call of the void */