Path: csiph.com!weretis.net!feeder6.news.weretis.net!news.misty.com!news.iecc.com!.POSTED.news.iecc.com!nerds-end From: antispam@math.uni.wroc.pl Newsgroups: comp.compilers Subject: Re: Undefined Behavior Optimizations in C Date: Fri, 13 Jan 2023 20:46:28 -0000 (UTC) Organization: Aioe.org NNTP Server Sender: news@iecc.com Approved: comp.compilers@iecc.com Message-ID: <23-01-055@comp.compilers> References: <23-01-009@comp.compilers> <23-01-011@comp.compilers> <23-01-012@comp.compilers> <23-01-017@comp.compilers> <23-01-018@comp.compilers> <23-01-021@comp.compilers> Injection-Info: gal.iecc.com; posting-host="news.iecc.com:2001:470:1f07:1126:0:676f:7373:6970"; logging-data="18899"; mail-complaints-to="abuse@iecc.com" Keywords: C, Fortran, comment Posted-Date: 13 Jan 2023 16:21:12 EST X-submission-address: compilers@iecc.com X-moderator-address: compilers-request@iecc.com X-FAQ-and-archives: http://compilers.iecc.com X-Notice: Filtered by postfilter v. 0.9.2 Xref: csiph.com comp.compilers:3323 Spiros Bousbouras wrote: > On Fri, 6 Jan 2023 10:33:29 -0800 (PST) > gah4 wrote: > > It turns out, though, to have a fair number of statements like: > > > > IF(I.GT.0.AND.X(I).EQ.3) ... > > > > That is, test the subscript and use it in the same IF statement. > > > > Now, this would not be a problem at all in C, with short-circuit IF > > evaluation required, but even now Fortran doesn't do short > > circuit IF evaluation. ... > > The analogous C code would be > if (I > 0 && X[I] == 3) ... No, Fortran array start from 1, C arrays start from 0. And Fortran is not obliged do do short circuit evaluation. So analogous C code would be char bv1 = (I>=0); char bv2 = (X[I] == 3); if (bv1 && bv2) ... I this code compiler may throw out 'bv1' (assume that it is true). Of course, normal C programmer would write if ((I>=0)&&(X[I] == 3)) which is fine due to short circuit evaluation. Reversing order: if ((X[I] == 3)&&(I>=0)) have the same problem as first version. Both first version and third version may appear as output of generators or due to macro expansion, so this is not entirely theoretical problem. OTOH, in Fortran and Pascal classic teaching was "do not do this". This was particularly important in Pascal, as standard required full evaluation. -- Waldek Hebisch [You can start Fortran arrays at zero if you want DIMENSION FOO(0:99) But I agree mostly people use the 1 default -John]