views:

148

answers:

4

Is there a common portable idiom in numerics code (I'm writing in D, but language-agnostic, C and C++ answers would be useful to me, too) to ensure that all stack-allocated doubles that are accessed frequently are aligned on 8-byte boundaries? I'm currently optimizing some numerics code where misaligned stack-allocated doubles (only aligned on 4-byte boundaries) is causing about a 1.5 to 2x performance hit.

+1  A: 

In C++ you can use __declspec(align(#)) like:

    __declspec(align(32)) struct Str1{
   int a, b, c, d, e;    };

or more appropriate to what you are looking for, a double aligned on a 32 bit boundary:

__declspec(align(32)) double a;

There is a nice article about data aligment on windows here, you might want to check it out.

Ando
+1  A: 

In 'C' you would use a union to force the alignment if you don't want to rely on compiler options or directives:

#include <stdint.h>

typedef union _foo
{
  uint64_t align;
  double d;
} foo

This will guarantee that your doubles are 64-bit aligned, it just makes accessing them a bit more tedious.

Alternatively, if you you don't mind relying on the compiler, gcc supports the #pragma pack(64) directive which enforces 64-bit alignment for everything.

SiegeX
Please leave a comment if you are going to down vote me so I can understand why you feel my answer is either wrong or not helpful.
SiegeX
@SiegeX: I wasn't the one who voted you down, but the most likely reason is that with most compilers, `double` and `uint64_t` will have the same alignment requirements, ie the union won't gain you anything
Christoph
+4  A: 

This is compiler specific. With GCC on x86, you'd use

-malign-double
Christoph
This just in: `double` claims to have been unfairly maligned!
caf
A: 

in D you may want to try align attribute or alignForSize library function

http://www.digitalmars.com/d/2.0/attribute.html#align

struct S
{   align(4) byte a;    // placed at offset 0
    align(4) byte b;    // placed at offset 1
}

align (1) struct S
{   byte a; // placed at offset 0
    byte[3] filler1;
    byte b; // placed at offset 4
    byte[3] filler2;
}

http://www.digitalmars.com/d/2.0/phobos/std_typecons.html#alignForSize

struct Banner {
  mixin(alignForSize!(byte[6], double)(["name", "height"]));
}
Michal Minich