Alignment
-
I have been aligning(16 byte) certain data types to allow me to use SSE commands on them, and have no problems apart from when I create instances of them on the stack, where I get a runtime exception in debug mode, complaining about the stack being corrupted when I leave the function that uses it, ie
void SomeProc() { Vector vec; // the aligned type // .. do stuff with vector .. } // <- stack corrupted
so I'm having to dovoid SomeProc() { Vector *vec=new Vector; // the aligned type // .. do stuff with vector .. delete vec; } // okay
which is time consuming. I have implemented ways to speed it up, but I would like to know if there's a simpler way. Thanks, G -
I have been aligning(16 byte) certain data types to allow me to use SSE commands on them, and have no problems apart from when I create instances of them on the stack, where I get a runtime exception in debug mode, complaining about the stack being corrupted when I leave the function that uses it, ie
void SomeProc() { Vector vec; // the aligned type // .. do stuff with vector .. } // <- stack corrupted
so I'm having to dovoid SomeProc() { Vector *vec=new Vector; // the aligned type // .. do stuff with vector .. delete vec; } // okay
which is time consuming. I have implemented ways to speed it up, but I would like to know if there's a simpler way. Thanks, GAFAIK changing the default alignment for a structure mustn't cause stack corruption issues. Maybe if you post the code for
SomeProc
we can spot the root of the problem. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo -
AFAIK changing the default alignment for a structure mustn't cause stack corruption issues. Maybe if you post the code for
SomeProc
we can spot the root of the problem. Joaquín M López Muñoz Telefónica, Investigación y DesarrolloRight: the class vector is defined thus:
// alignment for SIMD instructions #define jALIGN __declspec(align(16)) // The m_vector data structure for optimisation typedef jALIGN struct VecData { float x; float y; float z; float w; }VecData; class Vector { public: // stuff protected: VecData m_vec; // vector data };
and the test procedure isVector vec(1.0f,1.0f,1.0f,1.0f); Vector vans=mat1*vec; PVec(vans);
Where PVec just prints out the value of the vector. mat1 is a matrix defined thus:// the actual matrix type typedef float jALIGN jMat[16]; class Matrix { public: // stuff protected: jMat m_mat; };
and the transformation function for the non-SIMD version (it happens on all versions) isVector Matrix :: operator * (Vector &v) { float x,y,z,w; // temporary vector float vx,vy,vz,vw; // the parts of v // get the parts of v v.Get(vx,vy,vz,vw); // transform vector x=( vx*m_mat[jMatIdx(0,0)] ) + ( vy*m_mat[jMatIdx(1,0)] ) + ( vz*m_mat[jMatIdx(2,0)] ) + ( vw*m_mat[jMatIdx(3,0)] ); y=( vx*m_mat[jMatIdx(0,1)] ) + ( vy*m_mat[jMatIdx(1,1)] ) + ( vz*m_mat[jMatIdx(2,1)] ) + ( vw*m_mat[jMatIdx(3,1)] ); z=( vx*m_mat[jMatIdx(0,2)] ) + ( vy*m_mat[jMatIdx(1,2)] ) + ( vz*m_mat[jMatIdx(2,2)] ) + ( vw*m_mat[jMatIdx(3,2)] ); w=( vx*m_mat[jMatIdx(0,3)] ) + ( vy*m_mat[jMatIdx(1,3)] ) + ( vz*m_mat[jMatIdx(2,3)] ) + ( vw*m_mat[jMatIdx(3,3)] ); // tidy x/=w; y/=w; z/=w; w=1.0f; // and return return Vector(x,y,z); }
where jMatIdx is// to get [x][y] capability without a multiplication #define jMatIdx(x,y) ( ((y)<<2) + (x) )
Hope this helps. -
Right: the class vector is defined thus:
// alignment for SIMD instructions #define jALIGN __declspec(align(16)) // The m_vector data structure for optimisation typedef jALIGN struct VecData { float x; float y; float z; float w; }VecData; class Vector { public: // stuff protected: VecData m_vec; // vector data };
and the test procedure isVector vec(1.0f,1.0f,1.0f,1.0f); Vector vans=mat1*vec; PVec(vans);
Where PVec just prints out the value of the vector. mat1 is a matrix defined thus:// the actual matrix type typedef float jALIGN jMat[16]; class Matrix { public: // stuff protected: jMat m_mat; };
and the transformation function for the non-SIMD version (it happens on all versions) isVector Matrix :: operator * (Vector &v) { float x,y,z,w; // temporary vector float vx,vy,vz,vw; // the parts of v // get the parts of v v.Get(vx,vy,vz,vw); // transform vector x=( vx*m_mat[jMatIdx(0,0)] ) + ( vy*m_mat[jMatIdx(1,0)] ) + ( vz*m_mat[jMatIdx(2,0)] ) + ( vw*m_mat[jMatIdx(3,0)] ); y=( vx*m_mat[jMatIdx(0,1)] ) + ( vy*m_mat[jMatIdx(1,1)] ) + ( vz*m_mat[jMatIdx(2,1)] ) + ( vw*m_mat[jMatIdx(3,1)] ); z=( vx*m_mat[jMatIdx(0,2)] ) + ( vy*m_mat[jMatIdx(1,2)] ) + ( vz*m_mat[jMatIdx(2,2)] ) + ( vw*m_mat[jMatIdx(3,2)] ); w=( vx*m_mat[jMatIdx(0,3)] ) + ( vy*m_mat[jMatIdx(1,3)] ) + ( vz*m_mat[jMatIdx(2,3)] ) + ( vw*m_mat[jMatIdx(3,3)] ); // tidy x/=w; y/=w; z/=w; w=1.0f; // and return return Vector(x,y,z); }
where jMatIdx is// to get [x][y] capability without a multiplication #define jMatIdx(x,y) ( ((y)<<2) + (x) )
Hope this helps.Ummm... Everything seems fine. Where is
jALIGN
defined? Is it possible that"vector.h"
(or whatever the name of the file whereVector
is defined) could be#include
d in contexts where the definition ofjALIGN
differs? Joaquín M López Muñoz Telefónica, Investigación y Desarrollo -
Ummm... Everything seems fine. Where is
jALIGN
defined? Is it possible that"vector.h"
(or whatever the name of the file whereVector
is defined) could be#include
d in contexts where the definition ofjALIGN
differs? Joaquín M López Muñoz Telefónica, Investigación y Desarrollo