HP3000-L Archives

July 1996, Week 2

HP3000-L@RAVEN.UTC.EDU

Options: Use Monospaced Font
Show Text Part by Default
Show All Mail Headers

Message: [<< First] [< Prev] [Next >] [Last >>]
Topic: [<< First] [< Prev] [Next >] [Last >>]
Author: [<< First] [< Prev] [Next >] [Last >>]

Print Reply
Subject:
From:
Stan Sieler <[log in to unmask]>
Reply To:
Stan Sieler <[log in to unmask]>
Date:
Wed, 10 Jul 1996 15:46:06 -0700
Content-Type:
text/plain
Parts/Attachments:
text/plain (120 lines)
Carrying on the alignment discussion in C (whose subject line I've
forgotten), here's an example using the "#pragma HP_ALIGN" approach:
 
 
/* test.c  95/08/29  nm  info "-Aa -w3 -DHPXL=1"
auxobj libcansi.lib.sys
auxobj libcinit.lib.sys
-e test.pub
*/
 
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
 
#pragma HP_ALIGN MPE_16          /* MPE_16 not understood by lint */
 
struct cell_type1 {
    short field1;
    int   field2;
    };
 
#pragma HP_ALIGN NATURAL
 
struct cell_type2 {
    short field1;
             /* waste 2 bytes */
    int   field2;
    };
 
#pragma HP_ALIGN NOPADDING PUSH
 
struct cell_type3 {
    short field1;
    int   field2;
    };
 
#pragma HP_ALIGN POP
 
     /* the above "POP" reverts to the value *before* the prior HP_ALIGN */
     /* easy to verify...add a "#pragma HP_ALIGN NOPADDING" before the   */
     /* above PUSH line and you'll see that cell_type4 suddenly becomes  */
     /* 2 bytes smaller (because NOPADDING is in effect after the POP).  */
 
struct cell_type4 {
    short field1;
             /* waste 2 bytes */
    int   field2;
    };
 
#define SHOW(what,a1,b1,a2,b2) show_addr (what, (int) a1, b1, (int) a2,b2)
 
void show_addr (char *what, int a1, int b1, int a2, int b2)
   {
 
   int
      waste;
 
   waste = (a2 - (a1 + b1));
 
   if (waste != 0)
      (void) printf ("%s: %X %X, waste = %d\n", what, a1, a2, waste);
   else
      (void) printf ("%s: %X %X, no waste\n", what, a1, a2, waste);
 
   }
 
   /* Note: if HP_ALIGN moved to inside main(), won't work! */
 
#pragma HP_ALIGN NOPADDING
 
int main ()
{
 
struct cell_type1 cell1;
struct cell_type2 cell2;
struct cell_type3 cell3;
struct cell_type4 cell4;
 
struct cell_type5 {
    short field1;
    int   field2;
    };
 
struct cell_type5 cell5;
 
 
SHOW ("cell1         ",
                  &cell1.field1, sizeof (cell1.field1),
                  &cell1.field2, sizeof (cell1.field2));
 
SHOW ("cell2         ",
                  &cell2.field1, sizeof (cell2.field1),
                  &cell2.field2, sizeof (cell2.field2));
 
SHOW ("cell3         ",
                  &cell3.field1, sizeof (cell3.field1),
                  &cell3.field2, sizeof (cell3.field2));
 
SHOW ("cell4         ",
                  &cell4.field1, sizeof (cell4.field1),
                  &cell4.field2, sizeof (cell4.field2));
 
SHOW ("cell5         ",
                  &cell5.field1, sizeof (cell5.field1),
                  &cell5.field2, sizeof (cell5.field2));
 
return 0;
}
 
When compiled & run, the "even numbered" cell_types have 2 waste bytes
in the middle, the "odd numbered" ones don't.
 
NOTE: moving the "#pragma HP_ALIGN" *into* a routine seems to make it not
work...which is counterintuitive (and quite unlike Pascal/iX's treatment
of similar directives).
 
--
Stan Sieler                                          [log in to unmask]
                                     http://www.allegro.com/sieler.html

ATOM RSS1 RSS2