numkong 7.0.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 (294) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +495 -0
  3. package/binding.gyp +540 -0
  4. package/c/dispatch.h +512 -0
  5. package/c/dispatch_bf16.c +389 -0
  6. package/c/dispatch_bf16c.c +52 -0
  7. package/c/dispatch_e2m3.c +263 -0
  8. package/c/dispatch_e3m2.c +243 -0
  9. package/c/dispatch_e4m3.c +276 -0
  10. package/c/dispatch_e5m2.c +272 -0
  11. package/c/dispatch_f16.c +376 -0
  12. package/c/dispatch_f16c.c +58 -0
  13. package/c/dispatch_f32.c +378 -0
  14. package/c/dispatch_f32c.c +99 -0
  15. package/c/dispatch_f64.c +296 -0
  16. package/c/dispatch_f64c.c +98 -0
  17. package/c/dispatch_i16.c +96 -0
  18. package/c/dispatch_i32.c +89 -0
  19. package/c/dispatch_i4.c +150 -0
  20. package/c/dispatch_i64.c +86 -0
  21. package/c/dispatch_i8.c +289 -0
  22. package/c/dispatch_other.c +330 -0
  23. package/c/dispatch_u1.c +148 -0
  24. package/c/dispatch_u16.c +124 -0
  25. package/c/dispatch_u32.c +118 -0
  26. package/c/dispatch_u4.c +150 -0
  27. package/c/dispatch_u64.c +102 -0
  28. package/c/dispatch_u8.c +303 -0
  29. package/c/numkong.c +950 -0
  30. package/include/README.md +573 -0
  31. package/include/module.modulemap +129 -0
  32. package/include/numkong/attention/sapphireamx.h +1361 -0
  33. package/include/numkong/attention/sme.h +2066 -0
  34. package/include/numkong/attention.h +49 -0
  35. package/include/numkong/capabilities.h +748 -0
  36. package/include/numkong/cast/README.md +262 -0
  37. package/include/numkong/cast/haswell.h +975 -0
  38. package/include/numkong/cast/icelake.h +470 -0
  39. package/include/numkong/cast/neon.h +1192 -0
  40. package/include/numkong/cast/rvv.h +1021 -0
  41. package/include/numkong/cast/sapphire.h +262 -0
  42. package/include/numkong/cast/serial.h +2262 -0
  43. package/include/numkong/cast/skylake.h +856 -0
  44. package/include/numkong/cast/v128relaxed.h +180 -0
  45. package/include/numkong/cast.h +230 -0
  46. package/include/numkong/curved/README.md +223 -0
  47. package/include/numkong/curved/genoa.h +182 -0
  48. package/include/numkong/curved/haswell.h +276 -0
  49. package/include/numkong/curved/neon.h +205 -0
  50. package/include/numkong/curved/neonbfdot.h +212 -0
  51. package/include/numkong/curved/neonhalf.h +212 -0
  52. package/include/numkong/curved/rvv.h +305 -0
  53. package/include/numkong/curved/serial.h +207 -0
  54. package/include/numkong/curved/skylake.h +457 -0
  55. package/include/numkong/curved/smef64.h +506 -0
  56. package/include/numkong/curved.h +517 -0
  57. package/include/numkong/curved.hpp +144 -0
  58. package/include/numkong/dot/README.md +425 -0
  59. package/include/numkong/dot/alder.h +563 -0
  60. package/include/numkong/dot/genoa.h +315 -0
  61. package/include/numkong/dot/haswell.h +1688 -0
  62. package/include/numkong/dot/icelake.h +883 -0
  63. package/include/numkong/dot/neon.h +818 -0
  64. package/include/numkong/dot/neonbfdot.h +244 -0
  65. package/include/numkong/dot/neonfhm.h +360 -0
  66. package/include/numkong/dot/neonhalf.h +198 -0
  67. package/include/numkong/dot/neonsdot.h +508 -0
  68. package/include/numkong/dot/rvv.h +714 -0
  69. package/include/numkong/dot/rvvbb.h +72 -0
  70. package/include/numkong/dot/rvvbf16.h +123 -0
  71. package/include/numkong/dot/rvvhalf.h +129 -0
  72. package/include/numkong/dot/sapphire.h +141 -0
  73. package/include/numkong/dot/serial.h +838 -0
  74. package/include/numkong/dot/sierra.h +405 -0
  75. package/include/numkong/dot/skylake.h +1084 -0
  76. package/include/numkong/dot/sve.h +379 -0
  77. package/include/numkong/dot/svebfdot.h +74 -0
  78. package/include/numkong/dot/svehalf.h +123 -0
  79. package/include/numkong/dot/v128relaxed.h +1258 -0
  80. package/include/numkong/dot.h +1070 -0
  81. package/include/numkong/dot.hpp +94 -0
  82. package/include/numkong/dots/README.md +496 -0
  83. package/include/numkong/dots/alder.h +114 -0
  84. package/include/numkong/dots/genoa.h +94 -0
  85. package/include/numkong/dots/haswell.h +295 -0
  86. package/include/numkong/dots/icelake.h +171 -0
  87. package/include/numkong/dots/neon.h +120 -0
  88. package/include/numkong/dots/neonbfdot.h +58 -0
  89. package/include/numkong/dots/neonfhm.h +94 -0
  90. package/include/numkong/dots/neonhalf.h +57 -0
  91. package/include/numkong/dots/neonsdot.h +108 -0
  92. package/include/numkong/dots/rvv.h +2486 -0
  93. package/include/numkong/dots/sapphireamx.h +3973 -0
  94. package/include/numkong/dots/serial.h +2844 -0
  95. package/include/numkong/dots/sierra.h +97 -0
  96. package/include/numkong/dots/skylake.h +196 -0
  97. package/include/numkong/dots/sme.h +5372 -0
  98. package/include/numkong/dots/smebi32.h +461 -0
  99. package/include/numkong/dots/smef64.h +1318 -0
  100. package/include/numkong/dots/smehalf.h +47 -0
  101. package/include/numkong/dots/v128relaxed.h +294 -0
  102. package/include/numkong/dots.h +2804 -0
  103. package/include/numkong/dots.hpp +639 -0
  104. package/include/numkong/each/README.md +469 -0
  105. package/include/numkong/each/haswell.h +1658 -0
  106. package/include/numkong/each/icelake.h +272 -0
  107. package/include/numkong/each/neon.h +1104 -0
  108. package/include/numkong/each/neonbfdot.h +212 -0
  109. package/include/numkong/each/neonhalf.h +410 -0
  110. package/include/numkong/each/rvv.h +1121 -0
  111. package/include/numkong/each/sapphire.h +477 -0
  112. package/include/numkong/each/serial.h +260 -0
  113. package/include/numkong/each/skylake.h +1562 -0
  114. package/include/numkong/each.h +2146 -0
  115. package/include/numkong/each.hpp +434 -0
  116. package/include/numkong/geospatial/README.md +147 -0
  117. package/include/numkong/geospatial/haswell.h +593 -0
  118. package/include/numkong/geospatial/neon.h +571 -0
  119. package/include/numkong/geospatial/rvv.h +701 -0
  120. package/include/numkong/geospatial/serial.h +309 -0
  121. package/include/numkong/geospatial/skylake.h +577 -0
  122. package/include/numkong/geospatial/v128relaxed.h +613 -0
  123. package/include/numkong/geospatial.h +453 -0
  124. package/include/numkong/geospatial.hpp +235 -0
  125. package/include/numkong/matrix.hpp +336 -0
  126. package/include/numkong/maxsim/README.md +187 -0
  127. package/include/numkong/maxsim/alder.h +511 -0
  128. package/include/numkong/maxsim/genoa.h +115 -0
  129. package/include/numkong/maxsim/haswell.h +553 -0
  130. package/include/numkong/maxsim/icelake.h +480 -0
  131. package/include/numkong/maxsim/neonsdot.h +394 -0
  132. package/include/numkong/maxsim/sapphireamx.h +877 -0
  133. package/include/numkong/maxsim/serial.h +490 -0
  134. package/include/numkong/maxsim/sme.h +929 -0
  135. package/include/numkong/maxsim/v128relaxed.h +280 -0
  136. package/include/numkong/maxsim.h +571 -0
  137. package/include/numkong/maxsim.hpp +133 -0
  138. package/include/numkong/mesh/README.md +227 -0
  139. package/include/numkong/mesh/haswell.h +2235 -0
  140. package/include/numkong/mesh/neon.h +1329 -0
  141. package/include/numkong/mesh/neonbfdot.h +842 -0
  142. package/include/numkong/mesh/neonhalf.h +616 -0
  143. package/include/numkong/mesh/rvv.h +916 -0
  144. package/include/numkong/mesh/serial.h +742 -0
  145. package/include/numkong/mesh/skylake.h +1135 -0
  146. package/include/numkong/mesh/v128relaxed.h +1052 -0
  147. package/include/numkong/mesh.h +652 -0
  148. package/include/numkong/mesh.hpp +762 -0
  149. package/include/numkong/numkong.h +78 -0
  150. package/include/numkong/numkong.hpp +57 -0
  151. package/include/numkong/probability/README.md +173 -0
  152. package/include/numkong/probability/haswell.h +267 -0
  153. package/include/numkong/probability/neon.h +225 -0
  154. package/include/numkong/probability/rvv.h +409 -0
  155. package/include/numkong/probability/serial.h +169 -0
  156. package/include/numkong/probability/skylake.h +324 -0
  157. package/include/numkong/probability.h +383 -0
  158. package/include/numkong/probability.hpp +120 -0
  159. package/include/numkong/random.h +50 -0
  160. package/include/numkong/random.hpp +285 -0
  161. package/include/numkong/reduce/README.md +547 -0
  162. package/include/numkong/reduce/alder.h +632 -0
  163. package/include/numkong/reduce/genoa.h +201 -0
  164. package/include/numkong/reduce/haswell.h +3783 -0
  165. package/include/numkong/reduce/icelake.h +549 -0
  166. package/include/numkong/reduce/neon.h +3841 -0
  167. package/include/numkong/reduce/neonbfdot.h +353 -0
  168. package/include/numkong/reduce/neonfhm.h +665 -0
  169. package/include/numkong/reduce/neonhalf.h +157 -0
  170. package/include/numkong/reduce/neonsdot.h +357 -0
  171. package/include/numkong/reduce/rvv.h +3407 -0
  172. package/include/numkong/reduce/serial.h +757 -0
  173. package/include/numkong/reduce/sierra.h +338 -0
  174. package/include/numkong/reduce/skylake.h +3792 -0
  175. package/include/numkong/reduce/v128relaxed.h +2302 -0
  176. package/include/numkong/reduce.h +1597 -0
  177. package/include/numkong/reduce.hpp +633 -0
  178. package/include/numkong/scalar/README.md +89 -0
  179. package/include/numkong/scalar/haswell.h +113 -0
  180. package/include/numkong/scalar/neon.h +122 -0
  181. package/include/numkong/scalar/neonhalf.h +70 -0
  182. package/include/numkong/scalar/rvv.h +211 -0
  183. package/include/numkong/scalar/sapphire.h +63 -0
  184. package/include/numkong/scalar/serial.h +332 -0
  185. package/include/numkong/scalar/v128relaxed.h +56 -0
  186. package/include/numkong/scalar.h +683 -0
  187. package/include/numkong/set/README.md +179 -0
  188. package/include/numkong/set/haswell.h +334 -0
  189. package/include/numkong/set/icelake.h +485 -0
  190. package/include/numkong/set/neon.h +364 -0
  191. package/include/numkong/set/rvv.h +226 -0
  192. package/include/numkong/set/rvvbb.h +117 -0
  193. package/include/numkong/set/serial.h +174 -0
  194. package/include/numkong/set/sve.h +185 -0
  195. package/include/numkong/set/v128relaxed.h +240 -0
  196. package/include/numkong/set.h +457 -0
  197. package/include/numkong/set.hpp +114 -0
  198. package/include/numkong/sets/README.md +149 -0
  199. package/include/numkong/sets/haswell.h +63 -0
  200. package/include/numkong/sets/icelake.h +66 -0
  201. package/include/numkong/sets/neon.h +61 -0
  202. package/include/numkong/sets/serial.h +43 -0
  203. package/include/numkong/sets/smebi32.h +1099 -0
  204. package/include/numkong/sets/v128relaxed.h +58 -0
  205. package/include/numkong/sets.h +339 -0
  206. package/include/numkong/sparse/README.md +156 -0
  207. package/include/numkong/sparse/icelake.h +463 -0
  208. package/include/numkong/sparse/neon.h +288 -0
  209. package/include/numkong/sparse/serial.h +117 -0
  210. package/include/numkong/sparse/sve2.h +507 -0
  211. package/include/numkong/sparse/turin.h +322 -0
  212. package/include/numkong/sparse.h +363 -0
  213. package/include/numkong/sparse.hpp +113 -0
  214. package/include/numkong/spatial/README.md +435 -0
  215. package/include/numkong/spatial/alder.h +607 -0
  216. package/include/numkong/spatial/genoa.h +290 -0
  217. package/include/numkong/spatial/haswell.h +960 -0
  218. package/include/numkong/spatial/icelake.h +586 -0
  219. package/include/numkong/spatial/neon.h +773 -0
  220. package/include/numkong/spatial/neonbfdot.h +165 -0
  221. package/include/numkong/spatial/neonhalf.h +118 -0
  222. package/include/numkong/spatial/neonsdot.h +261 -0
  223. package/include/numkong/spatial/rvv.h +984 -0
  224. package/include/numkong/spatial/rvvbf16.h +123 -0
  225. package/include/numkong/spatial/rvvhalf.h +117 -0
  226. package/include/numkong/spatial/sapphire.h +343 -0
  227. package/include/numkong/spatial/serial.h +346 -0
  228. package/include/numkong/spatial/sierra.h +323 -0
  229. package/include/numkong/spatial/skylake.h +606 -0
  230. package/include/numkong/spatial/sve.h +224 -0
  231. package/include/numkong/spatial/svebfdot.h +122 -0
  232. package/include/numkong/spatial/svehalf.h +109 -0
  233. package/include/numkong/spatial/v128relaxed.h +717 -0
  234. package/include/numkong/spatial.h +1425 -0
  235. package/include/numkong/spatial.hpp +183 -0
  236. package/include/numkong/spatials/README.md +580 -0
  237. package/include/numkong/spatials/alder.h +94 -0
  238. package/include/numkong/spatials/genoa.h +94 -0
  239. package/include/numkong/spatials/haswell.h +219 -0
  240. package/include/numkong/spatials/icelake.h +113 -0
  241. package/include/numkong/spatials/neon.h +109 -0
  242. package/include/numkong/spatials/neonbfdot.h +60 -0
  243. package/include/numkong/spatials/neonfhm.h +92 -0
  244. package/include/numkong/spatials/neonhalf.h +58 -0
  245. package/include/numkong/spatials/neonsdot.h +109 -0
  246. package/include/numkong/spatials/rvv.h +1960 -0
  247. package/include/numkong/spatials/sapphireamx.h +1149 -0
  248. package/include/numkong/spatials/serial.h +226 -0
  249. package/include/numkong/spatials/sierra.h +96 -0
  250. package/include/numkong/spatials/skylake.h +184 -0
  251. package/include/numkong/spatials/sme.h +1901 -0
  252. package/include/numkong/spatials/smef64.h +465 -0
  253. package/include/numkong/spatials/v128relaxed.h +240 -0
  254. package/include/numkong/spatials.h +3021 -0
  255. package/include/numkong/spatials.hpp +508 -0
  256. package/include/numkong/tensor.hpp +1592 -0
  257. package/include/numkong/trigonometry/README.md +184 -0
  258. package/include/numkong/trigonometry/haswell.h +652 -0
  259. package/include/numkong/trigonometry/neon.h +639 -0
  260. package/include/numkong/trigonometry/rvv.h +699 -0
  261. package/include/numkong/trigonometry/serial.h +703 -0
  262. package/include/numkong/trigonometry/skylake.h +721 -0
  263. package/include/numkong/trigonometry/v128relaxed.h +666 -0
  264. package/include/numkong/trigonometry.h +467 -0
  265. package/include/numkong/trigonometry.hpp +166 -0
  266. package/include/numkong/types.h +1384 -0
  267. package/include/numkong/types.hpp +5603 -0
  268. package/include/numkong/vector.hpp +698 -0
  269. package/javascript/README.md +246 -0
  270. package/javascript/dist/cjs/numkong-wasm.d.ts +166 -0
  271. package/javascript/dist/cjs/numkong-wasm.js +617 -0
  272. package/javascript/dist/cjs/numkong.d.ts +343 -0
  273. package/javascript/dist/cjs/numkong.js +523 -0
  274. package/javascript/dist/cjs/package.json +3 -0
  275. package/javascript/dist/cjs/types.d.ts +284 -0
  276. package/javascript/dist/cjs/types.js +653 -0
  277. package/javascript/dist/esm/numkong-wasm.d.ts +166 -0
  278. package/javascript/dist/esm/numkong-wasm.js +595 -0
  279. package/javascript/dist/esm/numkong.d.ts +343 -0
  280. package/javascript/dist/esm/numkong.js +452 -0
  281. package/javascript/dist/esm/package.json +3 -0
  282. package/javascript/dist/esm/types.d.ts +284 -0
  283. package/javascript/dist/esm/types.js +630 -0
  284. package/javascript/dist-package-cjs.json +3 -0
  285. package/javascript/dist-package-esm.json +3 -0
  286. package/javascript/node-gyp-build.d.ts +1 -0
  287. package/javascript/numkong-wasm.ts +756 -0
  288. package/javascript/numkong.c +689 -0
  289. package/javascript/numkong.ts +575 -0
  290. package/javascript/tsconfig-base.json +39 -0
  291. package/javascript/tsconfig-cjs.json +8 -0
  292. package/javascript/tsconfig-esm.json +8 -0
  293. package/javascript/types.ts +674 -0
  294. package/package.json +87 -0
