Channel / lane movement for SSE and AVX?

What SSE / AVX instructions shuffle tracks from a to look like b and c ?

 float4 a = {data[0], data[1], data[2], data[3]}; float4 b = {data[1], data[2], data[3], data[0]}; // lanes shifted left float4 c = {data[3], data[0], data[1], data[2]}; // lanes shifted right float8 a = {data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]}; float8 b = {data[1], data[2], data[3], data[4], data[5], data[6], data[7], data[0]}; // lanes shifted left float8 c = {data[7], data[0], data[1], data[2], data[3], data[4], data[5], data[6]}; // lanes shifted right 

Background:

I have an algorithm that requires values ​​from its neighbors; this means that I am currently mixing aligned loads and unloaded loads:

 ( plate[row + 1][column] // aligned + plate[row - 1][column] // aligned + plate[row][column + 1] // unaligned + plate[row][column - 1] // unaligned + (4 * plate[row][column]) // aligned ) / 8; 

Here it is in SSE:

 __m128 bottom = _mm_load_ps(&from[row-1][column]); __m128 left = _mm_loadu_ps(&from[row][column-1]); __m128 middle = _mm_load_ps(&from[row][column]); __m128 right = _mm_loadu_ps(&from[row][column+1]); __m128 top = _mm_load_ps&from[row+1][column]); (top + bottom + left + right + _mm_set1_ps(4.0f) * middle) * _mm_set1_ps(0.125f); 

I realized that the values ​​in the current and left or right differ only in one value. Therefore, I had the idea that instead of performing two unaligned loads, I could shuffle the tracks, and then insert one value, another. I need to check the latency / bandwidth in the instructions to see if they will be faster, but I am not familiar with any of these SSE / AVX instructions.

+6
source share
1 answer

In recent Intel processors (Core i7, etc.), off-load is a reasonable approach, but on older processors it is relatively expensive. An alternative approach is to use _mm_alignr_epi8 ( PALIGNR ) - as a rule, you iterate through the line and support 3 consecutive vectors - after each iteration you move these vectors along one and then load a new vector, so there is only one load for each iteration.

 __m128 va = _mm_setzero_ps(); __m128 vb = _mm_load_ps(&from[row][0]); for (col = 0; col < N; col += 4) { __m128 vc = _mm_load_ps(&from[row][col + 4]); __m128 centre = vb; __m128 left = (__m128)_mm_alignr_epi8((__m128i)va, (__m128i)vb, sizeof(float)); __m128 right = (__m128)_mm_alignr_epi8((__m128i)vb, (__m128i)vc, 3 * sizeof(float)); // do stuff ... va = vb; // shuffle vectors along vb = vc; } 

AVX is a bit more complicated due to the limitations of 128-bit bands - you might be better off not sticking to unbalanced loads.

+4
source

Source: https://habr.com/ru/post/957692/


All Articles