mirror of
https://github.com/pgvector/pgvector.git
synced 2026-12-04 00:08:30 +00:00
Improved performance of halfvec L2 function with F16C
This commit is contained in:
1 parent
af9f907fa7
commit
305e756a31
1 file changed
+13
-3
+13
-3
@@ -95,23 +95,33 @@ HalfvecL2SquaredDistanceF16c(int dim, half * ax, half * bx)
|
||||
float distance;
|
||||
int i;
|
||||
float s[8];
|
||||
int count = (dim / 16) * 16;
|
||||
int count = (dim / 32) * 32;
|
||||
int count8 = (dim / 8) * 8;
|
||||
__m256 dist0 = _mm256_setzero_ps();
|
||||
__m256 dist1 = _mm256_setzero_ps();
|
||||
__m256 dist2 = _mm256_setzero_ps();
|
||||
__m256 dist3 = _mm256_setzero_ps();
|
||||
|
||||
for (i = 0; i < count; i += 16)
|
||||
for (i = 0; i < count; i += 32)
|
||||
{
|
||||
__m128i axi0 = _mm_loadu_si128((__m128i *) (ax + i));
|
||||
__m128i axi1 = _mm_loadu_si128((__m128i *) (ax + i + 8));
|
||||
__m128i axi2 = _mm_loadu_si128((__m128i *) (ax + i + 16));
|
||||
__m128i axi3 = _mm_loadu_si128((__m128i *) (ax + i + 24));
|
||||
__m128i bxi0 = _mm_loadu_si128((__m128i *) (bx + i));
|
||||
__m128i bxi1 = _mm_loadu_si128((__m128i *) (bx + i + 8));
|
||||
__m128i bxi2 = _mm_loadu_si128((__m128i *) (bx + i + 16));
|
||||
__m128i bxi3 = _mm_loadu_si128((__m128i *) (bx + i + 24));
|
||||
|
||||
__m256 diff0 = _mm256_sub_ps(_mm256_cvtph_ps(axi0), _mm256_cvtph_ps(bxi0));
|
||||
__m256 diff1 = _mm256_sub_ps(_mm256_cvtph_ps(axi1), _mm256_cvtph_ps(bxi1));
|
||||
__m256 diff2 = _mm256_sub_ps(_mm256_cvtph_ps(axi2), _mm256_cvtph_ps(bxi2));
|
||||
__m256 diff3 = _mm256_sub_ps(_mm256_cvtph_ps(axi3), _mm256_cvtph_ps(bxi3));
|
||||
|
||||
dist0 = _mm256_fmadd_ps(diff0, diff0, dist0);
|
||||
dist1 = _mm256_fmadd_ps(diff1, diff1, dist1);
|
||||
dist2 = _mm256_fmadd_ps(diff2, diff2, dist2);
|
||||
dist3 = _mm256_fmadd_ps(diff3, diff3, dist3);
|
||||
}
|
||||
|
||||
for (; i < count8; i += 8)
|
||||
@@ -124,7 +134,7 @@ HalfvecL2SquaredDistanceF16c(int dim, half * ax, half * bx)
|
||||
dist0 = _mm256_fmadd_ps(diff0, diff0, dist0);
|
||||
}
|
||||
|
||||
_mm256_storeu_ps(s, _mm256_add_ps(dist0, dist1));
|
||||
_mm256_storeu_ps(s, _mm256_add_ps(_mm256_add_ps(dist0, dist1), _mm256_add_ps(dist2, dist3)));
|
||||
|
||||
distance = s[0] + s[1] + s[2] + s[3] + s[4] + s[5] + s[6] + s[7];
|
||||
|
||||
|
||||
Reference in new issue
Block a user