Problem
I understand that aligning a 32-bit quantity such as a 'double' on a 32-bit boundary will result in faster data accesses since this quantity can be read/written in a single bus cycle instead of in two cycles.
How can I compile my program to ensure that my double variables are aligned in this way?
Solution
The following program demonstrates how this alignment can be achieved using the necessary compilation flags:
/*
** align.c
**
** To compile for 32-bit alignment:
**
** cc -O3 align.c
** cc -O1 -Knoframe ...
** cc -O2 -Knoframe ...
** icc ...
** icc -pad ...
**
*/char pad_ch;
double global_dbl;void check_align(double *dp, char msg[]) { if ((long) dp & 7) printf("Double(%s) is NOT aligned on 32-bit boundary\n", msg); else printf("Double(%s) IS aligned on 32-bit boundary\n", msg); }
int main()
{
double local_dbl;
double *dp; check_align(&local_dbl, "AUTO");
check_align(&global_dbl, "GLOBAL");
}.0