Skip to content

Commit 26bd889

Browse files
author
liu.shen
committed
fix #829: 兼容Intel Cascade Lake架构的CPU
1 parent 407e1b9 commit 26bd889

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

third_party/llamafile/iqk_mul_mat.inc

+23-2
Original file line numberDiff line numberDiff line change
@@ -2385,7 +2385,12 @@ struct SimpleBits {
23852385
__m256i values[4];
23862386
};
23872387

2388-
2388+
// fix for #829: 添加对 AVX512VPOPCNTDQ 的检测
2389+
#if defined(HAVE_FANCY_SIMD) && defined(__AVX512VPOPCNTDQ__)
2390+
#define HAVE_AVX512_POPCNT 1
2391+
#else
2392+
#define HAVE_AVX512_POPCNT 0
2393+
#endif
23892394

23902395
struct EvenSignHelper {
23912396
#if defined HAVE_FANCY_SIMD
@@ -2396,7 +2401,23 @@ struct EvenSignHelper {
23962401
};
23972402
IQK_ALWAYS_INLINE void sign_2_values(__m256i aux, __m256i * values) const {
23982403
aux = _mm256_and_si256(_mm256_srlv_epi32(aux, shifts), mask);
2399-
auto pcnt = _mm256_popcnt_epi32(aux);
2404+
2405+
// fix for #829: 兼容Intel Cascade Lake架构的CPU,如果不支持AVX512VPOPCNTDQ扩展,则使用替代实现
2406+
#if HAVE_AVX512_POPCNT
2407+
auto pcnt = _mm256_popcnt_epi32(aux);
2408+
2409+
#else
2410+
// 提供替代实现,使用标准的位计数方法
2411+
__m256i pcnt;
2412+
int* pcnt_ptr = reinterpret_cast<int*>(&pcnt);
2413+
int* aux_ptr = reinterpret_cast<int*>(&aux); // 直接获取 aux 的地址,避免不必要的复制
2414+
2415+
#pragma unroll 8 // 提示编译器展开循环,提高 SIMD 计算吞吐量
2416+
for (int i = 0; i < 8; i++) {
2417+
pcnt_ptr[i] = __builtin_popcount(aux_ptr[i]); // 使用编译器内置 popcount
2418+
}
2419+
#endif
2420+
24002421
sbits_t sbits;
24012422
sbits.vec = _mm256_cvtepi32_epi8(_mm256_or_si256(aux, _mm256_slli_epi32(_mm256_and_si256(pcnt, mone), 7)));
24022423
values[0] = _mm256_mask_sub_epi8(values[0], sbits.mask[0], _mm256_setzero_si256(), values[0]);

0 commit comments

Comments
 (0)