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.
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.
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.
This is compiler specific. With GCC on x86, you'd use
-malign-double
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"]));
}