Technical article 109395

Can I align double integers on 32-bit boundaries within my program?

OpenServer 5.0.0OpenServer 5.0.2OpenServer 5.0.4OpenServer 5.0.5OpenServer 5.0.6

Written · Last revised

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

From the Xinuos technical article database, record 109395. Reproduced with permission. The original is at www.sco.com/ta/109395, which is reachable but blocked from search engines. Xinuos marks this article against a retired product line. That refers to the product’s lifecycle, not to whether the advice still works — plenty of it still applies to running systems.

Next step

Need this fixed rather than explained?

Tell us the release, what the system runs and what it is doing. We will reply by email with what we would do next.

  • You deal with the people who do the work
  • No obligation, and no upgrade required before we will help
  • We tell you plainly if something is not worth doing

Send an enquiry

We reply by email. If a server is down, say so in the first line. You can also write to help@scoexpert.com.