usearch 2.21.4 → 2.25.0

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 (602) hide show
  1. package/README.md +24 -24
  2. package/binding.gyp +15 -37
  3. package/c/CMakeLists.txt +4 -5
  4. package/c/README.md +7 -7
  5. package/c/lib.cpp +32 -14
  6. package/c/test.c +83 -1
  7. package/c/usearch.h +24 -3
  8. package/cpp/CMakeLists.txt +4 -0
  9. package/cpp/README.md +5 -5
  10. package/cpp/bench.cpp +97 -116
  11. package/cpp/test.cpp +117 -20
  12. package/include/usearch/index.hpp +224 -48
  13. package/include/usearch/index_dense.hpp +105 -40
  14. package/include/usearch/index_plugins.hpp +1387 -204
  15. package/javascript/dist/cjs/usearch.d.ts +8 -3
  16. package/javascript/dist/cjs/usearch.js +12 -4
  17. package/javascript/dist/esm/usearch.d.ts +8 -3
  18. package/javascript/dist/esm/usearch.js +12 -4
  19. package/javascript/lib.cpp +17 -0
  20. package/javascript/usearch.ts +14 -6
  21. package/numkong/.clang-tidy +30 -0
  22. package/{simsimd → numkong}/.cmake-format.py +18 -0
  23. package/numkong/.editorconfig +12 -0
  24. package/numkong/.git-blame-ignore-revs +123 -0
  25. package/numkong/.mailmap +15 -0
  26. package/numkong/.spi.yml +11 -0
  27. package/numkong/.swift-format +14 -0
  28. package/numkong/CITATION.cff +81 -0
  29. package/numkong/CONTRIBUTING.md +500 -0
  30. package/numkong/Cargo.toml +54 -0
  31. package/numkong/MANIFEST.in +8 -0
  32. package/numkong/Package.swift +84 -0
  33. package/numkong/README.md +569 -0
  34. package/numkong/bench/README.md +217 -0
  35. package/numkong/bench/bench-browser.html +323 -0
  36. package/numkong/bench/bench.cpp +344 -0
  37. package/numkong/bench/bench.hpp +816 -0
  38. package/numkong/bench/bench.js +148 -0
  39. package/numkong/bench/bench.mjs +616 -0
  40. package/numkong/bench/bench_attention.cpp +113 -0
  41. package/numkong/bench/bench_cast.cpp +264 -0
  42. package/numkong/bench/bench_cross_amx.cpp +62 -0
  43. package/numkong/bench/bench_cross_arm.cpp +245 -0
  44. package/numkong/bench/bench_cross_blas.cpp +309 -0
  45. package/numkong/bench/bench_cross_loongarch.cpp +106 -0
  46. package/numkong/bench/bench_cross_power.cpp +84 -0
  47. package/numkong/bench/bench_cross_rvv.cpp +121 -0
  48. package/numkong/bench/bench_cross_serial.cpp +151 -0
  49. package/numkong/bench/bench_cross_sme.cpp +163 -0
  50. package/numkong/bench/bench_cross_wasm.cpp +142 -0
  51. package/numkong/bench/bench_cross_x86.cpp +455 -0
  52. package/numkong/bench/bench_curved.cpp +210 -0
  53. package/numkong/bench/bench_dot.cpp +315 -0
  54. package/numkong/bench/bench_each.cpp +503 -0
  55. package/numkong/bench/bench_geospatial.cpp +112 -0
  56. package/numkong/bench/bench_maxsim.cpp +154 -0
  57. package/numkong/bench/bench_mesh.cpp +148 -0
  58. package/numkong/bench/bench_probability.cpp +60 -0
  59. package/numkong/bench/bench_reduce.cpp +371 -0
  60. package/numkong/bench/bench_set.cpp +73 -0
  61. package/numkong/bench/bench_sparse.cpp +198 -0
  62. package/numkong/bench/bench_spatial.cpp +347 -0
  63. package/numkong/bench/bench_trigonometry.cpp +137 -0
  64. package/numkong/bench/swift/Bench.swift +506 -0
  65. package/numkong/binding.gyp +169 -0
  66. package/numkong/build.rs +484 -0
  67. package/numkong/c/dispatch.h +512 -0
  68. package/numkong/c/dispatch_bf16.c +447 -0
  69. package/numkong/c/dispatch_bf16c.c +52 -0
  70. package/numkong/c/dispatch_e2m3.c +296 -0
  71. package/numkong/c/dispatch_e3m2.c +284 -0
  72. package/numkong/c/dispatch_e4m3.c +318 -0
  73. package/numkong/c/dispatch_e5m2.c +316 -0
  74. package/numkong/c/dispatch_f16.c +452 -0
  75. package/numkong/c/dispatch_f16c.c +58 -0
  76. package/numkong/c/dispatch_f32.c +434 -0
  77. package/numkong/c/dispatch_f32c.c +99 -0
  78. package/numkong/c/dispatch_f64.c +348 -0
  79. package/numkong/c/dispatch_f64c.c +98 -0
  80. package/numkong/c/dispatch_i16.c +96 -0
  81. package/numkong/c/dispatch_i32.c +89 -0
  82. package/numkong/c/dispatch_i4.c +153 -0
  83. package/numkong/c/dispatch_i64.c +86 -0
  84. package/numkong/c/dispatch_i8.c +348 -0
  85. package/numkong/c/dispatch_other.c +348 -0
  86. package/numkong/c/dispatch_u1.c +193 -0
  87. package/numkong/c/dispatch_u16.c +124 -0
  88. package/numkong/c/dispatch_u32.c +118 -0
  89. package/numkong/c/dispatch_u4.c +153 -0
  90. package/numkong/c/dispatch_u64.c +102 -0
  91. package/numkong/c/dispatch_u8.c +364 -0
  92. package/numkong/c/numkong.c +940 -0
  93. package/numkong/cmake/nk_arm_isa_probes.cmake +61 -0
  94. package/numkong/cmake/nk_isa_probe.cmake +90 -0
  95. package/numkong/cmake/nk_loongarch_isa_probes.cmake +15 -0
  96. package/numkong/cmake/nk_math_lib.cmake +46 -0
  97. package/numkong/cmake/nk_power_isa_probes.cmake +15 -0
  98. package/numkong/cmake/nk_riscv_isa_probes.cmake +19 -0
  99. package/numkong/cmake/nk_x86_isa_probes.cmake +35 -0
  100. package/numkong/cmake/toolchain-aarch64-gnu.cmake +86 -0
  101. package/numkong/cmake/toolchain-android-arm64.cmake +25 -0
  102. package/numkong/cmake/toolchain-android-armv7.cmake +25 -0
  103. package/numkong/cmake/toolchain-loongarch64-gnu.cmake +68 -0
  104. package/numkong/cmake/toolchain-ppc64le-gnu.cmake +74 -0
  105. package/numkong/cmake/toolchain-riscv64-gnu.cmake +107 -0
  106. package/numkong/cmake/toolchain-riscv64-llvm.cmake +114 -0
  107. package/numkong/cmake/toolchain-wasi.cmake +77 -0
  108. package/numkong/cmake/toolchain-wasm.cmake +70 -0
  109. package/numkong/cmake/toolchain-wasm64.cmake +62 -0
  110. package/numkong/cmake/toolchain-x86_64-llvm.cmake +47 -0
  111. package/numkong/include/README.md +643 -0
  112. package/numkong/include/module.modulemap +129 -0
  113. package/numkong/include/numkong/attention/sapphireamx.h +1363 -0
  114. package/numkong/include/numkong/attention/sme.h +2075 -0
  115. package/numkong/include/numkong/attention.h +49 -0
  116. package/numkong/include/numkong/capabilities.h +930 -0
  117. package/numkong/include/numkong/cast/README.md +262 -0
  118. package/numkong/include/numkong/cast/diamond.h +64 -0
  119. package/numkong/include/numkong/cast/haswell.h +823 -0
  120. package/numkong/include/numkong/cast/icelake.h +475 -0
  121. package/numkong/include/numkong/cast/loongsonasx.h +252 -0
  122. package/numkong/include/numkong/cast/neon.h +1174 -0
  123. package/numkong/include/numkong/cast/powervsx.h +449 -0
  124. package/numkong/include/numkong/cast/rvv.h +970 -0
  125. package/numkong/include/numkong/cast/sapphire.h +262 -0
  126. package/numkong/include/numkong/cast/serial.h +2336 -0
  127. package/numkong/include/numkong/cast/skylake.h +915 -0
  128. package/numkong/include/numkong/cast/v128relaxed.h +537 -0
  129. package/numkong/include/numkong/cast.h +254 -0
  130. package/numkong/include/numkong/cast.hpp +44 -0
  131. package/numkong/include/numkong/curved/README.md +223 -0
  132. package/numkong/include/numkong/curved/genoa.h +182 -0
  133. package/numkong/include/numkong/curved/haswell.h +276 -0
  134. package/numkong/include/numkong/curved/neon.h +329 -0
  135. package/numkong/include/numkong/curved/neonbfdot.h +211 -0
  136. package/numkong/include/numkong/curved/rvv.h +305 -0
  137. package/numkong/include/numkong/curved/serial.h +207 -0
  138. package/numkong/include/numkong/curved/skylake.h +457 -0
  139. package/numkong/include/numkong/curved/smef64.h +530 -0
  140. package/numkong/include/numkong/curved.h +513 -0
  141. package/numkong/include/numkong/curved.hpp +144 -0
  142. package/numkong/include/numkong/dot/README.md +442 -0
  143. package/numkong/include/numkong/dot/alder.h +563 -0
  144. package/numkong/include/numkong/dot/diamond.h +158 -0
  145. package/numkong/include/numkong/dot/genoa.h +289 -0
  146. package/numkong/include/numkong/dot/haswell.h +1723 -0
  147. package/numkong/include/numkong/dot/icelake.h +997 -0
  148. package/numkong/include/numkong/dot/loongsonasx.h +671 -0
  149. package/numkong/include/numkong/dot/neon.h +869 -0
  150. package/numkong/include/numkong/dot/neonbfdot.h +243 -0
  151. package/numkong/include/numkong/dot/neonfhm.h +358 -0
  152. package/numkong/include/numkong/dot/neonfp8.h +323 -0
  153. package/numkong/include/numkong/dot/neonsdot.h +622 -0
  154. package/numkong/include/numkong/dot/powervsx.h +752 -0
  155. package/numkong/include/numkong/dot/rvv.h +722 -0
  156. package/numkong/include/numkong/dot/rvvbb.h +72 -0
  157. package/numkong/include/numkong/dot/rvvbf16.h +123 -0
  158. package/numkong/include/numkong/dot/rvvhalf.h +129 -0
  159. package/numkong/include/numkong/dot/sapphire.h +141 -0
  160. package/numkong/include/numkong/dot/serial.h +874 -0
  161. package/numkong/include/numkong/dot/sierra.h +405 -0
  162. package/numkong/include/numkong/dot/skylake.h +1116 -0
  163. package/numkong/include/numkong/dot/sve.h +420 -0
  164. package/numkong/include/numkong/dot/svebfdot.h +75 -0
  165. package/numkong/include/numkong/dot/svehalf.h +168 -0
  166. package/numkong/include/numkong/dot/svesdot.h +90 -0
  167. package/numkong/include/numkong/dot/v128relaxed.h +1293 -0
  168. package/numkong/include/numkong/dot.h +1136 -0
  169. package/numkong/include/numkong/dot.hpp +94 -0
  170. package/numkong/include/numkong/dots/README.md +496 -0
  171. package/numkong/include/numkong/dots/alder.h +117 -0
  172. package/numkong/include/numkong/dots/diamond.h +86 -0
  173. package/numkong/include/numkong/dots/genoa.h +100 -0
  174. package/numkong/include/numkong/dots/graniteamx.h +733 -0
  175. package/numkong/include/numkong/dots/haswell.h +310 -0
  176. package/numkong/include/numkong/dots/icelake.h +180 -0
  177. package/numkong/include/numkong/dots/loongsonasx.h +176 -0
  178. package/numkong/include/numkong/dots/neon.h +123 -0
  179. package/numkong/include/numkong/dots/neonbfdot.h +59 -0
  180. package/numkong/include/numkong/dots/neonfhm.h +96 -0
  181. package/numkong/include/numkong/dots/neonfp8.h +99 -0
  182. package/numkong/include/numkong/dots/neonsdot.h +144 -0
  183. package/numkong/include/numkong/dots/powervsx.h +194 -0
  184. package/numkong/include/numkong/dots/rvv.h +2593 -0
  185. package/numkong/include/numkong/dots/sapphireamx.h +4017 -0
  186. package/numkong/include/numkong/dots/serial.h +2867 -0
  187. package/numkong/include/numkong/dots/sierra.h +100 -0
  188. package/numkong/include/numkong/dots/skylake.h +207 -0
  189. package/numkong/include/numkong/dots/sme.h +5041 -0
  190. package/numkong/include/numkong/dots/smebi32.h +477 -0
  191. package/numkong/include/numkong/dots/smef64.h +1328 -0
  192. package/numkong/include/numkong/dots/smehalf.h +47 -0
  193. package/numkong/include/numkong/dots/v128relaxed.h +317 -0
  194. package/numkong/include/numkong/dots.h +3110 -0
  195. package/numkong/include/numkong/dots.hpp +641 -0
  196. package/numkong/include/numkong/each/README.md +465 -0
  197. package/numkong/include/numkong/each/haswell.h +1658 -0
  198. package/numkong/include/numkong/each/icelake.h +272 -0
  199. package/numkong/include/numkong/each/neon.h +1138 -0
  200. package/numkong/include/numkong/each/neonbfdot.h +211 -0
  201. package/numkong/include/numkong/each/neonhalf.h +318 -0
  202. package/numkong/include/numkong/each/rvv.h +1121 -0
  203. package/numkong/include/numkong/each/sapphire.h +343 -0
  204. package/numkong/include/numkong/each/serial.h +282 -0
  205. package/numkong/include/numkong/each/skylake.h +1562 -0
  206. package/numkong/include/numkong/each/v128relaxed.h +562 -0
  207. package/numkong/include/numkong/each.h +2232 -0
  208. package/numkong/include/numkong/each.hpp +434 -0
  209. package/numkong/include/numkong/geospatial/README.md +147 -0
  210. package/numkong/include/numkong/geospatial/haswell.h +633 -0
  211. package/numkong/include/numkong/geospatial/neon.h +615 -0
  212. package/numkong/include/numkong/geospatial/rvv.h +701 -0
  213. package/numkong/include/numkong/geospatial/serial.h +309 -0
  214. package/numkong/include/numkong/geospatial/skylake.h +613 -0
  215. package/numkong/include/numkong/geospatial/v128relaxed.h +652 -0
  216. package/numkong/include/numkong/geospatial.h +453 -0
  217. package/numkong/include/numkong/geospatial.hpp +235 -0
  218. package/numkong/include/numkong/matrix.hpp +336 -0
  219. package/numkong/include/numkong/maxsim/README.md +187 -0
  220. package/numkong/include/numkong/maxsim/alder.h +511 -0
  221. package/numkong/include/numkong/maxsim/genoa.h +115 -0
  222. package/numkong/include/numkong/maxsim/haswell.h +553 -0
  223. package/numkong/include/numkong/maxsim/icelake.h +480 -0
  224. package/numkong/include/numkong/maxsim/neonsdot.h +394 -0
  225. package/numkong/include/numkong/maxsim/sapphireamx.h +877 -0
  226. package/numkong/include/numkong/maxsim/serial.h +490 -0
  227. package/numkong/include/numkong/maxsim/sme.h +993 -0
  228. package/numkong/include/numkong/maxsim/v128relaxed.h +280 -0
  229. package/numkong/include/numkong/maxsim.h +571 -0
  230. package/numkong/include/numkong/maxsim.hpp +133 -0
  231. package/numkong/include/numkong/mesh/README.md +219 -0
  232. package/numkong/include/numkong/mesh/haswell.h +2013 -0
  233. package/numkong/include/numkong/mesh/neon.h +1821 -0
  234. package/numkong/include/numkong/mesh/neonbfdot.h +771 -0
  235. package/numkong/include/numkong/mesh/rvv.h +936 -0
  236. package/numkong/include/numkong/mesh/serial.h +721 -0
  237. package/numkong/include/numkong/mesh/skylake.h +2344 -0
  238. package/numkong/include/numkong/mesh/v128relaxed.h +1081 -0
  239. package/numkong/include/numkong/mesh.h +666 -0
  240. package/numkong/include/numkong/mesh.hpp +701 -0
  241. package/numkong/include/numkong/numkong.h +78 -0
  242. package/numkong/include/numkong/numkong.hpp +58 -0
  243. package/numkong/include/numkong/probability/README.md +177 -0
  244. package/numkong/include/numkong/probability/haswell.h +267 -0
  245. package/numkong/include/numkong/probability/neon.h +255 -0
  246. package/numkong/include/numkong/probability/rvv.h +414 -0
  247. package/numkong/include/numkong/probability/serial.h +181 -0
  248. package/numkong/include/numkong/probability/skylake.h +324 -0
  249. package/numkong/include/numkong/probability.h +380 -0
  250. package/numkong/include/numkong/probability.hpp +120 -0
  251. package/numkong/include/numkong/random.h +50 -0
  252. package/numkong/include/numkong/random.hpp +285 -0
  253. package/numkong/include/numkong/reduce/README.md +552 -0
  254. package/numkong/include/numkong/reduce/alder.h +636 -0
  255. package/numkong/include/numkong/reduce/genoa.h +201 -0
  256. package/numkong/include/numkong/reduce/haswell.h +3785 -0
  257. package/numkong/include/numkong/reduce/icelake.h +549 -0
  258. package/numkong/include/numkong/reduce/neon.h +3980 -0
  259. package/numkong/include/numkong/reduce/neonbfdot.h +142 -0
  260. package/numkong/include/numkong/reduce/neonfhm.h +293 -0
  261. package/numkong/include/numkong/reduce/neonsdot.h +357 -0
  262. package/numkong/include/numkong/reduce/rvv.h +3533 -0
  263. package/numkong/include/numkong/reduce/serial.h +753 -0
  264. package/numkong/include/numkong/reduce/sierra.h +338 -0
  265. package/numkong/include/numkong/reduce/skylake.h +3794 -0
  266. package/numkong/include/numkong/reduce/sve.h +52 -0
  267. package/numkong/include/numkong/reduce/v128relaxed.h +2302 -0
  268. package/numkong/include/numkong/reduce.h +1582 -0
  269. package/numkong/include/numkong/reduce.hpp +778 -0
  270. package/numkong/include/numkong/scalar/README.md +89 -0
  271. package/numkong/include/numkong/scalar/haswell.h +122 -0
  272. package/numkong/include/numkong/scalar/loongsonasx.h +74 -0
  273. package/numkong/include/numkong/scalar/neon.h +122 -0
  274. package/numkong/include/numkong/scalar/neonhalf.h +70 -0
  275. package/numkong/include/numkong/scalar/powervsx.h +96 -0
  276. package/numkong/include/numkong/scalar/rvv.h +211 -0
  277. package/numkong/include/numkong/scalar/sapphire.h +74 -0
  278. package/numkong/include/numkong/scalar/serial.h +332 -0
  279. package/numkong/include/numkong/scalar/v128relaxed.h +56 -0
  280. package/numkong/include/numkong/scalar.h +696 -0
  281. package/numkong/include/numkong/set/README.md +179 -0
  282. package/numkong/include/numkong/set/haswell.h +334 -0
  283. package/numkong/include/numkong/set/icelake.h +485 -0
  284. package/numkong/include/numkong/set/loongsonasx.h +181 -0
  285. package/numkong/include/numkong/set/neon.h +363 -0
  286. package/numkong/include/numkong/set/powervsx.h +326 -0
  287. package/numkong/include/numkong/set/rvv.h +226 -0
  288. package/numkong/include/numkong/set/rvvbb.h +117 -0
  289. package/numkong/include/numkong/set/serial.h +174 -0
  290. package/numkong/include/numkong/set/sve.h +187 -0
  291. package/numkong/include/numkong/set/v128relaxed.h +240 -0
  292. package/numkong/include/numkong/set.h +471 -0
  293. package/numkong/include/numkong/set.hpp +114 -0
  294. package/numkong/include/numkong/sets/README.md +149 -0
  295. package/numkong/include/numkong/sets/haswell.h +63 -0
  296. package/numkong/include/numkong/sets/icelake.h +66 -0
  297. package/numkong/include/numkong/sets/loongsonasx.h +52 -0
  298. package/numkong/include/numkong/sets/neon.h +61 -0
  299. package/numkong/include/numkong/sets/powervsx.h +65 -0
  300. package/numkong/include/numkong/sets/serial.h +43 -0
  301. package/numkong/include/numkong/sets/smebi32.h +1135 -0
  302. package/numkong/include/numkong/sets/v128relaxed.h +58 -0
  303. package/numkong/include/numkong/sets.h +382 -0
  304. package/numkong/include/numkong/sparse/README.md +156 -0
  305. package/numkong/include/numkong/sparse/icelake.h +463 -0
  306. package/numkong/include/numkong/sparse/neon.h +288 -0
  307. package/numkong/include/numkong/sparse/serial.h +117 -0
  308. package/numkong/include/numkong/sparse/sve2.h +504 -0
  309. package/numkong/include/numkong/sparse/turin.h +322 -0
  310. package/numkong/include/numkong/sparse.h +363 -0
  311. package/numkong/include/numkong/sparse.hpp +113 -0
  312. package/numkong/include/numkong/spatial/README.md +439 -0
  313. package/numkong/include/numkong/spatial/alder.h +604 -0
  314. package/numkong/include/numkong/spatial/diamond.h +240 -0
  315. package/numkong/include/numkong/spatial/genoa.h +222 -0
  316. package/numkong/include/numkong/spatial/haswell.h +979 -0
  317. package/numkong/include/numkong/spatial/icelake.h +1067 -0
  318. package/numkong/include/numkong/spatial/loongsonasx.h +483 -0
  319. package/numkong/include/numkong/spatial/neon.h +846 -0
  320. package/numkong/include/numkong/spatial/neonbfdot.h +164 -0
  321. package/numkong/include/numkong/spatial/neonfp8.h +258 -0
  322. package/numkong/include/numkong/spatial/neonsdot.h +429 -0
  323. package/numkong/include/numkong/spatial/powervsx.h +738 -0
  324. package/numkong/include/numkong/spatial/rvv.h +991 -0
  325. package/numkong/include/numkong/spatial/rvvbf16.h +128 -0
  326. package/numkong/include/numkong/spatial/rvvhalf.h +120 -0
  327. package/numkong/include/numkong/spatial/serial.h +347 -0
  328. package/numkong/include/numkong/spatial/sierra.h +516 -0
  329. package/numkong/include/numkong/spatial/skylake.h +605 -0
  330. package/numkong/include/numkong/spatial/sve.h +246 -0
  331. package/numkong/include/numkong/spatial/svebfdot.h +125 -0
  332. package/numkong/include/numkong/spatial/svehalf.h +136 -0
  333. package/numkong/include/numkong/spatial/svesdot.h +143 -0
  334. package/numkong/include/numkong/spatial/v128relaxed.h +969 -0
  335. package/numkong/include/numkong/spatial.h +1681 -0
  336. package/numkong/include/numkong/spatial.hpp +183 -0
  337. package/numkong/include/numkong/spatials/README.md +580 -0
  338. package/numkong/include/numkong/spatials/alder.h +94 -0
  339. package/numkong/include/numkong/spatials/diamond.h +82 -0
  340. package/numkong/include/numkong/spatials/genoa.h +94 -0
  341. package/numkong/include/numkong/spatials/graniteamx.h +173 -0
  342. package/numkong/include/numkong/spatials/haswell.h +219 -0
  343. package/numkong/include/numkong/spatials/icelake.h +113 -0
  344. package/numkong/include/numkong/spatials/loongsonasx.h +153 -0
  345. package/numkong/include/numkong/spatials/neon.h +109 -0
  346. package/numkong/include/numkong/spatials/neonbfdot.h +60 -0
  347. package/numkong/include/numkong/spatials/neonfhm.h +92 -0
  348. package/numkong/include/numkong/spatials/neonfp8.h +111 -0
  349. package/numkong/include/numkong/spatials/neonsdot.h +143 -0
  350. package/numkong/include/numkong/spatials/powervsx.h +153 -0
  351. package/numkong/include/numkong/spatials/rvv.h +1976 -0
  352. package/numkong/include/numkong/spatials/sapphireamx.h +1149 -0
  353. package/numkong/include/numkong/spatials/serial.h +248 -0
  354. package/numkong/include/numkong/spatials/sierra.h +96 -0
  355. package/numkong/include/numkong/spatials/skylake.h +184 -0
  356. package/numkong/include/numkong/spatials/sme.h +1927 -0
  357. package/numkong/include/numkong/spatials/smef64.h +483 -0
  358. package/numkong/include/numkong/spatials/v128relaxed.h +240 -0
  359. package/numkong/include/numkong/spatials.h +3374 -0
  360. package/numkong/include/numkong/spatials.hpp +509 -0
  361. package/numkong/include/numkong/tensor.hpp +1981 -0
  362. package/numkong/include/numkong/trigonometry/README.md +184 -0
  363. package/numkong/include/numkong/trigonometry/haswell.h +653 -0
  364. package/numkong/include/numkong/trigonometry/neon.h +638 -0
  365. package/numkong/include/numkong/trigonometry/rvv.h +700 -0
  366. package/numkong/include/numkong/trigonometry/serial.h +703 -0
  367. package/numkong/include/numkong/trigonometry/skylake.h +725 -0
  368. package/numkong/include/numkong/trigonometry/v128relaxed.h +667 -0
  369. package/numkong/include/numkong/trigonometry.h +467 -0
  370. package/numkong/include/numkong/trigonometry.hpp +166 -0
  371. package/numkong/include/numkong/types.h +1695 -0
  372. package/numkong/include/numkong/types.hpp +6027 -0
  373. package/numkong/include/numkong/vector.hpp +766 -0
  374. package/numkong/javascript/@numkong-darwin-arm64/package.json +26 -0
  375. package/numkong/javascript/@numkong-darwin-x64/package.json +26 -0
  376. package/numkong/javascript/@numkong-linux-arm64/package.json +25 -0
  377. package/numkong/javascript/@numkong-linux-x64/package.json +25 -0
  378. package/numkong/javascript/@numkong-win32-arm64/package.json +25 -0
  379. package/numkong/javascript/@numkong-win32-x64/package.json +25 -0
  380. package/numkong/javascript/README.md +254 -0
  381. package/numkong/javascript/node-gyp-build.d.ts +4 -0
  382. package/numkong/javascript/numkong-browser.ts +40 -0
  383. package/numkong/javascript/numkong-wasm.ts +750 -0
  384. package/numkong/javascript/numkong.c +712 -0
  385. package/numkong/javascript/numkong.ts +613 -0
  386. package/{simsimd → numkong}/javascript/tsconfig-base.json +6 -3
  387. package/numkong/javascript/tsconfig-cjs.json +13 -0
  388. package/{simsimd → numkong}/javascript/tsconfig-esm.json +1 -1
  389. package/numkong/javascript/types.ts +784 -0
  390. package/numkong/numkong.gypi +104 -0
  391. package/numkong/package.json +108 -0
  392. package/numkong/probes/arm_neon.c +8 -0
  393. package/numkong/probes/arm_neon_bfdot.c +9 -0
  394. package/numkong/probes/arm_neon_fhm.c +9 -0
  395. package/numkong/probes/arm_neon_half.c +8 -0
  396. package/numkong/probes/arm_neon_sdot.c +9 -0
  397. package/numkong/probes/arm_neonfp8.c +9 -0
  398. package/numkong/probes/arm_sme.c +16 -0
  399. package/numkong/probes/arm_sme2.c +16 -0
  400. package/numkong/probes/arm_sme2p1.c +16 -0
  401. package/numkong/probes/arm_sme_bf16.c +16 -0
  402. package/numkong/probes/arm_sme_bi32.c +16 -0
  403. package/numkong/probes/arm_sme_f64.c +16 -0
  404. package/numkong/probes/arm_sme_fa64.c +14 -0
  405. package/numkong/probes/arm_sme_half.c +16 -0
  406. package/numkong/probes/arm_sme_lut2.c +15 -0
  407. package/numkong/probes/arm_sve.c +18 -0
  408. package/numkong/probes/arm_sve2.c +20 -0
  409. package/numkong/probes/arm_sve2p1.c +18 -0
  410. package/numkong/probes/arm_sve_bfdot.c +20 -0
  411. package/numkong/probes/arm_sve_half.c +18 -0
  412. package/numkong/probes/arm_sve_sdot.c +21 -0
  413. package/numkong/probes/loongarch_lasx.c +12 -0
  414. package/numkong/probes/power_vsx.c +12 -0
  415. package/numkong/probes/probe.js +127 -0
  416. package/numkong/probes/riscv_rvv.c +14 -0
  417. package/numkong/probes/riscv_rvv_bb.c +15 -0
  418. package/numkong/probes/riscv_rvv_bf16.c +17 -0
  419. package/numkong/probes/riscv_rvv_half.c +14 -0
  420. package/numkong/probes/wasm_v128relaxed.c +11 -0
  421. package/numkong/probes/x86_alder.c +17 -0
  422. package/numkong/probes/x86_diamond.c +17 -0
  423. package/numkong/probes/x86_genoa.c +17 -0
  424. package/numkong/probes/x86_graniteamx.c +19 -0
  425. package/numkong/probes/x86_haswell.c +11 -0
  426. package/numkong/probes/x86_icelake.c +17 -0
  427. package/numkong/probes/x86_sapphire.c +16 -0
  428. package/numkong/probes/x86_sapphireamx.c +18 -0
  429. package/numkong/probes/x86_sierra.c +17 -0
  430. package/numkong/probes/x86_skylake.c +15 -0
  431. package/numkong/probes/x86_turin.c +17 -0
  432. package/numkong/rust/README.md +698 -0
  433. package/numkong/rust/capabilities.rs +96 -0
  434. package/numkong/rust/cast.rs +314 -0
  435. package/numkong/rust/curved.rs +391 -0
  436. package/numkong/rust/each.rs +2721 -0
  437. package/numkong/rust/geospatial.rs +343 -0
  438. package/numkong/rust/matrix.rs +5424 -0
  439. package/numkong/rust/maxsim.rs +540 -0
  440. package/numkong/rust/mesh.rs +837 -0
  441. package/numkong/rust/numkong.rs +409 -0
  442. package/numkong/rust/probability.rs +272 -0
  443. package/numkong/rust/reduce.rs +1249 -0
  444. package/numkong/rust/set.rs +194 -0
  445. package/numkong/rust/sparse.rs +513 -0
  446. package/numkong/rust/spatial.rs +1815 -0
  447. package/numkong/rust/tensor.rs +7565 -0
  448. package/numkong/rust/types.rs +3666 -0
  449. package/numkong/rust/vector.rs +1899 -0
  450. package/numkong/rustfmt.toml +2 -0
  451. package/numkong/swift/Matrix.swift +1388 -0
  452. package/numkong/swift/MaxSim.swift +151 -0
  453. package/numkong/swift/NumKong.swift +283 -0
  454. package/numkong/swift/Numerics.swift +329 -0
  455. package/numkong/swift/README.md +433 -0
  456. package/numkong/swift/Scalar.swift +741 -0
  457. package/numkong/swift/Tensor.swift +209 -0
  458. package/numkong/test/README.md +315 -0
  459. package/numkong/test/playwright.config.ts +16 -0
  460. package/numkong/test/swift/Test.swift +534 -0
  461. package/numkong/test/test-browser-bundle-runner.mjs +36 -0
  462. package/numkong/test/test-browser-bundle.html +188 -0
  463. package/numkong/test/test-browser-runner.mjs +43 -0
  464. package/numkong/test/test-browser.html +305 -0
  465. package/numkong/test/test-dtypes.mjs +453 -0
  466. package/numkong/test/test-wasm.mjs +267 -0
  467. package/numkong/test/test.cpp +442 -0
  468. package/numkong/test/test.hpp +580 -0
  469. package/numkong/test/test.mjs +363 -0
  470. package/numkong/test/test_base.py +1041 -0
  471. package/numkong/test/test_cast.cpp +242 -0
  472. package/numkong/test/test_cross.hpp +551 -0
  473. package/numkong/test/test_cross.py +467 -0
  474. package/numkong/test/test_cross_amx.cpp +50 -0
  475. package/numkong/test/test_cross_arm.cpp +225 -0
  476. package/numkong/test/test_cross_blas.cpp +354 -0
  477. package/numkong/test/test_cross_loongarch.cpp +90 -0
  478. package/numkong/test/test_cross_power.cpp +73 -0
  479. package/numkong/test/test_cross_rvv.cpp +109 -0
  480. package/numkong/test/test_cross_serial.cpp +181 -0
  481. package/numkong/test/test_cross_sme.cpp +148 -0
  482. package/numkong/test/test_cross_wasm.cpp +141 -0
  483. package/numkong/test/test_cross_x86.cpp +442 -0
  484. package/numkong/test/test_curved.cpp +182 -0
  485. package/numkong/test/test_curved.py +207 -0
  486. package/numkong/test/test_dot.cpp +353 -0
  487. package/numkong/test/test_dot.py +296 -0
  488. package/numkong/test/test_each.cpp +437 -0
  489. package/numkong/test/test_each.py +756 -0
  490. package/numkong/test/test_geospatial.cpp +155 -0
  491. package/numkong/test/test_geospatial.py +241 -0
  492. package/numkong/test/test_maxsim.cpp +138 -0
  493. package/numkong/test/test_maxsim.py +209 -0
  494. package/numkong/test/test_mesh.cpp +203 -0
  495. package/numkong/test/test_mesh.py +394 -0
  496. package/numkong/test/test_probability.cpp +136 -0
  497. package/numkong/test/test_probability.py +166 -0
  498. package/numkong/test/test_reduce.cpp +563 -0
  499. package/numkong/test/test_reduce.py +781 -0
  500. package/numkong/test/test_set.cpp +151 -0
  501. package/numkong/test/test_set.py +120 -0
  502. package/numkong/test/test_similarities.py +579 -0
  503. package/numkong/test/test_sparse.cpp +121 -0
  504. package/numkong/test/test_sparse.py +118 -0
  505. package/numkong/test/test_spatial.cpp +518 -0
  506. package/numkong/test/test_spatial.py +269 -0
  507. package/numkong/test/test_tensor.cpp +884 -0
  508. package/numkong/test/test_tensor.py +1417 -0
  509. package/numkong/test/test_trigonometry.cpp +156 -0
  510. package/numkong/test/test_trigonometry.py +180 -0
  511. package/package.json +7 -8
  512. package/prebuilds/darwin-arm64+x64/usearch.node +0 -0
  513. package/prebuilds/linux-arm64/usearch.node +0 -0
  514. package/prebuilds/linux-x64/usearch.node +0 -0
  515. package/fp16/CMakeLists.txt +0 -176
  516. package/fp16/LICENSE +0 -11
  517. package/fp16/README.md +0 -20
  518. package/fp16/bench/alt-element.cc +0 -89
  519. package/fp16/bench/from-alt-array.cc +0 -175
  520. package/fp16/bench/from-ieee-array.cc +0 -399
  521. package/fp16/bench/ieee-element.cc +0 -272
  522. package/fp16/bench/to-alt-array.cc +0 -91
  523. package/fp16/bench/to-ieee-array.cc +0 -303
  524. package/fp16/cmake/DownloadGoogleBenchmark.cmake +0 -15
  525. package/fp16/cmake/DownloadGoogleTest.cmake +0 -15
  526. package/fp16/cmake/DownloadPSimd.cmake +0 -15
  527. package/fp16/configure.py +0 -65
  528. package/fp16/confu.yaml +0 -8
  529. package/fp16/include/fp16/__init__.py +0 -0
  530. package/fp16/include/fp16/avx.py +0 -50
  531. package/fp16/include/fp16/avx2.py +0 -53
  532. package/fp16/include/fp16/bitcasts.h +0 -92
  533. package/fp16/include/fp16/fp16.h +0 -451
  534. package/fp16/include/fp16/psimd.h +0 -131
  535. package/fp16/include/fp16.h +0 -11
  536. package/fp16/jni/Android.mk +0 -6
  537. package/fp16/jni/Application.mk +0 -4
  538. package/fp16/test/alt-from-fp32-value.cc +0 -498
  539. package/fp16/test/alt-to-fp32-bits.cc +0 -263
  540. package/fp16/test/alt-to-fp32-psimd.cc +0 -145
  541. package/fp16/test/alt-to-fp32-value.cc +0 -348
  542. package/fp16/test/alt-to-fp32x2-psimd.cc +0 -245
  543. package/fp16/test/bitcasts.cc +0 -56
  544. package/fp16/test/ieee-from-fp32-value.cc +0 -523
  545. package/fp16/test/ieee-to-fp32-bits.cc +0 -333
  546. package/fp16/test/ieee-to-fp32-psimd.cc +0 -312
  547. package/fp16/test/ieee-to-fp32-value.cc +0 -420
  548. package/fp16/test/ieee-to-fp32x2-psimd.cc +0 -541
  549. package/fp16/test/peachpy/alt-xmm-to-fp32-xmm-avx.cc +0 -96
  550. package/fp16/test/peachpy/alt-xmm-to-fp32-ymm-avx2.cc +0 -96
  551. package/fp16/test/peachpy/stubs.py +0 -38
  552. package/fp16/test/tables.cc +0 -530
  553. package/fp16/test/tables.h +0 -13
  554. package/fp16/third-party/THHalf.h +0 -92
  555. package/fp16/third-party/eigen-half.h +0 -170
  556. package/fp16/third-party/float16-compressor.h +0 -80
  557. package/fp16/third-party/half.hpp +0 -2916
  558. package/fp16/third-party/npy-halffloat.h +0 -328
  559. package/simsimd/.git-blame-ignore-revs +0 -2
  560. package/simsimd/CONTRIBUTING.md +0 -321
  561. package/simsimd/Cargo.toml +0 -49
  562. package/simsimd/MANIFEST.in +0 -4
  563. package/simsimd/Package.swift +0 -28
  564. package/simsimd/README.md +0 -1493
  565. package/simsimd/binding.gyp +0 -35
  566. package/simsimd/build.rs +0 -70
  567. package/simsimd/c/lib.c +0 -374
  568. package/simsimd/include/module.modulemap +0 -4
  569. package/simsimd/include/simsimd/binary.h +0 -486
  570. package/simsimd/include/simsimd/curved.h +0 -1541
  571. package/simsimd/include/simsimd/dot.h +0 -1853
  572. package/simsimd/include/simsimd/elementwise.h +0 -2465
  573. package/simsimd/include/simsimd/geospatial.h +0 -43
  574. package/simsimd/include/simsimd/mesh.h +0 -69
  575. package/simsimd/include/simsimd/probability.h +0 -606
  576. package/simsimd/include/simsimd/simsimd.h +0 -2567
  577. package/simsimd/include/simsimd/sparse.h +0 -1384
  578. package/simsimd/include/simsimd/spatial.h +0 -2335
  579. package/simsimd/include/simsimd/types.h +0 -666
  580. package/simsimd/javascript/fallback.ts +0 -233
  581. package/simsimd/javascript/lib.c +0 -125
  582. package/simsimd/javascript/node-gyp-build.d.ts +0 -1
  583. package/simsimd/javascript/simsimd.ts +0 -179
  584. package/simsimd/javascript/tsconfig-cjs.json +0 -8
  585. package/simsimd/package-lock.json +0 -1761
  586. package/simsimd/package.json +0 -61
  587. package/simsimd/rust/lib.rs +0 -1821
  588. package/simsimd/scripts/bench.cxx +0 -1170
  589. package/simsimd/scripts/bench.ipynb +0 -187
  590. package/simsimd/scripts/bench.js +0 -127
  591. package/simsimd/scripts/bench.rs +0 -876
  592. package/simsimd/scripts/bench_vectors.py +0 -734
  593. package/simsimd/scripts/bench_vectors_live.py +0 -213
  594. package/simsimd/scripts/test.c +0 -187
  595. package/simsimd/scripts/test.mjs +0 -220
  596. package/simsimd/scripts/test.py +0 -1505
  597. package/simsimd/swift/SimSIMD.swift +0 -149
  598. package/simsimd/swift/Test.swift +0 -128
  599. /package/{simsimd → numkong}/CODE_OF_CONDUCT.md +0 -0
  600. /package/{simsimd → numkong}/LICENSE +0 -0
  601. /package/{simsimd → numkong}/javascript/dist-package-cjs.json +0 -0
  602. /package/{simsimd → numkong}/javascript/dist-package-esm.json +0 -0
