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=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!news.eternal-september.org!news.eternal-september.org!feeder.eternal-september.org!newsfeed.fsmpi.rwth-aachen.de!news-2.dfn.de!news.dfn.de!news.uni-weimar.de!medsec1.medien.uni-weimar.de!lucks From: Stefan.Lucks@uni-weimar.de Newsgroups: comp.lang.ada Subject: Re: integers of 1, 2, 4 bytes Date: Sun, 15 Jun 2014 21:26:28 +0200 Organization: Bauhaus-Universitaet Weimar Message-ID: References: NNTP-Posting-Host: medsec1.medien.uni-weimar.de Mime-Version: 1.0 Content-Type: MULTIPART/MIXED; BOUNDARY="8323329-274713839-1402860390=:4330" X-Trace: pinkpiglet.scc.uni-weimar.de 1402860333 12743 141.54.178.228 (15 Jun 2014 19:25:33 GMT) X-Complaints-To: news@pinkpiglet.scc.uni-weimar.de NNTP-Posting-Date: Sun, 15 Jun 2014 19:25:33 +0000 (UTC) X-X-Sender: lucks@debian In-Reply-To: User-Agent: Alpine 2.10 (DEB 1266 2009-07-14) Xref: news.eternal-september.org comp.lang.ada:20326 Date: 2014-06-15T21:26:28+02:00 List-Id: This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. --8323329-274713839-1402860390=:4330 Content-Type: TEXT/PLAIN; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: QUOTED-PRINTABLE On Sun, 15 Jun 2014, hreba wrote: > My Ada book says that range checks are applied to constrained subtypes=20 > (like that one) but not to unconstrained subtypes, but but overflow check= s=20 > are applied nevertheless. So I came up with > > type Int16 is range -32768 .. 32767; > for Int16'Size use 16; > type INT is new Int16'Base; If you *want* to get rid of range checks, there are pragmas to disable=20 them. (Not that I would consider this a good idea at all ...). And I am=20 not a language lawyer, but I claim that the Ada compiler will perform=20 exactly the same range checks for INT, as it does for Int16 (see below). So please just use Int16 as defined in type Int16 is range -32768 .. 32767; for Int16'Size use 16; and disable range checking when and where you really need aggressively=20 optimized codes (e.g., in the innermost loop ...). So why does Ada perform range checks for INT? The reason is that INT is=20 not "unconstrained", but actually in the range INT'First .. INT'Last for=20 some INT'First and INT'Last of the compilers choice. INT'First might be=20 smaller than Int16'First, and INT'Last might exceed Int16'Last, but there= =20 are still ranges to be checked. And, given the Size attribute, there=20 appears no choice for the compiler anyway: INT'FIRST=3DInt16'First and=20 INT'Last=3DInt16'Last imply exactly the same range chekcs for INT as for=20 Int16. > as a more efficient definition. If you have specific performance constraints, you should try to optimize=20 specific subprograms or code fragments, e.g., by disabling range checks=20 within the innermost loop or so. > Now it seems the range specification doesn't matter any more and that I= =20 > could equally write > > type Int16 is range -1 .. 1; > for Int16'Size use 16; > type INT is new Int16'Base; > > Would you really define a 16 bit integer type this way? Firstly, this makes your program hard to read, so this is poor software=20 engineering. Secondly, it does not do what you want it to do -- i.e., it=20 does not omit the range checks. And finally, the Ada compiler has a great= =20 deal of freedom what Int16'Base actually is. While I find it hard to=20 imagine that an Ada compiler for any target machine with 2-complement=20 arithmetic would not set Int16'Base to the range -2**15 .. +2**15-1, the=20 Ada compiler might still surprise you. > And how about non-negative types? Would the following have the values > 0 .. 255? Now for non-negative integers, there are modular types, which actually=20 support arithmetic operations without range checking: type Byte is mod 2**8; for Byte'Size use 8; (And similar for 2- and 4-byte arithmetic ...) ------ I love the taste of Cryptanalysis in the morning! ------ --Stefan.Lucks (at) uni-weimar.de, Bauhaus-Universit=E4t Weimar, Germany-- --8323329-274713839-1402860390=:4330--