From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,25b9eb5c3a89bced X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!t-online.de!newsfeed.freenet.de!newsfeed01.chello.at!newsfeed02.chello.at!news.hispeed.ch.POSTED!not-for-mail Message-ID: <1201812.r16X2gYOJE@linux1.krischik.com> From: Martin Krischik Subject: Re: where exactly c++,c fail and Ada gets thru' Newsgroups: comp.lang.ada Date: Sat, 29 Apr 2006 09:03:59 +0200 References: <1145852356.559455.222600@i39g2000cwa.googlegroups.com> <1145855124.720029.35280@t31g2000cwb.googlegroups.com> <1235818.stAph9vF03@linux1.krischik.com> <1146207623.635562.289570@i39g2000cwa.googlegroups.com> User-Agent: KNode/0.10.1 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7Bit X-Complaints-To: abuse@hispeed.ch Organization: hispeed.ch NNTP-Posting-Host: 80.218.119.160 (80.218.119.160) NNTP-Posting-Date: Sat, 29 Apr 2006 10:00:04 +0200 X-Trace: 7da9b44531d04f57fd20503241 Xref: g2news2.google.com comp.lang.ada:3997 Date: 2006-04-29T09:03:59+02:00 List-Id: Maciej Sobczak wrote: > Martin Krischik wrote: > >> The point is - of corse - it that the semantic tree of the Ada >> compiler contains the information that X will be in the range of (1 .. >> 5) and then the optimizer can easily calculate that (1 .. 5) + 3 < 10 >> and then optimize away the check. >> >> The sematinc tree of the C++ will contain the information that X is in >> the range of (-2*32 .. +2*32-1) and that is a pretty useless >> information to the optimizer. > > Except that the optimizer is not limited to use only this information. > With the two example ranges (1..5 and 1..10) the result of inlining will > be that the following operations - all operating on the single value - > will be placed one after another: > > 1. compare against 5 > 2. add 3 > 3. compare against 10 > > It doesn't take a rocket scientist to deduce that the third operation > has a dead branch and can be therefore eliminated. But it is the callers responsibility to check the input parameter before calling the function. That way range checks need only be performed on input and type convertions. Remember that Ada defines + as: function "+" is (L, R: in Integer'Base) return Integer'Base; Back to my example: If you call F(3) no check is needed. The caller knows that 3 is in range and the callee knows that X + 3 is in range. Of course: If the optimizer spots that is a different story. Now I do wonder if REH managed some template/boolean magic to get the same result on C++. Martin -- mailto://krischik@users.sourceforge.net Ada programming at: http://ada.krischik.com