HP C/iX Enhancements [ COMMUNICATOR 3000 MPE MPE/iX RELEASE 4.0 ] MPE/iX Communicators
COMMUNICATOR 3000 MPE MPE/iX RELEASE 4.0
HP C/iX Enhancements
by Erryl Johnson
Systems Technology Division
16-BIT DATA ALIGNMENT IN STRUCTS AND UNIONS
The HP C compiler now allows data in structs and unions to be aligned on
half-word (16-bit) boundaries. This capability simplifies transferring
data to and from files and databases created on MPE V that contain
half-word aligned data. The new HP_ALIGN pragma and the new +u command
line option implement this capability.
HP_ALIGN pragma
This new pragma allows code generated by the compiler to align data
structures so they are compatible with data formats of other systems,
such as MPE V. The pragma eliminates the need to develop custom data
format conversion routines or special purpose record structures when
passing data between different platforms. The HP_ALIGN pragma applies
only to the alignment of data items within structs and unions.
Using The HP_ALIGN Pragma
To use the HP_ALIGN pragma include it within your source file. The
pragma has two options, MPE_16 and POP. Using MPE_16 causes ints, floats
and doubles within structs and unions to be half-word aligned. Alignment
of the structs and unions will start and end on at least a half-word
boundary. the POP option turns off the HP_ALIGN pragma, and alignment
reverts to word (32-bit). For example:
#pragma HP_ALIGN MPE_16
struct {char a; double b; int c; } d;
#pragma HP_ALIGN POP
This enhancement has made it simpler to transfer half-word aligned data
to and from files and databases, that were created on MPE V. An example
situation this enhancement addresses is illustrated in the following data
file record which contains a 16-bit integer and a 32-bit floating point
number.
byte 0 byte 1 byte 2 byte 3 byte 4 byte 5
|_________|________|________|________|________|________|
|<-- short int --->|<------------- float ------------->|
|__________________|___________________________________|
The ideal way to transfer this record is by reading it into a struct that
is defined with the same format as the file record, as the following
shows.
struct { short i; float r; } rec;
.
len = FREAD (fnum,rec,size)
Because "r" is half-word aligned in the struct, the floating point number
read into it cannot be accessed properly, as shown in the following
diagram of the "rec" struct.
byte 0 byte 1 byte 2 byte 3 byte 4 byte 5 byte 6 byte 7
|________|________|________|________|________|________|________|________|
|<-- int data --->|<----------- float data ---------->| |
|_________________|___________________________________|_________________|
|<--short int i;->| |<------------ float r; ----------->|
One way to solve this problem is to read the record into a character
array, instead of a struct, and move the elements byte-by-byte to a union
that consists of a character array and a variable. For example:
char rec[6];
union {char xa[2]; short i;} u1;
union {char xb[4]; float r;} u2;
.
.
len = FREAD(fnum, rec, size);
u1.xa[0] = rec[0];
u1.xa[1] = rec[1];
.
.
u2.xb[3] = rec[5];
This cumbersome code can be eliminated by using the HP_ALIGN pragma. For
example:
#pragma HP_ALIGN MPE_16
struct { short i; float r; } rec;
.
.
len = FREAD (fnum,rec,size)
byte 0 byte 1 byte 2 byte 3 byte 4 byte 5 byte 6 byte 7
|________|________|________|________|________|________|________|________|
|<-- int data --->|<----------- float data ---------->| |
|_________________|___________________________________|_________________|
|<--short int i;->|<----------- float r; ------------>| |
The item "r" is now aligned on a half-word boundary and can be properly
accessed.
To achieve the best performance, the HP_ALIGN pragma should be used for
input and output operations involving half-word aligned data. Access to
half-word aligned variables should be minimized and encapsulated in the
application input and output routines. The half-word aligned data should
be assigned from variables declared with word alignment immediately prior
to output operations, and assigned to word aligned variables immediately
after input operations.
+u Command Line Option
Pointers in HP C/iX by default use 32-bit word addresses. A run time
error will occur if a pointer to a word-sized, or larger, item accesses a
half-word aligned item. The use of half-word aligned structures can
produce this situation. For example, in the following, a run time error
will occur because "val" points to a half-word aligned item.
#pragma HP_ALIGN MPE_16
struct {short i; float r; } rec;
float *val
val = &rec.r;
The +u option resolves this by causing the compiler to generate code to
access pointers with half-word addressing.
For best performance this option should be encapsulated, and after
reading a data item into a half-word aligned structure, immediately copy
the contents, member by member, to a word aligned structure.
LOCAL IDENTIFIER MAP CHANGE
Because of an internal change to the HP C compiler, the local identifier
map for each procedure now appears immediately after the listing of the
procedure. Previously, the local identifier maps for all procedures
appeared together at the end of the listing. This changed because the HP
C/iX compiler now compiles on a per function basis, previously it
compiled on a per file basis.
NEW PROCEDURE _INIT_X11_GLOBALS
HP C/iX programs prepared on MPE/iX 4.0 call a new procedure _init_x11_g
which did not exist in MPE/iX 3.1 and earlier. The procedure _init_x11_g
is called via procedure _start, which is called by all HP C/iX programs.
Procedure _init_x11_globals is used for initialization in the Motif/iX
subsystem. If the Motif/iX subsystem is not installed a stub procedure f
_init_x11_globals is linked from the libc library.
MPE/iX Communicators