Thread overview
struct alignments
Apr 13, 2008
dominik
Apr 16, 2008
Don
Apr 16, 2008
Lionello Lunesu
Apr 16, 2008
Don Clugston
April 13, 2008
I'm working on my vector and matrix math library, and now I'm prototyping my SIMD optimizations, obviously I would like data alignment.. fact is I have no clue how to do it ( I vaguely remember align directive)

for example something like (mixed C++ and D! :) ), what would I need for pragma pack equivalent in D then for a struct?

#pragma pack(16)
struct Vector4d {
  union {
   struct { float x = 0.0f, y = 0.0f, z = 0.0f, w = 0.0f; };
   struct { float r, g, b, a; };
   struct { float cell[4]; };
   Repeat!(float, 4) tuple;
  };
...
}

also somewhere down the road in the code I'd like equivalent of declspec align too, for example something like

Vector4d opAdd( ref Vector4d v ) {

version(NORMAL) {
    return Vector4d( x + v.x, y + v.y, z + v.z, w + v.w );
}
else version(SIMD) {
    __declspec(align(16)) Vector4d v4_ret;
    asm {
        mov esi, this
        mov edi, v
        movaps xmm0, [esi]
        addps xmm0, [edi]
        movaps v4_ret, xmm0
    }
    return v4_ret;
}
}

or something like that, its illustrative only :)


April 16, 2008
dominik wrote:
> I'm working on my vector and matrix math library, and now I'm prototyping my SIMD optimizations, obviously I would like data alignment.. fact is I have 

Good news!

> no clue how to do it ( I vaguely remember align directive)

> 
> for example something like (mixed C++ and D! :) ), what would I need for pragma pack equivalent in D then for a struct?
> 
> #pragma pack(16)
> struct Vector4d {



align(16) struct Vector4d {


> also somewhere down the road in the code I'd like equivalent of declspec align too, for example something like
> 
>     __declspec(align(16)) Vector4d v4_ret;
>     asm {
>         mov esi, this
>         mov edi, v

asm {
   align 16;
   mov esi, this;
   mov edi, v;
}


Bad news!

One problem is that the stack (and hence all local variables) are only aligned to 32 bytes. This means you can only use SSE aligned loads (movaps, movapd) on static and heap variables. (This is something that D really should fix while it has the chance).
April 16, 2008
> One problem is that the stack (and hence all local variables) are only aligned to 32 bytes. This means you can only use SSE aligned loads (movaps, movapd) on static and heap variables. (This is something that D really should fix while it has the chance).

bits?
April 16, 2008
Lionello Lunesu wrote:
> 
>> One problem is that the stack (and hence all local variables) are only aligned to 32 bytes. This means you can only use SSE aligned loads (movaps, movapd) on static and heap variables. (This is something that D really should fix while it has the chance).
> 
> bits?

Um actually the '32 bytes' is completely wrong. You need to have 128 bit alignment = 16 bytes, but currently DMD only lets you have 64 bit alignment for locals. Since the D calling convention is different from C, D could actually define that 128 bit alignment is preserved for function calls (ie, stack pushes/pops between function calls must always be a multiple of 16 bytes). Extern(C) and extern(Windows) functions will need a stack alignment fixup at the start of them.