https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128709

            Bug ID: 128709
           Summary: Inefficient AVX2 byte shuffle
           Product: gcc
           Version: 16.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: zoltan at hidvegi dot com
  Target Milestone: ---

Consider this code on x64-64-v3 (Intel AVX2):

#include <x86intrin.h>

typedef    char   vec8_t __attribute__ ((__vector_size__(32)));
typedef    long  vec64_t __attribute__ ((__vector_size__(32)));

vec8_t shuf(vec8_t a, vec8_t b) {
    return __builtin_shuffle(a, b);
}

vec8_t shuf2(vec8_t a, vec8_t b) {
    vec64_t a64 = (vec64_t) a;
    vec64_t b64 = (vec64_t) b;
    __m256i lo = (__m256i) __builtin_shuffle(a64, (vec64_t){0,1,0,1});
    __m256i hi = (__m256i) __builtin_shuffle(a64, (vec64_t){2,3,2,3});
    __m256i t = _mm256_blendv_epi8((__m256i)lo, (__m256i)hi, (__m256i) b << 3);
    return (vec8_t) _mm256_shuffle_epi8(t, (__m256i) b);
}

The two are functionally the same, but the suf2 version is much more efficient.
__builtin_shuffle generates poor code.

Reply via email to