0

I would like to have a __m128i variable, and do some operation like this:

unsigned char* myArray;
__m128i fourValues;

//Do some strange reference assignment, e.g.:
//int32_t& a = *((int32_t*) &fourValues);
//int32_t& b = *(((int32_t*) &fourValues) + 1);
//int32_t& c = *(((int32_t*) &fourValues) + 2);
//int32_t& d = *(((int32_t*) &fourValues) + 3);

for (int i =0; i < someSize; i+=4) {
  a = d + myArray[i];
  b = a + myArray[i+1];
  c = b + myArray[i+2];
  d = c + myArray[i+3];
  //Do something with fourValues;
  }

where a,b,c,d are (or behave as) int32_t variables, and are the first, second, third and fourth 32bits of fourValues. Is that possible?

Paul R
  • 208,748
  • 37
  • 389
  • 560
Antonio
  • 19,451
  • 13
  • 99
  • 197
  • Use the explicit load and store operations. That way your `__m128i` variable will actually live in an SSE register, which is the most efficient. Also look for ways to do your manipulation using SSE instructions so you won't need to load and store... but a single load/store pair is much more efficient than aliasing. – Ben Voigt Oct 16 '13 at 15:00
  • Note: if you post your current *actual* scalar code for this then you may get more specific solutions using SSE. – Paul R Oct 16 '13 at 15:39

1 Answers1

0

You can use

int _mm_extract_epi32( 
   __m128i a,
   const int ndx 
);

see - http://msdn.microsoft.com/en-us/library/bb531460%28v=vs.90%29.aspx

EDIT: Note that this only works for int SSE vectors, I see there's quite a lot of debate going around here - Get member of __m128 by index? , which refers to a __m128, but in your case it should be possible to use that. Still, always worth to get a heads up that this isn't generic enough for floats.

For writing you can use the equivalient _mm_insert_epi32, as you point out.

Community
  • 1
  • 1
Leeor
  • 19,260
  • 5
  • 56
  • 87
  • 1
    The question asks about assignment, not just reading individual elements. – Ben Voigt Oct 16 '13 at 14:58
  • 1
    This might work in combination with _mm_insert_epi32 http://msdn.microsoft.com/en-us/library/bb514012%28v=vs.90%29.aspx. However, SSE4 is not among my possibility at the moment, pity... – Antonio Oct 16 '13 at 16:51
  • Ah, didn't notice the writing part. Anyway, since you want to avoid SSE4 - Personally I would have used direct memory access and unsafe casting, because i'm a horrible programmer, but that's very platform specific and wouldn't do as an answer in this site, sorry – Leeor Oct 16 '13 at 17:21