com.mrunbelievable.maxmath
v3.0.0
Published
A supplementary SIMD math library to Unity.Mathematics, extending it to all C# numeric types while adding many new types and functions. Written entirely with hardware intrinsics, using Unity.Burst.
Downloads
227
Readme
MaxMath
MaxMath is the most powerful and extensive SIMD math library available to Unity developers. Built on top of Unity.Mathematics and utilizing Unity.Burst, it introduces the following key features:
- Support For All Primitive Data Types: MaxMath adds support for
(s)byte,(u)short, and(u)longvectors and matrices. These data types come with specialized overloads for all functions in Unity.Mathematics. Additionally, specializedRandom8/16/32/64/128types are available for efficient pseudo-random number generation. - Wider Vectors With Full Hardware Support: Vector types are expanded to 256 bits, enabling types like
byte32,short16,int8, andfloat8. This allows you to leverage the full potential of SIMD computation. - Many Additional Functions: MaxMath includes a massive library of mathematical functions not found in Unity.Mathematics, with about five times as many highly optimized functions at your disposal. Each function is fully documented with XML annotations. A full list is provided further below.
- Exotic Data Types: MaxMath introduces data types such as
(U)Int128(scalar only), 128-bitquadrupleprecision floats (scalar only), and 8-bitquarterprecision floats (in both scalar and vector forms). Additionally,Divider<T>offers highly optimized integer division operations, extending and outperforming specialized libraries like libdivide. - Written Entirely With Hardware Intrinsics: MaxMath guarantees optimal performance by utilizing specialized CPU instructions for both ARM and x86 ISAs, while abstracting these complexities away from the user entirely.
- Extends The Burst Compiler: MaxMath integrates deeply with Unity.Burst and LLVM, leveraging
Unity.Burst.CompilerServices.Constant.IsConstantExpression<T>()to include code typically only found in optimizing compilers. This functionality allows MaxMath to choose more optimized code paths at compile time, and users can influence this behavior via the optionalPromiseenum parameter available in many functions. - Easy To Use: MaxMath is just as easy to use as Unity.Mathematics. It supports features like
implicitandexplicittype conversions, making it seamless for you to use if you expect typical C# behavior of primitive types. - Extensive Test Coverage: MaxMath is backed by 250,000 lines of unit tests for its 750,000 lines of code, as well as
DEBUGonly runtime checks where appropriate, together ensuring it is production ready.
Why MaxMath over Unity.Mathematics?
Prior to version 3.0, MaxMath served as a supplementary library to Unity.Mathematics. Starting with 3.0, it is a drop-in replacement/wrapper for Unity.Mathematics, fully compatible while extending it with additional MaxMath functionality.
MaxMath builds on Unity.Mathematics with additional functionality and numerous performance improvements (See the 3.0 patch notes for the changes). Several bugs in Unity.Mathematics have not been fixed for multiple years, particularly with regards to half.
MaxMath is actively maintained, with recent releases and public GitHub Issues/Discussions for feedback. By comparison, Unity.Mathematics still shows a substantial amount of open issues and feature requests in its public repository, and its README notes that pull requests are not accepted.
Types



















Functions
Note: The function signatures (but not their names), parameter names, and descriptions shown below have been intentionally simplified to keep this catalog concise and easy to browse. They do not reflect the naming conventions or XML documentation used throughout MaxMath, which provide substantially more descriptive parameter names, detailed explanations, and full IntelliSense support in IDEs (see the example below).
This section is intended solely as a high-level overview of the available functionality rather than a replacement for the library's built-in documentation.
In addition, many functions have an optional enum Promise parameter for optimized code paths not listed here.
Each operation is available for all applicable scalar, vector, and matrix types.
An example for XML documentation used in MaxMath:

What Kind Of Performance To Expect

