__declspec(align(16))

__declspec(align(16))

Hello,

Attending to the following code (C++):

class __declspec(align(16)) Point
{
public:
__m128 m;
float v[4];
};

class __declspec(align(16)) CPS
{
public:
int a;
Point b;
};

int main()
{
cout << sizeof(Point) << endl;
cout << sizeof(CPS) << endl;
CPS rrr[2];
cout << &rrr[0] << endl;
cout << &rrr[0].b.m << endl;
cout << &rrr[1] << endl;
cout << &rrr[1].b.m << endl;
return 0;
}

Standar output:

16
24
0x0012FF20
0x0012FF28
0x0012FF38
0x0012FF40

Im using the 6.0 Version of the Intel C++ Compiler. Why
does the size of class "CPS" equal to 24? The compiler, is integrated with 6.0 Version of Microsoft Visual Studio
under Windows XP Profesional. We use debug mode options for Microsoft Visual.

The "rrr" array, disaligns the __m128 member of the class "Point". That is why, the size of the class CPS isnt 16 multiple. However, when we run the same code, under linux, and the linux version for the Intel C++ Compiler 6.0, the size of the class CPS equals to 32 so the arrays will align the __m128 members of "Point".
We are suspicious of "__declspec(align(16))", we think that it doesnt work properly. What is the matter with the alignement??? We need to align to 16 byte boundary the __m128 members, in order to use the SSE instructions.
So, how can I align that members, in all situations?

Thanks in advance,

ruben

publicaciones de 6 / 0 nuevos
Último envío
Para obtener más información sobre las optimizaciones del compilador, consulte el aviso sobre la optimización.

One other version of the same problem: array members do not obey the alignment on Windows:

#include

using namespace std;

class __declspec(align(16,0)) Vector
{
public:
float f[3];
};

int main()
{
cout << sizeof(Vector) << endl;

Vector v;
cout << &v << endl;

Vector vv[2];
cout << sizeof(vv) << endl;
cout << &vv[0] << endl;
cout << &vv[1] << endl;

return 0;
}

On linux, you get:
annwn:~> icc -o b b.cc; b
16
bffff670
32
bffff680
bffff690

So sizeof(Vector) is 16, and array members stay aligned.
On Windows, you get 12 for sizeof(Vector), and array members are not aligned.

???

TIA

(Note: I will post one other problem related to this...)

After adding enough headers to compile, my Intel 7.0 Windows and linux compilers give sizeof's 32 and 48, and all pointers 16-byte aligned. Likewise, g++ on linux, with the declspec()'s removed to permit compilation, does the same, as do both Intel compilers.

g++ complains the headers required by the Intel compilers aren't standard compliant, but it accepts them on linux. On Windows, my g++ rejects any close variant of your code.

I've seen cases where Intel compilers don't align properly without use of declspec(), but this is not among them. I don't know how the extreme confusion caused by declspec() is to be resolved, but I would avoid it where it's unnecessary.

Sorry Timothy, I made a mistake in my first post regarding my Point class. Actually my Point class is like this:

class __declspec(align(16)) Point
{
public:
union
{
__m128 m;
float v[4];
};
};

Can u check it again, please???

Thanks & Regards

Now, on Windows:
icl 7.0 result:
16
32
0x0012FF40
0x0012FF50
0x0012FF60
0x0012FF70

gcc-3.2 result:
11: warning: 'align' attribute directive ignored

16
24
0x22feb8
0x22fec0
0x22fed0
0x22fed8

Yes, using icl 7.0 on Windows I also get the same results, but using icl 6.0 on Windows I get the results formerly posted. Anyhow it's very easy to misalign the __m128 elements of the array (using either 6.0 or 7.0). Just revert the order of the class CPS elements and you will get a misaligned acces.

class __declspec(align(16)) CPS
{
public:
Point b;
int a;
};

Thanks again.

Deje un comentario

Por favor inicie sesión para agregar un comentario. ¿No es socio? Únase ya