CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Aug 2010
    Posts
    3

    How to align Multidimensional array to 16-byte boundary

    In order to use the aligned SSE instruction, the data must be aligned.
    I have aligned the array using __declspec(align(16)) int array[16][32],

    I am loading data using "_mm_load_si128((__m128i*)&array[0][0]);" In this case the data is aligned to 16 byte,
    but the problem is when i am loading the array from next column like "_mm_load_si128((__m128i*)&array[0][1])" then program get crashed because of accessing the unaligned memory.

    So here the question is, As i have aligned the array to 16 byte and still i need to align every element of array as 16-bit aligned in order to access the aligned version of load and store instruction.

    Can any one please tell me, is there any other way to solve this problem.
    Please revet if you need more details.

    Thanking you,
    Mandar

  2. #2
    Join Date
    Jul 2002
    Posts
    2,543

    Re: How to align Multidimensional array to 16-byte boundary

    You cannot apply SSE instructions from arbitrary array member, only from 16 bytes aligned. If your requirement is to start operation from any array member, select closest 16 bytes aligned member, and handle remaining elements without SSE.
    BTW, [16][32] is small array to apply SSE for it, just use plain C++. If you have arrays with more than 10000 elements, SSE may be an option.

  3. #3
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: How to align Multidimensional array to 16-byte boundary

    Quote Originally Posted by MandarChoure View Post
    ...but the problem is when i am loading the array from next column like "_mm_load_si128((__m128i*)&array[0][1])" then program get crashed because of accessing the unaligned memory.
    What is "next column"? array[0][1] is the second int element in your array, with offset of 4 (NOT 16-byte aligned).
    See http://www.fredosaurus.com/notes-cpp...ry-layout.html for 2-D Array Memory Layout
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  4. #4
    Join Date
    Aug 2010
    Posts
    3

    Re: How to align Multidimensional array to 16-byte boundary

    Quote Originally Posted by Alex F View Post
    You cannot apply SSE instructions from arbitrary array member, only from 16 bytes aligned. If your requirement is to start operation from any array member, select closest 16 bytes aligned member, and handle remaining elements without SSE.
    BTW, [16][32] is small array to apply SSE for it, just use plain C++. If you have arrays with more than 10000 elements, SSE may be an option.
    Yes you are correct,
    But I observed that using of aligned SSE instruction I got better performance than unaligned instruction, thats why I am using this.
    But the problem is to access arbitrary element of array (which is not 16 bytes aligned).

  5. #5
    Join Date
    Jul 2002
    Posts
    2,543

    Re: How to align Multidimensional array to 16-byte boundary

    Consider siggle-dimentional int array. You can start SSE instruction from elements 0, 3, 7 ... If you need to start, for example, form 4, use 7 instead of 4, and handle elements 4,5,6 without SSE.

  6. #6
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: How to align Multidimensional array to 16-byte boundary

    it seems strange to me that you are using an array of ints and then casting to __m128i*. why aren't you using an array of __m128i's ?

    but if you always need to load 4 ints and can't be guaranteed of alignment. Then i'd make an inline function...

    Code:
    inline myLoadmm128(__m128i* p)
    {
         if ((DWORD)p & 0xF)
              _mm_loadu_si128(p);
        else
              _mm_load_si128(p);
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured