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!mx02.eternal-september.org!.POSTED!not-for-mail From: "Jeffrey R. Carter" Newsgroups: comp.lang.ada Subject: Re: Curiosity about different integer types Date: Sat, 18 Jun 2016 16:05:21 -0700 Organization: Also freenews.netfront.net; news.tornevall.net; news.eternal-september.org Message-ID: References: <63c033c6-10b5-44fc-8d93-a786392a6058@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Injection-Date: Sat, 18 Jun 2016 23:05:28 -0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="48b46be33beed75863f69afa437f956b"; logging-data="20157"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18KhuxwcnMeeTAejY8cgzL6hMC3OJM1Pzg=" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.8.0 In-Reply-To: <63c033c6-10b5-44fc-8d93-a786392a6058@googlegroups.com> Cancel-Lock: sha1:gofp3oUzIHaJbmXUSuXO2jaj9rU= X-Enigmail-Draft-Status: N1110 Xref: news.eternal-september.org comp.lang.ada:30799 Date: 2016-06-18T16:05:21-07:00 List-Id: On 06/18/2016 02:26 PM, mockturtle wrote: > > type Foo is new Integer range 0 .. 115; > > type Bar is new Integer range -1000 .. 1000; > > My gut feeling is that, despite the fact that they are both derived from Integer, there is no guarantee that the "binary type" underneath them is the same. For example, Foo could be implemented as an 8-bit integer, while Bar could be implemented as a 16-bit integer, or maybe both could be implemented as (say) 32-bit integers. > > Am I right? I tried to do some experiments with gcc and both types are implemented with the same binary type, but it could be only for efficiency reasons... "new T" means that the base type should be the same as T'Base. Note that all operations are done using a representation with at least the range of the base type. So Foo'Base will have the same representation as Integer'Base. Using representation clauses, you can arrange for objects of your types to be stored in less space than Integer. But any operations on those values will expand them to the size of Integer (or larger). "new T" is not something that you should use very often, especially for a T that's a predefined type like Integer. It's usually better to declare type Foo is range 0 .. 115; type Bar is range -1000 .. 1000; and let the compiler choose the representation of the base type. Even if you need to guarantee a certain base range for some intermediate values, it's usually better to declare that range explicitly. type Foo_Base is range -(2 ** 31 - 1) .. 2 ** 31 - 1; subtype Foo is Foo_Base range 0 .. 115; Making Foo "new Integer" to get a similar base range might earn you a nasty surprise if you port your code to a compiler that has 16-bit Integer. -- Jeff Carter "My legs are gray, my ears are gnarled, my eyes are old and bent." Monty Python's Life of Brian 81