From 5d333d5af3d60b61a5da36f1df79337a9850d76c Mon Sep 17 00:00:00 2001 From: Ash Vardanian <1983160+ashvardanian@users.noreply.github.com> Date: Mon, 9 Oct 2023 17:46:27 -0700 Subject: [PATCH] Fix: Missing `__builtin_clzll` symbol --- stringzilla/stringzilla.h | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/stringzilla/stringzilla.h b/stringzilla/stringzilla.h index 94bbde44..cfcd0220 100644 --- a/stringzilla/stringzilla.h +++ b/stringzilla/stringzilla.h @@ -806,14 +806,26 @@ inline static void sz_sort_insertion(sz_sequence_t *sequence, sz_sequence_compar } } -// Utility functions inline static sz_size_t _sz_log2i(sz_size_t n) { - if (n == 0) return 0; // to avoid undefined behavior with __builtin_clz + if (n == 0) return 0; + #if defined(__LP64__) || defined(_WIN64) // 64-bit +#ifdef _MSC_VER + unsigned long index; + _BitScanReverse64(&index, n); + return index; +#else return 63 - __builtin_clzll(n); +#endif #else // 32-bit +#ifdef _MSC_VER + unsigned long index; + _BitScanReverse(&index, n); + return index; +#else return 31 - __builtin_clz(n); #endif +#endif } inline static void _sz_sift_down(