usearch 2.17.1 → 2.19.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/README.md +6 -2
  2. package/c/lib.cpp +5 -0
  3. package/c/module.modulemap +4 -0
  4. package/c/usearch.h +12 -4
  5. package/cpp/README.md +1 -1
  6. package/cpp/bench.cpp +1 -1
  7. package/cpp/test.cpp +110 -16
  8. package/include/usearch/index.hpp +48 -35
  9. package/include/usearch/index_dense.hpp +34 -19
  10. package/include/usearch/index_plugins.hpp +29 -7
  11. package/javascript/README.md +8 -6
  12. package/javascript/dist/cjs/usearch.d.ts +9 -4
  13. package/javascript/dist/cjs/usearch.js +23 -8
  14. package/javascript/dist/esm/usearch.d.ts +9 -4
  15. package/javascript/dist/esm/usearch.js +21 -7
  16. package/javascript/lib.cpp +137 -59
  17. package/javascript/usearch.test.js +359 -192
  18. package/javascript/usearch.ts +35 -11
  19. package/package.json +1 -1
  20. package/prebuilds/darwin-arm64+x64/usearch.node +0 -0
  21. package/prebuilds/linux-arm64/usearch.node +0 -0
  22. package/prebuilds/linux-x64/usearch.node +0 -0
  23. package/simsimd/CONTRIBUTING.md +23 -8
  24. package/simsimd/Cargo.lock +1 -1
  25. package/simsimd/Cargo.toml +1 -1
  26. package/simsimd/README.md +49 -9
  27. package/simsimd/build.rs +7 -3
  28. package/simsimd/c/lib.c +6 -1
  29. package/simsimd/include/simsimd/binary.h +3 -3
  30. package/simsimd/include/simsimd/dot.h +2 -2
  31. package/simsimd/include/simsimd/elementwise.h +26 -17
  32. package/simsimd/include/simsimd/simsimd.h +125 -49
  33. package/simsimd/include/simsimd/sparse.h +30 -29
  34. package/simsimd/include/simsimd/spatial.h +4 -3
  35. package/simsimd/include/simsimd/types.h +19 -8
  36. package/simsimd/javascript/lib.c +0 -1
  37. package/simsimd/package-lock.json +411 -362
  38. package/simsimd/package.json +9 -9
  39. package/simsimd/rust/lib.rs +63 -29
  40. package/simsimd/scripts/bench.cxx +13 -0
  41. package/simsimd/scripts/bench_cosine.rs +8 -0
  42. package/simsimd/scripts/bench_native.rs +4 -0
  43. package/simsimd/scripts/bench_sqeuclidean.rs +8 -0
  44. package/simsimd/scripts/test.c +22 -0
  45. package/simsimd/scripts/test.py +80 -4
  46. package/simsimd/swift/SimSIMD.swift +17 -0
  47. package/simsimd/swift/Test.swift +36 -0
@@ -27,14 +27,16 @@
27
27
  // - `SIMSIMD_INTERNAL` is used for internal helper functions with unstable APIs.
28
28
  // - `SIMSIMD_DYNAMIC` is used for functions that are part of the public API, but are dispatched at runtime.
29
29
  //
30
+ // On GCC we mark the functions as `nonnull` informing that none of the arguments can be `NULL`.
31
+ // Marking with `pure` and `const` isn't possible as outputing to a pointer is a "side effect".
30
32
  #if defined(_WIN32) || defined(__CYGWIN__)
31
33
  #define SIMSIMD_DYNAMIC __declspec(dllexport)
32
34
  #define SIMSIMD_PUBLIC inline static
33
35
  #define SIMSIMD_INTERNAL inline static
34
36
  #elif defined(__GNUC__) || defined(__clang__)
35
- #define SIMSIMD_DYNAMIC __attribute__((visibility("default")))
36
- #define SIMSIMD_PUBLIC __attribute__((unused)) inline static
37
- #define SIMSIMD_INTERNAL inline static
37
+ #define SIMSIMD_DYNAMIC __attribute__((visibility("default"))) __attribute__((nonnull))
38
+ #define SIMSIMD_PUBLIC __attribute__((unused, nonnull)) inline static
39
+ #define SIMSIMD_INTERNAL __attribute__((always_inline)) inline static
38
40
  #else
39
41
  #define SIMSIMD_DYNAMIC
40
42
  #define SIMSIMD_PUBLIC inline static
@@ -223,7 +225,7 @@
223
225
  #endif