@@ -0,0 +1,246 @@
1
+ # NumKong for JavaScript
2
+
3
+ NumKong's JavaScript package brings low-latency vector kernels to Node and Bun-style runtimes, targeting the space between handwritten loops over `TypedArray`s and much heavier tensor frameworks.
4
+ It keeps the JS surface intentionally compact: dense distances, dot products, binary metrics, probability divergences, packed GEMM-like matrix multiplication, symmetric Gram matrices, dtype-tagged low-precision storage, typed views, and runtime capability inspection.
5
+
6
+ ## Quickstart
7
+
8
+ ```ts
9
+ import { dot } from "numkong";
10
+
11
+ const a = new Float32Array([1, 2, 3]);
12
+ const b = new Float32Array([4, 5, 6]);
13
+ console.log(dot(a, b)); // 32
14
+ ```
15
+
16
+ ## Highlights
17
+
18
+ This SDK is deliberately smaller than Python or Rust.
19
+ Its job is to make the hot vector kernels easy to use from modern JavaScript runtimes.
20
+
21
+ __TypedArray-first API.__
22
+ Standard `Float32Array`, `Float64Array`, `Int8Array`, and `Uint8Array` work directly.
23
+ __DType tags for exotic storage.__
24
+ `f16`, `bf16`, fp8, and packed bits stay explicit.
25
+ __Owned and borrowed views.__
26
+ `Vector`, `VectorView`, and base tensor wrappers preserve dtype metadata.
27
+ __Portable runtime story.__
28
+ The same package can target native addons and WASM runtimes.
29
+ __No fake tensor-framework scope.__
30
+ This binding stays centered on the vector families it actually exports.
31
+
32
+ ## Ecosystem Comparison
33
+
34
+ | Feature | NumKong | [mathjs](https://github.com/josdejong/mathjs) | [tensorflow.js](https://github.com/tensorflow/tfjs) |
35
+ | ---------------------------- | -------------------------------------------------------------- | --------------------------------------------- | --------------------------------------------------- |
36
+ | Operation families | dots, distances, binary, probability, cast, packed, symmetric | general arithmetic, matrix ops, statistics | matmul, elementwise, reductions |
37
+ | Precision | BFloat16 through sub-byte; automatic widening; Kahan summation | Float64 only; standard accuracy | Float32 primarily; no sub-byte; standard accuracy |
38
+ | Runtime SIMD dispatch | auto-selects best ISA per-thread across x86, ARM, RISC-V | none; pure JS | fixed at build time via WASM SIMD or WebGL |
39
+ | Packed matrix, GEMM-like | `dotsPack` + `dotsPacked`; persistent packing; amortized | `math.multiply` — no persistent packing | `tf.matMul` — no persistent packing |
40
+ | Symmetric kernels, SYRK-like | `dotsSymmetric`; upper triangle only; row-range partitioning | no duplicate-pair skipping | no duplicate-pair skipping |
41
+ | WASM fallback | yes — portable, runs in browser without native addon | yes — pure JS, no native required | yes — also WebGL/WebGPU |
42
+ | Bundle size | small | moderate | large |
43
+
44
+ ## Installation
45
+
46
+ The package targets Node `>= 22`.
47
+
48
+ ```sh
49
+ npm install numkong
50
+ yarn add numkong
51
+ pnpm add numkong
52
+ bun add numkong
53
+ ```
54
+
55
+ If you build from source, the package uses `node-gyp-build` on install and TypeScript sources under `javascript/`.
56
+
57
+ ## Browser and WASM
58
+
59
+ NumKong ships pre-built WASM binaries attached to each [GitHub Release](https://github.com/ashvardanian/NumKong/releases).
60
+ Download `numkong.js` and `numkong.wasm` and serve them from the same directory.
61
+
62
+ ```html
63
+ <script type="module">
64
+ import * as numkong from "./numkong-wasm.js";
65
+ import NumKongModule from "./numkong.js";
66
+
67
+ const wasm = await NumKongModule();
68
+ numkong.initWasm(wasm);
69
+
70
+ // Same API as the native addon
71
+ const a = new Float32Array([1, 2, 3]);
72
+ const b = new Float32Array([4, 5, 6]);
73
+ console.log(numkong.dot(a, b));
74
+ </script>
75
+ ```
76
+
77
+ For Node.js WASM usage without the native addon:
78
+
79
+ ```js
80
+ import * as numkong from "numkong/wasm";
81
+ import NumKongModule from "./numkong.js";
82
+
83
+ const wasm = await NumKongModule();
84
+ numkong.initWasm(wasm);
85
+ ```
86
+
87
+ ## Dot Products
88
+
89
+ Dot products are separate from distances because dtype tagging and low-precision storage matter more here.
90
+
91
+ ```ts
92
+ import { dot, inner } from "numkong";
93
+
94
+ const a = new Float32Array([1, 2, 3]);
95
+ const b = new Float32Array([4, 5, 6]);
96
+
97
+ console.log(dot(a, b));
98
+ console.log(inner(a, b)); // alias for ecosystem familiarity
99
+ ```
100
+
101
+ For non-native numeric layouts, pass an explicit `DType` or wrap the storage in a typed NumKong view.
102
+
103
+ ## Dense Distances
104
+
105
+ Dense distance entrypoints work directly on the standard numeric `TypedArray` types.
106
+
107
+ ```ts
108
+ import { sqeuclidean, euclidean, angular } from "numkong";
109
+
110
+ const a = new Float32Array([1, 2, 3]);
111
+ const b = new Float32Array([4, 5, 6]);
112
+
113
+ console.log(sqeuclidean(a, b)); // equivalent shape to a manual sum((a[i] - b[i]) ** 2)
114
+ console.log(euclidean(a, b));
115
+ console.log(angular(a, b));
116
+ ```
117
+
118
+ When the storage type is one of the standard JS typed arrays, dtype inference is automatic.
119
+
120
+ ## Binary Metrics
121
+
122
+ Binary metrics operate on packed storage rather than on generic boolean arrays.
123
+
124
+ ```ts
125
+ import { toBinary, hamming, jaccard } from "numkong";
126
+
127
+ const a = toBinary(new Float32Array([1, -2, 3, -4, 5, -6, 7, -8]));
128
+ const b = toBinary(new Float32Array([1, 2, -3, -4, 5, 6, -7, -8]));
129
+
130
+ console.log(hamming(a, b));
131
+ console.log(jaccard(a, b));
132
+ ```
133
+
134
+ That is a better model for sign-quantized embeddings and semantic hashes than looping over JS booleans.
135
+
136
+ ## Probability Metrics
137
+
138
+ ```ts
139
+ import { kullbackleibler, jensenshannon } from "numkong";
140
+
141
+ const p = new Float32Array([0.2, 0.3, 0.5]);
142
+ const q = new Float32Array([0.1, 0.3, 0.6]);
143
+
144
+ console.log(kullbackleibler(p, q));
145
+ console.log(jensenshannon(p, q));
146
+ ```
147
+
148
+ These are direct divergence kernels, not scalar JS loops hidden behind a helper.
149
+
150
+ ## DType Tags and Low-Precision Arrays
151
+
152
+ JavaScript has no built-in `f16`, `bf16`, fp8, or packed-bit numeric model.
153
+ NumKong handles that with explicit dtype tags and wrapper arrays.
154
+
155
+ The supported low-precision types and their bit layouts are:
156
+
157
+ - `Float16Array`: 1 sign + 5 exponent + 10 mantissa bits, 2 bytes per element, range ±65504, supports Inf and NaN
158
+ - `BFloat16Array`: 1 sign + 8 exponent + 7 mantissa bits, 2 bytes per element, full `f32` dynamic range, supports Inf and NaN
159
+ - `E4M3Array`: 1 sign + 4 exponent + 3 mantissa bits, 1 byte per element, range ±448, no Inf, NaN is 0x7F only
160
+ - `E5M2Array`: 1 sign + 5 exponent + 2 mantissa bits, 1 byte per element, range ±57344, supports Inf and NaN
161
+ - `BinaryArray`: packed bits in bytes, 8 elements per byte
162
+
163
+ Use `.byteLength` for the exact payload size.
164
+
165
+ ```ts
166
+ import { Float16Array, E4M3Array, DType, dot, angular } from "numkong";
167
+
168
+ const a16 = new Float16Array([1, 2, 3]);
169
+ const b16 = new Float16Array([4, 5, 6]);
170
+ console.log(dot(a16, b16, DType.F16));
171
+ console.log(a16.byteLength);
172
+
173
+ const a8 = new E4M3Array([1, 2, 3]);
174
+ const b8 = new E4M3Array([4, 5, 6]);
175
+ console.log(angular(a8, b8, DType.E4M3));
176
+ console.log(a8.byteLength);
177
+ ```
178
+
179
+ If the underlying storage is a raw `Uint16Array` or `Uint8Array`, JS itself cannot know whether those bytes mean integers, `f16`, `bf16`, mini-floats, or packed bits.
180
+ That is exactly when the `DType` tag becomes mandatory.
181
+ You can also pass it to `cast` to convert many values between all supported types:
182
+
183
+ ```ts
184
+ numkong.cast(f32Source, "f32", bf16Dest, "bf16");
185
+ numkong.cast(f32Source, "f32", bf16Dest, DType.E4M3);
186
+ ```
187
+
188
+ ## Vector Views and Owned Buffers
189
+
190
+ The wrapper hierarchy exists to keep dtype and ownership explicit across addon and WASM boundaries.
191
+
192
+ - `TensorBase` carries `buffer`, `byteOffset`, and `dtype`
193
+ - `VectorBase` adds rank-1 semantics
194
+ - `VectorView` is a zero-copy borrowed wrapper over existing memory
195
+ - `Vector` owns its `ArrayBuffer`
196
+
197
+ ```ts
198
+ import { VectorView, Vector, DType, dot } from "numkong";
199
+
200
+ const raw = new Uint16Array([0x3c00, 0x4000, 0x4200]); // f16 payload, not uint16 values
201
+ const view = VectorView.from(raw, DType.F16);
202
+
203
+ const owned = new Vector(3, DType.F32);
204
+ owned.toTypedArray().set(new Float32Array([4, 5, 6]));
205
+
206
+ console.log(dot(view, view));
207
+ console.log(dot(owned, owned));
208
+ console.log(owned.byteLength);
209
+ ```
210
+
211
+ Use `VectorView` when the bytes already live somewhere else.
212
+ Use `Vector` when NumKong should own the storage.
213
+
214
+ ## Capabilities and Runtime Selection
215
+
216
+ Capability detection is explicit:
217
+
218
+ ```ts
219
+ import { Capability, getCapabilities, hasCapability } from "numkong";
220
+
221
+ const caps = getCapabilities();
222
+
223
+ console.log(caps);
224
+ console.log(hasCapability(Capability.HASWELL));
225
+ console.log(hasCapability(Capability.NEON));
226
+ ```
227
+
228
+ The exact bitmask depends on whether you are running the native addon or a WASM runtime.
229
+
230
+ There is no `configure_thread` call in the JS binding.
231
+ Thread configuration is managed internally by the native addon or WASM runtime.
232
+
233
+ ## Native Addon and WASM Runtimes
234
+
235
+ The top-level package is native-first.
236
+ It loads the compiled addon through `node-gyp-build`.
237
+
238
+ The repository also ships WASM wrappers.
239
+ Those are useful for portable or sandboxed environments.
240
+ They should not be described as feature-complete replacements for the native-heavy SDKs.
241
+
242
+ Practical guidance:
243
+
244
+ - Use the native addon for the lowest host-call latency.
245
+ - Use the WASM path when portability matters more than absolute latency.
246
+ - Keep your expectations scoped to the vector and matrix-oriented API that this binding actually exports.
@@ -0,0 +1,166 @@
1
+ /**
2
+ * @brief WASM wrapper for NumKong providing N-API compatible interface
3
+ * @file javascript/numkong-wasm.ts
4
+ * @date February 6, 2026
5
+ *
6
+ * This module wraps the Emscripten-compiled WASM module to provide the same
7
+ * TypeScript API as the native N-API bindings. It handles:
8
+ * - Zero-copy TensorBase interop for cross-module WASM sharing
9
+ * - TypedArray type detection and dispatch
10
+ * - Result extraction from WASM heap
11
+ * - Error handling
12
+ * - Both wasm32 and wasm64 (memory64) modes
13
+ */
14
+ import { TensorBase, Matrix, PackedMatrix, DType } from './types.js';
15
+ /**
16
+ * Emscripten module interface.
17
+ * In wasm64 (memory64) mode, pointer/size params and returns become bigint.
18
+ * We use `any` for pointer arguments to support both modes uniformly.
19
+ */
20
+ interface EmscriptenModule {
21
+ _malloc(size: any): any;
22
+ _free(ptr: any): void;
23
+ wasmMemory: {
24
+ buffer: ArrayBuffer;
25
+ };
26
+ _nk_dot_f32(a: any, b: any, n: any, result: any): void;
27
+ _nk_angular_f32(a: any, b: any, n: any, result: any): void;
28
+ _nk_sqeuclidean_f32(a: any, b: any, n: any, result: any): void;
29
+ _nk_euclidean_f32(a: any, b: any, n: any, result: any): void;
30
+ _nk_dot_f64(a: any, b: any, n: any, result: any): void;
31
+ _nk_angular_f64(a: any, b: any, n: any, result: any): void;
32
+ _nk_sqeuclidean_f64(a: any, b: any, n: any, result: any): void;
33
+ _nk_euclidean_f64(a: any, b: any, n: any, result: any): void;
34
+ _nk_dot_f16(a: any, b: any, n: any, result: any): void;
35
+ _nk_angular_f16(a: any, b: any, n: any, result: any): void;
36
+ _nk_sqeuclidean_f16(a: any, b: any, n: any, result: any): void;
37
+ _nk_euclidean_f16(a: any, b: any, n: any, result: any): void;
38
+ _nk_dot_bf16(a: any, b: any, n: any, result: any): void;
39
+ _nk_angular_bf16(a: any, b: any, n: any, result: any): void;
40
+ _nk_sqeuclidean_bf16(a: any, b: any, n: any, result: any): void;
41
+ _nk_euclidean_bf16(a: any, b: any, n: any, result: any): void;
42
+ _nk_dot_i8(a: any, b: any, n: any, result: any): void;
43
+ _nk_angular_i8(a: any, b: any, n: any, result: any): void;
44
+ _nk_sqeuclidean_i8(a: any, b: any, n: any, result: any): void;
45
+ _nk_euclidean_i8(a: any, b: any, n: any, result: any): void;
46
+ _nk_dot_u8(a: any, b: any, n: any, result: any): void;
47
+ _nk_angular_u8(a: any, b: any, n: any, result: any): void;
48
+ _nk_sqeuclidean_u8(a: any, b: any, n: any, result: any): void;
49
+ _nk_euclidean_u8(a: any, b: any, n: any, result: any): void;
50
+ _nk_hamming_u1(a: any, b: any, n: any, result: any): void;
51
+ _nk_hamming_u8(a: any, b: any, n: any, result: any): void;
52
+ _nk_jaccard_u1(a: any, b: any, n: any, result: any): void;
53
+ _nk_jaccard_u16(a: any, b: any, n: any, result: any): void;
54
+ _nk_kld_f32(a: any, b: any, n: any, result: any): void;
55
+ _nk_kld_f64(a: any, b: any, n: any, result: any): void;
56
+ _nk_jsd_f32(a: any, b: any, n: any, result: any): void;
57
+ _nk_jsd_f64(a: any, b: any, n: any, result: any): void;
58
+ _nk_capabilities(): any;
59
+ [key: string]: any;
60
+ }
61
+ /**
62
+ * Initializes the WASM backend with an Emscripten module instance.
63
+ * @param wasmModule - The Emscripten-compiled WASM module to use.
64
+ */
65
+ export declare function initWasm(wasmModule: EmscriptenModule): void;
66
+ /**
67
+ * Computes the squared Euclidean distance between two vectors.
68
+ * @param a - First vector (TypedArray or TensorBase).
69
+ * @param b - Second vector (must match type and length of a).
70
+ * @returns The squared Euclidean distance between a and b.
71
+ */
72
+ export declare function sqeuclidean(a: TensorBase | any, b: TensorBase | any): number;
73
+ /**
74
+ * Computes the Euclidean distance between two vectors.
75
+ * @param a - First vector (TypedArray or TensorBase).
76
+ * @param b - Second vector (must match type and length of a).
77
+ * @returns The Euclidean distance between a and b.
78
+ */
79
+ export declare function euclidean(a: TensorBase | any, b: TensorBase | any): number;
80
+ /**
81
+ * Computes the angular distance between two vectors.
82
+ * @param a - First vector (TypedArray or TensorBase).
83
+ * @param b - Second vector (must match type and length of a).
84
+ * @returns The angular distance between a and b.
85
+ */
86
+ export declare function angular(a: TensorBase | any, b: TensorBase | any): number;
87
+ /**
88
+ * Computes the dot product of two vectors.
89
+ * @param a - First vector (TypedArray or TensorBase).
90
+ * @param b - Second vector (must match type and length of a).
91
+ * @returns The dot product of a and b.
92
+ */
93
+ export declare function dot(a: TensorBase | any, b: TensorBase | any): number;
94
+ /** Alias for {@link dot}. */
95
+ export declare const inner: typeof dot;
96
+ /**
97
+ * Computes the bitwise Hamming distance between two vectors.
98
+ *
99
+ * Following N-API behavior, always treats input as u1 (binary/bit-packed),
100
+ * even if passed as Uint8Array. Each byte represents 8 bits.
101
+ *
102
+ * @param a - First bit-packed vector (Uint8Array or TensorBase).
103
+ * @param b - Second bit-packed vector (must match length of a).
104
+ * @returns The Hamming distance (number of differing bits) between a and b.
105
+ */
106
+ export declare function hamming(a: TensorBase | Uint8Array | any, b: TensorBase | Uint8Array | any): number;
107
+ /**
108
+ * Computes the bitwise Jaccard distance between two vectors.
109
+ *
110
+ * Following N-API behavior, always treats input as u1 (binary/bit-packed),
111
+ * even if passed as Uint8Array. Each byte represents 8 bits.
112
+ *
113
+ * @param a - First bit-packed vector (Uint8Array or TensorBase).
114
+ * @param b - Second bit-packed vector (must match length of a).
115
+ * @returns The Jaccard distance (1 - Jaccard similarity) between a and b.
116
+ */
117
+ export declare function jaccard(a: TensorBase | Uint8Array | any, b: TensorBase | Uint8Array | any): number;
118
+ /**
119
+ * Computes the Kullback-Leibler divergence between two probability distributions.
120
+ * @param a - First probability distribution (Float32Array, Float64Array, or TensorBase).
121
+ * @param b - Second probability distribution (must match type and length of a).
122
+ * @returns The KL divergence KL(a || b).
123
+ */
124
+ export declare function kullbackleibler(a: TensorBase | Float64Array | Float32Array, b: TensorBase | Float64Array | Float32Array): number;
125
+ /**
126
+ * Computes the Jensen-Shannon distance between two probability distributions.
127
+ * @param a - First probability distribution (Float32Array, Float64Array, or TensorBase).
128
+ * @param b - Second probability distribution (must match type and length of a).
129
+ * @returns The Jensen-Shannon distance between a and b.
130
+ */
131
+ export declare function jensenshannon(a: TensorBase | Float64Array | Float32Array, b: TensorBase | Float64Array | Float32Array): number;
132
+ /**
133
+ * Returns the runtime-detected SIMD capabilities as a bitmask.
134
+ * @returns Bitmask of capability flags (use with Capability constants).
135
+ */
136
+ export declare function getCapabilities(): bigint;
137
+ /**
138
+ * Checks if a specific SIMD capability is available at runtime.
139
+ * @param cap - Capability flag to check (from Capability constants).
140
+ * @returns True if the capability is available.
141
+ */
142
+ export declare function hasCapability(cap: bigint): boolean;
143
+ /**
144
+ * Query the packed buffer byte count for a given matrix shape and dtype.
145
+ */
146
+ export declare function dotsPackedSize(width: number, depth: number, dtype: DType): number;
147
+ /**
148
+ * Pack a Matrix for use with packed GEMM-like operations.
149
+ */
150
+ export declare function dotsPack(matrix: Matrix): PackedMatrix;
151
+ export declare function dotsPacked(a: Matrix, packed: PackedMatrix, out?: Matrix): Matrix;
152
+ export declare function angularsPacked(a: Matrix, packed: PackedMatrix, out?: Matrix): Matrix;
153
+ export declare function euclideansPacked(a: Matrix, packed: PackedMatrix, out?: Matrix): Matrix;
154
+ export declare function dotsSymmetric(vectors: Matrix, out?: Matrix, options?: {
155
+ rowStart?: number;
156
+ rowCount?: number;
157
+ }): Matrix;
158
+ export declare function angularsSymmetric(vectors: Matrix, out?: Matrix, options?: {
159
+ rowStart?: number;
160
+ rowCount?: number;
161
+ }): Matrix;
162
+ export declare function euclideansSymmetric(vectors: Matrix, out?: Matrix, options?: {
163
+ rowStart?: number;
164
+ rowCount?: number;
165
+ }): Matrix;
166
+ export {};