Doubles alignment on 8-byte boundaries?

Is there a common portable idiom in numerical code (I write in D, but answers to the agnostic language, C and C ++ would also be useful to me) to ensure that all the duplicate stack numbers that are often accessed are stacked are aligned by 8 byte boundaries? I am currently optimizing some numerical code code in which shifted double values ​​on the stack (only for alignment at 4-byte boundaries) lead to a performance hit of 1.5 to 2x.

+4
source share
4 answers

This is a specific compiler. With GCC on x86 you should use

-malign-double 
+4
source

In 'C', you must use union to force 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 ensures that yours double with 64-bit alignment, it just makes accessing them a little more tedious.

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

+3
source

In C ++, you can use __ declspec (align (#)) , for example:

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

or more suitable for what you are looking for, double-aligned on a 32-bit border:

 __declspec(align(32)) double a; 

There is a good article on data attributes in the windows here, you can check it out.

+2
source

in D, you can try to align attribute or library function alignForSize

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"])); } 
+1
source

Source: https://habr.com/ru/post/1303038/


All Articles