   SSE problem
  I have implemented some math functions with SSE extensions, but I get memory exceptions (Access violation reading location 0xffffff) from time to time and they occur completely random.It's kinda weird cuz I get the error only on one function which is like other SSE optimized functions in my code and they all use "moveaps" with no errors.I thought it would be a problem with memory alignment, and played around with __declspec(align(16)) on my class declaration but the problem's not gone.here is my class declaration :class HMR_CORE_API hmrVector4{public:union{HMR_ALIGN16 struct{float x;float y;float z;float w;};HMR_ALIGN16 float v[4];};...and here is the code that throws the weird exception :void hmrVector4::add( const hmrVector4& v1, const hmrVector4& v2, hmrVector4& r ){const float* v1f = (const float*)v1;const float* v2f = (const float*)v2;float* rf = (float*)r;__asm{movecx, v1fmovedi, v2fmovedx, rfmovapsxmm0, [ecx]   // ----> exception !!!movapsxmm1, [edi]addpsxmm0, xmm1movaps[edx], xmm0}r.w = 1.0f;}could some give me a hint on this ?thanks----- EDIT -----note that the casting to (const float*) is defined in an operator, so I get the pointer --> hmrVector4::operator const float*() const { return v; }[Edited by - sepul on September 14, 2007 5:49:32 AM]
  This is incorrect:const float* v1f = (const float*)v1;const float* v2f = (const float*)v2;float* rf = (float*)r;and should be:const float* v1f = (const float*)&v1const float* v2f = (const float*)&v2float* rf = (float*)&rbut better still (and more C++):const float* v1f = reinterpret_cast <const float*>(&v1);const float* v2f = reinterpret_cast <const float*>(&v2);float* rf = reinterpret_cast <const float*>(&r);but you should be able to eliminate these intermediate variables by accessing the parameters directly from within the assembler block.Skizz   ;  I'm not all that familiar with SSE, but I don't see anything obviously wrong with the assembly there.To me it looks like:const float* v1f = (const float*)v1;const float* v2f = (const float*)v2;float* rf = (float*)r;Should be replaced with:const float* v1f = v1.v;const float* v2f = v2.v;float* rf = r.v;That at least avoids the nasty casts, which you really should avoid when you can. They don't even compile for me.If that doesn't fix it then the bug is probably in the calling code. Something is calling that function with an invalid reference for v1. When it crashes use the debugger to step up the call stack and see if the function that called that one is the problem.Edit: Looks like Skizz beat me to the casting problems.   ;  sorry I didn't mentioned that, the code is right, cuz I have defined an operator for float* casting ...here is the codeclass hmrVector4 { ...operator const float*() const { return v; }};  If you're dynamically allocating arrays of vectors, make sure they are aligned to 16 bytes as well. __declspec(align(16)) only works for data on the stack.You can use _aligned_malloc with Visual C++, or use the tricky code below:void *allocate16(unsigned int bytes){    unsigned char *block;    int *aligned;   block = new unsigned char[bytes + 20];    aligned = (int*)((int)(block + 20) & 0xFFFFFFF0);    aligned[-1] = (int)block;    return aligned;}void deallocate16(void *memory){    if(memory)    {   delete[] ((int**)memory)[-1];    }}This works on 32-bit code only!   ;Quote: If you're dynamically allocating arrays of vectors, make sure they are aligned to 16 bytes as well. __declspec(align(16)) only works for data on the stack.thanks for the tip, I didn't know that, but I haven't used dynamic allocation yet,  the vectors are statically allocated;  Works fine for me. When it happens you should check using the debugger that the value in ecx is 1) 16-byte aligned 2) equal to the value of the v1f pointer. Both values should be addresses in the current stack frame (i.e. very close to the value in esp).   