Skip to content

Commit cd9cea3

Browse files
committed
move intrinsics to the utils
1 parent 1a9cab9 commit cd9cea3

4 files changed

Lines changed: 54 additions & 26 deletions

File tree

libsql-sqlite3/src/sqliteInt.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5321,6 +5321,8 @@ int sqlite3AddInt64(i64*,i64);
53215321
int sqlite3SubInt64(i64*,i64);
53225322
int sqlite3MulInt64(i64*,i64);
53235323
int sqlite3AbsInt32(int);
5324+
int sqlite3PopCount32(u32);
5325+
int sqlite3PopCount64(u64);
53245326
#ifdef SQLITE_ENABLE_8_3_NAMES
53255327
void sqlite3FileSuffix3(const char*, char*);
53265328
#else

libsql-sqlite3/src/util.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1542,6 +1542,43 @@ int sqlite3SafetyCheckSickOrOk(sqlite3 *db){
15421542
}
15431543
}
15441544

1545+
1546+
// [sum(map(int, bin(i)[2:])) for i in range(256)]
1547+
static int BitsCount[256] = {
1548+
0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
1549+
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
1550+
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
1551+
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
1552+
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
1553+
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
1554+
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
1555+
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
1556+
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
1557+
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
1558+
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
1559+
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
1560+
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
1561+
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
1562+
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
1563+
4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8,
1564+
};
1565+
1566+
int sqlite3PopCount32(u32 a){
1567+
#if GCC_VERSION>=5004000 && !defined(__INTEL_COMPILER)
1568+
return __builtin_popcount(a);
1569+
#else
1570+
return BitsCount[a >> 24] + BitsCount[(a >> 16) & 0xff] + BitsCount[(a >> 8) & 0xff] + BitsCount[a & 0xff];
1571+
#endif
1572+
}
1573+
1574+
int sqlite3PopCount64(u64 a){
1575+
#if GCC_VERSION>=5004000 && !defined(__INTEL_COMPILER)
1576+
return __builtin_popcountll(a);
1577+
#else
1578+
return sqlite3PopCount32(a >> 32) + sqlite3PopCount32(a & 0xffffffff);
1579+
#endif
1580+
}
1581+
15451582
/*
15461583
** Attempt to add, subtract, or multiply the 64-bit signed value iB against
15471584
** the other 64-bit signed integer at *pA and store the result in *pA.

libsql-sqlite3/src/vector1bit.c

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -70,38 +70,27 @@ size_t vector1BitSerializeToBlob(
7070
return (pVector->dims + 7) / 8;
7171
}
7272

73-
// [sum(map(int, bin(i)[2:])) for i in range(256)]
74-
static int BitsCount[256] = {
75-
0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
76-
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
77-
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
78-
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
79-
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
80-
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
81-
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
82-
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
83-
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
84-
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
85-
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
86-
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
87-
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
88-
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
89-
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
90-
4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8,
91-
};
92-
9373
int vector1BitDistanceHamming(const Vector *v1, const Vector *v2){
9474
int diff = 0;
95-
u8 *e1 = v1->data;
96-
u8 *e2 = v2->data;
97-
int i;
75+
u8 *e1U8 = v1->data;
76+
u32 *e1U32 = v1->data;
77+
u8 *e2U8 = v2->data;
78+
u32 *e2U32 = v2->data;
79+
int i, len8, len32, offset8;
9880

9981
assert( v1->dims == v2->dims );
10082
assert( v1->type == VECTOR_TYPE_1BIT );
10183
assert( v2->type == VECTOR_TYPE_1BIT );
10284

103-
for(i = 0; i < v1->dims; i += 8){
104-
diff += BitsCount[e1[i/8] ^ e2[i/8]];
85+
len8 = (v1->dims + 7) / 8;
86+
len32 = v1->dims / 32;
87+
offset8 = len32 * 4;
88+
89+
for(i = 0; i < len32; i++){
90+
diff += sqlite3PopCount32(e1U32[i] ^ e2U32[i]);
91+
}
92+
for(i = offset8; i < len8; i++){
93+
diff += sqlite3PopCount32(e1U8[i] ^ e2U8[i]);
10594
}
10695
return diff;
10796
}

libsql-sqlite3/src/vectordiskann.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
#include "sqliteInt.h"
5555
#include "vectorIndexInt.h"
5656

57-
#define SQLITE_VECTOR_TRACE
57+
// #define SQLITE_VECTOR_TRACE
5858
#if defined(SQLITE_DEBUG) && defined(SQLITE_VECTOR_TRACE)
5959
#define DiskAnnTrace(X) sqlite3DebugPrintf X;
6060
#else

0 commit comments

Comments
 (0)