-
August 10th, 2010, 11:58 AM
#1
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
-
August 10th, 2010, 12:07 PM
#2
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.
-
August 10th, 2010, 01:05 PM
#3
Re: How to align Multidimensional array to 16-byte boundary
Originally Posted by MandarChoure
...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...
-
August 10th, 2010, 07:06 PM
#4
Re: How to align Multidimensional array to 16-byte boundary
Originally Posted by Alex F
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).
-
August 11th, 2010, 12:05 AM
#5
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.
-
August 11th, 2010, 02:35 AM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|