Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Union with __m256 and array of two __m128

Can I have a union like this

  union eight_floats_t
  {
    __m256 a;
    __m128 b[2];
  };
  eight_floats_t eight_floats;

to have an instant access to the two 128 bit parts of a 256 bit register?

Edit: I was asking to understand the performance impact of this approach.

like image 665
Yoav Avatar asked Dec 16 '22 17:12

Yoav


2 Answers

You certainly can do that. The C and C++ languages allow you do it. And it will most likely do what you want it to do.

However, the fact that you're using AVX means you care about performance. So it might be useful to know that this is one of the most common (performance) traps that SSE programmers fall into. (and many don't notice)

Problem 1:

Current compilers implement such a union using a memory location. So that's the first problem, every time you access the union from a different field, it forces the data to memory and reads it back. That's one slow-down.

Here's what MSVC2010 generates for (with optimizations):

eight_floats a;
a.a = vecA[0];

__m128 fvecA = a.b[0];
__m128 fvecB = a.b[1];
fvecA = _mm_add_ps(fvecA,fvecB);

vmovaps YMMWORD PTR a$[rbp], ymm0
movaps  xmm1, XMMWORD PTR a$[rbp+16]
addps   xmm1, XMMWORD PTR a$[rbp]
movaps  XMMWORD PTR fvecA$[rbp], xmm1
movss   xmm1, DWORD PTR fvecA$[rbp]

You can see that it's being flushed to memory.

Problem 2:

The second slow-down is even worse. When you write something to memory, and immediately access it with a different word-size, you will likely trigger a store-to-load stall. (typically on the order of > 10 cycles)

This is because the load-store queues on current processors aren't usually designed to handle this (unusual) situation. So they deal with it by simply flushing the queues to memory.


The "correct" way to access the lower and upper half of AVX datatypes is to use:

  • _mm256_extractf128_ps()
  • _mm256_insertf128_ps()
  • _mm256_castps256_ps128()

and family. Likewise for the other datatypes as well.

That said, it is possible that the compiler may be smart enough to recognize what you are doing and use those instructions anyway. (At least MSVC2010 doesn't.)

like image 56
Mysticial Avatar answered Dec 18 '22 10:12

Mysticial


Yes, you can. Have you tried it?

Do be aware that the C standard says that it's unspecified behavior to access a member of a union which was not the one most recently written to -- specifically, if you write to one member and then read a different one, the other one has unspecified values (C99 §6.2.6.1/7). However, it is an extremely common idiom and is well-supported by all major compilers. As a practical matter, reading and writing to any member of a union, in any order, is acceptable practice (source).

like image 31
Adam Rosenfield Avatar answered Dec 18 '22 11:12

Adam Rosenfield