i960 Processor Compiler User's Guide
12-14
12
/* Read binary data from a file and copy into a
* structure that has different alignment rules. */
main()
{
int fdesc;
unsigned char *ptr;
/* Assume file opened and ready for reading...
* Then read one struct’s worth of bytes. */
if (read(fdesc, packed, 7) != 7) {
/* Handle read error. */
}
/* Fill up structure. Done. */
ptr = packed;
960_struct.first = *(char *)ptr;
ptr += sizeof(960_struct.first);
960_struct.second = GET_UNALIGNED(ptr,int);
/* *(int *)ptr; */
ptr += sizeof(960_struct.second);
960_struct.third = GET_UNALIGNED(ptr,short);
/* *(short *)ptr; */
}
Although the code shown above is expensive in terms of performance,
using
#pragma align also has a significant performance penalty. To get
the best performance, use the default alignment rules and use pragmas
only where absolutely necessary. See the discussions of gcc960’s
pragma
align
and pragma pack in this manual for a detailed discussion of
alignment.