@@ -211842,150 +211842,6 @@ SQLITE_PRIVATE void sqlite3RegisterVectorFunctions(void){
211842211842#endif /* !defined(SQLITE_OMIT_VECTOR) */
211843211843
211844211844/************** End of vector.c **********************************************/
211845- /************** Begin file vector1bit.c **************************************/
211846- /*
211847- ** 2024-07-04
211848- **
211849- ** Copyright 2024 the libSQL authors
211850- **
211851- ** Permission is hereby granted, free of charge, to any person obtaining a copy of
211852- ** this software and associated documentation files (the "Software"), to deal in
211853- ** the Software without restriction, including without limitation the rights to
211854- ** use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
211855- ** the Software, and to permit persons to whom the Software is furnished to do so,
211856- ** subject to the following conditions:
211857- **
211858- ** The above copyright notice and this permission notice shall be included in all
211859- ** copies or substantial portions of the Software.
211860- **
211861- ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
211862- ** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
211863- ** FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
211864- ** COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
211865- ** IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
211866- ** CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
211867- **
211868- ******************************************************************************
211869- **
211870- ** 1-bit vector format utilities.
211871- */
211872- #ifndef SQLITE_OMIT_VECTOR
211873- /* #include "sqliteInt.h" */
211874-
211875- /* #include "vectorInt.h" */
211876-
211877- /* #include <math.h> */
211878-
211879- /**************************************************************************
211880- ** Utility routines for debugging
211881- **************************************************************************/
211882-
211883- void vector1BitDump(const Vector *pVec){
211884- u8 *elems = pVec->data;
211885- unsigned i;
211886-
211887- assert( pVec->type == VECTOR_TYPE_FLOAT1BIT );
211888-
211889- printf("f1bit: [");
211890- for(i = 0; i < pVec->dims; i++){
211891- printf("%s%d", i == 0 ? "" : ", ", ((elems[i / 8] >> (i & 7)) & 1) ? +1 : -1);
211892- }
211893- printf("]\n");
211894- }
211895-
211896- /**************************************************************************
211897- ** Utility routines for vector serialization and deserialization
211898- **************************************************************************/
211899-
211900- size_t vector1BitSerializeToBlob(
211901- const Vector *pVector,
211902- unsigned char *pBlob,
211903- size_t nBlobSize
211904- ){
211905- u8 *elems = pVector->data;
211906- u8 *pPtr = pBlob;
211907- unsigned i;
211908-
211909- assert( pVector->type == VECTOR_TYPE_FLOAT1BIT );
211910- assert( pVector->dims <= MAX_VECTOR_SZ );
211911- assert( nBlobSize >= (pVector->dims + 7) / 8 );
211912-
211913- for(i = 0; i < (pVector->dims + 7) / 8; i++){
211914- pPtr[i] = elems[i];
211915- }
211916- return (pVector->dims + 7) / 8;
211917- }
211918-
211919- // [sum(map(int, bin(i)[2:])) for i in range(256)]
211920- static int BitsCount[256] = {
211921- 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
211922- 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
211923- 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
211924- 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
211925- 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
211926- 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
211927- 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
211928- 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
211929- 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
211930- 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
211931- 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
211932- 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
211933- 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
211934- 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
211935- 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
211936- 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8,
211937- };
211938-
211939- static inline int sqlite3PopCount32(u32 a){
211940- #if GCC_VERSION>=5004000 && !defined(__INTEL_COMPILER)
211941- return __builtin_popcount(a);
211942- #else
211943- return BitsCount[a >> 24] + BitsCount[(a >> 16) & 0xff] + BitsCount[(a >> 8) & 0xff] + BitsCount[a & 0xff];
211944- #endif
211945- }
211946-
211947- int vector1BitDistanceHamming(const Vector *v1, const Vector *v2){
211948- int diff = 0;
211949- u8 *e1U8 = v1->data;
211950- u32 *e1U32 = v1->data;
211951- u8 *e2U8 = v2->data;
211952- u32 *e2U32 = v2->data;
211953- int i, len8, len32, offset8;
211954-
211955- assert( v1->dims == v2->dims );
211956- assert( v1->type == VECTOR_TYPE_FLOAT1BIT );
211957- assert( v2->type == VECTOR_TYPE_FLOAT1BIT );
211958-
211959- len8 = (v1->dims + 7) / 8;
211960- len32 = v1->dims / 32;
211961- offset8 = len32 * 4;
211962-
211963- for(i = 0; i < len32; i++){
211964- diff += sqlite3PopCount32(e1U32[i] ^ e2U32[i]);
211965- }
211966- for(i = offset8; i < len8; i++){
211967- diff += sqlite3PopCount32(e1U8[i] ^ e2U8[i]);
211968- }
211969- return diff;
211970- }
211971-
211972- void vector1BitDeserializeFromBlob(
211973- Vector *pVector,
211974- const unsigned char *pBlob,
211975- size_t nBlobSize
211976- ){
211977- u8 *elems = pVector->data;
211978-
211979- assert( pVector->type == VECTOR_TYPE_FLOAT1BIT );
211980- assert( 0 <= pVector->dims && pVector->dims <= MAX_VECTOR_SZ );
211981- assert( nBlobSize >= (pVector->dims + 7) / 8 );
211982-
211983- memcpy(elems, pBlob, (pVector->dims + 7) / 8);
211984- }
211985-
211986- #endif /* !defined(SQLITE_OMIT_VECTOR) */
211987-
211988- /************** End of vector1bit.c ******************************************/
211989211845/************** Begin file vectordiskann.c ***********************************/
211990211846/*
211991211847** 2024-03-23
@@ -213765,6 +213621,150 @@ void diskAnnCloseIndex(DiskAnnIndex *pIndex){
213765213621#endif /* !defined(SQLITE_OMIT_VECTOR) */
213766213622
213767213623/************** End of vectordiskann.c ***************************************/
213624+ /************** Begin file vectorfloat1bit.c *********************************/
213625+ /*
213626+ ** 2024-07-04
213627+ **
213628+ ** Copyright 2024 the libSQL authors
213629+ **
213630+ ** Permission is hereby granted, free of charge, to any person obtaining a copy of
213631+ ** this software and associated documentation files (the "Software"), to deal in
213632+ ** the Software without restriction, including without limitation the rights to
213633+ ** use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
213634+ ** the Software, and to permit persons to whom the Software is furnished to do so,
213635+ ** subject to the following conditions:
213636+ **
213637+ ** The above copyright notice and this permission notice shall be included in all
213638+ ** copies or substantial portions of the Software.
213639+ **
213640+ ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
213641+ ** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
213642+ ** FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
213643+ ** COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
213644+ ** IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
213645+ ** CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
213646+ **
213647+ ******************************************************************************
213648+ **
213649+ ** 1-bit vector format utilities.
213650+ */
213651+ #ifndef SQLITE_OMIT_VECTOR
213652+ /* #include "sqliteInt.h" */
213653+
213654+ /* #include "vectorInt.h" */
213655+
213656+ /* #include <math.h> */
213657+
213658+ /**************************************************************************
213659+ ** Utility routines for debugging
213660+ **************************************************************************/
213661+
213662+ void vector1BitDump(const Vector *pVec){
213663+ u8 *elems = pVec->data;
213664+ unsigned i;
213665+
213666+ assert( pVec->type == VECTOR_TYPE_FLOAT1BIT );
213667+
213668+ printf("f1bit: [");
213669+ for(i = 0; i < pVec->dims; i++){
213670+ printf("%s%d", i == 0 ? "" : ", ", ((elems[i / 8] >> (i & 7)) & 1) ? +1 : -1);
213671+ }
213672+ printf("]\n");
213673+ }
213674+
213675+ /**************************************************************************
213676+ ** Utility routines for vector serialization and deserialization
213677+ **************************************************************************/
213678+
213679+ size_t vector1BitSerializeToBlob(
213680+ const Vector *pVector,
213681+ unsigned char *pBlob,
213682+ size_t nBlobSize
213683+ ){
213684+ u8 *elems = pVector->data;
213685+ u8 *pPtr = pBlob;
213686+ unsigned i;
213687+
213688+ assert( pVector->type == VECTOR_TYPE_FLOAT1BIT );
213689+ assert( pVector->dims <= MAX_VECTOR_SZ );
213690+ assert( nBlobSize >= (pVector->dims + 7) / 8 );
213691+
213692+ for(i = 0; i < (pVector->dims + 7) / 8; i++){
213693+ pPtr[i] = elems[i];
213694+ }
213695+ return (pVector->dims + 7) / 8;
213696+ }
213697+
213698+ // [sum(map(int, bin(i)[2:])) for i in range(256)]
213699+ static int BitsCount[256] = {
213700+ 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
213701+ 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
213702+ 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
213703+ 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
213704+ 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
213705+ 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
213706+ 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
213707+ 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
213708+ 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
213709+ 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
213710+ 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
213711+ 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
213712+ 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
213713+ 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
213714+ 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
213715+ 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8,
213716+ };
213717+
213718+ static inline int sqlite3PopCount32(u32 a){
213719+ #if GCC_VERSION>=5004000 && !defined(__INTEL_COMPILER)
213720+ return __builtin_popcount(a);
213721+ #else
213722+ return BitsCount[a >> 24] + BitsCount[(a >> 16) & 0xff] + BitsCount[(a >> 8) & 0xff] + BitsCount[a & 0xff];
213723+ #endif
213724+ }
213725+
213726+ int vector1BitDistanceHamming(const Vector *v1, const Vector *v2){
213727+ int diff = 0;
213728+ u8 *e1U8 = v1->data;
213729+ u32 *e1U32 = v1->data;
213730+ u8 *e2U8 = v2->data;
213731+ u32 *e2U32 = v2->data;
213732+ int i, len8, len32, offset8;
213733+
213734+ assert( v1->dims == v2->dims );
213735+ assert( v1->type == VECTOR_TYPE_FLOAT1BIT );
213736+ assert( v2->type == VECTOR_TYPE_FLOAT1BIT );
213737+
213738+ len8 = (v1->dims + 7) / 8;
213739+ len32 = v1->dims / 32;
213740+ offset8 = len32 * 4;
213741+
213742+ for(i = 0; i < len32; i++){
213743+ diff += sqlite3PopCount32(e1U32[i] ^ e2U32[i]);
213744+ }
213745+ for(i = offset8; i < len8; i++){
213746+ diff += sqlite3PopCount32(e1U8[i] ^ e2U8[i]);
213747+ }
213748+ return diff;
213749+ }
213750+
213751+ void vector1BitDeserializeFromBlob(
213752+ Vector *pVector,
213753+ const unsigned char *pBlob,
213754+ size_t nBlobSize
213755+ ){
213756+ u8 *elems = pVector->data;
213757+
213758+ assert( pVector->type == VECTOR_TYPE_FLOAT1BIT );
213759+ assert( 0 <= pVector->dims && pVector->dims <= MAX_VECTOR_SZ );
213760+ assert( nBlobSize >= (pVector->dims + 7) / 8 );
213761+
213762+ memcpy(elems, pBlob, (pVector->dims + 7) / 8);
213763+ }
213764+
213765+ #endif /* !defined(SQLITE_OMIT_VECTOR) */
213766+
213767+ /************** End of vectorfloat1bit.c *************************************/
213768213768/************** Begin file vectorfloat32.c ***********************************/
213769213769/*
213770213770** 2024-07-04
@@ -214183,7 +214183,6 @@ void vectorF64DeserializeFromBlob(
214183214183**
214184214184** libSQL vector search.
214185214185*/
214186- /* #include "vectorInt.h" */
214187214186#ifndef SQLITE_OMIT_VECTOR
214188214187/* #include "sqlite3.h" */
214189214188/* #include "vdbeInt.h" */
0 commit comments