@@ -0,0 +1,784 @@
1
+ /**
2
+ * @brief Custom TypedArray classes for non-native numeric types.
3
+ * @file javascript/types.ts
4
+ * @author Ash Vardanian
5
+ * @date February 3, 2026
6
+ *
7
+ * This file provides TypedArray wrappers for numeric types not natively supported
8
+ * by JavaScript, using NumKong's SIMD-optimized conversion functions from the C library.
9
+ */
10
+
11
+ // Import conversion functions from the compiled native module
12
+ // These will be defined in numkong.ts after the module is loaded
13
+ let conversionFunctions: {
14
+ castF16ToF32: (bits: number) => number;
15
+ castF32ToF16: (value: number) => number;
16
+ castBF16ToF32: (bits: number) => number;
17
+ castF32ToBF16: (value: number) => number;
18
+ castE4M3ToF32: (bits: number) => number;
19
+ castF32ToE4M3: (value: number) => number;
20
+ castE5M2ToF32: (bits: number) => number;
21
+ castF32ToE5M2: (value: number) => number;
22
+ cast: (src: TypedArray, srcType: string, dst: TypedArray, dstType: string) => void;
23
+ };
24
+
25
+ // This will be called by numkong.ts after loading the module
26
+ export function setConversionFunctions(fns: typeof conversionFunctions) {
27
+ conversionFunctions = fns;
28
+ }
29
+
30
+ // Type alias for any TypedArray
31
+ export type TypedArray = Float64Array | Float32Array | Int32Array | Int8Array | Uint8Array | Uint16Array | Uint32Array;
32
+
33
+ /** @brief Numeric data type enum — integer switch, compiles to jump table. */
34
+ export enum DType {
35
+ F64 = 0,
36
+ F32 = 1,
37
+ F16 = 2,
38
+ BF16 = 3,
39
+ E4M3 = 4,
40
+ E5M2 = 5,
41
+ E2M3 = 6,
42
+ E3M2 = 7,
43
+ I8 = 8,
44
+ U8 = 9,
45
+ U1 = 10,
46
+ I32 = 11,
47
+ U32 = 12,
48
+ }
49
+
50
+ /** @brief O(1) array lookup for DType → string conversion (needed at N-API/WASM boundaries). */
51
+ export const DTYPE_STRINGS: readonly string[] = [
52
+ 'f64', 'f32', 'f16', 'bf16', 'e4m3', 'e5m2', 'e2m3', 'e3m2', 'i8', 'u8', 'u1', 'i32', 'u32',
53
+ ];
54
+
55
+ /** @brief Convert a DType enum value to its string representation. */
56
+ export function dtypeToString(d: DType): string { return DTYPE_STRINGS[d]; }
57
+
58
+ /** @brief Infer the DType from a TypedArray instance. */
59
+ function inferDtype(arr: TypedArray): DType {
60
+ if (arr instanceof Float64Array) return DType.F64;
61
+ if (arr instanceof Float32Array) return DType.F32;
62
+ if (arr instanceof Int32Array) return DType.I32;
63
+ if (arr instanceof Int8Array) return DType.I8;
64
+ if (arr instanceof Uint8Array) return DType.U8;
65
+ if (arr instanceof Uint16Array) return DType.F16;
66
+ if (arr instanceof Uint32Array) return DType.U32;
67
+ throw new Error(`Cannot infer dtype from ${(arr as any).constructor.name}`);
68
+ }
69
+
70
+ /**
71
+ * @brief Abstract base class for all tensor types.
72
+ *
73
+ * All fields are embedded — zero dynamic allocation. DType is a numeric enum
74
+ * (integer switch). Mirrors the C++ pattern: buffer + byteOffset + dtype.
75
+ */
76
+ export abstract class TensorBase {
77
+ readonly buffer: ArrayBuffer;
78
+ readonly byteOffset: number;
79
+ readonly dtype: DType;
80
+
81
+ protected constructor(buffer: ArrayBuffer, byteOffset: number, dtype: DType) {
82
+ this.buffer = buffer;
83
+ this.byteOffset = byteOffset;
84
+ this.dtype = dtype;
85
+ }
86
+
87
+ abstract get length(): number;
88
+ abstract get rank(): number;
89
+
90
+ /** @brief Bytes per element for this tensor's dtype (compiles to jump table). */
91
+ get bytesPerElement(): number {
92
+ switch (this.dtype) {
93
+ case DType.F64: return 8;
94
+ case DType.F32: case DType.I32: case DType.U32: return 4;
95
+ case DType.F16: case DType.BF16: return 2;
96
+ default: return 1;
97
+ }
98
+ }
99
+
100
+ /** @brief Total byte length of the tensor data. */
101
+ get byteLength(): number { return this.length * this.bytesPerElement; }
102
+ }
103
+
104
+ /**
105
+ * @brief Abstract rank-1 tensor base class.
106
+ */
107
+ export abstract class VectorBase extends TensorBase {
108
+ readonly length: number;
109
+
110
+ protected constructor(buffer: ArrayBuffer, byteOffset: number, length: number, dtype: DType) {
111
+ super(buffer, byteOffset, dtype);
112
+ this.length = length;
113
+ }
114
+
115
+ get rank(): 1 { return 1; }
116
+ }
117
+
118
+ /**
119
+ * @brief Non-owning rank-1 tensor view (like std::span<T>).
120
+ *
121
+ * Zero-copy wrapper for existing memory. Ideal for cross-module WASM interop
122
+ * where data already lives on the WASM heap.
123
+ */
124
+ export class VectorView extends VectorBase {
125
+ constructor(buffer: ArrayBuffer, byteOffset: number, length: number, dtype: DType) {
126
+ super(buffer, byteOffset, length, dtype);
127
+ }
128
+
129
+ toString(): string {
130
+ return `VectorView(${this.length}, ${dtypeToString(this.dtype)})`;
131
+ }
132
+
133
+ [Symbol.for('nodejs.util.inspect.custom')](): string {
134
+ return this.toString();
135
+ }
136
+
137
+ /** @brief Create a VectorView from any TypedArray, inferring or accepting dtype. */
138
+ static from(arr: TypedArray, dtype?: DType): VectorView {
139
+ const d = dtype ?? inferDtype(arr);
140
+ return new VectorView(arr.buffer as ArrayBuffer, arr.byteOffset, arr.length, d);
141
+ }
142
+ }
143
+
144
+ /**
145
+ * @brief Owning rank-1 tensor (like std::vector<T>).
146
+ *
147
+ * Allocates its own ArrayBuffer. Use for storing results or when you need
148
+ * independent ownership of the data.
149
+ */
150
+ export class Vector extends VectorBase {
151
+ constructor(length: number, dtype: DType);
152
+ constructor(buffer: ArrayBuffer, length: number, dtype: DType);
153
+ constructor(lengthOrBuffer: number | ArrayBuffer, dtypeOrLength: DType | number, dtype?: DType) {
154
+ if (typeof lengthOrBuffer === 'number') {
155
+ const length = lengthOrBuffer;
156
+ const dt = dtypeOrLength as DType;
157
+ let bpe: number;
158
+ switch (dt) {
159
+ case DType.F64: bpe = 8; break;
160
+ case DType.F32: case DType.I32: case DType.U32: bpe = 4; break;
161
+ case DType.F16: case DType.BF16: bpe = 2; break;
162
+ default: bpe = 1; break;
163
+ }
164
+ super(new ArrayBuffer(length * bpe), 0, length, dt);
165
+ } else {
166
+ super(lengthOrBuffer, 0, dtypeOrLength as number, dtype!);
167
+ }
168
+ }
169
+
170
+ toString(): string {
171
+ return `Vector(${this.length}, ${dtypeToString(this.dtype)})`;
172
+ }
173
+
174
+ [Symbol.for('nodejs.util.inspect.custom')](): string {
175
+ return this.toString();
176
+ }
177
+
178
+ /** @brief Create an owning Vector by copying data from a TypedArray. */
179
+ static fromTypedArray(arr: TypedArray, dtype?: DType): Vector {
180
+ const d = dtype ?? inferDtype(arr);
181
+ return new Vector((arr.buffer as ArrayBuffer).slice(arr.byteOffset, arr.byteOffset + arr.byteLength), arr.length, d);
182
+ }
183
+
184
+ /** @brief Create an owning Vector by copying data from any TensorBase. */
185
+ static fromView(view: TensorBase): Vector {
186
+ return new Vector(view.buffer.slice(view.byteOffset, view.byteOffset + view.byteLength), view.length, view.dtype);
187
+ }
188
+
189
+ /** @brief Return a TypedArray view over this Vector's owned buffer (zero-copy). */
190
+ toTypedArray(): TypedArray {
191
+ switch (this.dtype) {
192
+ case DType.F64: return new Float64Array(this.buffer, 0, this.length);
193
+ case DType.F32: return new Float32Array(this.buffer, 0, this.length);
194
+ case DType.I32: return new Int32Array(this.buffer, 0, this.length);
195
+ case DType.U32: return new Uint32Array(this.buffer, 0, this.length);
196
+ case DType.F16: case DType.BF16: return new Uint16Array(this.buffer, 0, this.length);
197
+ case DType.I8: return new Int8Array(this.buffer, 0, this.length);
198
+ default: return new Uint8Array(this.buffer, 0, this.length);
199
+ }
200
+ }
201
+ }
202
+
203
+ /**
204
+ * @brief Abstract rank-2 tensor base class.
205
+ *
206
+ * All 4 dimension fields are embedded — no dynamic allocation.
207
+ * Strides are in bytes to match the C API directly.
208
+ */
209
+ export abstract class MatrixBase extends TensorBase {
210
+ readonly rows: number;
211
+ readonly cols: number;
212
+ readonly rowStride: number;
213
+ readonly colStride: number;
214
+
215
+ protected constructor(
216
+ buffer: ArrayBuffer, byteOffset: number, dtype: DType,
217
+ rows: number, cols: number, rowStride: number, colStride: number,
218
+ ) {
219
+ super(buffer, byteOffset, dtype);
220
+ this.rows = rows;
221
+ this.cols = cols;
222
+ this.rowStride = rowStride;
223
+ this.colStride = colStride;
224
+ }
225
+
226
+ get length(): number { return this.rows * this.cols; }
227
+ get rank(): 2 { return 2; }
228
+ }
229
+
230
+ /**
231
+ * @brief Owning rank-2 tensor (row-major, C-contiguous by default).
232
+ *
233
+ * Strides are byte strides. Default for C-contiguous layout:
234
+ * rowStride = cols * bytesPerElement, colStride = bytesPerElement.
235
+ */
236
+ export class Matrix extends MatrixBase {
237
+ constructor(rows: number, cols: number, dtype: DType);
238
+ constructor(buffer: ArrayBuffer, byteOffset: number, dtype: DType, rows: number, cols: number, rowStride?: number, colStride?: number);
239
+ constructor(
240
+ rowsOrBuffer: number | ArrayBuffer,
241
+ colsOrByteOffset: number,
242
+ dtype: DType,
243
+ rows?: number,
244
+ cols?: number,
245
+ rowStride?: number,
246
+ colStride?: number,
247
+ ) {
248
+ if (typeof rowsOrBuffer === 'number') {
249
+ const r = rowsOrBuffer;
250
+ const c = colsOrByteOffset;
251
+ let bpe: number;
252
+ switch (dtype) {
253
+ case DType.F64: bpe = 8; break;
254
+ case DType.F32: case DType.I32: case DType.U32: bpe = 4; break;
255
+ case DType.F16: case DType.BF16: bpe = 2; break;
256
+ default: bpe = 1; break;
257
+ }
258
+ super(new ArrayBuffer(r * c * bpe), 0, dtype, r, c, c * bpe, bpe);
259
+ } else {
260
+ const r = rows!;
261
+ const c = cols!;
262
+ let bpe: number;
263
+ switch (dtype) {
264
+ case DType.F64: bpe = 8; break;
265
+ case DType.F32: case DType.I32: case DType.U32: bpe = 4; break;
266
+ case DType.F16: case DType.BF16: bpe = 2; break;
267
+ default: bpe = 1; break;
268
+ }
269
+ super(rowsOrBuffer, colsOrByteOffset, dtype, r, c, rowStride ?? c * bpe, colStride ?? bpe);
270
+ }
271
+ }
272
+
273
+ toString(): string {
274
+ return `Matrix(${this.rows}\u00d7${this.cols}, ${dtypeToString(this.dtype)})`;
275
+ }
276
+
277
+ [Symbol.for('nodejs.util.inspect.custom')](): string {
278
+ return this.toString();
279
+ }
280
+
281
+ static fromTypedArray(array: TypedArray, rows: number, cols: number, dtype?: DType): Matrix {
282
+ const d = dtype ?? inferDtype(array);
283
+ const buf = (array.buffer as ArrayBuffer).slice(array.byteOffset, array.byteOffset + array.byteLength);
284
+ return new Matrix(buf, 0, d, rows, cols);
285
+ }
286
+
287
+ toTypedArray(): TypedArray {
288
+ switch (this.dtype) {
289
+ case DType.F64: return new Float64Array(this.buffer, this.byteOffset, this.rows * this.cols);
290
+ case DType.F32: return new Float32Array(this.buffer, this.byteOffset, this.rows * this.cols);
291
+ case DType.I32: return new Int32Array(this.buffer, this.byteOffset, this.rows * this.cols);
292
+ case DType.U32: return new Uint32Array(this.buffer, this.byteOffset, this.rows * this.cols);
293
+ case DType.F16: case DType.BF16: return new Uint16Array(this.buffer, this.byteOffset, this.rows * this.cols);
294
+ case DType.I8: return new Int8Array(this.buffer, this.byteOffset, this.rows * this.cols);
295
+ default: return new Uint8Array(this.buffer, this.byteOffset, this.rows * this.cols);
296
+ }
297
+ }
298
+
299
+ row(index: number): VectorView {
300
+ return new VectorView(this.buffer, this.byteOffset + index * this.rowStride, this.cols, this.dtype);
301
+ }
302
+ }
303
+
304
+ /**
305
+ * @brief Opaque packed matrix container.
306
+ *
307
+ * Packed layout is not indexable — this is a data container for packed GEMM kernels.
308
+ * N-API path: buffer is a V8-managed ArrayBuffer, auto-freed by GC.
309
+ * WASM path: stores a heap pointer, dispose() calls Module._free().
310
+ */
311
+ export class PackedMatrix {
312
+ readonly width: number;
313
+ readonly depth: number;
314
+ readonly dtype: DType;
315
+ readonly byteLength: number;
316
+ readonly buffer: ArrayBuffer;
317
+ private _disposed: boolean = false;
318
+
319
+ constructor(buffer: ArrayBuffer, width: number, depth: number, dtype: DType, byteLength: number) {
320
+ this.buffer = buffer;
321
+ this.width = width;
322
+ this.depth = depth;
323
+ this.dtype = dtype;
324
+ this.byteLength = byteLength;
325
+ }
326
+
327
+ dispose(): void { this._disposed = true; }
328
+ get disposed(): boolean { return this._disposed; }
329
+
330
+ toString(): string {
331
+ return `PackedMatrix(${this.width}\u00d7${this.depth}, ${dtypeToString(this.dtype)}, ${this.byteLength} bytes)`;
332
+ }
333
+
334
+ [Symbol.for('nodejs.util.inspect.custom')](): string {
335
+ return this.toString();
336
+ }
337
+ }
338
+
339
+ /** @brief Kernel family identifiers for output dtype resolution. */
340
+ export type KernelFamily = 'dots' | 'angulars' | 'euclideans';
341
+
342
+ /**
343
+ * @brief Determines the output dtype for a given kernel family and input dtype.
344
+ * Mirrors nk_kernel_output_dtype from C.
345
+ */
346
+ export function outputDtype(family: KernelFamily, input: DType): DType {
347
+ switch (input) {
348
+ case DType.F64: return DType.F64;
349
+ case DType.F32: return DType.F64;
350
+ case DType.F16: case DType.BF16: case DType.E4M3: case DType.E5M2: case DType.E2M3: case DType.E3M2:
351
+ return DType.F32;
352
+ case DType.I8: return family === 'dots' ? DType.I32 : DType.F32;
353
+ case DType.U8: return family === 'dots' ? DType.U32 : DType.F32;
354
+ default: return DType.F32;
355
+ }
356
+ }
357
+
358
+ /**
359
+ * @brief IEEE 754 Half Precision Float (f16)
360
+ *
361
+ * 16-bit floating point: 1 sign bit, 5 exponent bits, 10 mantissa bits
362
+ * Range: ~±65504, precision: ~3-4 decimal digits
363
+ *
364
+ * Common in GPU inference, model compression, and mixed-precision training.
365
+ * Supported natively on Apple Silicon, NVIDIA GPUs (fp16), AMD GPUs.
366
+ */
367
+ export class Float16Array extends Uint16Array {
368
+ constructor(length: number | ArrayLike<number> | ArrayBufferLike, byteOffset?: number, arrayLength?: number) {
369
+ if (typeof length === 'number') {
370
+ super(length);
371
+ } else if (ArrayBuffer.isView(length) || length instanceof ArrayBuffer) {
372
+ super(length as any, byteOffset, arrayLength);
373
+ } else {
374
+ // Convert from array-like of numbers
375
+ const src = length as ArrayLike<number>;
376
+ const arr = new Uint16Array(src.length);
377
+ if (conversionFunctions) {
378
+ for (let i = 0; i < src.length; i++) {
379
+ arr[i] = conversionFunctions.castF32ToF16(src[i]);
380
+ }
381
+ }
382
+ super(arr);
383
+ }
384
+ }
385
+
386
+ /**
387
+ * @brief Converts the entire f16 array to f32 (Float32Array).
388
+ * @returns Float32Array with decoded values
389
+ */
390
+ toFloat32Array(): Float32Array {
391
+ if (!conversionFunctions) {
392
+ throw new Error('Conversion functions not initialized');
393
+ }
394
+ const result = new Float32Array(this.length);
395
+ for (let i = 0; i < this.length; i++) {
396
+ result[i] = conversionFunctions.castF16ToF32(this[i]);
397
+ }
398
+ return result;
399
+ }
400
+
401
+ /**
402
+ * @brief Gets the f32 value at the specified index.
403
+ * @param index Array index
404
+ * @returns Decoded f32 value
405
+ */
406
+ getFloat32(index: number): number {
407
+ if (!conversionFunctions) {
408
+ throw new Error('Conversion functions not initialized');
409
+ }
410
+ return conversionFunctions.castF16ToF32(this[index]);
411
+ }
412
+
413
+ /**
414
+ * @brief Sets the value at the specified index from an f32 value.
415
+ * @param index Array index
416
+ * @param value f32 value to encode and store
417
+ */
418
+ setFloat32(index: number, value: number): void {
419
+ if (!conversionFunctions) {
420
+ throw new Error('Conversion functions not initialized');
421
+ }
422
+ this[index] = conversionFunctions.castF32ToF16(value);
423
+ }
424
+
425
+ toString(): string {
426
+ if (!conversionFunctions) return `Float16Array(${this.length})`;
427
+ const limit = Math.min(this.length, 20);
428
+ const parts: string[] = [];
429
+ for (let i = 0; i < limit; i++) {
430
+ const f = conversionFunctions.castF16ToF32(this[i]);
431
+ parts.push(`${f} [0x${this[i].toString(16).padStart(4, '0')}]`);
432
+ }
433
+ const suffix = this.length > 20 ? ', ...' : '';
434
+ return `Float16Array(${this.length}) [${parts.join(', ')}${suffix}]`;
435
+ }
436
+
437
+ [Symbol.for('nodejs.util.inspect.custom')](): string {
438
+ return this.toString();
439
+ }
440
+ }
441
+
442
+ /**
443
+ * @brief Brain Float 16 (bf16)
444
+ *
445
+ * 16-bit floating point: 1 sign bit, 8 exponent bits, 7 mantissa bits
446
+ * Range: same as f32 (~±3.4e38), precision: ~2-3 decimal digits
447
+ *
448
+ * Designed by Google for TPUs, optimized for ML training (wider range than f16).
449
+ * Supported on Google TPUs, Intel Sapphire Rapids, AMD Genoa, ARM Neoverse V2.
450
+ * Truncated f32 (top 16 bits), making conversion very cheap.
451
+ */
452
+ export class BFloat16Array extends Uint16Array {
453
+ constructor(length: number | ArrayLike<number> | ArrayBufferLike, byteOffset?: number, arrayLength?: number) {
454
+ if (typeof length === 'number') {
455
+ super(length);
456
+ } else if (ArrayBuffer.isView(length) || length instanceof ArrayBuffer) {
457
+ super(length as any, byteOffset, arrayLength);
458
+ } else {
459
+ const src = length as ArrayLike<number>;
460
+ const arr = new Uint16Array(src.length);
461
+ if (conversionFunctions) {
462
+ for (let i = 0; i < src.length; i++) {
463
+ arr[i] = conversionFunctions.castF32ToBF16(src[i]);
464
+ }
465
+ }
466
+ super(arr);
467
+ }
468
+ }
469
+
470
+ toFloat32Array(): Float32Array {
471
+ if (!conversionFunctions) {
472
+ throw new Error('Conversion functions not initialized');
473
+ }
474
+ const result = new Float32Array(this.length);
475
+ for (let i = 0; i < this.length; i++) {
476
+ result[i] = conversionFunctions.castBF16ToF32(this[i]);
477
+ }
478
+ return result;
479
+ }
480
+
481
+ getFloat32(index: number): number {
482
+ if (!conversionFunctions) {
483
+ throw new Error('Conversion functions not initialized');
484
+ }
485
+ return conversionFunctions.castBF16ToF32(this[index]);
486
+ }
487
+
488
+ setFloat32(index: number, value: number): void {
489
+ if (!conversionFunctions) {
490
+ throw new Error('Conversion functions not initialized');
491
+ }
492
+ this[index] = conversionFunctions.castF32ToBF16(value);
493
+ }
494
+
495
+ toString(): string {
496
+ if (!conversionFunctions) return `BFloat16Array(${this.length})`;
497
+ const limit = Math.min(this.length, 20);
498
+ const parts: string[] = [];
499
+ for (let i = 0; i < limit; i++) {
500
+ const f = conversionFunctions.castBF16ToF32(this[i]);
501
+ parts.push(`${f} [0x${this[i].toString(16).padStart(4, '0')}]`);
502
+ }
503
+ const suffix = this.length > 20 ? ', ...' : '';
504
+ return `BFloat16Array(${this.length}) [${parts.join(', ')}${suffix}]`;
505
+ }
506
+
507
+ [Symbol.for('nodejs.util.inspect.custom')](): string {
508
+ return this.toString();
509
+ }
510
+ }
511
+
512
+ /**
513
+ * @brief FP8 E4M3 (4-bit exponent, 3-bit mantissa)
514
+ *
515
+ * 8-bit floating point: 1 sign bit, 4 exponent bits, 3 mantissa bits
516
+ * Range: ~±448, precision: ~1 decimal digit
517
+ *
518
+ * Optimized for forward pass inference with higher precision than E5M2.
519
+ * Supported on NVIDIA Hopper H100 GPUs, AMD Instinct MI300.
520
+ */
521
+ export class E4M3Array extends Uint8Array {
522
+ constructor(length: number | ArrayLike<number> | ArrayBufferLike, byteOffset?: number, arrayLength?: number) {
523
+ if (typeof length === 'number') {
524
+ super(length);
525
+ } else if (ArrayBuffer.isView(length) || length instanceof ArrayBuffer) {
526
+ super(length as any, byteOffset, arrayLength);
527
+ } else {
528
+ const src = length as ArrayLike<number>;
529
+ const arr = new Uint8Array(src.length);
530
+ if (conversionFunctions) {
531
+ for (let i = 0; i < src.length; i++) {
532
+ arr[i] = conversionFunctions.castF32ToE4M3(src[i]);
533
+ }
534
+ }
535
+ super(arr);
536
+ }
537
+ }
538
+
539
+ toFloat32Array(): Float32Array {
540
+ if (!conversionFunctions) {
541
+ throw new Error('Conversion functions not initialized');
542
+ }
543
+ const result = new Float32Array(this.length);
544
+ for (let i = 0; i < this.length; i++) {
545
+ result[i] = conversionFunctions.castE4M3ToF32(this[i]);
546
+ }
547
+ return result;
548
+ }
549
+
550
+ getFloat32(index: number): number {
551
+ if (!conversionFunctions) {
552
+ throw new Error('Conversion functions not initialized');
553
+ }
554
+ return conversionFunctions.castE4M3ToF32(this[index]);
555
+ }
556
+
557
+ setFloat32(index: number, value: number): void {
558
+ if (!conversionFunctions) {
559
+ throw new Error('Conversion functions not initialized');
560
+ }
561
+ this[index] = conversionFunctions.castF32ToE4M3(value);
562
+ }
563
+
564
+ toString(): string {
565
+ if (!conversionFunctions) return `E4M3Array(${this.length})`;
566
+ const limit = Math.min(this.length, 20);
567
+ const parts: string[] = [];
568
+ for (let i = 0; i < limit; i++) {
569
+ const f = conversionFunctions.castE4M3ToF32(this[i]);
570
+ parts.push(`${f} [0x${this[i].toString(16).padStart(2, '0')}]`);
571
+ }
572
+ const suffix = this.length > 20 ? ', ...' : '';
573
+ return `E4M3Array(${this.length}) [${parts.join(', ')}${suffix}]`;
574
+ }
575
+
576
+ [Symbol.for('nodejs.util.inspect.custom')](): string {
577
+ return this.toString();
578
+ }
579
+ }
580
+
581
+ /**
582
+ * @brief FP8 E5M2 (5-bit exponent, 2-bit mantissa)
583
+ *
584
+ * 8-bit floating point: 1 sign bit, 5 exponent bits, 2 mantissa bits
585
+ * Range: ~±57344, precision: <1 decimal digit
586
+ *
587
+ * Optimized for backward pass training with wider range than E4M3.
588
+ * Supported on NVIDIA Hopper H100 GPUs, AMD Instinct MI300.
589
+ */
590
+ export class E5M2Array extends Uint8Array {
591
+ constructor(length: number | ArrayLike<number> | ArrayBufferLike, byteOffset?: number, arrayLength?: number) {
592
+ if (typeof length === 'number') {
593
+ super(length);
594
+ } else if (ArrayBuffer.isView(length) || length instanceof ArrayBuffer) {
595
+ super(length as any, byteOffset, arrayLength);
596
+ } else {
597
+ const src = length as ArrayLike<number>;
598
+ const arr = new Uint8Array(src.length);
599
+ if (conversionFunctions) {
600
+ for (let i = 0; i < src.length; i++) {
601
+ arr[i] = conversionFunctions.castF32ToE5M2(src[i]);
602
+ }
603
+ }
604
+ super(arr);
605
+ }
606
+ }
607
+
608
+ toFloat32Array(): Float32Array {
609
+ if (!conversionFunctions) {
610
+ throw new Error('Conversion functions not initialized');
611
+ }
612
+ const result = new Float32Array(this.length);
613
+ for (let i = 0; i < this.length; i++) {
614
+ result[i] = conversionFunctions.castE5M2ToF32(this[i]);
615
+ }
616
+ return result;
617
+ }
618
+
619
+ getFloat32(index: number): number {
620
+ if (!conversionFunctions) {
621
+ throw new Error('Conversion functions not initialized');
622
+ }
623
+ return conversionFunctions.castE5M2ToF32(this[index]);
624
+ }
625
+
626
+ setFloat32(index: number, value: number): void {
627
+ if (!conversionFunctions) {
628
+ throw new Error('Conversion functions not initialized');
629
+ }
630
+ this[index] = conversionFunctions.castF32ToE5M2(value);
631
+ }
632
+
633
+ toString(): string {
634
+ if (!conversionFunctions) return `E5M2Array(${this.length})`;
635
+ const limit = Math.min(this.length, 20);
636
+ const parts: string[] = [];
637
+ for (let i = 0; i < limit; i++) {
638
+ const f = conversionFunctions.castE5M2ToF32(this[i]);
639
+ parts.push(`${f} [0x${this[i].toString(16).padStart(2, '0')}]`);
640
+ }
641
+ const suffix = this.length > 20 ? ', ...' : '';
642
+ return `E5M2Array(${this.length}) [${parts.join(', ')}${suffix}]`;
643
+ }
644
+
645
+ [Symbol.for('nodejs.util.inspect.custom')](): string {
646
+ return this.toString();
647
+ }
648
+ }
649
+
650
+ /**
651
+ * @brief Binary Array (u1) - Bit-packed binary vectors
652
+ *
653
+ * 1-bit per element, packed into bytes (8 bits per byte)
654
+ * Used for binary embeddings, hashing, and Hamming/Jaccard distances.
655
+ *
656
+ * Common in semantic search with binary quantization (Cohere, Voyage).
657
+ */
658
+ export class BinaryArray extends Uint8Array {
659
+ private _bitLength: number;
660
+
661
+ constructor(bitLength: number) {
662
+ const byteLength = Math.ceil(bitLength / 8);
663
+ super(byteLength);
664
+ this._bitLength = bitLength;
665
+ }
666
+
667
+ /**
668
+ * @brief Gets the bit value at the specified index.
669
+ * @param index Bit index (0 to bitLength-1)
670
+ * @returns 0 or 1
671
+ */
672
+ getBit(index: number): number {
673
+ if (index < 0 || index >= this._bitLength) {
674
+ throw new RangeError('Index out of bounds');
675
+ }
676
+ const byteIndex = index >>> 3; // index / 8
677
+ const bitIndex = index & 7; // index % 8
678
+ return (this[byteIndex] >>> bitIndex) & 1;
679
+ }
680
+
681
+ /**
682
+ * @brief Sets the bit value at the specified index.
683
+ * @param index Bit index (0 to bitLength-1)
684
+ * @param value 0 or 1
685
+ */
686
+ setBit(index: number, value: number): void {
687
+ if (index < 0 || index >= this._bitLength) {
688
+ throw new RangeError('Index out of bounds');
689
+ }
690
+ const byteIndex = index >>> 3;
691
+ const bitIndex = index & 7;
692
+ if (value) {
693
+ this[byteIndex] |= (1 << bitIndex);
694
+ } else {
695
+ this[byteIndex] &= ~(1 << bitIndex);
696
+ }
697
+ }
698
+
699
+ /**
700
+ * @brief Returns the logical bit length of the array.
701
+ */
702
+ get bitLength(): number {
703
+ return this._bitLength;
704
+ }
705
+
706
+ /**
707
+ * @brief Creates a BinaryArray from a Float32Array (positive values = 1, else 0).
708
+ * @param vector Source floating-point vector
709
+ * @returns Binary array with quantized values
710
+ */
711
+ static fromFloat32Array(vector: Float32Array): BinaryArray {
712
+ const binary = new BinaryArray(vector.length);
713
+ for (let i = 0; i < vector.length; i++) {
714
+ if (vector[i] > 0) {
715
+ binary.setBit(i, 1);
716
+ }
717
+ }
718
+ return binary;
719
+ }
720
+
721
+ /**
722
+ * @brief Creates a BinaryArray from a Float64Array (positive values = 1, else 0).
723
+ * @param vector Source floating-point vector
724
+ * @returns Binary array with quantized values
725
+ */
726
+ static fromFloat64Array(vector: Float64Array): BinaryArray {
727
+ const binary = new BinaryArray(vector.length);
728
+ for (let i = 0; i < vector.length; i++) {
729
+ if (vector[i] > 0) {
730
+ binary.setBit(i, 1);
731
+ }
732
+ }
733
+ return binary;
734
+ }
735
+
736
+ toString(): string {
737
+ const limit = Math.min(this.length, 20);
738
+ const parts: string[] = [];
739
+ for (let i = 0; i < limit; i++) {
740
+ parts.push(`0b${this[i].toString(2).padStart(8, '0')}`);
741
+ }
742
+ const suffix = this.length > 20 ? ', ...' : '';
743
+ return `BinaryArray(${this._bitLength}) [${parts.join(', ')}${suffix}]`;
744
+ }
745
+
746
+ [Symbol.for('nodejs.util.inspect.custom')](): string {
747
+ return this.toString();
748
+ }
749
+ }
750
+
751
+ /**
752
+ * @brief Type guard to check if an object is a Float16Array.
753
+ */
754
+ export function isFloat16Array(obj: any): obj is Float16Array {
755
+ return obj instanceof Float16Array;
756
+ }
757
+
758
+ /**
759
+ * @brief Type guard to check if an object is a BFloat16Array.
760
+ */
761
+ export function isBFloat16Array(obj: any): obj is BFloat16Array {
762
+ return obj instanceof BFloat16Array;
763
+ }
764
+
765
+ /**
766
+ * @brief Type guard to check if an object is an E4M3Array.
767
+ */
768
+ export function isE4M3Array(obj: any): obj is E4M3Array {
769
+ return obj instanceof E4M3Array;
770
+ }
771
+
772
+ /**
773
+ * @brief Type guard to check if an object is an E5M2Array.
774
+ */
775
+ export function isE5M2Array(obj: any): obj is E5M2Array {
776
+ return obj instanceof E5M2Array;
777
+ }
778
+
779
+ /**
780
+ * @brief Type guard to check if an object is a BinaryArray.
781
+ */
782
+ export function isBinaryArray(obj: any): obj is BinaryArray {
783
+ return obj instanceof BinaryArray;
784
+ }