224
226
  #endif // !defined(SIMSIMD_TARGET_SIERRA) || ...
225
227
 
226
- #ifdef _MSC_VER
228
+ #if defined(_MSC_VER)
227
229
  #include <intrin.h>
228
230
  #else
229
231
 
@@ -257,6 +259,13 @@
257
259
  #define SIMSIMD_LOG(x) (log(x))
258
260
  #endif
259
261
 
262
+ #if defined(__GNUC__) || defined(__clang__)
263
+ #define SIMSIMD_COPY16(destination_ptr, source_ptr) __builtin_memcpy((destination_ptr), (source_ptr), 2)
264
+ #else
265
+ #include <string.h> /* fallback for exotic compilers */
266
+ #define SIMSIMD_COPY16(destination_ptr, source_ptr) memcpy((destination_ptr), (source_ptr), 2)
267
+ #endif
268
+
260
269
  #if !defined(SIMSIMD_F32_DIVISION_EPSILON)
261
270
  #define SIMSIMD_F32_DIVISION_EPSILON (1e-7)
262
271
  #endif
@@ -539,7 +548,8 @@ SIMSIMD_INTERNAL simsimd_f32_t simsimd_approximate_log(simsimd_f32_t number) {
539
548
  * https://github.com/OpenCyphal/libcanard/blob/636795f4bc395f56af8d2c61d3757b5e762bb9e5/canard.c#L811-L834
540
549
  */
541
550
  SIMSIMD_INTERNAL simsimd_f32_t simsimd_f16_to_f32(simsimd_f16_t const *x_ptr) {
542
- unsigned short x = *(unsigned short const *)x_ptr;
551
+ unsigned short x;
552
+ SIMSIMD_COPY16(&x, x_ptr);
543
553
  unsigned int exponent = (x & 0x7C00) >> 10;
544
554
  unsigned int mantissa = (x & 0x03FF) << 13;
545
555
  simsimd_f32i32_t mantissa_conv;
@@ -569,7 +579,7 @@ SIMSIMD_INTERNAL void simsimd_f32_to_f16(simsimd_f32_t x, simsimd_f16_t *result_
569
579
  unsigned short result = ((b & 0x80000000) >> 16) | (e > 112) * ((((e - 112) << 10) & 0x7C00) | (m >> 13)) |
570
580
  ((e < 113) & (e > 101)) * ((((0x007FF000 + m) >> (125 - e)) + 1) >> 1) |
571
581
  ((e > 143) * 0x7FFF);
572
- *(unsigned short *)result_ptr = result;
582
+ SIMSIMD_COPY16(result_ptr, &result);
573
583
  }
574
584
 
575
585
  /**
@@ -580,7 +590,8 @@ SIMSIMD_INTERNAL void simsimd_f32_to_f16(simsimd_f32_t x, simsimd_f16_t *result_
580
590
  * https://cloud.google.com/blog/products/ai-machine-learning/bfloat16-the-secret-to-high-performance-on-cloud-tpus
581
591
  */
582
592
  SIMSIMD_INTERNAL simsimd_f32_t simsimd_bf16_to_f32(simsimd_bf16_t const *x_ptr) {
583
- unsigned short x = *(unsigned short const *)x_ptr;
593
+ unsigned short x;
594
+ SIMSIMD_COPY16(&x, x_ptr);
584
595
  simsimd_f32i32_t conv;
585
596
  conv.i = x << 16; // Zero extends the mantissa
586
597
  return conv.f;
@@ -599,7 +610,7 @@ SIMSIMD_INTERNAL void simsimd_f32_to_bf16(simsimd_f32_t x, simsimd_bf16_t *resul
599
610
  conv.i >>= 16;
600
611
  // The top 16 bits will be zeroed out anyways
601
612
  // conv.i &= 0xFFFF;
602
- *(unsigned short *)result_ptr = (unsigned short)conv.i;
613
+ SIMSIMD_COPY16(result_ptr, &conv.i);
603
614
  }
604
615
 
605
616
  SIMSIMD_INTERNAL simsimd_u32_t simsimd_u32_rol(simsimd_u32_t x, int n) { return (x << n) | (x >> (32 - n)); }
@@ -4,7 +4,6 @@
4
4
  * @author Ash Vardanian
5
5
  * @date October 18, 2023
6
6
  *
7
- * @copyright Copyright (c) 2023
8
7
  * @see NodeJS docs: https://nodejs.org/api/n-api.html
9
8
  */
10
9