Convert array of eight bytes to eight integers

120 Views Asked by At

I am working with the Xeon Phi Knights Landing. I need to do a gather operation from an array of doubles. The list of indices comes from an array of chars. The gather operations are either _mm512_i32gather_pd or _mm512_i64gather_pd. As I understand it, I either need to convert eight chars to to eight 32-bit integers or eight chars to 64-bit integers. I have gone with the first choice for _mm512_i32gather_pd.

I have created two functions get_index and get_index2 to convert eight chars to a __m256i. The assembly for get_index is simpler than for get_index2 see https://godbolt.org/z/lhg9fX. However, in my code get_index2 is significantly faster. Why is this? I am using ICC 18. Maybe there is a better solution than either of these two functions?

#include <x86intrin.h>
#include <inttypes.h>

__m256i get_index(char *index) {                                                                                                                                      
  int64_t x = *(int64_t *)&index[0];                                                                                                                                             
  const __m256i t3 = _mm256_setr_epi8(
    0,0x80,0x80,0x80,
    1,0x80,0x80,0x80,
    2,0x80,0x80,0x80,
    3,0x80,0x80,0x80,
    4,0x80,0x80,0x80,
    5,0x80,0x80,0x80,
    6,0x80,0x80,0x80,
    7,0x80,0x80,0x80);                                                                                                                                                     

  __m256i t2 = _mm256_set1_epi64x(x);                                                                                                                                            
  __m256i t4 = _mm256_shuffle_epi8(t2, t3);                                                                                                                                      
  return t4;                                                                                                                                                                     
}                

__m256i get_index2(char *index) {
  const __m256i t3 = _mm256_setr_epi8(
    0,0x80,0x80,0x80,
    1,0x80,0x80,0x80,
    2,0x80,0x80,0x80,
    3,0x80,0x80,0x80,
    4,0x80,0x80,0x80,
    5,0x80,0x80,0x80,
    6,0x80,0x80,0x80,
    7,0x80,0x80,0x80);
  __m128i t1  = _mm_loadl_epi64((__m128i*)index);
  __m256i t2 = _mm256_inserti128_si256(_mm256_castsi128_si256(t1), t1, 1);
  __m256i t4 = _mm256_shuffle_epi8(t2, t3);
  return t4;
}
0

There are 0 best solutions below