Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SIMD : registers changing value during execution

So something strange is happening in my code at the moment, it is about the following register __m256i local, assigned during a computation somewhere, as well as __m256i mask, which is unrelated to local.

Where running the following :

  std::cout << _mm256_extract_epi32 (local, 0) << ", " << _mm256_extract_epi32(local,1) << ", " << _mm256_extract_epi32(local,2) << ", " << _mm256_extract_epi32(local,3) << ", " << _mm256_extract_epi32(local,4) << ", " << _mm256_extract_epi32(local,5) << ", " << _mm256_extract_epi32(local,6) << ", " << _mm256_extract_epi32(local,7) << std::endl;

  for (int l = 0; l < 8; ++l)
  {
    if (mask[l]) mask[l] = 0; else mask[l] = 1;
  }
  std::cout << _mm256_extract_epi32 (local, 0) << ", " << _mm256_extract_epi32(local,1) << ", " << _mm256_extract_epi32(local,2) << ", " << _mm256_extract_epi32(local,3) << ", " << _mm256_extract_epi32(local,4) << ", " << _mm256_extract_epi32(local,5) << ", " << _mm256_extract_epi32(local,6) << ", " << _mm256_extract_epi32(local,7) << std::endl;`

I obtain 519, 519, 519, 519, 519, 519, 519, 519 for the first output and 0, 0, 0, 0, 0, 0, 0, 0 for the second, even though they come from the same register. Any idea how something like this could happen ?

like image 204
Philippe Avatar asked Nov 28 '25 01:11

Philippe


1 Answers

Is the intent of your for-loop to access the 32-bit words within the 256-bit mask vector? That's not the correct way to do that. When you use the subscript operator to access mask, the compiler is overrunning the location of the actual mask variable in memory, and clobbering the subsequent 7x 256-byte region following it.

If you want to easily access the 32-bit words within mask, try declaring it as:

union vec8x32_t
{
  __m256i  vector;
  uint32_t words[8];
};

vec8x32_t mask;

Then you can do like:

// Do some AVX thing with mask:
mask.vector = _mm256_set_epi32(0, 1, 0, 1, 0, 1, 0, 1);

// Manipulate the components of mask:
for (int l = 0; l < 8; ++l)
{
  if (mask.words[l]) mask.words[l] = 0; else mask.words[l] = 1;
}
like image 175
toojays Avatar answered Nov 29 '25 15:11

toojays



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!