Path: csiph.com!xmission!news.snarked.org!news.linkpendium.com!news.linkpendium.com!news.iecc.com!.POSTED.news.iecc.com!nerds-end From: Martin Ward Newsgroups: comp.compilers Subject: Re: Optimization techniques and undefined behavior Date: Thu, 2 May 2019 14:54:46 +0100 Organization: Compilers Central Lines: 33 Sender: news@iecc.com Approved: comp.compilers@iecc.com Message-ID: <19-05-007@comp.compilers> References: <19-05-003@comp.compilers> <19-04-021@comp.compilers> <19-04-023@comp.compilers> <19-04-037@comp.compilers> <19-04-039@comp.compilers> <19-04-042@comp.compilers> <19-04-045@comp.compilers> <19-04-049@comp.compilers> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Info: gal.iecc.com; posting-host="news.iecc.com:2001:470:1f07:1126:0:676f:7373:6970"; logging-data="66462"; mail-complaints-to="abuse@iecc.com" Keywords: optimize, arithmetic, errors Posted-Date: 02 May 2019 12:09:37 EDT 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:2243 On 01/05/19 12:40, Bart wrote: > for (int i=0; i<1000000000; ++i) { > y=x*3/3; > z+=y; > ++x; > } Given that gcc can optimise y=x*3/3 to y=x I would have expected the whole loop to be recognised as computing the sum of an arithmetic sequence and therefore optimised away completely. This blog post: https://xebia.com/blog/gcc-compiler-optimizations-dissection-of-a-benchmark/ discusses a program with the loop: for( a = 0; a < arg1; a++ ) { sum += a; } and claims that gcc with -O3 will optimise the loop to take only costant time. But this is not the case on my machine with gcc 4.9.2 -- Martin Dr Martin Ward | Email: martin@gkc.org.uk | http://www.gkc.org.uk G.K.Chesterton site: http://www.gkc.org.uk/gkc | Erdos number: 4