Question

I try to write on a random positions in an int array. To be sure I can access the memory on a random position I tried to align the whole block of memory.

int * array = memalign(16384*2,16384*sizeof(short));

Later on I write in the vector like this:

__m128i * index = (__m128i *) &array[RANDOMNUMBER];
_mm_store_si128(index+, ...) 

This instruction leads to an bad access. If I use the _mm_storeu_si128 instruction I don't get it. Can somebody explain me why this doesn't work?

Était-ce utile?

La solution

The argument for _mm_store_si128 must be 16 byte-aligned. A randomly chosen element of an int-array will only be sizeof(int)-aligned (even if the array itself is 16 byte-aligned). So you must make sure that the index into the array is a multiple of (16/sizeof(int)), e.g. like so:

__m128i * index = (__m128i *) &array[(RANDOMNUMBER / (16/sizeof(int))) * (16/sizeof(int))];

This will guarantee that the write will be properly aligned. Whether this is what your code really needs to do is another question...

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top