comp.lang.ada
 help / color / mirror / Atom feed
* Getting modular type size in bits
@ 2003-03-14 15:03 Jano
  2003-03-14 15:41 ` Robert A Duff
  0 siblings, 1 reply; 5+ messages in thread
From: Jano @ 2003-03-14 15:03 UTC (permalink / raw)


Hello,

maybe I'm overlooking something really silly:

I have a generic function, for example:

generic
   type Number is Mod <>;
function Do(X: Number) return Number;

I need to perform operations based on the logical type size, i.e.
if the type is

type Number is Mod 2**32;

I want to know that the type size is 5. I'm aware of the 'modulus 
attribute, and 'size. If I understand correctly, 'size returns the size 
allocated by the compiler for the type (normally 8 in this case), not 
the _minimum_ size to allocate it (which is what I would need in that 
case).

I can think of performing a log2 op, or some iterative proc to find it, 
but I would know if there is some ultra-efficient method (because the 
compiler has to know, I suppose, that size), because that function could 
be potentially called many times in loops.

If I were using a generic package, I could perform that calculus in 
elaboration time, but is a generic function and I'd like to maintain it 
like that if possible. Other possibility but I can't think a mean of 
doing it would be a static expression who evaluates to that value, place 
it in the declarative part of the function, and hope that it will not be 
re-evaluated in each call... I don't know if it works that way.

I'm getting off course? Can you point me in the right direction?

Many thanks,

-- 
-------------------------
Jano
402450[at]cepsz.unizar.es
-------------------------



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

* Re: Getting modular type size in bits
  2003-03-14 15:03 Getting modular type size in bits Jano
@ 2003-03-14 15:41 ` Robert A Duff
  2003-03-14 15:48   ` Lutz Donnerhacke
  2003-03-14 21:05   ` Jano
  0 siblings, 2 replies; 5+ messages in thread
From: Robert A Duff @ 2003-03-14 15:41 UTC (permalink / raw)


Jano <402450@cepsz.unizar.es> writes:

> type Number is Mod 2**32;
> 
> I want to know that the type size is 5. I'm aware of the 'modulus 
> attribute, and 'size. If I understand correctly, 'size returns the size 
> allocated by the compiler for the type (normally 8 in this case), not 
> the _minimum_ size to allocate it (which is what I would need in that 
> case).

'Size returns the minimum -- i.e. how big it would be if it were a
component of a packed record.  That's 32 in this case.  If it were
"mod 2**5", then Number'Size would be 5, not 8.

- Bob



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

* Re: Getting modular type size in bits
  2003-03-14 15:41 ` Robert A Duff
@ 2003-03-14 15:48   ` Lutz Donnerhacke
  2003-03-14 17:20     ` Robert A Duff
  2003-03-14 21:05   ` Jano
  1 sibling, 1 reply; 5+ messages in thread
From: Lutz Donnerhacke @ 2003-03-14 15:48 UTC (permalink / raw)


* Robert A Duff wrote:
> Jano <402450@cepsz.unizar.es> writes:
>> I want to know that the type size is 5. I'm aware of the 'modulus 
>> attribute, and 'size. If I understand correctly, 'size returns the size 
>> allocated by the compiler for the type (normally 8 in this case), not 
>> the _minimum_ size to allocate it (which is what I would need in that 
>> case).
>
> 'Size returns the minimum -- i.e. how big it would be if it were a
> component of a packed record.  That's 32 in this case.  If it were
> "mod 2**5", then Number'Size would be 5, not 8.

$ cat t.adb
with Ada.Text_IO;

procedure t is
   type A is mod 2**5;
   for A'Size use 8;
begin
   Ada.Text_IO.Put_Line(Natural'Image(A'Size));
   declare
      type B is new A;
   begin
      Ada.Text_IO.Put_Line(Natural'Image(B'Size));
   end;
end t;
$ ./t
 8
 8
$ _

     



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

* Re: Getting modular type size in bits
  2003-03-14 15:48   ` Lutz Donnerhacke
@ 2003-03-14 17:20     ` Robert A Duff
  0 siblings, 0 replies; 5+ messages in thread
From: Robert A Duff @ 2003-03-14 17:20 UTC (permalink / raw)


Lutz Donnerhacke <lutz@iks-jena.de> writes:

> * Robert A Duff wrote:
> > Jano <402450@cepsz.unizar.es> writes:
> >> I want to know that the type size is 5. I'm aware of the 'modulus 
> >> attribute, and 'size. If I understand correctly, 'size returns the size 
> >> allocated by the compiler for the type (normally 8 in this case), not 
> >> the _minimum_ size to allocate it (which is what I would need in that 
> >> case).
> >
> > 'Size returns the minimum -- i.e. how big it would be if it were a
> > component of a packed record.  That's 32 in this case.  If it were
> > "mod 2**5", then Number'Size would be 5, not 8.
> 
> $ cat t.adb
> with Ada.Text_IO;
> 
> procedure t is
>    type A is mod 2**5;
>    for A'Size use 8;
> begin
>    Ada.Text_IO.Put_Line(Natural'Image(A'Size));
>    declare
>       type B is new A;
>    begin
>       Ada.Text_IO.Put_Line(Natural'Image(B'Size));
>    end;
> end t;
> $ ./t
>  8
>  8
> $ _

So?  You asked for 8, and you got 8.  And it will be (at least) 8 bits
in a packed record.  If you take out the "for A'Size ...", which was not
in the original poster's example, it should be 5.

- Bob



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

* Re: Getting modular type size in bits
  2003-03-14 15:41 ` Robert A Duff
  2003-03-14 15:48   ` Lutz Donnerhacke
@ 2003-03-14 21:05   ` Jano
  1 sibling, 0 replies; 5+ messages in thread
From: Jano @ 2003-03-14 21:05 UTC (permalink / raw)


Robert A Duff (bobduff@shell01.TheWorld.com) dice...
> Jano <402450@cepsz.unizar.es> writes:
> 
> > type Number is Mod 2**32;
> > 
> > I want to know that the type size is 5. I'm aware of the 'modulus 
> > attribute, and 'size. If I understand correctly, 'size returns the size 
> > allocated by the compiler for the type (normally 8 in this case), not 
> > the _minimum_ size to allocate it (which is what I would need in that 
> > case).
> 
> 'Size returns the minimum -- i.e. how big it would be if it were a
> component of a packed record.  That's 32 in this case.  If it were
> "mod 2**5", then Number'Size would be 5, not 8.

Sorry about the typo. Really, the declaration should be, as you 
correctly note,

type Number is mod 2 ** 5;

<minutes later>

Argh, and testing now 'size, I see it returns 5. Mind you, to that I was 
refering with "silly thing". Because I tested it previously and made the 
same mistake, using the modulus in place of the size!!!!!! Silly me, 
silly me, silly me... now I should be happy because my problem is solved  
:) but anyways...

In regard to Lutz post, is fine for me to obtain size 8 if you force the 
size explicitely.

Many thanks,

-- 
-------------------------
Jano
402450[at]cepsz.unizar.es
-------------------------



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

end of thread, other threads:[~2003-03-14 21:05 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-03-14 15:03 Getting modular type size in bits Jano
2003-03-14 15:41 ` Robert A Duff
2003-03-14 15:48   ` Lutz Donnerhacke
2003-03-14 17:20     ` Robert A Duff
2003-03-14 21:05   ` Jano

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