MaxMath was designed with performance as a primary objective from the very beginning. While it started as an extension of Unity.Mathematics to cover every C# primitive type - including integer vectors, matrices, and numerous additional mathematical functions - it has since evolved into a complete rewrite and replacement of it.
The 1.x development cycle focused on vectorizing virtually every operation and mathematical function. The 2.x cycle introduced architecture-specific implementations with optimized instruction selection and fallbacks across multiple SIMD instruction sets, initially targeting x86 (AVX2 → AVX → SSE4 → SSE2) before expanding to ARM NEON. The current 3.x cycle removes fundamental limitations inherited from Unity.Mathematics, such as inefficient boolean vector representations (and non-vectorized or poorly-vectorized algorithms), by introducing native SIMD masks internally while preserving a familiar boolean vector API.
Performance work has been an ongoing effort throughout every release. Nearly every function and operator has undergone repeated optimization with an emphasis on minimizing latency, reducing instruction count and code size, and generating predictable machine code. Whenever practical, implementations are written directly with SIMD intrinsics to retain precise control over the generated assembly and avoid compiler-dependent code generation.
Many operations employ state-of-the-art algorithms drawn from academic literature, while others are based on novel implementations developed specifically for MaxMath to achieve exceptionally efficient SIMD execution. Combined with extensive architecture-specific specialization and years of iterative optimization, the result is a library essentially generating hand-written machine code out-of-the-box while remaining entirely accessible through a high-level C# API.
Recommendations for Optimal Performance
- It is recommended, just like with Unity.Mathematics, to use vector types that use up an entire SIMD register (128 and 256 bits, respectively). LLVM has a very hard time optimizing code which does not follow this recommendation
- Wherever possible, it is adviced not to declare boolean vectors as temporary variables on the stack. MaxMath uses a 1:1 representation of SIMD masks for boolean data in the form of
ref structs namedmask[BITS]x[COLUMNS]x[ROWS]. They are implicitly convertible to and from boolean vectors and matrices and should therefore be invisible to the user and cannot leave the stack, so the expected behaviour from using the Unity.Mathematics and earlier MaxMath API is still guaranteed, yet converting to and from boolean types comes at a performance cost already present in both Unity.Mathematics (although rarer) and MaxMath. Since the mask types arepublicand have the exact same API as corresponding boolean types, you are free to use them instead, yet their obscurity through specialization opens up a larger surface area for programmer error. - One of the core strengths of MaxMath is that it provides operations whose implementations are better than what a compiler can synthesize from primitive operators; prefer using dedicated MaxMath operations over composing equivalent expressions manually. Many functions map to specialized SIMD instructions or architecture-specific implementations that cannot be recognized reliably by Burst or LLVM. If you are able to exploit specialized SIMD instructions for mathematical algorithms, consider submitting the resulting code to MaxMath as a pull-request. Otherwise you are very much welcome to open a feature request via the issue tracker.
#Migrating from Unity.Mathematics ⚠️ Back up your project before migrating.
Although the migration is usually straightforward, some serialized values, especially vector fields in the Inspector, may need to be rechecked afterwards.
For most projects, a simple find-and-replace is enough: replace Unity.Mathematics with MaxMath.
Sub-namespaces were merged into MaxMath directly. After replacing Unity.Mathematics with MaxMath, replace:
MaxMath.Geometry.Math → MaxMath.math
and
MaxMath.Geometry → MaxMath
A number of types, members, and Unity.Mathematics.math functions have been marked as obsolete and may need small code adjustments during migration in order to avoid warnings and improve performance.
Vector fields are exposed as ref properties in MaxMath. This means you cannot take their address in an unsafe context, and you must initialize the vector before assigning to its components either directly or using the out keyword.
Because fields are no longer exposed directly, serialized Unity.Mathematics vector values shown in the Inspector may be invalidated during migration. MaxMath uses custom property serialization for these types.
For best performance, avoid declaring bool{X} vectors directly, such as bool3 cmp = myVec3a > myVec3b. MaxMath uses optimized mask types behind the scenes. That code still works (bool vectors exist in MaxMath and are not marked as obsolete, all mask types are implicitly convertible to and from bool vectors and matrices), and keeping it as-is does not hurt performance compared to Unity.Mathematics - this would simply be an additional, sometimes very relevant performance optimization.
If you encountered another required step during migration, please open an issue to report it.
How To Install This Library
It is highly encouraged to use the Scoped Registries feature for installing MaxMath.
Installing using Scoped Registries:
- Open your Unity project.
- Go to Edit → Project Settings → Package Manager.
- Under Scoped Registries, click + to add a new registry.
- Enter the registry details:
- Click Save.
- Open Window → Package Manager.
- In the package list, select My Registries.
- Install MaxMath from the registry.
Why use a scoped registry?
- Easy updates – Receive new versions directly through Unity’s Package Manager without manual downloads.
- Version management – Switch, lock, or roll back versions cleanly using Unity’s built-in tooling.
- Cleaner projects – No need to store package files inside your repository or project folder.
- Dependency resolution – Unity automatically handles dependencies and compatibility.
- Team-friendly – Everyone on the project uses the same source and versions with minimal setup.
- Faster setup – Install in a few clicks instead of manually importing or maintaining local packages.
- No IDE clutter – The package source code does not appear in your IDE, keeping your workspace focused on your own project code.
Donations
If this repository has been valuable to your projects and you'd like to support my work, consider making a donation.


