{"id":13450060,"url":"https://github.com/ashvardanian/SimSIMD","last_synced_at":"2025-03-23T16:30:55.287Z","repository":{"id":157820683,"uuid":"613772664","full_name":"ashvardanian/SimSIMD","owner":"ashvardanian","description":"Up to 200x Faster Dot Products \u0026 Similarity Metrics — for Python, Rust, C, JS, and Swift, supporting f64, f32, f16 real \u0026 complex, i8, and bit vectors using SIMD for both AVX2, AVX-512, NEON, SVE, \u0026 SVE2 📐","archived":false,"fork":false,"pushed_at":"2025-02-26T19:29:58.000Z","size":1884,"stargazers_count":1286,"open_issues_count":25,"forks_count":73,"subscribers_count":20,"default_branch":"main","last_synced_at":"2025-03-15T15:49:59.716Z","etag":null,"topics":["arm-neon","arm-sve","assembly","avx2","avx512","bfloat16","blas","blas-libraries","distance-calculation","float16","information-retrieval","metrics","neon","numpy","scipy","simd","simd-instructions","similarity-measures","similarity-search","vector-search"],"latest_commit_sha":null,"homepage":"https://ashvardanian.com/posts/simsimd-faster-scipy/","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ashvardanian.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-03-14T08:41:48.000Z","updated_at":"2025-03-14T10:07:55.000Z","dependencies_parsed_at":"2023-10-03T04:02:48.379Z","dependency_job_id":"397ab411-df0f-4e1d-baaf-24b58bd14606","html_url":"https://github.com/ashvardanian/SimSIMD","commit_stats":{"total_commits":743,"total_committers":26,"mean_commits":"28.576923076923077","dds":"0.19650067294751006","last_synced_commit":"81799b6dd8cf28ac71873db7fec34ed85f381ce8"},"previous_names":[],"tags_count":136,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ashvardanian%2FSimSIMD","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ashvardanian%2FSimSIMD/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ashvardanian%2FSimSIMD/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ashvardanian%2FSimSIMD/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ashvardanian","download_url":"https://codeload.github.com/ashvardanian/SimSIMD/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245130694,"owners_count":20565694,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["arm-neon","arm-sve","assembly","avx2","avx512","bfloat16","blas","blas-libraries","distance-calculation","float16","information-retrieval","metrics","neon","numpy","scipy","simd","simd-instructions","similarity-measures","similarity-search","vector-search"],"created_at":"2024-07-31T07:00:29.077Z","updated_at":"2025-03-23T16:30:55.251Z","avatar_url":"https://github.com/ashvardanian.png","language":"C","readme":"![SimSIMD banner](https://github.com/ashvardanian/ashvardanian/blob/master/repositories/SimSIMD.jpg?raw=true)\n\nComputing dot-products, similarity measures, and distances between low- and high-dimensional vectors is ubiquitous in Machine Learning, Scientific Computing, Geo-Spatial Analysis, and Information Retrieval.\nThese algorithms generally have linear complexity in time, constant or linear complexity in space, and are data-parallel.\nIn other words, it is easily parallelizable and vectorizable and often available in packages like BLAS (level 1) and LAPACK, as well as higher-level `numpy` and `scipy` Python libraries.\nIronically, even with decades of evolution in compilers and numerical computing, [most libraries can be 3-200x slower than hardware potential][benchmarks] even on the most popular hardware, like 64-bit x86 and Arm CPUs.\nMoreover, most lack mixed-precision support, which is crucial for modern AI!\nThe rare few that support minimal mixed precision, run only on one platform, and are vendor-locked, by companies like Intel and Nvidia.\nSimSIMD provides an alternative.\n1️⃣ SimSIMD functions are practically as fast as `memcpy`.\n2️⃣ Unlike BLAS, most kernels are designed for mixed-precision and bit-level operations.\n3️⃣ SimSIMD often [ships more binaries than NumPy][compatibility] and has more backends than most BLAS implementations, and more high-level interfaces than most libraries.\n\n[benchmarks]: https://ashvardanian.com/posts/simsimd-faster-scipy\n[compatibility]: https://pypi.org/project/simsimd/#files\n\n\u003cdiv\u003e\n\u003ca href=\"https://pepy.tech/project/simsimd\"\u003e\n    \u003cimg alt=\"PyPI\" src=\"https://static.pepy.tech/personalized-badge/simsimd?period=total\u0026units=abbreviation\u0026left_color=black\u0026right_color=blue\u0026left_text=SimSIMD%20Python%20installs\" /\u003e\n\u003c/a\u003e\n\u003ca href=\"https://www.npmjs.com/package/simsimd\"\u003e\n    \u003cimg alt=\"npm\" src=\"https://img.shields.io/npm/dy/simsimd?label=JavaScript%20NPM%20installs\" /\u003e\n\u003c/a\u003e\n\u003ca href=\"https://crates.io/crates/simsimd\"\u003e\n    \u003cimg alt=\"rust\" src=\"https://img.shields.io/crates/d/simsimd?label=Rust%20Crate%20installs\" /\u003e\n\u003c/a\u003e\n\u003cimg alt=\"GitHub code size in bytes\" src=\"https://img.shields.io/github/languages/code-size/ashvardanian/simsimd\"\u003e\n\u003ca href=\"https://github.com/ashvardanian/SimSIMD/actions/workflows/release.yml\"\u003e\n    \u003cimg alt=\"GitHub Actions Ubuntu\" src=\"https://img.shields.io/github/actions/workflow/status/ashvardanian/SimSIMD/release.yml?branch=main\u0026label=Ubuntu\u0026logo=github\u0026color=blue\"\u003e\n\u003c/a\u003e\n\u003ca href=\"https://github.com/ashvardanian/SimSIMD/actions/workflows/release.yml\"\u003e\n    \u003cimg alt=\"GitHub Actions Windows\" src=\"https://img.shields.io/github/actions/workflow/status/ashvardanian/SimSIMD/release.yml?branch=main\u0026label=Windows\u0026logo=windows\u0026color=blue\"\u003e\n\u003c/a\u003e\n\u003ca href=\"https://github.com/ashvardanian/SimSIMD/actions/workflows/release.yml\"\u003e\n    \u003cimg alt=\"GitHub Actions MacOS\" src=\"https://img.shields.io/github/actions/workflow/status/ashvardanian/SimSIMD/release.yml?branch=main\u0026label=MacOS\u0026logo=apple\u0026color=blue\"\u003e\n\u003c/a\u003e\n\u003ca href=\"https://github.com/ashvardanian/SimSIMD/actions/workflows/release.yml\"\u003e\n    \u003cimg alt=\"GitHub Actions CentOS Linux\" src=\"https://img.shields.io/github/actions/workflow/status/ashvardanian/SimSIMD/release.yml?branch=main\u0026label=CentOS\u0026logo=centos\u0026color=blue\"\u003e\n\u003c/a\u003e\n\n\u003c/div\u003e\n\n## Features\n\n__SimSIMD__ (Arabic: \"سيمسيم دي\") is a mixed-precision math library of __over 350 SIMD-optimized kernels__ extensively used in AI, Search, and DBMS workloads.\nNamed after the iconic [\"Open Sesame\"](https://en.wikipedia.org/wiki/Open_sesame) command that opened doors to treasure in _Ali Baba and the Forty Thieves_, SimSimd can help you 10x the cost-efficiency of your computational pipelines.\nImplemented distance functions include:\n\n- Euclidean (L2) and Cosine (Angular) spatial distances for Vector Search. _[docs][docs-spatial]_\n- Dot-Products for real \u0026 complex vectors for DSP \u0026 Quantum computing. _[docs][docs-dot]_\n- Hamming (~ Manhattan) and Jaccard (~ Tanimoto) bit-level distances. _[docs][docs-binary]_\n- Set Intersections for Sparse Vectors and Text Analysis. _[docs][docs-sparse]_\n- Mahalanobis distance and Quadratic forms for Scientific Computing. _[docs][docs-curved]_\n- Kullback-Leibler and Jensen–Shannon divergences for probability distributions. _[docs][docs-probability]_\n- Fused-Multiply-Add (FMA) and Weighted Sums to replace BLAS level 1 functions. _[docs][docs-fma]_\n- For Levenshtein, Needleman–Wunsch, and Smith-Waterman, check [StringZilla][stringzilla].\n- 🔜 Haversine and Vincenty's formulae for Geospatial Analysis.\n\n[docs-spatial]: #cosine-similarity-reciprocal-square-root-and-newton-raphson-iteration\n[docs-curved]: #curved-spaces-mahalanobis-distance-and-bilinear-quadratic-forms\n[docs-sparse]: #set-intersection-galloping-and-binary-search\n[docs-binary]: https://github.com/ashvardanian/SimSIMD/pull/138\n[docs-dot]: #complex-dot-products-conjugate-dot-products-and-complex-numbers\n[docs-probability]: #logarithms-in-kullback-leibler--jensenshannon-divergences\n[docs-fma]: #mixed-precision-in-fused-multiply-add-and-weighted-sums\n[scipy]: https://docs.scipy.org/doc/scipy/reference/spatial.distance.html#module-scipy.spatial.distance\n[numpy]: https://numpy.org/doc/stable/reference/generated/numpy.inner.html\n[stringzilla]: https://github.com/ashvardanian/stringzilla\n\nMoreover, SimSIMD...\n\n- handles `float64`, `float32`, `float16`, and `bfloat16` real \u0026 complex vectors.\n- handles `int8` integral, `int4` sub-byte, and `b8` binary vectors.\n- handles sparse `uint32` and `uint16` sets, and weighted sparse vectors.\n- is a zero-dependency [header-only C 99](#using-simsimd-in-c) library.\n- has [Python](#using-simsimd-in-python), [Rust](#using-simsimd-in-rust), [JS](#using-simsimd-in-javascript), and [Swift](#using-simsimd-in-swift) bindings.\n- has Arm backends for NEON, Scalable Vector Extensions (SVE), and SVE2.\n- has x86 backends for Haswell, Skylake, Ice Lake, Genoa, and Sapphire Rapids.\n- with both compile-time and runtime CPU feature detection easily integrates anywhere!\n\nDue to the high-level of fragmentation of SIMD support in different x86 CPUs, SimSIMD generally uses the names of select Intel CPU generations for its backends.\nThey, however, also work on AMD CPUs.\nIntel Haswell is compatible with AMD Zen 1/2/3, while AMD Genoa Zen 4 covers AVX-512 instructions added to Intel Skylake and Ice Lake.\nYou can learn more about the technical implementation details in the following blog-posts:\n\n- [Uses Horner's method for polynomial approximations, beating GCC 12 by 119x](https://ashvardanian.com/posts/gcc-12-vs-avx512fp16/).\n- [Uses Arm SVE and x86 AVX-512's masked loads to eliminate tail `for`-loops](https://ashvardanian.com/posts/simsimd-faster-scipy/#tails-of-the-past-the-significance-of-masked-loads).\n- [Substitutes LibC's `sqrt` with Newton Raphson iterations](https://github.com/ashvardanian/SimSIMD/releases/tag/v5.4.0).\n- [Uses Galloping and SVE2 histograms to intersect sparse vectors](https://ashvardanian.com/posts/simd-set-intersections-sve2-avx512/).\n- For Python: [avoids slow PyBind11, SWIG, \u0026 `PyArg_ParseTuple`](https://ashvardanian.com/posts/pybind11-cpython-tutorial/) [using faster calling convention](https://ashvardanian.com/posts/discount-on-keyword-arguments-in-python/).\n- For JavaScript: [uses typed arrays and NAPI for zero-copy calls](https://ashvardanian.com/posts/javascript-ai-vector-search/).\n\n## Benchmarks\n\n\u003ctable style=\"width: 100%; text-align: center; table-layout: fixed;\"\u003e\n  \u003ccolgroup\u003e\n    \u003ccol style=\"width: 33%;\"\u003e\n    \u003ccol style=\"width: 33%;\"\u003e\n    \u003ccol style=\"width: 33%;\"\u003e\n  \u003c/colgroup\u003e\n  \u003ctr\u003e\n    \u003cth align=\"center\"\u003eNumPy\u003c/th\u003e\n    \u003cth align=\"center\"\u003eC 99\u003c/th\u003e\n    \u003cth align=\"center\"\u003eSimSIMD\u003c/th\u003e\n  \u003c/tr\u003e\n  \u003c!-- Cosine distances with different precision levels --\u003e\n  \u003ctr\u003e\n    \u003ctd colspan=\"4\" align=\"center\"\u003ecosine distances between 1536d vectors in \u003ccode\u003eint8\u003c/code\u003e\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd align=\"center\"\u003e \u003c!-- scipy.spatial.distance.cosine --\u003e\n      🚧 overflows\u003cbr/\u003e\n    \u003c/td\u003e\n    \u003ctd align=\"center\"\u003e \u003c!-- serial --\u003e\n      \u003cspan style=\"color:#ABABAB;\"\u003ex86:\u003c/span\u003e \u003cb\u003e10,548,600\u003c/b\u003e ops/s\u003cbr/\u003e\n      \u003cspan style=\"color:#ABABAB;\"\u003earm:\u003c/span\u003e \u003cb\u003e11,379,300\u003c/b\u003e ops/s\n    \u003c/td\u003e\n    \u003ctd align=\"center\"\u003e \u003c!-- simsimd --\u003e\n      \u003cspan style=\"color:#ABABAB;\"\u003ex86:\u003c/span\u003e \u003cb\u003e16,151,800\u003c/b\u003e ops/s\u003cbr/\u003e\n      \u003cspan style=\"color:#ABABAB;\"\u003earm:\u003c/span\u003e \u003cb\u003e13,524,000\u003c/b\u003e ops/s\n    \u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd colspan=\"4\" align=\"center\"\u003ecosine distances between 1536d vectors in \u003ccode\u003ebfloat16\u003c/code\u003e\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd align=\"center\"\u003e \u003c!-- scipy.spatial.distance.cosine --\u003e\n      🚧 not supported\u003cbr/\u003e\n    \u003c/td\u003e\n    \u003ctd align=\"center\"\u003e \u003c!-- serial --\u003e\n      \u003cspan style=\"color:#ABABAB;\"\u003ex86:\u003c/span\u003e \u003cb\u003e119,835\u003c/b\u003e ops/s\u003cbr/\u003e\n      \u003cspan style=\"color:#ABABAB;\"\u003earm:\u003c/span\u003e \u003cb\u003e403,909\u003c/b\u003e ops/s\n    \u003c/td\u003e\n    \u003ctd align=\"center\"\u003e \u003c!-- simsimd --\u003e\n      \u003cspan style=\"color:#ABABAB;\"\u003ex86:\u003c/span\u003e \u003cb\u003e9,738,540\u003c/b\u003e ops/s\u003cbr/\u003e\n      \u003cspan style=\"color:#ABABAB;\"\u003earm:\u003c/span\u003e \u003cb\u003e4,881,900\u003c/b\u003e ops/s\n    \u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd colspan=\"4\" align=\"center\"\u003ecosine distances between 1536d vectors in \u003ccode\u003efloat16\u003c/code\u003e\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd align=\"center\"\u003e \u003c!-- scipy.spatial.distance.cosine --\u003e\n      \u003cspan style=\"color:#ABABAB;\"\u003ex86:\u003c/span\u003e \u003cb\u003e40,481\u003c/b\u003e ops/s\u003cbr/\u003e\n      \u003cspan style=\"color:#ABABAB;\"\u003earm:\u003c/span\u003e \u003cb\u003e21,451\u003c/b\u003e ops/s\n    \u003c/td\u003e\n    \u003ctd align=\"center\"\u003e \u003c!-- serial --\u003e\n      \u003cspan style=\"color:#ABABAB;\"\u003ex86:\u003c/span\u003e \u003cb\u003e501,310\u003c/b\u003e ops/s\u003cbr/\u003e\n      \u003cspan style=\"color:#ABABAB;\"\u003earm:\u003c/span\u003e \u003cb\u003e871,963\u003c/b\u003e ops/s\n    \u003c/td\u003e\n    \u003ctd align=\"center\"\u003e \u003c!-- simsimd --\u003e\n      \u003cspan style=\"color:#ABABAB;\"\u003ex86:\u003c/span\u003e \u003cb\u003e7,627,600\u003c/b\u003e ops/s\u003cbr/\u003e\n      \u003cspan style=\"color:#ABABAB;\"\u003earm:\u003c/span\u003e \u003cb\u003e3,316,810\u003c/b\u003e ops/s\n    \u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd colspan=\"4\" align=\"center\"\u003ecosine distances between 1536d vectors in \u003ccode\u003efloat32\u003c/code\u003e\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd align=\"center\"\u003e \u003c!-- scipy.spatial.distance.cosine --\u003e\n      \u003cspan style=\"color:#ABABAB;\"\u003ex86:\u003c/span\u003e \u003cb\u003e253,902\u003c/b\u003e ops/s\u003cbr/\u003e\n      \u003cspan style=\"color:#ABABAB;\"\u003earm:\u003c/span\u003e \u003cb\u003e46,394\u003c/b\u003e ops/s\n    \u003c/td\u003e\n    \u003ctd align=\"center\"\u003e \u003c!-- serial --\u003e\n      \u003cspan style=\"color:#ABABAB;\"\u003ex86:\u003c/span\u003e \u003cb\u003e882,484\u003c/b\u003e ops/s\u003cbr/\u003e\n      \u003cspan style=\"color:#ABABAB;\"\u003earm:\u003c/span\u003e \u003cb\u003e399,661\u003c/b\u003e ops/s\n    \u003c/td\u003e\n    \u003ctd align=\"center\"\u003e \u003c!-- simsimd --\u003e\n      \u003cspan style=\"color:#ABABAB;\"\u003ex86:\u003c/span\u003e \u003cb\u003e8,202,910\u003c/b\u003e ops/s\u003cbr/\u003e\n      \u003cspan style=\"color:#ABABAB;\"\u003earm:\u003c/span\u003e \u003cb\u003e3,400,620\u003c/b\u003e ops/s\n    \u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd colspan=\"4\" align=\"center\"\u003ecosine distances between 1536d vectors in \u003ccode\u003efloat64\u003c/code\u003e\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd align=\"center\"\u003e \u003c!-- scipy.spatial.distance.cosine --\u003e\n      \u003cspan style=\"color:#ABABAB;\"\u003ex86:\u003c/span\u003e \u003cb\u003e212,421\u003c/b\u003e ops/s\u003cbr/\u003e\n      \u003cspan style=\"color:#ABABAB;\"\u003earm:\u003c/span\u003e \u003cb\u003e52,904\u003c/b\u003e ops/s\n    \u003c/td\u003e\n    \u003ctd align=\"center\"\u003e \u003c!-- serial --\u003e\n      \u003cspan style=\"color:#ABABAB;\"\u003ex86:\u003c/span\u003e \u003cb\u003e839,301\u003c/b\u003e ops/s\u003cbr/\u003e\n      \u003cspan style=\"color:#ABABAB;\"\u003earm:\u003c/span\u003e \u003cb\u003e837,126\u003c/b\u003e ops/s\n    \u003c/td\u003e\n    \u003ctd align=\"center\"\u003e \u003c!-- simsimd --\u003e\n      \u003cspan style=\"color:#ABABAB;\"\u003ex86:\u003c/span\u003e \u003cb\u003e1,538,530\u003c/b\u003e ops/s\u003cbr/\u003e\n      \u003cspan style=\"color:#ABABAB;\"\u003earm:\u003c/span\u003e \u003cb\u003e1,678,920\u003c/b\u003e ops/s\n    \u003c/td\u003e\n  \u003c/tr\u003e\n\n  \u003c!-- Euclidean distance with different precision level --\u003e\n  \u003ctr\u003e\n    \u003ctd colspan=\"4\" align=\"center\"\u003eeuclidean distance between 1536d vectors in \u003ccode\u003eint8\u003c/code\u003e\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd align=\"center\"\u003e \u003c!-- scipy.spatial.distance.sqeuclidean --\u003e\n      \u003cspan style=\"color:#ABABAB;\"\u003ex86:\u003c/span\u003e \u003cb\u003e252,113\u003c/b\u003e ops/s\u003cbr/\u003e\n      \u003cspan style=\"color:#ABABAB;\"\u003earm:\u003c/span\u003e \u003cb\u003e177,443\u003c/b\u003e ops/s\n    \u003c/td\u003e\n    \u003ctd align=\"center\"\u003e \u003c!-- serial --\u003e\n      \u003cspan style=\"color:#ABABAB;\"\u003ex86:\u003c/span\u003e \u003cb\u003e6,690,110\u003c/b\u003e ops/s\u003cbr/\u003e\n      \u003cspan style=\"color:#ABABAB;\"\u003earm:\u003c/span\u003e \u003cb\u003e4,114,160\u003c/b\u003e ops/s\n    \u003c/td\u003e\n    \u003ctd align=\"center\"\u003e \u003c!-- simsimd --\u003e\n      \u003cspan style=\"color:#ABABAB;\"\u003ex86:\u003c/span\u003e \u003cb\u003e18,989,000\u003c/b\u003e ops/s\u003cbr/\u003e\n      \u003cspan style=\"color:#ABABAB;\"\u003earm:\u003c/span\u003e \u003cb\u003e18,878,200\u003c/b\u003e ops/s\n    \u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd colspan=\"4\" align=\"center\"\u003eeuclidean distance between 1536d vectors in \u003ccode\u003ebfloat16\u003c/code\u003e\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd align=\"center\"\u003e \u003c!-- scipy.spatial.distance.sqeuclidean --\u003e\n      🚧 not supported\u003cbr/\u003e\n    \u003c/td\u003e\n    \u003ctd align=\"center\"\u003e \u003c!-- serial --\u003e\n      \u003cspan style=\"color:#ABABAB;\"\u003ex86:\u003c/span\u003e \u003cb\u003e119,842\u003c/b\u003e ops/s\u003cbr/\u003e\n      \u003cspan style=\"color:#ABABAB;\"\u003earm:\u003c/span\u003e \u003cb\u003e1,049,230\u003c/b\u003e ops/s\n    \u003c/td\u003e\n    \u003ctd align=\"center\"\u003e \u003c!-- simsimd --\u003e\n      \u003cspan style=\"color:#ABABAB;\"\u003ex86:\u003c/span\u003e \u003cb\u003e9,727,210\u003c/b\u003e ops/s\u003cbr/\u003e\n      \u003cspan style=\"color:#ABABAB;\"\u003earm:\u003c/span\u003e \u003cb\u003e4,233,420\u003c/b\u003e ops/s\n    \u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd colspan=\"4\" align=\"center\"\u003eeuclidean distance between 1536d vectors in \u003ccode\u003efloat16\u003c/code\u003e\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd align=\"center\"\u003e \u003c!-- scipy.spatial.distance.sqeuclidean --\u003e\n      \u003cspan style=\"color:#ABABAB;\"\u003ex86:\u003c/span\u003e \u003cb\u003e54,621\u003c/b\u003e ops/s\u003cbr/\u003e\n      \u003cspan style=\"color:#ABABAB;\"\u003earm:\u003c/span\u003e \u003cb\u003e71,793\u003c/b\u003e ops/s\n    \u003c/td\u003e\n    \u003ctd align=\"center\"\u003e \u003c!-- serial --\u003e\n      \u003cspan style=\"color:#ABABAB;\"\u003ex86:\u003c/span\u003e \u003cb\u003e196,413\u003c/b\u003e ops/s\u003cbr/\u003e\n      \u003cspan style=\"color:#ABABAB;\"\u003earm:\u003c/span\u003e \u003cb\u003e911,370\u003c/b\u003e ops/s\n    \u003c/td\u003e\n    \u003ctd align=\"center\"\u003e \u003c!-- simsimd --\u003e\n      \u003cspan style=\"color:#ABABAB;\"\u003ex86:\u003c/span\u003e \u003cb\u003e19,466,800\u003c/b\u003e ops/s\u003cbr/\u003e\n      \u003cspan style=\"color:#ABABAB;\"\u003earm:\u003c/span\u003e \u003cb\u003e3,522,760\u003c/b\u003e ops/s\n    \u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd colspan=\"4\" align=\"center\"\u003eeuclidean distance between 1536d vectors in \u003ccode\u003efloat32\u003c/code\u003e\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd align=\"center\"\u003e \u003c!-- scipy.spatial.distance.sqeuclidean --\u003e\n      \u003cspan style=\"color:#ABABAB;\"\u003ex86:\u003c/span\u003e \u003cb\u003e424,944\u003c/b\u003e ops/s\u003cbr/\u003e\n      \u003cspan style=\"color:#ABABAB;\"\u003earm:\u003c/span\u003e \u003cb\u003e292,629\u003c/b\u003e ops/s\n    \u003c/td\u003e\n    \u003ctd align=\"center\"\u003e \u003c!-- serial --\u003e\n      \u003cspan style=\"color:#ABABAB;\"\u003ex86:\u003c/span\u003e \u003cb\u003e1,295,210\u003c/b\u003e ops/s\u003cbr/\u003e\n      \u003cspan style=\"color:#ABABAB;\"\u003earm:\u003c/span\u003e \u003cb\u003e1,055,940\u003c/b\u003e ops/s\n    \u003c/td\u003e\n    \u003ctd align=\"center\"\u003e \u003c!-- simsimd --\u003e\n      \u003cspan style=\"color:#ABABAB;\"\u003ex86:\u003c/span\u003e \u003cb\u003e8,924,100\u003c/b\u003e ops/s\u003cbr/\u003e\n      \u003cspan style=\"color:#ABABAB;\"\u003earm:\u003c/span\u003e \u003cb\u003e3,602,650\u003c/b\u003e ops/s\n    \u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd colspan=\"4\" align=\"center\"\u003eeuclidean distance between 1536d vectors in \u003ccode\u003efloat64\u003c/code\u003e\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd align=\"center\"\u003e \u003c!-- scipy.spatial.distance.sqeuclidean --\u003e\n      \u003cspan style=\"color:#ABABAB;\"\u003ex86:\u003c/span\u003e \u003cb\u003e334,929\u003c/b\u003e ops/s\u003cbr/\u003e\n      \u003cspan style=\"color:#ABABAB;\"\u003earm:\u003c/span\u003e \u003cb\u003e237,505\u003c/b\u003e ops/s\n    \u003c/td\u003e\n    \u003ctd align=\"center\"\u003e \u003c!-- serial --\u003e\n      \u003cspan style=\"color:#ABABAB;\"\u003ex86:\u003c/span\u003e \u003cb\u003e1,215,190\u003c/b\u003e ops/s\u003cbr/\u003e\n      \u003cspan style=\"color:#ABABAB;\"\u003earm:\u003c/span\u003e \u003cb\u003e905,782\u003c/b\u003e ops/s\n    \u003c/td\u003e\n    \u003ctd align=\"center\"\u003e \u003c!-- simsimd --\u003e\n      \u003cspan style=\"color:#ABABAB;\"\u003ex86:\u003c/span\u003e \u003cb\u003e1,701,740\u003c/b\u003e ops/s\u003cbr/\u003e\n      \u003cspan style=\"color:#ABABAB;\"\u003earm:\u003c/span\u003e \u003cb\u003e1,735,840\u003c/b\u003e ops/s\n    \u003c/td\u003e\n  \u003c/tr\u003e\n  \u003c!-- Bilinear forms --\u003e\n  \u003c!-- Sparse set intersections --\u003e\n\u003c/table\u003e\n\n\u003e For benchmarks we mostly use 1536-dimensional vectors, like the embeddings produced by the OpenAI Ada API.\n\u003e The code was compiled with GCC 12, using glibc v2.35.\n\u003e The benchmarks performed on Arm-based Graviton3 AWS `c7g` instances and `r7iz` Intel Sapphire Rapids.\n\u003e Most modern Arm-based 64-bit CPUs will have similar relative speedups.\n\u003e Variance withing x86 CPUs will be larger.\n\nSimilar speedups are often observed even when compared to BLAS and LAPACK libraries underlying most numerical computing libraries, including NumPy and SciPy in Python.\nBroader benchmarking results:\n\n- [Apple M2 Pro](https://ashvardanian.com/posts/simsimd-faster-scipy/#appendix-1-performance-on-apple-m2-pro).\n- [Intel Sapphire Rapids](https://ashvardanian.com/posts/simsimd-faster-scipy/#appendix-2-performance-on-4th-gen-intel-xeon-platinum-8480).\n- [AWS Graviton 3](https://ashvardanian.com/posts/simsimd-faster-scipy/#appendix-3-performance-on-aws-graviton-3).\n\n## Using SimSIMD in Python\n\nThe package is intended to replace the usage of `numpy.inner`, `numpy.dot`, and `scipy.spatial.distance`.\nAside from drastic performance improvements, SimSIMD significantly improves accuracy in mixed precision setups.\nNumPy and SciPy, processing `int8`, `uint8` or `float16` vectors, will use the same types for accumulators, while SimSIMD can combine `int8` enumeration, `int16` multiplication, and `int32` accumulation to avoid overflows entirely.\nThe same applies to processing `float16` and `bfloat16` values with `float32` precision.\n\n### Installation\n\nUse the following snippet to install SimSIMD and list available hardware acceleration options available on your machine:\n\n```sh\npip install simsimd\npython -c \"import simsimd; print(simsimd.get_capabilities())\"   # for hardware introspection\npython -c \"import simsimd; help(simsimd)\"                       # for documentation\n```\n\nWith precompiled binaries, SimSIMD ships `.pyi` interface files for type hinting and static analysis.\nYou can check all the available functions in [`python/annotations/__init__.pyi`](https://github.com/ashvardanian/SimSIMD/blob/main/python/annotations/__init__.pyi).\n\n### One-to-One Distance\n\n```py\nimport simsimd\nimport numpy as np\n\nvec1 = np.random.randn(1536).astype(np.float32)\nvec2 = np.random.randn(1536).astype(np.float32)\ndist = simsimd.cosine(vec1, vec2)\n```\n\nSupported functions include `cosine`, `inner`, `sqeuclidean`, `hamming`, `jaccard`, `kulbackleibler`, `jensenshannon`, and `intersect`.\nDot products are supported for both real and complex numbers:\n\n```py\nvec1 = np.random.randn(768).astype(np.float64) + 1j * np.random.randn(768).astype(np.float64)\nvec2 = np.random.randn(768).astype(np.float64) + 1j * np.random.randn(768).astype(np.float64)\n\ndist = simsimd.dot(vec1.astype(np.complex128), vec2.astype(np.complex128))\ndist = simsimd.dot(vec1.astype(np.complex64), vec2.astype(np.complex64))\ndist = simsimd.vdot(vec1.astype(np.complex64), vec2.astype(np.complex64)) # conjugate, same as `np.vdot`\n```\n\nUnlike SciPy, SimSIMD allows explicitly stating the precision of the input vectors, which is especially useful for mixed-precision setups.\nThe `dtype` argument can be passed both by name and as a positional argument:\n\n```py\ndist = simsimd.cosine(vec1, vec2, \"int8\")\ndist = simsimd.cosine(vec1, vec2, \"float16\")\ndist = simsimd.cosine(vec1, vec2, \"float32\")\ndist = simsimd.cosine(vec1, vec2, \"float64\")\ndist = simsimd.hamming(vec1, vec2, \"bin8\")\n```\n\nBinary distance functions are computed at a bit-level.\nMeaning a vector of 10x 8-bit integers will be treated as a sequence of 80 individual bits or dimensions.\nThis differs from NumPy, that can't handle smaller-than-byte types, but you can still avoid the `bin8` argument by reinterpreting the vector as booleans:\n\n```py\nvec1 = np.random.randint(2, size=80).astype(np.uint8).packbits().view(np.bool_)\nvec2 = np.random.randint(2, size=80).astype(np.uint8).packbits().view(np.bool_)\nhamming_distance = simsimd.hamming(vec1, vec2)\njaccard_distance = simsimd.jaccard(vec1, vec2)\n```\n\nWith other frameworks, like PyTorch, one can get a richer type-system than NumPy, but the lack of good CPython interoperability makes it hard to pass data without copies.\nHere is an example of using SimSIMD with PyTorch to compute the cosine similarity between two `bfloat16` vectors:\n\n```py\nimport numpy as np\nbuf1 = np.empty(8, dtype=np.uint16)\nbuf2 = np.empty(8, dtype=np.uint16)\n\n# View the same memory region with PyTorch and randomize it\nimport torch\nvec1 = torch.asarray(memoryview(buf1), copy=False).view(torch.bfloat16)\nvec2 = torch.asarray(memoryview(buf2), copy=False).view(torch.bfloat16)\ntorch.randn(8, out=vec1)\ntorch.randn(8, out=vec2)\n\n# Both libs will look into the same memory buffers and report the same results\ndist_slow = 1 - torch.nn.functional.cosine_similarity(vec1, vec2, dim=0)\ndist_fast = simsimd.cosine(buf1, buf2, \"bfloat16\")\n```\n\nIt also allows using SimSIMD for half-precision complex numbers, which NumPy does not support.\nFor that, view data as continuous even-length `np.float16` vectors and override type-resolution with `complex32` string.\n\n```py\nvec1 = np.random.randn(1536).astype(np.float16)\nvec2 = np.random.randn(1536).astype(np.float16)\nsimd.dot(vec1, vec2, \"complex32\")\nsimd.vdot(vec1, vec2, \"complex32\")\n```\n\nWhen dealing with sparse representations and integer sets, you can apply the `intersect` function to two 1-dimensional arrays of `uint16` or `uint32` integers:\n\n```py\nfrom random import randint\nimport numpy as np\nimport simsimd as simd\n\nlength1, length2 = randint(1, 100), randint(1, 100)\nvec1 = np.sort(np.random.randint(0, 1000, length1).astype(np.uint16))\nvec2 = np.sort(np.random.randint(0, 1000, length2).astype(np.uint16))\n\nslow_result = len(np.intersect1d(vec1, vec2))\nfast_result = simd.intersect(vec1, vec2)\nassert slow_result == fast_result\n```\n\n### One-to-Many Distances\n\nEvery distance function can be used not only for one-to-one but also one-to-many and many-to-many distance calculations.\nFor one-to-many:\n\n```py\nvec1 = np.random.randn(1536).astype(np.float32) # rank 1 tensor\nbatch1 = np.random.randn(1, 1536).astype(np.float32) # rank 2 tensor\nbatch2 = np.random.randn(100, 1536).astype(np.float32)\n\ndist_rank1 = simsimd.cosine(vec1, batch2)\ndist_rank2 = simsimd.cosine(batch1, batch2)\n```\n\n### Many-to-Many Distances\n\nAll distance functions in SimSIMD can be used to compute many-to-many distances.\nFor two batches of 100 vectors to compute 100 distances, one would call it like this:\n\n```py\nbatch1 = np.random.randn(100, 1536).astype(np.float32)\nbatch2 = np.random.randn(100, 1536).astype(np.float32)\ndist = simsimd.cosine(batch1, batch2)\n```\n\nInput matrices must have identical shapes.\nThis functionality isn't natively present in NumPy or SciPy, and generally requires creating intermediate arrays, which is inefficient and memory-consuming.\n\n### Many-to-Many All-Pairs Distances\n\nOne can use SimSIMD to compute distances between all possible pairs of rows across two matrices (akin to [`scipy.spatial.distance.cdist`](https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.distance.cdist.html)).\nThe resulting object will have a type `DistancesTensor`, zero-copy compatible with NumPy and other libraries.\nFor two arrays of 10 and 1,000 entries, the resulting tensor will have 10,000 cells:\n\n```py\nimport numpy as np\nfrom simsimd import cdist, DistancesTensor\n\nmatrix1 = np.random.randn(1000, 1536).astype(np.float32)\nmatrix2 = np.random.randn(10, 1536).astype(np.float32)\ndistances: DistancesTensor = simsimd.cdist(matrix1, matrix2, metric=\"cosine\")   # zero-copy, managed by SimSIMD\ndistances_array: np.ndarray = np.array(distances, copy=True)                    # now managed by NumPy\n```\n\n### Element-wise Kernels\n\nSimSIMD also provides mixed-precision element-wise kernels, where the input vectors and the output have the same numeric type, but the intermediate accumulators are of a higher precision.\n\n```py\nimport numpy as np\nfrom simsimd import fma, wsum\n\n# Let's take two FullHD video frames\nfirst_frame = np.random.randn(1920 * 1024).astype(np.uint8)  \nsecond_frame = np.random.randn(1920 * 1024).astype(np.uint8)\naverage_frame = np.empty_like(first_frame)\nwsum(first_frame, second_frame, alpha=0.5, beta=0.5, out=average_frame)\n\n# Slow analog with NumPy:\nslow_average_frame = (0.5 * first_frame + 0.5 * second_frame).astype(np.uint8)\n```\n\nSimilarly, the `fma` takes three arguments and computes the fused multiply-add operation.\nIn applications like Machine Learning you may also benefit from using the \"brain-float\" format not natively supported by NumPy.\nIn 3D Graphics, for example, we can use FMA to compute the [Phong shading model](https://en.wikipedia.org/wiki/Phong_shading):\n\n```py\n# Assume a FullHD frame with random values for simplicity\nlight_intensity = np.random.rand(1920 * 1080).astype(np.float16)  # Intensity of light on each pixel\ndiffuse_component = np.random.rand(1920 * 1080).astype(np.float16)  # Diffuse reflectance on the surface\nspecular_component = np.random.rand(1920 * 1080).astype(np.float16)  # Specular reflectance for highlights\noutput_color = np.empty_like(light_intensity)  # Array to store the resulting color intensity\n\n# Define the scaling factors for diffuse and specular contributions\nalpha = 0.7  # Weight for the diffuse component\nbeta = 0.3   # Weight for the specular component\n\n# Formula: color = alpha * light_intensity * diffuse_component + beta * specular_component\nfma(light_intensity, diffuse_component, specular_component, \n    dtype=\"float16\", # Optional, unless it can't be inferred from the input\n    alpha=alpha, beta=beta, out=output_color)\n\n# Slow analog with NumPy for comparison\nslow_output_color = (alpha * light_intensity * diffuse_component + beta * specular_component).astype(np.float16)\n```\n\n### Multithreading and Memory Usage\n\nBy default, computations use a single CPU core.\nTo override this behavior, use the `threads` argument.\nSet it to `0` to use all available CPU cores.\nHere is an example of dealing with large sets of binary vectors:\n\n```py\nndim = 1536 # OpenAI Ada embeddings\nmatrix1 = np.packbits(np.random.randint(2, size=(10_000, ndim)).astype(np.uint8))\nmatrix2 = np.packbits(np.random.randint(2, size=(1_000, ndim)).astype(np.uint8))\n\ndistances = simsimd.cdist(matrix1, matrix2, \n    metric=\"hamming\",   # Unlike SciPy, SimSIMD doesn't divide by the number of dimensions\n    out_dtype=\"uint8\",  # so we can use `uint8` instead of `float64` to save memory.\n    threads=0,          # Use all CPU cores with OpenMP.\n    dtype=\"bin8\",       # Override input argument type to `bin8` eight-bit words.\n)\n```\n\nBy default, the output distances will be stored in double-precision `float64` floating-point numbers.\nThat behavior may not be space-efficient, especially if you are computing the hamming distance between short binary vectors, that will generally fit into 8x smaller `uint8` or `uint16` types.\nTo override this behavior, use the `dtype` argument.\n\n### Helper Functions\n\nYou can turn specific backends on or off depending on the exact environment.\nA common case may be avoiding AVX-512 on older AMD CPUs and [Intel Ice Lake](https://travisdowns.github.io/blog/2020/08/19/icl-avx512-freq.html) CPUs to ensure the CPU doesn't change the frequency license and throttle performance.\n\n```py\n$ simsimd.get_capabilities()\n\u003e {'serial': True, 'neon': False, 'sve': False, 'neon_f16': False, 'sve_f16': False, 'neon_bf16': False, 'sve_bf16': False, 'neon_i8': False, 'sve_i8': False, 'haswell': True, 'skylake': True, 'ice': True, 'genoa': True, 'sapphire': True, 'turin': True}\n$ simsimd.disable_capability(\"sapphire\")\n$ simsimd.enable_capability(\"sapphire\")\n```\n\n### Using Python API with USearch\n\nWant to use it in Python with [USearch](https://github.com/unum-cloud/usearch)?\nYou can wrap the raw C function pointers SimSIMD backends into a `CompiledMetric` and pass it to USearch, similar to how it handles Numba's JIT-compiled code.\n\n```py\nfrom usearch.index import Index, CompiledMetric, MetricKind, MetricSignature\nfrom simsimd import pointer_to_sqeuclidean, pointer_to_cosine, pointer_to_inner\n\nmetric = CompiledMetric(\n    pointer=pointer_to_cosine(\"f16\"),\n    kind=MetricKind.Cos,\n    signature=MetricSignature.ArrayArraySize,\n)\n\nindex = Index(256, metric=metric)\n```\n\n## Using SimSIMD in Rust\n\nTo install, add the following to your `Cargo.toml`:\n\n```toml\n[dependencies]\nsimsimd = \"...\"\n```\n\nBefore using the SimSIMD library, ensure you have imported the necessary traits and types into your Rust source file.\nThe library provides several traits for different distance/similarity kinds - `SpatialSimilarity`, `BinarySimilarity`, and `ProbabilitySimilarity`.\n\n### Spatial Similarity: Cosine and Euclidean Distances\n\n```rust\nuse simsimd::SpatialSimilarity;\n\nfn main() {\n    let vector_a: Vec\u003cf32\u003e = vec![1.0, 2.0, 3.0];\n    let vector_b: Vec\u003cf32\u003e = vec![4.0, 5.0, 6.0];\n\n    // Compute the cosine similarity between vector_a and vector_b\n    let cosine_similarity = f32::cosine(\u0026vector_a, \u0026vector_b)\n        .expect(\"Vectors must be of the same length\");\n\n    println!(\"Cosine Similarity: {}\", cosine_similarity);\n\n    // Compute the squared Euclidean distance between vector_a and vector_b\n    let sq_euclidean_distance = f32::sqeuclidean(\u0026vector_a, \u0026vector_b)\n        .expect(\"Vectors must be of the same length\");\n\n    println!(\"Squared Euclidean Distance: {}\", sq_euclidean_distance);\n}\n```\n\nSpatial similarity functions are available for `f64`, `f32`, `f16`, and `i8` types.\n\n### Dot-Products: Inner and Complex Inner Products\n\n```rust\nuse simsimd::SpatialSimilarity;\nuse simsimd::ComplexProducts;\n\nfn main() {\n    let vector_a: Vec\u003cf32\u003e = vec![1.0, 2.0, 3.0, 4.0];\n    let vector_b: Vec\u003cf32\u003e = vec![5.0, 6.0, 7.0, 8.0];\n\n    // Compute the inner product between vector_a and vector_b\n    let inner_product = SpatialSimilarity::dot(\u0026vector_a, \u0026vector_b)\n        .expect(\"Vectors must be of the same length\");\n\n    println!(\"Inner Product: {}\", inner_product);\n\n    // Compute the complex inner product between complex_vector_a and complex_vector_b\n    let complex_inner_product = ComplexProducts::dot(\u0026vector_a, \u0026vector_b)\n        .expect(\"Vectors must be of the same length\");\n\n    let complex_conjugate_inner_product = ComplexProducts::vdot(\u0026vector_a, \u0026vector_b)\n        .expect(\"Vectors must be of the same length\");\n\n    println!(\"Complex Inner Product: {:?}\", complex_inner_product); // -18, 69\n    println!(\"Complex C. Inner Product: {:?}\", complex_conjugate_inner_product); // 70, -8\n}\n```\n\nComplex inner products are available for `f64`, `f32`, and `f16` types.\n\n### Probability Distributions: Jensen-Shannon and Kullback-Leibler Divergences\n\n```rust\nuse simsimd::SpatialSimilarity;\n\nfn main() {\n    let vector_a: Vec\u003cf32\u003e = vec![1.0, 2.0, 3.0];\n    let vector_b: Vec\u003cf32\u003e = vec![4.0, 5.0, 6.0];\n\n    let cosine_similarity = f32::jensenshannon(\u0026vector_a, \u0026vector_b)\n        .expect(\"Vectors must be of the same length\");\n\n    println!(\"Cosine Similarity: {}\", cosine_similarity);\n\n    let sq_euclidean_distance = f32::kullbackleibler(\u0026vector_a, \u0026vector_b)\n        .expect(\"Vectors must be of the same length\");\n\n    println!(\"Squared Euclidean Distance: {}\", sq_euclidean_distance);\n}\n```\n\nProbability similarity functions are available for `f64`, `f32`, and `f16` types.\n\n### Binary Similarity: Hamming and Jaccard Distances\n\nSimilar to spatial distances, one can compute bit-level distance functions between slices of unsigned integers:\n\n```rust\nuse simsimd::BinarySimilarity;\n\nfn main() {\n    let vector_a = \u0026[0b11110000, 0b00001111, 0b10101010];\n    let vector_b = \u0026[0b11110000, 0b00001111, 0b01010101];\n\n    // Compute the Hamming distance between vector_a and vector_b\n    let hamming_distance = u8::hamming(\u0026vector_a, \u0026vector_b)\n        .expect(\"Vectors must be of the same length\");\n\n    println!(\"Hamming Distance: {}\", hamming_distance);\n\n    // Compute the Jaccard distance between vector_a and vector_b\n    let jaccard_distance = u8::jaccard(\u0026vector_a, \u0026vector_b)\n        .expect(\"Vectors must be of the same length\");\n\n    println!(\"Jaccard Distance: {}\", jaccard_distance);\n}\n```\n\nBinary similarity functions are available only for `u8` types.\n\n### Half-Precision Floating-Point Numbers\n\nRust has no native support for half-precision floating-point numbers, but SimSIMD provides a `f16` type.\nIt has no functionality - it is a `transparent` wrapper around `u16` and can be used with `half` or any other half-precision library.\n\n```rust\nuse simsimd::SpatialSimilarity;\nuse simsimd::f16 as SimF16;\nuse half::f16 as HalfF16;\n\nfn main() {\n    let vector_a: Vec\u003cHalfF16\u003e = ...\n    let vector_b: Vec\u003cHalfF16\u003e = ...\n\n    let buffer_a: \u0026[SimF16] = unsafe { std::slice::from_raw_parts(a_half.as_ptr() as *const SimF16, a_half.len()) };\n    let buffer_b: \u0026[SimF16] = unsafe { std::slice::from_raw_parts(b_half.as_ptr() as *const SimF16, b_half.len()) };\n\n    // Compute the cosine similarity between vector_a and vector_b\n    let cosine_similarity = SimF16::cosine(\u0026vector_a, \u0026vector_b)\n        .expect(\"Vectors must be of the same length\");\n\n    println!(\"Cosine Similarity: {}\", cosine_similarity);\n}\n```\n\n### Half-Precision Brain-Float Numbers\n\nThe \"brain-float-16\" is a popular machine learning format.\nIt's broadly supported in hardware and is very machine-friendly, but software support is still lagging behind. \n[Unlike NumPy](https://github.com/numpy/numpy/issues/19808), you can already use `bf16` datatype in SimSIMD.\nLuckily, to downcast `f32` to `bf16` you only have to drop the last 16 bits:\n\n```py\nimport numpy as np\nimport simsimd as simd\n\na = np.random.randn(ndim).astype(np.float32)\nb = np.random.randn(ndim).astype(np.float32)\n\n# NumPy doesn't natively support brain-float, so we need a trick!\n# Luckily, it's very easy to reduce the representation accuracy\n# by simply masking the low 16-bits of our 32-bit single-precision\n# numbers. We can also add `0x8000` to round the numbers.\na_f32rounded = ((a.view(np.uint32) + 0x8000) \u0026 0xFFFF0000).view(np.float32)\nb_f32rounded = ((b.view(np.uint32) + 0x8000) \u0026 0xFFFF0000).view(np.float32)\n\n# To represent them as brain-floats, we need to drop the second half\na_bf16 = np.right_shift(a_f32rounded.view(np.uint32), 16).astype(np.uint16)\nb_bf16 = np.right_shift(b_f32rounded.view(np.uint32), 16).astype(np.uint16)\n\n# Now we can compare the results\nexpected = np.inner(a_f32rounded, b_f32rounded)\nresult = simd.inner(a_bf16, b_bf16, \"bf16\")\n```\n\n### Dynamic Dispatch in Rust\n\nSimSIMD provides a [dynamic dispatch](#dynamic-dispatch) mechanism to select the most advanced micro-kernel for the current CPU.\nYou can query supported backends and use the `SimSIMD::capabilities` function to select the best one.\n\n```rust\nprintln!(\"uses neon: {}\", capabilities::uses_neon());\nprintln!(\"uses sve: {}\", capabilities::uses_sve());\nprintln!(\"uses haswell: {}\", capabilities::uses_haswell());\nprintln!(\"uses skylake: {}\", capabilities::uses_skylake());\nprintln!(\"uses ice: {}\", capabilities::uses_ice());\nprintln!(\"uses genoa: {}\", capabilities::uses_genoa());\nprintln!(\"uses sapphire: {}\", capabilities::uses_sapphire());\nprintln!(\"uses turin: {}\", capabilities::uses_turin());\n```\n\n## Using SimSIMD in JavaScript\n\nTo install, choose one of the following options depending on your environment:\n\n- `npm install --save simsimd`\n- `yarn add simsimd`\n- `pnpm add simsimd`\n- `bun install simsimd`\n\nThe package is distributed with prebuilt binaries, but if your platform is not supported, you can build the package from the source via `npm run build`.\nThis will automatically happen unless you install the package with the `--ignore-scripts` flag or use Bun.\nAfter you install it, you will be able to call the SimSIMD functions on various `TypedArray` variants:\n\n```js\nconst { sqeuclidean, cosine, inner, hamming, jaccard } = require('simsimd');\n\nconst vectorA = new Float32Array([1.0, 2.0, 3.0]);\nconst vectorB = new Float32Array([4.0, 5.0, 6.0]);\n\nconst distance = sqeuclidean(vectorA, vectorB);\nconsole.log('Squared Euclidean Distance:', distance);\n```\n\nOther numeric types and precision levels are supported as well.\nFor double-precision floating-point numbers, use `Float64Array`:\n\n```js\nconst vectorA = new Float64Array([1.0, 2.0, 3.0]);\nconst vectorB = new Float64Array([4.0, 5.0, 6.0]);\nconst distance = cosine(vectorA, vectorB);\n```\n\nWhen doing machine learning and vector search with high-dimensional vectors you may want to quantize them to 8-bit integers.\nYou may want to project values from the $[-1, 1]$ range to the $[-127, 127]$ range and then cast them to `Int8Array`:\n\n```js\nconst quantizedVectorA = new Int8Array(vectorA.map(v =\u003e (v * 127)));\nconst quantizedVectorB = new Int8Array(vectorB.map(v =\u003e (v * 127)));\nconst distance = cosine(quantizedVectorA, quantizedVectorB);\n```\n\nA more extreme quantization case would be to use binary vectors.\nYou can map all positive values to `1` and all negative values and zero to `0`, packing eight values into a single byte.\nAfter that, Hamming and Jaccard distances can be computed.\n\n```js\nconst { toBinary, hamming } = require('simsimd');\n\nconst binaryVectorA = toBinary(vectorA);\nconst binaryVectorB = toBinary(vectorB);\nconst distance = hamming(binaryVectorA, binaryVectorB);\n```\n\n## Using SimSIMD in Swift\n\nTo install, simply add the following dependency to your `Package.swift`:\n\n```swift\ndependencies: [\n    .package(url: \"https://github.com/ashvardanian/simsimd\")\n]\n```\n\nThe package provides the most common spatial metrics for `Int8`, `Float16`, `Float32`, and `Float64` vectors.\n\n```swift\nimport SimSIMD\n\nlet vectorA: [Int8] = [1, 2, 3]\nlet vectorB: [Int8] = [4, 5, 6]\n\nlet cosineSimilarity = vectorA.cosine(vectorB)  // Computes the cosine similarity\nlet dotProduct = vectorA.dot(vectorB)           // Computes the dot product\nlet sqEuclidean = vectorA.sqeuclidean(vectorB)  // Computes the squared Euclidean distance\n```\n\n## Using SimSIMD in C\n\nFor integration within a CMake-based project, add the following segment to your `CMakeLists.txt`:\n\n```cmake\nFetchContent_Declare(\n    simsimd\n    GIT_REPOSITORY https://github.com/ashvardanian/simsimd.git\n    GIT_SHALLOW TRUE\n)\nFetchContent_MakeAvailable(simsimd)\n```\n\nAfter that, you can use the SimSIMD library in your C code in several ways.\nSimplest of all, you can include the headers, and the compiler will automatically select the most recent CPU extensions that SimSIMD will use.\n\n```c\n#include \u003csimsimd/simsimd.h\u003e\n\nint main() {\n    simsimd_flush_denormals(); // Optional, to avoid performance penalties on denormal numbers\n\n    simsimd_f32_t vector_a[1536];\n    simsimd_f32_t vector_b[1536];\n    simsimd_kernel_punned_t distance_function = simsimd_metric_punned(\n        simsimd_metric_cos_k,   // Metric kind, like the angular cosine distance\n        simsimd_datatype_f32_k, // Data type, like: f16, f32, f64, i8, b8, and complex variants\n        simsimd_cap_any_k);     // Which CPU capabilities are we allowed to use\n    simsimd_distance_t distance;\n    distance_function(vector_a, vector_b, 1536, \u0026distance);\n    return 0;\n}\n```\n\n### Dynamic Dispatch in C\n\nTo avoid hard-coding the backend, you can rely on `c/lib.c` to prepackage all possible backends in one binary, and select the most recent CPU features at runtime.\nThat feature of the C library is called [dynamic dispatch](#dynamic-dispatch) and is extensively used in the Python, JavaScript, and Rust bindings.\nTo test which CPU features are available on the machine at runtime, use the following APIs:\n\n```c\nint uses_dynamic_dispatch = simsimd_uses_dynamic_dispatch(); // Check if dynamic dispatch was enabled\nsimsimd_capability_t capabilities = simsimd_capabilities();  // Returns a bitmask\n\nint uses_neon = simsimd_uses_neon();\nint uses_sve = simsimd_uses_sve();\nint uses_haswell = simsimd_uses_haswell();\nint uses_skylake = simsimd_uses_skylake();\nint uses_ice = simsimd_uses_ice();\nint uses_genoa = simsimd_uses_genoa();\nint uses_sapphire = simsimd_uses_sapphire();\n```\n\nTo override compilation settings and switch between runtime and compile-time dispatch, define the following macro:\n\n```c\n#define SIMSIMD_DYNAMIC_DISPATCH 1 // or 0\n```\n\n### Spatial Distances: Cosine and Euclidean Distances\n\n```c\n#include \u003csimsimd/simsimd.h\u003e\n\nint main() {\n    simsimd_i8_t i8[1536];\n    simsimd_i8_t u8[1536];\n    simsimd_f64_t f64s[1536];\n    simsimd_f32_t f32s[1536];\n    simsimd_f16_t f16s[1536];\n    simsimd_bf16_t bf16s[1536];\n    simsimd_distance_t distance;\n\n    // Cosine distance between two vectors\n    simsimd_cos_i8(i8s, i8s, 1536, \u0026distance);\n    simsimd_cos_u8(u8s, u8s, 1536, \u0026distance);\n    simsimd_cos_f16(f16s, f16s, 1536, \u0026distance);\n    simsimd_cos_f32(f32s, f32s, 1536, \u0026distance);\n    simsimd_cos_f64(f64s, f64s, 1536, \u0026distance);\n    simsimd_cos_bf16(bf16s, bf16s, 1536, \u0026distance);\n    \n    // Euclidean distance between two vectors\n    simsimd_l2sq_i8(i8s, i8s, 1536, \u0026distance);\n    simsimd_l2sq_u8(u8s, u8s, 1536, \u0026distance);\n    simsimd_l2sq_f16(f16s, f16s, 1536, \u0026distance);\n    simsimd_l2sq_f32(f32s, f32s, 1536, \u0026distance);\n    simsimd_l2sq_f64(f64s, f64s, 1536, \u0026distance);\n    simsimd_l2sq_bf16(bf16s, bf16s, 1536, \u0026distance);\n\n    return 0;\n}\n```\n\n### Dot-Products: Inner and Complex Inner Products\n\n```c\n#include \u003csimsimd/simsimd.h\u003e\n\nint main() {\n    // SimSIMD provides \"sized\" type-aliases without relying on `stdint.h`\n    simsimd_i8_t i8[1536];\n    simsimd_i8_t u8[1536];\n    simsimd_f16_t f16s[1536];\n    simsimd_f32_t f32s[1536];\n    simsimd_f64_t f64s[1536];\n    simsimd_bf16_t bf16s[1536];\n    simsimd_distance_t product;\n\n    // Inner product between two real vectors\n    simsimd_dot_i8(i8s, i8s, 1536, \u0026product);\n    simsimd_dot_u8(u8s, u8s, 1536, \u0026product);\n    simsimd_dot_f16(f16s, f16s, 1536, \u0026product);\n    simsimd_dot_f32(f32s, f32s, 1536, \u0026product);\n    simsimd_dot_f64(f64s, f64s, 1536, \u0026product);\n    simsimd_dot_bf16(bf16s, bf16s, 1536, \u0026product);\n\n    // SimSIMD provides complex types with `real` and `imag` fields\n    simsimd_f64c_t f64s[768];\n    simsimd_f32c_t f32s[768];\n    simsimd_f16c_t f16s[768];\n    simsimd_bf16c_t bf16s[768];\n    simsimd_distance_t products[2]; // real and imaginary parts\n\n    // Complex inner product between two vectors\n    simsimd_dot_f16c(f16cs, f16cs, 768, \u0026products[0]);\n    simsimd_dot_f32c(f32cs, f32cs, 768, \u0026products[0]);\n    simsimd_dot_f64c(f64cs, f64cs, 768, \u0026products[0]);\n    simsimd_dot_bf16c(bf16cs, bf16cs, 768, \u0026products[0]);\n\n    // Complex conjugate inner product between two vectors\n    simsimd_vdot_f16c(f16cs, f16cs, 768, \u0026products[0]);\n    simsimd_vdot_f32c(f32cs, f32cs, 768, \u0026products[0]);\n    simsimd_vdot_f64c(f64cs, f64cs, 768, \u0026products[0]);\n    simsimd_vdot_bf16c(bf16cs, bf16cs, 768, \u0026products[0]);\n    return 0;\n}\n```\n\n### Binary Distances: Hamming and Jaccard Distances\n\n```c\n#include \u003csimsimd/simsimd.h\u003e\n\nint main() {\n    simsimd_b8_t b8s[1536 / 8]; // 8 bits per word\n    simsimd_distance_t distance;\n    simsimd_hamming_b8(b8s, b8s, 1536 / 8, \u0026distance);\n    simsimd_jaccard_b8(b8s, b8s, 1536 / 8, \u0026distance);\n    return 0;\n}\n```\n\n### Probability Distributions: Jensen-Shannon and Kullback-Leibler Divergences\n\n```c\n#include \u003csimsimd/simsimd.h\u003e\n\nint main() {\n    simsimd_f64_t f64s[1536];\n    simsimd_f32_t f32s[1536];\n    simsimd_f16_t f16s[1536];\n    simsimd_distance_t divergence;\n\n    // Jensen-Shannon divergence between two vectors\n    simsimd_js_f16(f16s, f16s, 1536, \u0026divergence);\n    simsimd_js_f32(f32s, f32s, 1536, \u0026divergence);\n    simsimd_js_f64(f64s, f64s, 1536, \u0026divergence);\n\n    // Kullback-Leibler divergence between two vectors\n    simsimd_kl_f16(f16s, f16s, 1536, \u0026divergence);\n    simsimd_kl_f32(f32s, f32s, 1536, \u0026divergence);\n    simsimd_kl_f64(f64s, f64s, 1536, \u0026divergence);\n    return 0;\n}\n```\n\n### Half-Precision Floating-Point Numbers\n\nIf you aim to utilize the `_Float16` functionality with SimSIMD, ensure your development environment is compatible with C 11.\nFor other SimSIMD functionalities, C 99 compatibility will suffice.\nTo explicitly disable half-precision support, define the following macro before imports:\n\n```c\n#define SIMSIMD_NATIVE_F16 0 // or 1\n#define SIMSIMD_NATIVE_BF16 0 // or 1\n#include \u003csimsimd/simsimd.h\u003e\n```\n\n### Compilation Settings and Debugging\n\n`SIMSIMD_DYNAMIC_DISPATCH`:\n\n\u003e By default, SimSIMD is a header-only library.\n\u003e But if you are running on different generations of devices, it makes sense to pre-compile the library for all supported generations at once, and dispatch at runtime.\n\u003e This flag does just that and is used to produce the `simsimd.so` shared library, as well as the Python and other bindings.\n\nFor Arm: `SIMSIMD_TARGET_NEON`, `SIMSIMD_TARGET_SVE`, `SIMSIMD_TARGET_SVE2`, `SIMSIMD_TARGET_NEON_F16`, `SIMSIMD_TARGET_SVE_F16`, `SIMSIMD_TARGET_NEON_BF16`, `SIMSIMD_TARGET_SVE_BF16`.\nFor x86: (`SIMSIMD_TARGET_HASWELL`, `SIMSIMD_TARGET_SKYLAKE`, `SIMSIMD_TARGET_ICE`, `SIMSIMD_TARGET_GENOA`, `SIMSIMD_TARGET_SAPPHIRE`, `SIMSIMD_TARGET_TURIN`, `SIMSIMD_TARGET_SIERRA`. \n\n\u003e By default, SimSIMD automatically infers the target architecture and pre-compiles as many kernels as possible.\n\u003e In some cases, you may want to explicitly disable some of the kernels.\n\u003e Most often it's due to compiler support issues, like the lack of some recent intrinsics or low-precision numeric types.\n\u003e In other cases, you may want to disable some kernels to speed up the compilation process and trim the binary size.\n\n`SIMSIMD_SQRT`, `SIMSIMD_RSQRT`, `SIMSIMD_LOG`:\n\n\u003e By default, for __non__-SIMD backends, SimSIMD may use `libc` functions like `sqrt` and `log`.\n\u003e Those are generally very accurate, but slow, and introduce a dependency on the C standard library.\n\u003e To avoid that you can override those definitions with your custom implementations, like: `#define SIMSIMD_RSQRT(x) (1 / sqrt(x))`.\n\n## Algorithms \u0026 Design Decisions 📚\n\nIn general there are a few principles that SimSIMD follows:\n\n- Avoid loop unrolling.\n- Never allocate memory.\n- Never throw exceptions or set `errno`.\n- Keep all function arguments the size of the pointer.\n- Avoid returning from public interfaces, use out-arguments instead.\n- Don't over-optimize for old CPUs and single- and double-precision floating-point numbers.\n- Prioritize mixed-precision and integer operations, and new ISA extensions.\n- Prefer saturated arithmetic and avoid overflows.\n\nPossibly, in the future:\n\n- Best effort computation silencing `NaN` components in low-precision inputs. \n- Detect overflows and report the distance with a \"signaling\" `NaN`.\n\nLast, but not the least - don't build unless there is a demand for it.\nSo if you have a specific use-case, please open an issue or a pull request, and ideally, bring in more users with similar needs.\n\n### Cosine Similarity, Reciprocal Square Root, and Newton-Raphson Iteration\n\nThe cosine similarity is the most common and straightforward metric used in machine learning and information retrieval.\nInterestingly, there are multiple ways to shoot yourself in the foot when computing it.\nThe cosine similarity is the inverse of the cosine distance, which is the cosine of the angle between two vectors.\n\n```math\n\\text{CosineSimilarity}(a, b) = \\frac{a \\cdot b}{\\|a\\| \\cdot \\|b\\|}\n```\n\n```math\n\\text{CosineDistance}(a, b) = 1 - \\frac{a \\cdot b}{\\|a\\| \\cdot \\|b\\|}\n```\n\nIn NumPy terms, SimSIMD implementation is similar to:\n\n```python\nimport numpy as np\n\ndef cos_numpy(a: np.ndarray, b: np.ndarray) -\u003e float:\n    ab, a2, b2 = np.dot(a, b), np.dot(a, a), np.dot(b, b) # Fused in SimSIMD\n    if a2 == 0 and b2 == 0: result = 0                    # Same in SciPy\n    elif ab == 0: result = 1                              # Division by zero error in SciPy\n    else: result = 1 - ab / (sqrt(a2) * sqrt(b2))         # Bigger rounding error in SciPy\n    return result\n```\n\nIn SciPy, however, the cosine distance is computed as `1 - ab / np.sqrt(a2 * b2)`.\nIt handles the edge case of a zero and non-zero argument pair differently, resulting in a division by zero error.\nIt's not only less efficient, but also less accurate, given how the reciprocal square roots are computed.\nThe C standard library provides the `sqrt` function, which is generally very accurate, but slow.\nThe `rsqrt` in-hardware implementations are faster, but have different accuracy characteristics.\n\n- SSE `rsqrtps` and AVX `vrsqrtps`: $1.5 \\times 2^{-12}$ maximal relative error.\n- AVX-512 `vrsqrt14pd` instruction: $2^{-14}$ maximal relative error.\n- NEON `frsqrte` instruction has no documented error bounds, but [can be][arm-rsqrt] $2^{-3}$.\n\n[arm-rsqrt]: https://gist.github.com/ashvardanian/5e5cf585d63f8ab6d240932313c75411\n\nTo overcome the limitations of the `rsqrt` instruction, SimSIMD uses the Newton-Raphson iteration to refine the initial estimate for high-precision floating-point numbers.\nIt can be defined as:\n\n```math\nx_{n+1} = x_n \\cdot (3 - x_n \\cdot x_n) / 2\n```\n\nOn 1536-dimensional inputs on Intel Sapphire Rapids CPU a single such iteration can result in a 2-3 orders of magnitude relative error reduction:\n\n| Datatype   |         NumPy Error | SimSIMD w/out Iteration |             SimSIMD |\n| :--------- | ------------------: | ----------------------: | ------------------: |\n| `bfloat16` | 1.89e-08 ± 1.59e-08 |     3.07e-07 ± 3.09e-07 | 3.53e-09 ± 2.70e-09 |\n| `float16`  | 1.67e-02 ± 1.44e-02 |     2.68e-05 ± 1.95e-05 | 2.02e-05 ± 1.39e-05 |\n| `float32`  | 2.21e-08 ± 1.65e-08 |     3.47e-07 ± 3.49e-07 | 3.77e-09 ± 2.84e-09 |\n| `float64`  | 0.00e+00 ± 0.00e+00 |     3.80e-07 ± 4.50e-07 | 1.35e-11 ± 1.85e-11 |\n\n### Curved Spaces, Mahalanobis Distance, and Bilinear Quadratic Forms\n\nThe Mahalanobis distance is a generalization of the Euclidean distance, which takes into account the covariance of the data.\nIt's very similar in its form to the bilinear form, which is a generalization of the dot product.\n\n```math\n\\text{BilinearForm}(a, b, M) = a^T M b\n```\n\n```math\n\\text{Mahalanobis}(a, b, M) = \\sqrt{(a - b)^T M^{-1} (a - b)}\n```\n\nBilinear Forms can be seen as one of the most important linear algebraic operations, surprisingly missing in BLAS and LAPACK.\nThey are versatile and appear in various domains:\n\n- In Quantum Mechanics, the expectation value of an observable $A$ in a state $\\psi$ is given by $\\langle \\psi | A | \\psi \\rangle$, which is a bilinear form.\n- In Machine Learning, in Support Vector Machines (SVMs), bilinear forms define kernel functions that measure similarity between data points.\n- In Differential Geometry, the metric tensor, which defines distances and angles on a manifold, is a bilinear form on the tangent space.\n- In Economics, payoff functions in certain Game Theoretic problems can be modeled as bilinear forms of players' strategies.\n- In Physics, interactions between electric and magnetic fields can be expressed using bilinear forms.\n\nBroad applications aside, the lack of a specialized primitive for bilinear forms in BLAS and LAPACK means significant performance overhead.\nA $vector * matrix * vector$ product is a scalar, whereas its constituent parts ($vector * matrix$ and $matrix * vector$) are vectors:\n\n- They need memory to be stored in: $O(n)$ allocation.\n- The data will be written to memory and read back, wasting CPU cycles.\n\nSimSIMD doesn't produce intermediate vector results, like `a @ M @ b`, but computes the bilinear form directly.\n\n### Set Intersection, Galloping, and Binary Search\n\nThe set intersection operation is generally defined as the number of elements that are common between two sets, represented as sorted arrays of integers.\nThe most common way to compute it is a linear scan:\n\n```c\nsize_t intersection_size(int *a, int *b, size_t n, size_t m) {\n    size_t i = 0, j = 0, count = 0;\n    while (i \u003c n \u0026\u0026 j \u003c m) {\n        if (a[i] \u003c b[j]) i++;\n        else if (a[i] \u003e b[j]) j++;\n        else i++, j++, count++;\n    }\n    return count;\n}\n```\n\nAlternatively, one can use the binary search to find the elements in the second array that are present in the first one.\nOn every step the checked region of the second array is halved, which is called the _galloping search_.\nIt's faster, but only when large arrays of very different sizes are intersected.\nThird approach is to use the SIMD instructions to compare multiple elements at once:\n\n- Using string-intersection instructions on x86, like `pcmpestrm`.\n- Using integer-intersection instructions in AVX-512, like `vp2intersectd`.\n- Using vanilla equality checks present in all SIMD instruction sets.\n\nAfter benchmarking, the last approach was chosen, as it's the most flexible and often the fastest.\n\n### Complex Dot Products, Conjugate Dot Products, and Complex Numbers\n\nComplex dot products are a generalization of the dot product to complex numbers.\nThey are supported by most BLAS packages, but almost never in mixed precision.\nSimSIMD defines `dot` and `vdot` kernels as:\n\n```math\n\\text{dot}(a, b) = \\sum_{i=0}^{n-1} a_i \\cdot b_i\n```\n\n```math\n\\text{vdot}(a, b) = \\sum_{i=0}^{n-1} a_i \\cdot \\bar{b_i}\n```\n\nWhere $\\bar{b_i}$ is the complex conjugate of $b_i$.\nPutting that into Python code for scalar arrays:\n    \n```python\ndef dot(a: List[number], b: List[number]) -\u003e number:\n    a_real, a_imaginary = a[0::2], a[1::2]\n    b_real, b_imaginary = b[0::2], b[1::2]\n    ab_real, ab_imaginary = 0, 0\n    for ar, ai, br, bi in zip(a_real, a_imaginary, b_real, b_imaginary):\n        ab_real += ar * br - ai * bi\n        ab_imaginary += ar * bi + ai * br\n    return ab_real, ab_imaginary\n\ndef vdot(a: List[number], b: List[number]) -\u003e number:\n    a_real, a_imaginary = a[0::2], a[1::2]\n    b_real, b_imaginary = b[0::2], b[1::2]\n    ab_real, ab_imaginary = 0, 0\n    for ar, ai, br, bi in zip(a_real, a_imaginary, b_real, b_imaginary):\n        ab_real += ar * br + ai * bi\n        ab_imaginary += ar * bi - ai * br\n    return ab_real, ab_imaginary\n```\n\n### Logarithms in Kullback-Leibler \u0026 Jensen–Shannon Divergences\n\nThe Kullback-Leibler divergence is a measure of how one probability distribution diverges from a second, expected probability distribution.\nJensen-Shannon divergence is a symmetrized and smoothed version of the Kullback-Leibler divergence, which can be used as a distance metric between probability distributions.\n\n```math\n\\text{KL}(P || Q) = \\sum_{i} P(i) \\log \\frac{P(i)}{Q(i)}\n```\n\n```math\n\\text{JS}(P, Q) = \\frac{1}{2} \\text{KL}(P || M) + \\frac{1}{2} \\text{KL}(Q || M), M = \\frac{P + Q}{2}\n```\n\nBoth functions are defined for non-negative numbers, and the logarithm is a key part of their computation.\n\n### Mixed Precision in Fused-Multiply-Add and Weighted Sums\n\nThe Fused-Multiply-Add (FMA) operation is a single operation that combines element-wise multiplication and addition with different scaling factors.\nThe Weighted Sum is it's simplified variant without element-wise multiplication.\n\n```math\n\\text{FMA}_i(A, B, C, \\alpha, \\beta) = \\alpha \\cdot A_i \\cdot B_i + \\beta \\cdot C_i\n```\n\n```math\n\\text{WSum}_i(A, B, \\alpha, \\beta) = \\alpha \\cdot A_i + \\beta \\cdot B_i\n```\n\nIn NumPy terms, the implementation may look like:\n\n```py\nimport numpy as np\ndef wsum(A: np.ndarray, B: np.ndarray, /, Alpha: float, Beta: float) -\u003e np.ndarray:\n    assert A.dtype == B.dtype, \"Input types must match and affect the output style\"\n    return (Alpha * A + Beta * B).astype(A.dtype)\ndef fma(A: np.ndarray, B: np.ndarray, C: np.ndarray, /, Alpha: float, Beta: float) -\u003e np.ndarray:\n    assert A.dtype == B.dtype and A.dtype == C.dtype, \"Input types must match and affect the output style\"\n    return (Alpha * A * B + Beta * C).astype(A.dtype)\n```\n\nThe tricky part is implementing those operations in mixed precision, where the scaling factors are of different precision than the input and output vectors.\nSimSIMD uses double-precision floating-point scaling factors for any input and output precision, including `i8` and `u8` integers and `f16` and `bf16` floats.\nDepending on the generation of the CPU, given native support for `f16` addition and multiplication, the `f16` temporaries are used for `i8` and `u8` multiplication, scaling, and addition.\nFor `bf16`, native support is generally limited to dot-products with subsequent partial accumulation, which is not enough for the FMA and WSum operations, so `f32` is used as a temporary.\n\n### Auto-Vectorization \u0026 Loop Unrolling\n\nOn the Intel Sapphire Rapids platform, SimSIMD was benchmarked against auto-vectorized code using GCC 12.\nGCC handles single-precision `float` but might not be the best choice for `int8` and `_Float16` arrays, which have been part of the C language since 2011.\n\n| Kind                      | GCC 12 `f32` | GCC 12 `f16` | SimSIMD `f16` | `f16` improvement |\n| :------------------------ | -----------: | -----------: | ------------: | ----------------: |\n| Inner Product             |    3,810 K/s |      192 K/s |     5,990 K/s |          __31 x__ |\n| Cosine Distance           |    3,280 K/s |      336 K/s |     6,880 K/s |          __20 x__ |\n| Euclidean Distance ²      |    4,620 K/s |      147 K/s |     5,320 K/s |          __36 x__ |\n| Jensen-Shannon Divergence |    1,180 K/s |       18 K/s |     2,140 K/s |         __118 x__ |\n\n### Dynamic Dispatch\n\nMost popular software is precompiled and distributed with fairly conservative CPU optimizations, to ensure compatibility with older hardware.\nDatabase Management platforms, like ClickHouse, and Web Browsers, like Google Chrome,need to run on billions of devices, and they can't afford to be picky about the CPU features.\nFor such users SimSIMD provides a dynamic dispatch mechanism, which selects the most advanced micro-kernel for the current CPU at runtime.\n\n\u003ctable\u003e\n  \u003ctr\u003e\n    \u003cth\u003eSubset\u003c/th\u003e\n    \u003cth\u003eF\u003c/th\u003e\n    \u003cth\u003eCD\u003c/th\u003e\n    \u003cth\u003eER\u003c/th\u003e\n    \u003cth\u003ePF\u003c/th\u003e\n    \u003cth\u003e4FMAPS\u003c/th\u003e\n    \u003cth\u003e4VNNIW\u003c/th\u003e\n    \u003cth\u003eVPOPCNTDQ\u003c/th\u003e\n    \u003cth\u003eVL\u003c/th\u003e\n    \u003cth\u003eDQ\u003c/th\u003e\n    \u003cth\u003eBW\u003c/th\u003e\n    \u003cth\u003eIFMA\u003c/th\u003e\n    \u003cth\u003eVBMI\u003c/th\u003e\n    \u003cth\u003eVNNI\u003c/th\u003e\n    \u003cth\u003eBF16\u003c/th\u003e\n    \u003cth\u003eVBMI2\u003c/th\u003e\n    \u003cth\u003eBITALG\u003c/th\u003e\n    \u003cth\u003eVPCLMULQDQ\u003c/th\u003e\n    \u003cth\u003eGFNI\u003c/th\u003e\n    \u003cth\u003eVAES\u003c/th\u003e\n    \u003cth\u003eVP2INTERSECT\u003c/th\u003e\n    \u003cth\u003eFP16\u003c/th\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\u003ca href=\"https://en.wikipedia.org/wiki/Xeon_Phi#Knights_Landing\"\u003eKnights Landing\u003c/a\u003e (Xeon Phi x200, 2016)\u003c/td\u003e\n    \u003ctd colspan=\"2\" rowspan=\"9\" style=\"background:#9EFF9E;color:black;vertical-align:middle;text-align:center;\"\u003eYes\u003c/td\u003e\n    \u003ctd colspan=\"2\" rowspan=\"2\" style=\"background:#9EFF9E;color:black;vertical-align:middle;text-align:center;\"\u003eYes\u003c/td\u003e\n    \u003ctd colspan=\"17\" style=\"background:#FFC7C7;color:black;vertical-align:middle;text-align:center;\"\u003eNo\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\u003ca href=\"https://en.wikipedia.org/wiki/Xeon_Phi#Knights_Mill\"\u003eKnights Mill\u003c/a\u003e (Xeon Phi x205, 2017)\u003c/td\u003e\n    \u003ctd colspan=\"3\" style=\"background:#9EFF9E;color:black;vertical-align:middle;text-align:center;\"\u003eYes\u003c/td\u003e\n    \u003ctd colspan=\"14\" style=\"background:#FFC7C7;color:black;vertical-align:middle;text-align:center;\"\u003eNo\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\n      \u003ca href=\"https://en.wikipedia.org/wiki/Skylake_(microarchitecture)#Skylake-SP_(14_nm)_Scalable_Performance\"\u003eSkylake-SP\u003c/a\u003e, \n      \u003ca href=\"https://en.wikipedia.org/wiki/Skylake_(microarchitecture)#Mainstream_desktop_processors\"\u003eSkylake-X\u003c/a\u003e (2017)\n    \u003c/td\u003e\n    \u003ctd colspan=\"4\" rowspan=\"11\" style=\"background:#FFC7C7;color:black;vertical-align:middle;text-align:center;\"\u003eNo\u003c/td\u003e\n    \u003ctd rowspan=\"4\" style=\"background:#FFC7C7;color:black;vertical-align:middle;text-align:center;\"\u003eNo\u003c/td\u003e\n    \u003ctd colspan=\"3\" rowspan=\"4\" style=\"background:#9EFF9E;color:black;vertical-align:middle;text-align:center;\"\u003eYes\u003c/td\u003e\n    \u003ctd colspan=\"11\" style=\"background:#FFC7C7;color:black;vertical-align:middle;text-align:center;\"\u003eNo\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\u003ca href=\"https://en.wikipedia.org/wiki/Cannon_Lake_(microarchitecture)\"\u003eCannon Lake\u003c/a\u003e (2018)\u003c/td\u003e\n    \u003ctd colspan=\"2\" style=\"background:#9EFF9E;color:black;vertical-align:middle;text-align:center;\"\u003eYes\u003c/td\u003e\n    \u003ctd colspan=\"9\" style=\"background:#FFC7C7;color:black;vertical-align:middle;text-align:center;\"\u003eNo\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\u003ca href=\"https://en.wikipedia.org/wiki/Cascade_Lake_(microarchitecture)\"\u003eCascade Lake\u003c/a\u003e (2019)\u003c/td\u003e\n    \u003ctd colspan=\"2\" rowspan=\"2\" style=\"background:#FFC7C7;color:black;vertical-align:middle;text-align:center;\"\u003eNo\u003c/td\u003e\n    \u003ctd rowspan=\"2\" style=\"background:#9EFF9E;color:black;vertical-align:middle;text-align:center;\"\u003eYes\u003c/td\u003e\n    \u003ctd colspan=\"8\" style=\"background:#FFC7C7;color:black;vertical-align:middle;text-align:center;\"\u003eNo\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\u003ca href=\"https://en.wikipedia.org/wiki/Cooper_Lake_(microarchitecture)\"\u003eCooper Lake\u003c/a\u003e (2020)\u003c/td\u003e\n    \u003ctd style=\"background:#9EFF9E;color:black;vertical-align:middle;text-align:center;\"\u003eYes\u003c/td\u003e\n    \u003ctd colspan=\"7\" style=\"background:#FFC7C7;color:black;vertical-align:middle;text-align:center;\"\u003eNo\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\u003ca href=\"https://en.wikipedia.org/wiki/Ice_Lake_(microarchitecture)\"\u003eIce Lake\u003c/a\u003e (2019)\u003c/td\u003e\n    \u003ctd colspan=\"7\" rowspan=\"3\" style=\"background:#9EFF9E;color:black;vertical-align:middle;text-align:center;\"\u003eYes\u003c/td\u003e\n    \u003ctd rowspan=\"3\" style=\"background:#FFC7C7;color:black;vertical-align:middle;text-align:center;\"\u003eNo\u003c/td\u003e\n    \u003ctd colspan=\"5\" rowspan=\"3\" style=\"background:#9EFF9E;color:black;vertical-align:middle;text-align:center;\"\u003eYes\u003c/td\u003e\n    \u003ctd colspan=\"2\" style=\"background:#FFC7C7;color:black;vertical-align:middle;text-align:center;\"\u003eNo\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\u003ca href=\"https://en.wikipedia.org/wiki/Tiger_Lake_(microarchitecture)\"\u003eTiger Lake\u003c/a\u003e (2020)\u003c/td\u003e\n    \u003ctd style=\"background:#9EFF9E;color:black;vertical-align:middle;text-align:center;\"\u003eYes\u003c/td\u003e\n    \u003ctd style=\"background:#FFC7C7;color:black;vertical-align:middle;text-align:center;\"\u003eNo\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\u003ca href=\"https://en.wikipedia.org/wiki/Rocket_Lake\"\u003eRocket Lake\u003c/a\u003e (2021)\u003c/td\u003e\n    \u003ctd colspan=\"2\" style=\"background:#FFC7C7;color:black;vertical-align:middle;text-align:center;\"\u003eNo\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\u003ca href=\"https://en.wikipedia.org/wiki/Alder_Lake\"\u003eAlder Lake\u003c/a\u003e (2021)\u003c/td\u003e\n    \u003ctd colspan=\"2\" style=\"background:#FFB;color:black;vertical-align:middle;text-align:center;\"\u003ePartial\u003c/td\u003e\n    \u003ctd colspan=\"15\" style=\"background:#FFB;color:black;vertical-align:middle;text-align:center;\"\u003ePartial\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\u003ca href=\"https://en.wikipedia.org/wiki/Zen_4\"\u003eZen 4\u003c/a\u003e (2022)\u003c/td\u003e\n    \u003ctd colspan=\"2\" rowspan=\"3\" style=\"background:#9EFF9E;color:black;vertical-align:middle;text-align:center;\"\u003eYes\u003c/td\u003e\n    \u003ctd colspan=\"13\" rowspan=\"3\" style=\"background:#9EFF9E;color:black;vertical-align:middle;text-align:center;\"\u003eYes\u003c/td\u003e\n    \u003ctd colspan=\"2\" style=\"background:#FFC7C7;color:black;vertical-align:middle;text-align:center;\"\u003eNo\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\u003ca href=\"https://en.wikipedia.org/wiki/Sapphire_Rapids_(microprocessor)\"\u003eSapphire Rapids\u003c/a\u003e (2023)\u003c/td\u003e\n    \u003ctd style=\"background:#FFC7C7;color:black;vertical-align:middle;text-align:center;\"\u003eNo\u003c/td\u003e\n    \u003ctd style=\"background:#9EFF9E;color:black;vertical-align:middle;text-align:center;\"\u003eYes\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e\u003ca href=\"https://en.wikipedia.org/wiki/Zen_5\"\u003eZen 5\u003c/a\u003e (2024)\u003c/td\u003e\n    \u003ctd style=\"background:#9EFF9E;color:black;vertical-align:middle;text-align:center;\"\u003eYes\u003c/td\u003e\n    \u003ctd style=\"background:#FFC7C7;color:black;vertical-align:middle;text-align:center;\"\u003eNo\u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\nYou can compile SimSIMD on an old CPU, like Intel Haswell, and run it on a new one, like AMD Genoa, and it will automatically use the most advanced instructions available.\nReverse is also true, you can compile on a new CPU and run on an old one, and it will automatically fall back to the most basic instructions.\nMoreover, the very first time you prove for CPU capabilities with `simsimd_capabilities()`, it initializes the dynamic dispatch mechanism, and all subsequent calls will be faster and won't face race conditions in multi-threaded environments.\n\n## Target Specific Backends\n\nSimSIMD exposes all kernels for all backends, and you can select the most advanced one for the current CPU without relying on built-in dispatch mechanisms.\nThat's handy for testing and benchmarking, but also in case you want to dispatch a very specific kernel for a very specific CPU, bypassing SimSIMD assignment logic.\nAll of the function names follow the same pattern: `simsimd_{function}_{type}_{backend}`.\n\n- The backend can be `serial`, `haswell`, `skylake`, `ice`, `genoa`, `sapphire`, `turin`, `neon`, or `sve`.\n- The type can be `f64`, `f32`, `f16`, `bf16`, `f64c`, `f32c`, `f16c`, `bf16c`, `i8`, or `b8`.\n- The function can be `dot`, `vdot`, `cos`, `l2sq`, `hamming`, `jaccard`, `kl`, `js`, or `intersect`.\n\nTo avoid hard-coding the backend, you can use the `simsimd_kernel_punned_t` to pun the function pointer and the `simsimd_capabilities` function to get the available backends at runtime.\nTo match all the function names, consider a RegEx:\n\n```regex\nSIMSIMD_PUBLIC void simsimd_\\w+_\\w+_\\w+\\(\n```\n\nOn Linux, you can use the following command to list all unique functions:\n\n```sh\n$ grep -oP 'SIMSIMD_PUBLIC void simsimd_\\w+_\\w+_\\w+\\(' include/simsimd/*.h | sort | uniq\n\u003e include/simsimd/binary.h:SIMSIMD_PUBLIC void simsimd_hamming_b8_haswell(\n\u003e include/simsimd/binary.h:SIMSIMD_PUBLIC void simsimd_hamming_b8_ice(\n\u003e include/simsimd/binary.h:SIMSIMD_PUBLIC void simsimd_hamming_b8_neon(\n\u003e include/simsimd/binary.h:SIMSIMD_PUBLIC void simsimd_hamming_b8_serial(\n\u003e include/simsimd/binary.h:SIMSIMD_PUBLIC void simsimd_hamming_b8_sve(\n\u003e include/simsimd/binary.h:SIMSIMD_PUBLIC void simsimd_jaccard_b8_haswell(\n\u003e include/simsimd/binary.h:SIMSIMD_PUBLIC void simsimd_jaccard_b8_ice(\n\u003e include/simsimd/binary.h:SIMSIMD_PUBLIC void simsimd_jaccard_b8_neon(\n\u003e include/simsimd/binary.h:SIMSIMD_PUBLIC void simsimd_jaccard_b8_serial(\n\u003e include/simsimd/binary.h:SIMSIMD_PUBLIC void simsimd_jaccard_b8_sve(\n```\n","funding_links":[],"categories":["Math","C","Others","Libraries","Sdks \u0026 Libraries","Neural Network"],"sub_categories":["Data structures"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fashvardanian%2FSimSIMD","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fashvardanian%2FSimSIMD","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fashvardanian%2FSimSIMD/lists"}