PDA

View Full Version : Aligned allocation and VC++.NET


Dissident Dan
Oct23-05, 08:08 PM
Has anyone else experience the new operator not working correctly for 16-byte aligned data types? I am using Visual Studio .NET 2003.

I have a class that has its first component 16 byte-aligned via the "__declspec(align(16))" directive because I am using SSE.


class Vector {
public:
// data members
union {
__declspec(align(16)) float comps[4];
struct {
float x,y,z,w;
};
struct {
float r, g, b, a;
};
struct {
float yaw, pitch, roll;
};
};


When dynamically allocating and array of this class using the new operator, I get an 8 byte-aligned pointer. It needs to be 16 byte-aligned to not crash when using SSE intrinsics.

I have even tried using "__declspec(align(16))" on the pointer and between the "new" and the data type. Example:


__declspec(align(16)) Vector *tan1 = new __declspec(align(16)) Vector[vertexCount * 2];


I have resorted to using the old C functions _aligned_malloc() and _aligned_free().

This must be a bug with Visual Studio, right? Anyone know of any fixes?

dduardo
Oct23-05, 08:44 PM
You're probably going to need to post this on a microsoft newsgroup or forum. I'm not familiar with MS specific code. In gcc __attribute__ (aligned(16)) is used.

[edit] This is just a guess, but when using __attribute__ I believe it goes after the allocation statement. Something like this:

float comps[4] __attribute__ (aligned(16));

This might be the same case with __delcspec

Dissident Dan
Dec3-05, 06:12 PM
It turns out that you need to overload the new and delete operators using aligned malloc and free functions.

Check out this PDF for more info:
http://homepage.te.hik.se/personal/tkama/audio_procME/papers/17664_alignment.pdf

adrean87
Feb19-08, 11:21 AM
huh?

zyh
Mar5-08, 06:17 AM
The help file( CHM ) supplied by intel c++ compiler hase some example on this topic. Also, www.codeproject.com hase some articles to introduce SSE.