Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aligning stack variables in D

Tags:

stack

alignment

d

In D, you can align struct/class members by using the align keyword, e.g.:

struct Vec4 { align(16) float[4] elems; }

However, it appears that you can't do the same on the stack:

void foo()
{
    align(16) float[4] vec; // error: found 'align' instead of statement
}

Is there a way to align data on the stack? In particular, I want to create an 16-byte aligned array of floats to load into XMM registers using movaps, which is significantly faster than movups.

e.g.

void foo()
{
    float[4] v = [1.0f, 2.0f, 3.0f, 4.0f];
    asm
    {
        movaps XMM0, v; // v must be 16-byte aligned for this to work.
        ...
    }
}
like image 693
Peter Alexander Avatar asked Sep 10 '11 22:09

Peter Alexander


1 Answers

If you are willing to burn an extra 16 bytes you can do the alignment your self at run time. Aside from that, I wouldn't know.

like image 186
BCS Avatar answered Oct 16 '22 04:10

BCS