Skip to content

Commit 5a95d47

Browse files
Upgrade AVX2 code for SQ8 (#2942)
Summary: More efficient code for SQ8 for AVX2. For clang-15, improves a number of Instructions per cycle (IPC) from 2.49 to 3.20 Pull Request resolved: #2942 Reviewed By: algoriddle Differential Revision: D47946167 Pulled By: mdouze fbshipit-source-id: da864bac8d452f2eb111ca356e54a8a69cd03dbf
1 parent 0aae4d3 commit 5a95d47

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

faiss/impl/ScalarQuantizer.cpp

+9-12
Original file line numberDiff line numberDiff line change
@@ -74,18 +74,15 @@ struct Codec8bit {
7474
}
7575

7676
#ifdef __AVX2__
77-
static __m256 decode_8_components(const uint8_t* code, int i) {
78-
uint64_t c8 = *(uint64_t*)(code + i);
79-
__m128i c4lo = _mm_cvtepu8_epi32(_mm_set1_epi32(c8));
80-
__m128i c4hi = _mm_cvtepu8_epi32(_mm_set1_epi32(c8 >> 32));
81-
// __m256i i8 = _mm256_set_m128i(c4lo, c4hi);
82-
__m256i i8 = _mm256_castsi128_si256(c4lo);
83-
i8 = _mm256_insertf128_si256(i8, c4hi, 1);
84-
__m256 f8 = _mm256_cvtepi32_ps(i8);
85-
__m256 half = _mm256_set1_ps(0.5f);
86-
f8 = _mm256_add_ps(f8, half);
87-
__m256 one_255 = _mm256_set1_ps(1.f / 255.f);
88-
return _mm256_mul_ps(f8, one_255);
77+
static inline __m256 decode_8_components(const uint8_t* code, int i) {
78+
const uint64_t c8 = *(uint64_t*)(code + i);
79+
80+
const __m128i i8 = _mm_set1_epi64x(c8);
81+
const __m256i i32 = _mm256_cvtepu8_epi32(i8);
82+
const __m256 f8 = _mm256_cvtepi32_ps(i32);
83+
const __m256 half_one_255 = _mm256_set1_ps(0.5f / 255.f);
84+
const __m256 one_255 = _mm256_set1_ps(1.f / 255.f);
85+
return _mm256_fmadd_ps(f8, one_255, half_one_255);
8986
}
9087
#endif
9188
};

0 commit comments

Comments
 (0)