HP C Programmer's Guide (92434-90009)
Chapter 5 125
Programming for Portability
General Portability Considerations
General Portability Considerations
This section summarizes some of the general considerations to take into account when
writing portable HP C programs. Some of the features listed here may be different on other
implementations of C. Differences between Series 300/400 versus 700/800
implementations are also noted in this section.
Data Type Sizes and Alignments
Table 2-1 in Chapter 2 shows the sizes and alignments of the C data types on the different
architectures.
Differences in data alignment can cause problems when porting code or data between
systems that have different alignment schemes. For example, if you write a C program on
Series 300/400 that writes records to a file, then read the file using the same program on
Series 700/800, it may not work properly because the data may fall on different byte
boundaries within the file due to alignment differences. To help alleviate this problem, HP
C provides the HP_ALIGN pragma, which forces a particular alignment scheme, regardless
of the architecture on which it is used. The HP_ALIGN pragma is described in Chapter 2.
Accessing Unaligned Data
The Series 700/800 like all PA-RISC processors requires data to be accessed from locations
that are aligned on multiples of the data size. The C compiler provides an option to access
data from misaligned addresses using code sequences that load and store data in smaller
pieces, but this option will increase code size and reduce performance. A bus error
handling routine is also available to handle misaligned accesses but can reduce
performance severely if used heavily.
Here are your specific alternatives for avoiding bus errors:
1. Change your code to eliminate misaligned data, if possible. This is the only way to get
maximum performance, but it may be difficult or impossible to do. The more of this you
can do, the less you'll need the next two alternatives.
2. Use the +u
bytes
compiler option available at 9.0 to allow 2-byte alignment. However,
the +u
bytes
option, as noted above, creates big, slow code compared to the default code
generation which is able to load a double precision number with one 8-byte load
operation. Refer to the HP C/HP-UX Reference Manual (Series 700/800) for more
information.
3. Finally, you can use allow_unaligned_data_access() to avoid alignment errors.
allow_unaligned_data_access() sets up a signal handler for the SIGBUS signal.
When the SIGBUS signal occurs, the signal handler extracts the unaligned data from
memory byte by byte.
To implement, just add a call to allow_unaligned_data_access() within your main
program before the first access to unaligned data occurs. Then link with -lhppa. Any
alignment bus errors that occur are trapped and emulated by a routine in the
libhppa.a library in a manner that will be transparent to you. The performance