Skip to content

Commit fab929b

Browse files
committed
fix: add MSVC-compatible __builtin_popcountl implementation
1 parent aa7f3e7 commit fab929b

1 file changed

Lines changed: 25 additions & 24 deletions

File tree

sqlite-vec.c

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,31 @@ static u8 hamdist_table[256] = {
551551
4, 5, 5, 6, 5, 6, 6, 7, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
552552
4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8};
553553

554+
// MSVC-compatible __builtin_popcountl - must be defined before first use
555+
#ifdef _MSC_VER
556+
#if !defined(__clang__) && (defined(_M_ARM) || defined(_M_ARM64))
557+
// From
558+
// https://github.com/ngtcp2/ngtcp2/blob/b64f1e77b5e0d880b93d31f474147fae4a1d17cc/lib/ngtcp2_ringbuf.c,
559+
// line 34-43
560+
static unsigned int __builtin_popcountl(u64 x) {
561+
unsigned int c = 0;
562+
for (; x; ++c) {
563+
x &= x - 1;
564+
}
565+
return c;
566+
}
567+
#else
568+
#include <intrin.h>
569+
#ifdef _WIN64
570+
#define __builtin_popcountl __popcnt64
571+
#else
572+
static unsigned int __builtin_popcountl(u64 n) {
573+
return __popcnt((u32)n) + __popcnt((u32)(n >> 32));
574+
}
575+
#endif
576+
#endif
577+
#endif
578+
554579
static f32 distance_cosine_bit_u64(u64 *a, u64 *b, size_t n) {
555580
f32 dot = 0;
556581
f32 aMag = 0;
@@ -634,30 +659,6 @@ static f32 distance_hamming_u8(u8 *a, u8 *b, size_t n) {
634659
return (f32)same;
635660
}
636661

637-
#ifdef _MSC_VER
638-
#if !defined(__clang__) && (defined(_M_ARM) || defined(_M_ARM64))
639-
// From
640-
// https://github.com/ngtcp2/ngtcp2/blob/b64f1e77b5e0d880b93d31f474147fae4a1d17cc/lib/ngtcp2_ringbuf.c,
641-
// line 34-43
642-
static unsigned int __builtin_popcountl(u64 x) {
643-
unsigned int c = 0;
644-
for (; x; ++c) {
645-
x &= x - 1;
646-
}
647-
return c;
648-
}
649-
#else
650-
#include <intrin.h>
651-
#ifdef _WIN64
652-
#define __builtin_popcountl __popcnt64
653-
#else
654-
static unsigned int __builtin_popcountl(u64 n) {
655-
return __popcnt((u32)n) + __popcnt((u32)(n >> 32));
656-
}
657-
#endif
658-
#endif
659-
#endif
660-
661662
static f32 distance_hamming_u64(u64 *a, u64 *b, size_t n) {
662663
int same = 0;
663664
for (unsigned long i = 0; i < n; i++) {

0 commit comments

Comments
 (0)