comp.lang.ada
 help / color / mirror / Atom feed
* Curiosity about different integer types
@ 2016-06-18 21:26 mockturtle
  2016-06-18 21:57 ` Dmitry A. Kazakov
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: mockturtle @ 2016-06-18 21:26 UTC (permalink / raw)


Dear all,
few days ago a curiosity came to my mind.  Consider this

  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...   

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Curiosity about different integer types
  2016-06-18 21:26 Curiosity about different integer types mockturtle
@ 2016-06-18 21:57 ` Dmitry A. Kazakov
  2016-06-18 22:04 ` Björn Lundin
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Dmitry A. Kazakov @ 2016-06-18 21:57 UTC (permalink / raw)


On 2016-06-18 23:26, mockturtle wrote:

> few days ago a curiosity came to my mind.  Consider this
>
>   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...

No. I am not a language lawyer, but

    type Foo is new Integer range 0 .. 115;

is a kind of  abbreviation of

    type <anonymous> is new Integer; -- Clone
    subtype Foo is <anonymous> range 0..115; -- Constrain

The relevant point is IMO ARM 3.4 (9)

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Curiosity about different integer types
  2016-06-18 21:26 Curiosity about different integer types mockturtle
  2016-06-18 21:57 ` Dmitry A. Kazakov
@ 2016-06-18 22:04 ` Björn Lundin
  2016-06-18 23:05 ` Jeffrey R. Carter
  2016-06-20 11:39 ` mockturtle
  3 siblings, 0 replies; 5+ messages in thread
From: Björn Lundin @ 2016-06-18 22:04 UTC (permalink / raw)


On 2016-06-18 23:26, mockturtle wrote:
> Dear all,
> few days ago a curiosity came to my mind.  Consider this
> 
>   type Foo is new Integer range 0 .. 115;
>   type Bar is new Integer range -1000 .. 1000;

try add
for Foo'Size use 8;
and they are different

> 
> 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.

to make them same do

for Foo'Size use Bar'Size;

-- 
--
Björn

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Curiosity about different integer types
  2016-06-18 21:26 Curiosity about different integer types mockturtle
  2016-06-18 21:57 ` Dmitry A. Kazakov
  2016-06-18 22:04 ` Björn Lundin
@ 2016-06-18 23:05 ` Jeffrey R. Carter
  2016-06-20 11:39 ` mockturtle
  3 siblings, 0 replies; 5+ messages in thread
From: Jeffrey R. Carter @ 2016-06-18 23:05 UTC (permalink / raw)


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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Curiosity about different integer types
  2016-06-18 21:26 Curiosity about different integer types mockturtle
                   ` (2 preceding siblings ...)
  2016-06-18 23:05 ` Jeffrey R. Carter
@ 2016-06-20 11:39 ` mockturtle
  3 siblings, 0 replies; 5+ messages in thread
From: mockturtle @ 2016-06-20 11:39 UTC (permalink / raw)


Thank you to all for your answers.  Now everything it is clearer.

Riccardo

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2016-06-20 11:39 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-06-18 21:26 Curiosity about different integer types mockturtle
2016-06-18 21:57 ` Dmitry A. Kazakov
2016-06-18 22:04 ` Björn Lundin
2016-06-18 23:05 ` Jeffrey R. Carter
2016-06-20 11:39 ` mockturtle

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox