Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Alignment

Alignment

Scheduled Pinned Locked Moved C / C++ / MFC
graphicsdata-structuresdebuggingperformance
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gregs
    wrote on last edited by
    #1

    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 do void 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

    J 1 Reply Last reply
    0
    • G gregs

      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 do void 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

      J Offline
      J Offline
      Joaquin M Lopez Munoz
      wrote on last edited by
      #2

      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 Desarrollo

      G 1 Reply Last reply
      0
      • J Joaquin M Lopez Munoz

        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 Desarrollo

        G Offline
        G Offline
        gregs
        wrote on last edited by
        #3

        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 is Vector 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) is Vector 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.

        J 1 Reply Last reply
        0
        • G gregs

          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 is Vector 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) is Vector 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.

          J Offline
          J Offline
          Joaquin M Lopez Munoz
          wrote on last edited by
          #4

          Ummm... Everything seems fine. Where is jALIGN defined? Is it possible that "vector.h" (or whatever the name of the file where Vector is defined) could be #included in contexts where the definition of jALIGN differs? Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

          G 1 Reply Last reply
          0
          • J Joaquin M Lopez Munoz

            Ummm... Everything seems fine. Where is jALIGN defined? Is it possible that "vector.h" (or whatever the name of the file where Vector is defined) could be #included in contexts where the definition of jALIGN differs? Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

            G Offline
            G Offline
            gregs
            wrote on last edited by
            #5

            No, it is defined in a single header file. For the moment, as I can't get around this, I have scrapped the alignment and just rewritten the SSE code, but it is annoying!

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups