Commit a956a852c for llama.cpp
commit a956a852c9c8bc1fb07206364838ddef299178c0
Author: miyan <1138989048@qq.com>
Date: Sun Nov 15 02:06:52 2026 +0800
Fix ctz64 on 32bit (#32273)
diff --git a/src/llama-kv-cells.h b/src/llama-kv-cells.h
index 85a8e0f75..b0202cb28 100644
--- a/src/llama-kv-cells.h
+++ b/src/llama-kv-cells.h
@@ -27,8 +27,16 @@
static inline int ctz64(uint64_t x) {
#if defined(_MSC_VER)
unsigned long i;
+#if defined(_WIN64)
_BitScanForward64(&i, x);
return (int) i;
+#else
+ if (_BitScanForward(&i, (unsigned long) x)) {
+ return (int) i;
+ }
+ _BitScanForward(&i, (unsigned long) (x >> 32));
+ return (int) i + 32;
+#endif
#else
return __builtin_ctzll(x);
#endif
diff --git a/tests/test-kv-cache.cpp b/tests/test-kv-cache.cpp
index 6089c170f..c8255f572 100644
--- a/tests/test-kv-cache.cpp
+++ b/tests/test-kv-cache.cpp
@@ -16,6 +16,11 @@ static void init_model(llama_model & model, uint32_t n_layer) {
}
int main() {
+ for (int bit = 0; bit < 64; ++bit) {
+ GGML_ASSERT(ctz64(uint64_t{1} << bit) == bit);
+ GGML_ASSERT(ctz64(UINT64_MAX << bit) == bit);
+ }
+
llama_backend_init();
{