Vecmat
A simple math and linear algebra library in C for 2D/3D graphics, machine learning, physics, and science.
- Runtime SIMD dispatch
- Modular backends, widths, features, and devices
- Device API (CPU, GPU, NPU, accelerator)
- Unified precision (fp64/fp32)
- Clip-space (OpenGL, Vulkan, D3D)
- Dense algebra and GEMM
- Sparse and dense solvers
- Integrators
Advance Physics Features
- Continuum strain measures — Right Cauchy-Green, Green-Lagrange, Almansi, and logarithmic strain from the stretch or the deformation gradient.
- Local equations of state — ideal gas, stiffened gas, Mie-Grüneisen on a linear Hugoniot, and Jones-Wilkins-Lee, with sound speed from the same derivative.
- One-cell hydro primitives — Newtonian viscous stress, von Neumann-Richtmyer artificial viscosity, Rusanov face flux, and a CFL time-step limit.
- SPH — cubic and Wendland kernels, linked-cell neighbors, artificial viscosity, and a fused pressure-plus-viscosity force.
- Radiation — Planck spectrum, angular quadrature, ray transport, and Rosseland diffusion.
- Geometry and tensors — closest point on a triangle, barycentric coordinates, triangle AABB, and Voigt-6 symmetric-tensor algebra.
- Array and SoA helpers — element-wise divide and rsqrt, reductions, orthorhombic periodics, and structure-of-arrays geometry.
Philosophy
Vecmat is a heartfelt ❤ love letter to the C programming language — showcasing that readability and ease-of-use shouldn't be second to performance or functionality.
Priorities
- Elegance, simplicity, and readability matter more than squeezing every cycle.
- Performance lives under the same names; it does not own the API.
Goals
- One common, easy-to-read API.
- Usability first, then performance. Default functions take and return values by copy, so call sites stay simple.
- Keep the public API stable. Speedups live behind the same names.
- Stay portable C11, easy to pull in with CMake (
FetchContentorfind_package). - Runtime SIMD dispatch so apps do not pass ISA flags.
What this library is not
- At the current state — not a complete replacement for GLM+OpenBLAS+PETSc stack.
- Not a graphics engine, not a physics engine, or an ocean / SPH / constraint product — but is built to support such products.
- No SSE on purpose. Pre-AVX x86 runs the scalar kernels.
Product contract
Four layers. A name that is not ready for one of them is experimental: it uses a vm_x_* prefix. -DVECMAT_EXPERIMENTAL=ON (off by default) builds those sources and defines VECMAT_EXPERIMENTAL, so <vecmat.h> declares them. The header can also be included on its own.
| Layer | What it is |
|---|---|
| Core | vector2/3/4, matrix2/3/4, quaternion, clip-space, transforms, easing. What called every frame. |
| Fast path | _ptr kernels, vm_cpu_*, runtime dispatch, vm_. Out-of-tree backends attach here. |
| Device | Buffers, one in-order queue, and a few ops. Device 0 is the CPU. Other devices call vm_. |
| Numerics extras | vm_, heap vm_, LU / QR / SVD / Cholesky, CSR / KSP, integrators, grid primitives. |
Include one header:
#include <vecmat.h>
Or, include individual functionality, types.h include config.h, and is included in the headers.
#include <vecmat/vec.h> #include <vecmat/mat.h> #include <vecmat/quat.h>
Core
- Default interfaces use value types and obvious names (
vector3,matrix4,quaternion). - Angles are radians on unsuffixed APIs. Write
VM_or call theDEG(90) _degsuffix at the human/config edge;VM_documents an already-radian literal.RAD(M_ PI_ 2) - Layout is column-major. Matrix products are
ABso(AB)v == A(Bv)(column vector on the right). Everymat*_mulbrief states that. - Components are
.x/.y/.z,m11,m21, …, or a flat.v[]. - BSD 3-Clause License.
- Tests use
unitest.handexcept.h.
Precision
- One library exports both float widths. Float symbols are
name32andname64(vec4_add32,vec4_add64). -DVECMAT_FLOAT_ABI=32or64omits the other set. Integer names andvm_cpu_*have no width suffix.- A vendor float entry point uses
VECMAT_. It pastesSYMBOL 32or64with the same rule asinclude/. When both widths are built, the f64 objects definevecmat/ abi.h VECMAT_ABI_REPLICAand skip integer bodies, so those unsuffixed symbols are emitted once. See Modules. - Call sites keep
vec4_andadd vector4. Those names are the 32-bit symbols unlessVECMAT_USE_F64is defined before the#include.vector4is that translation unit's width. - Integer width is configure-time (
vm_). It is separate from the float width.int_ t vm_reports which float widths are in the linked library.compiled_ float_ bits() vm_is non-zero when this translation unit's width is not one of them.abi_ mismatch() - 0.4.5 renumbers the
VM_CPU_*feature bits into per-family ranges (see Dispatch model). Rebuild modules and any code that stored a mask as a number.
Math types
- Float vectors: 2D, 3D, 4D (
vector2/vector3/vector4). - Integer vectors: same sizes (
vector2i/vector3i/vector4i). - Float and integer matrices: 2×2, 3×3, 4×4.
- Quaternions for rotation.
- Easing functions for animation-style interpolation (
vm_,smoothstep vm_, and the rest ofquadratic_ ease_ in easing.h). The unsuffixed names are deprecated wrappers for this release. - One-segment curves: quadratic and cubic Bézier, Hermite, and Catmull-Rom (
curve.h). - Clip-space presets for OpenGL (
RH_NO), Vulkan (RH_ZO) and Direct3D (LH_ZO).
Two ways to call everything
- By-value helpers for everyday code.
_ptrkernels for hot paths and SIMD. Those are what backends implement.
Fast path
The real work lives in _ptr functions (pointers in, pointers out). Scalar queries (length, dot, distance, angle, determinants, equality) keep the by-value function as the body. Their _ptr names are header adapters: same result, inputs passed by pointer, not exported kernels.
Selection order: registered backends (by priority) → SVE2 → SVE → NEON → AVX-512F → AVX2 (+FMA) → AVX → scalar
vm_ installs a complete _ptr table (vm_). Call it before the first vm_. The same pointer is idempotent; a table with a missing slot is rejected. Public code does not dlopen.
vm_cpu_init(); printf("compiled=%s runtime=%s selected=%s\n", vm_cpu_name(vm_cpu_compiled_features()), vm_cpu_name(vm_cpu_runtime_features()), vm_cpu_name(vm_cpu_selected_features()));
vm_ is thread-safe (C11 atomics, double-checked locking) and idempotent. Concurrent first-use of dispatched kernels is safe.
ISA matrix
| Backend | CMake flag | Default | Dispatched ops | CI label |
|---|---|---|---|---|
| Scalar | (always) | ON | full public _ptr set | run on every job (fallback) |
| AVX | VECMAT_ENABLE_AVX | ON on x86-64 | dispatched vec4 maps, mat4 mul / transpose / mul_vec, quat mul / normalize, GEMM ukernel | compiled + run on Linux / Windows x86-64 when the host has AVX |
| AVX2 (FMA) | VECMAT_ENABLE_AVX2 | ON on x86-64 | same set; lerp / mat4 / quat use FMA | compiled + typically selected on GitHub x86-64 runners |
| AVX-512F | VECMAT_ENABLE_AVX512F | ON on x86-64 | same set | compiled on x86-64 jobs; runtime only if the host has AVX-512F |
| NEON / ASIMD | VECMAT_ENABLE_NEON | ON on AArch64 | same dispatched set (in-tree Armv8-A ASIMD schedule) | compiled + run on Linux aarch64 and Windows ARM64 jobs |
| SVE | VECMAT_ENABLE_SVE | ON on AArch64 | same dispatched set; f64 walks svcntd() chunks | compile-tested only (permanent; no native or qemu run job) |
| SVE2 | VECMAT_ENABLE_SVE2 | ON on AArch64 | same dispatched set; f64 walks svcntd() chunks | compile-tested only (permanent; no native or qemu run job) |
The GEMM ukernel slot in that table stays 8×8. With no registered backend, the packer uses 12×8 on AVX2 and 16×14 on AVX-512 when that kernel was compiled. When N spans more than one NC panel, a packed A slice that fits in 4 MiB is reused across those panels. See Numerics extras.
x86 floor is AVX. Pre-AVX x86 runs scalar kernels. There is no SSE backend, and none will be added for completeness.
AVX2 also needs FMA. Its kernels are built with -mfma, so the tier is picked only when vm_ also has VM_ (CPUID.1:ECX bit 12 plus the OS YMM state AVX needs). A CPU or VM that hides FMA falls to the next tier: AVX for the dispatched set, scalar for the direct-call kernels (array, batch3, eos, sph). AVX-512F kernels are also built with -mfma, but every AVX-512F CPU has FMA3, so that tier checks only VM_; a hypervisor that masks FMA while exposing AVX-512F is unsupported. The vm_soa3_* AVX2 kernels emit no FMA and need only AVX2. VM_ is a feature bit, not a tier: vm_ never returns it and VECMAT_FORCE_ISA has no fma value. A forced avx2 or avx512f binds that tier without checking FMA, just as it does not check the ISA itself.
Supported means a green job with that label. A native run counts. A qemu run counts only when the label says qemu. SVE and SVE2 stay compile-tested, so they are not supported. Force-ISA jobs run scalar on every platform, avx and avx2 on x86-64, and neon on AArch64. avx512f runs only when the host advertises it; otherwise that job is compile-tested. VECMAT_FORCE_ISA does not make an uncompiled ISA selectable.
SVE and SVE2 kernels: SVE and SVE2 f64 vec4 / mat4 kernels cover four doubles in svcntd() chunks, so a 128-bit core takes two passes and a 256-bit core takes one. f64 quat_ uses the scalar formula: svtbl needs all four lanes in one register. MSVC has no SVE intrinsics, so those kernels are off on MSVC for both widths.
MSVC ARM64 jobs compile and run tests. VECMAT_FORCE_ISA (scalar, avx, avx2, avx512f, neon, sve, sve2) overrides the compiled/runtime pick when that ISA is compiled in. A registered backend still wins.
Optional CPU backends are not in the default library. They register through vm_ and can be left out of a build. The docs contain some examples:
GPUs, NPUs, and accelerators use the device API. Device 0 is the CPU. A device module registers with vm_. It is not a CPU feature bit and it does not fill vm_. See Devices.
Public modules live in vecmat-vendors. The recommended layout is a vendors/ checkout under this tree (optional submodule). Then -DVECMAT_MODULES=arm/fp16 is enough. If the repository is checked out beside the tree instead, pass -DVECMAT_MODULES_PATH=../vecmat-vendors. Each entry is a backend, a width, a feature, or a device. Private modules are loaded the same way from their own tree. The NEON row above is the in-tree Armv8-A schedule. Tests and benchmarks call vm_backend_modules_register() and do not include a backend header. A backend may append VECMAT_MODULE_TEST_SOURCES and VECMAT_MODULE_BENCH_SOURCES; those files live in the vendor tree. A device module builds its own library. The example is nvidia/ga10x in Modules.
Windows shared builds export with VEC_. Float exports are name32 / name64. Integer names and vm_cpu_* stay unsuffixed.
Numerics extras
Heap matrices, GEMM, sparse solvers, integrators, and the clip-space helpers are in Numerics extras. That page is the contract for GEMM tiles and packed-A reuse.
Documentation
Guides, also built as pages on the docs site:
API pages use the m.css Doxygen theme with a custom Dark Fire palette (doc/m-theme-dark-fire.css). doc/conf.py and doc/Doxyfile-mcss drive that pipeline. The published HTML is built from the public headers (include/ and include/vecmat/*.h); briefs and @param / @return live on the declarations, docs on implementations only in special cases that required or static functions.
Generate local docs with m.css
python3 -m venv .venv && source .venv/bin/activate python3 -m pip install jinja2 Pygments git clone --depth 1 https://github.com/mosra/m.css /tmp/m.css python3 /tmp/m.css/documentation/doxygen.py doc/conf.py
HTML lands in doc/html/. Doxygen writes XML to doc/xml/ first; both directories are git-ignored.
Stock Doxygen HTML
cd doc && doxygen Doxyfile
Relevant resources
CMake Integration
Source using
if(NOT TARGET vecmat::vecmat) include(FetchContent) FetchContent_Declare(vecmat GIT_REPOSITORY https://github.com/alkavan/vecmat.git GIT_TAG v0.5.2 ) FetchContent_MakeAvailable(vecmat) endif() target_link_libraries(my_app PRIVATE vecmat::vecmat)
Installed Package
find_package(vecmat 0.5 CONFIG REQUIRED) target_link_libraries(my_app PRIVATE vecmat::vecmat)
System integration / Out-of-source build and installation
cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug \ -DVECMAT_BUILD_TESTS=ON \ -DCMAKE_INSTALL_PREFIX="$HOME/.local" cmake --build build -j cmake --install build
Note: Use -DVECMAT_INSTALL=ON only when install rules were turned off or vecmat isn't top-level — and you still want cmake --install to install it.
Scalar precision flags
Integer width is selected when configuring the library and published on vecmat::vecmat. Float width is per translation unit: a BOTH library holds both symbol sets, and a TU picks the 64-bit aliases with VECMAT_USE_F64. -DVECMAT_USE_F64=ON applies that to this build's tests and benchmarks. It does not omit name32; pass -DVECMAT_FLOAT_ABI=64 for that.
Defaults (no flags): unsuffixed float names are the 32-bit symbols, vm_ is int32_t.
| CMake flag | Header macro | Effect |
|---|---|---|
-DVECMAT_FLOAT_ABI=BOTH/32/64 | VECMAT_HAVE_ABI_32/_64 | Which float symbol sets are in the .a / .so (default BOTH) |
-DVECMAT_USE_F64=ON | VECMAT_USE_F64 | This TU's unsuffixed float names alias name64 |
-DVECMAT_USE_INT8=ON | VECMAT_USE_INT8 | vm_ is int8_t |
-DVECMAT_USE_INT16=ON | VECMAT_USE_INT16 | vm_ is int16_t |
-DVECMAT_USE_INT32=ON | VECMAT_USE_INT32 | vm_ is int32_t |
The integer flags are mutually exclusive. CMake will error if more than one is ON. Integer width must match the library. Float width does not: a BOTH lib accepts both.
Configure from the command line:
cmake -S . -B build \ -DVECMAT_USE_F64=ON \ -DVECMAT_USE_INT16=ON \ -DVECMAT_BUILD_TESTS=ON
With FetchContent, set the cache variables before FetchContent_MakeAvailable:
set(VECMAT_USE_F64 ON CACHE BOOL "" FORCE) set(VECMAT_USE_INT16 ON CACHE BOOL "" FORCE) FetchContent_MakeAvailable(vecmat)
Without CMake, define the same macros yourself (compiler flag or before the library include):
cc -DVECMAT_USE_F64 -DVECMAT_USE_INT16 ...
#define VECMAT_USE_F64 #define VECMAT_USE_INT16 #include <vecmat.h>
Integer macros must match the linked library. VECMAT_USE_F64 is per TU.
Other CMake options
| CMake flag | Default | Effect |
|---|---|---|
VECMAT_RUNTIME_DISPATCH | ON, except MSVC | Build the ISA probes and the dispatched kernels. OFF leaves scalar bodies only. |
VECMAT_ENABLE_AVX / AVX2 / AVX512F | ON on x86-64 | Compile that kernel set. Ignored off x86-64. See the ISA matrix. |
VECMAT_ENABLE_NEON / SVE / SVE2 | ON on AArch64 | Compile that kernel set. SVE/SVE2 are ignored on MSVC. See the ISA matrix. |
VECMAT_MODULES | empty | Module paths such as arm/fp16, relative to vendors/ or VECMAT_MODULES_PATH. |
VECMAT_MODULES_PATH | empty | Extra roots when modules are not under vendors/. |
VECMAT_ENABLE_FP16 | OFF | IEEE binary16 and vm_. See doc/. Not a vm_ width. |
VECMAT_ENABLE_BF16 | OFF | bfloat16 and vm_. See doc/. Not a vm_ width. |
VECMAT_EXPERIMENTAL | OFF | Build vm_x_* sources and define VECMAT_EXPERIMENTAL. |
VECMAT_BUILD_TESTS | ON when Vecmat is the top-level project | Build vecmat_tests and vecmat_benchmarks. |
VECMAT_INSTALL | ON when Vecmat is the top-level project | Generate the install rules. |
VECMAT_BUILD_ABI_32 and VECMAT_BUILD_ABI_64 are derived from VECMAT_FLOAT_ABI. Do not set them. VECMAT_FORCE_ISA and VECMAT_GEMM_THREADS are environment variables, not CMake options.
Usage and Examples
- Basic usage — include, values, and an affine model matrix.
- Vector usage — constructors, slide / reflect, a particle step.
- Matrix usage — column-major layout,
mat4_, a camera.trs