Skip to content

Commit ac3d0dd

Browse files
authored
perf(turbo): fold four UINT4 distance sums in AVX-512 lanes (#930)
1 parent aaf9d03 commit ac3d0dd

1 file changed

Lines changed: 12 additions & 26 deletions

File tree

‎src/turbo/distance/avx512_vnni/uniform_uint4/squared_euclidean.cc‎

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,32 +21,18 @@ inline int32_t Reduce(__m512i value) {
2121
// keeps the entire reduction in SIMD registers.
2222
static ailego_force_inline __m128i ReduceFour(__m512i a, __m512i b, __m512i c,
2323
__m512i d) {
24-
const __m256i a8 = _mm256_add_epi32(_mm512_castsi512_si256(a),
25-
_mm512_extracti64x4_epi64(a, 1));
26-
const __m256i b8 = _mm256_add_epi32(_mm512_castsi512_si256(b),
27-
_mm512_extracti64x4_epi64(b, 1));
28-
const __m256i c8 = _mm256_add_epi32(_mm512_castsi512_si256(c),
29-
_mm512_extracti64x4_epi64(c, 1));
30-
const __m256i d8 = _mm256_add_epi32(_mm512_castsi512_si256(d),
31-
_mm512_extracti64x4_epi64(d, 1));
32-
33-
const __m128i a4 = _mm_add_epi32(_mm256_castsi256_si128(a8),
34-
_mm256_extracti128_si256(a8, 1));
35-
const __m128i b4 = _mm_add_epi32(_mm256_castsi256_si128(b8),
36-
_mm256_extracti128_si256(b8, 1));
37-
const __m128i c4 = _mm_add_epi32(_mm256_castsi256_si128(c8),
38-
_mm256_extracti128_si256(c8, 1));
39-
const __m128i d4 = _mm_add_epi32(_mm256_castsi256_si128(d8),
40-
_mm256_extracti128_si256(d8, 1));
41-
42-
const __m128i ab_low = _mm_unpacklo_epi32(a4, b4);
43-
const __m128i cd_low = _mm_unpacklo_epi32(c4, d4);
44-
const __m128i ab_high = _mm_unpackhi_epi32(a4, b4);
45-
const __m128i cd_high = _mm_unpackhi_epi32(c4, d4);
46-
return _mm_add_epi32(_mm_add_epi32(_mm_unpacklo_epi64(ab_low, cd_low),
47-
_mm_unpackhi_epi64(ab_low, cd_low)),
48-
_mm_add_epi32(_mm_unpacklo_epi64(ab_high, cd_high),
49-
_mm_unpackhi_epi64(ab_high, cd_high)));
24+
// Fold the four rows together while all four 128-bit lanes are active.
25+
// Each lane of abcd holds the partial sums for rows a, b, c and d.
26+
const __m512i ab = _mm512_add_epi32(_mm512_unpacklo_epi32(a, b),
27+
_mm512_unpackhi_epi32(a, b));
28+
const __m512i cd = _mm512_add_epi32(_mm512_unpacklo_epi32(c, d),
29+
_mm512_unpackhi_epi32(c, d));
30+
const __m512i abcd = _mm512_add_epi32(_mm512_unpacklo_epi64(ab, cd),
31+
_mm512_unpackhi_epi64(ab, cd));
32+
const __m256i halves = _mm256_add_epi32(_mm512_castsi512_si256(abcd),
33+
_mm512_extracti64x4_epi64(abcd, 1));
34+
return _mm_add_epi32(_mm256_castsi256_si128(halves),
35+
_mm256_extracti128_si256(halves, 1));
5036
}
5137

5238
static ailego_force_inline void StoreFour(const __m512i *sums,

0 commit comments

Comments
 (0)