HP C Programmer's Guide (92434-90009)

126 Chapter5
Programming for Portability
General Portability Considerations
degradation will be significant, but if it only occurs in a few places in your program it
shouldn't be a big concern.
Whether you use alternative 2 or 3 above depends on your specific code.
The +u
bytes
option costs significantly less per access than the handler, but it costs you on
every access, whether your data is aligned or not, and it can make your code quite a bit
bigger. You should use it selectively if you can isolate the routines in your program that
may be exposed to misaligned pointers.
There is a performance degradation associated with alternative 3 because each unaligned
access has to trap to a library routine. You can use the unaligned_access_count variable
to check the number of unaligned accesses in your program. If the number is fairly large,
you should probably use 2. If you only occasionally use a misaligned pointer, it is probably
better just use the allow_unaligned_data_access handler. There is a stiff penalty per
bus error, but it doesn't cause your program to fail and it won't cost you anything when you
operate on aligned data.
The following is a an example of its use within a C program:
extern int unaligned_access_count;
/* This variable keeps a count
of unaligned accesses. */
char arr[]="abcdefgh";
char *cp, *cp2;
int i=99, j=88, k;
int *ip; /* This line would normally result in a
bus error on Series 700 or 800 */
main()
{
allow_unaligned_data_access();
cp = (char *)&i;
cp2 = &arr[1];
for (k=0; k<4; k++)
cp2[k] = * (cp+k);
ip = (int *)&arr[1];
j = *ip;
printf("%d\n", j);
printf("unaligned_access_count is : %d\n", unaligned_access_count);
}
To compile and link this program, enter
cc
filename
.c -lhppa
This enables you to link the program with allow_unaligned_data_access() and the int
unaligned_access_count that reside in /usr/lib/libhppa.a.
Note that there is a performance degradation associated with using this library since each
unaligned access has to trap to a library routine. You can use the
unaligned_access_count variable to check the number of unaligned accesses in your
program. If the number is fairly large, you should probably use the compiler option.