comp.lang.ada
 help / color / mirror / Atom feed
From: Brian Nettleton <bn@aonix.com>
To: brian.orpin@gecm.com
Subject: Re: Representation clauses and records
Date: 1997/11/21
Date: 1997-11-21T00:00:00+00:00	[thread overview]
Message-ID: <3475F02C.5585B1B6@aonix.com> (raw)
In-Reply-To: 347000b1.4909762@news.geccs.gecm.com


Brian Orpin wrote:

> I have played with this before but never fully understood it.  Now I need
> to understand it and my colleagues and I have had many hours discussing
> it.  The compiler being used is Ada 83.
>
> The question is simple; when declaring a representation clause for a
> record there is an optional alignment clause using 'mod at'.  What effect
> does this have on the component representation clause and how does this
> relate to the storage size of the system?
>
> Having read the LRM I get the impression that it is to do with word
> boundaries and alignment but I do not understand the implications of
> using different values for the 'at mod' statement.
>
> Any explanations gratefully received.  This is not homework :-))
>
> --
> Brian Orpin    (These thoughts are my own ......... for once!)
> brian.orpin@ge_XXX_cm.com or BrianOrpin@Big_XXX_foot.com
> http://www.borpin.demon.co.uk/  **Anti-spam reply-to remove X**

Alignment is an important consideration for the representation of data
items at the hardware level.  Many (most?) hardware
architectures/implementations
(ie. Intel Pentium, PowerPC, SPARC, etc.) have performance penalties
associated with mis-aligned data items.  Loading a word into a register
for a mis-aligned address usually involves either extra instructions or extra

hardware cycles.  This means that there are different space/time tradeoffs
for
different alignments.  Consider (simple example with 32 bit word assumption):

  type Space_Optimized_Array_Element is record
    Field1 : Integer;
    Field2 : Boolean;
  end record;

  for Space_Optimized_Array_Element use record
    at mod 1;
    Field1 at 0 range 0 .. 31;
    Field2 at 4 range 0 .. 8;
  end record;

  type Space_Optimized_Array_Type is array (1..2) of
Space_Optimized_Array_Element;
  pragma Pack(Space_Optimized_Array); -- Ada83 method to assure space opt.

  Space_Optimized_Array : Space_Optimized_Array_Type;

And:

  type Time_Optimized_Array_Element is record
    Field1 : Integer;
    Field2 : Boolean;
  end record;

  for Time_Optimized_Array_Element use record
    at mod 4;
    Field1 at 0 range 0 .. 31;
    Field2 at 4 range 0 .. 8;
  end record;

  type Time_Optimized_Array_Type is array (1..2) of
Time_Optimized_Array_Element;

  Time_Optimized_Array : Time_Optimized_Array_Type;


Layout of Space_Optimized_Array and Time_Optimized_Array:

Offset (in bytes)        Data Item
----------------        ----------
0                               Space_Optimized_Array(1).Field1
4                               Space_Optimized_Array(1).Field2
5 --misaligned           Space_Optimized_Array(2).Field1
9                               Space_Optimized_Array(2).Field2

0                                Time_Optimized_Array(1).Field1
4                                Time_Optimized_Array(1).Field2
8 --aligned                 Time_Optimized_Array(2).Field1
12                              Time_Optimized_Array(2).Field2


Now the Space_Optimized_Array should be 10 bytes in length and accessing
Field1 may take more CPU cycles, and Time_Optimized_Array is generally 16
bytes in length, but accesses to Field1 should always take a minimum number
of CPU cycles.

-Brian Nettleton
Aonix






       reply	other threads:[~1997-11-21  0:00 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <347000b1.4909762@news.geccs.gecm.com>
1997-11-21  0:00 ` Brian Nettleton [this message]
1997-11-21  0:00   ` Representation clauses and records Brian Nettleton
1997-11-24  0:00     ` Martin M Dowie
1997-11-25  0:00       ` Robert Dewar
1997-11-27  0:00         ` Martin M Dowie
1997-11-21  0:00   ` Matthew Heaney
     [not found]   ` <347a8dc3.3438484@news.geccs.gecm.com>
1997-11-25  0:00     ` Matthew Heaney
1997-12-16  0:00 ` Pascal MALAISE
1997-12-16  0:00   ` Larry Kilgallen
1997-12-17  0:00     ` Pascal MALAISE
1997-12-18  0:00       ` David J. Fiander
1997-12-18  0:00         ` Tucker Taft
1997-12-18  0:00         ` Robert Dewar
replies disabled

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