MaxMath
MaxMath copied to clipboard
A C# SIMD math library for use with Unity only, substantially extending Unity.Mathematics by new types and functions, using Unity.Burst.
MaxMath
A C# SIMD math library for use with Unity only, supplementary to Unity.Mathematics using Unity.Burst.
It adds (s)byte, (u)short and (u)long SIMD vectors and matrices to the ones already provided by Unity.Mathematics. Almost all functions present in Unity.Mathematics have been transcribed to work with the new vector and matrix types in addition to many useful functions having been added.
Note:
- C Sharp Dev Tools (conditionally compiled runtime checks) is required. Unit tests for this library are included in this repository.
- This library utilizes Avx2 as often as possible. Optimized fallback procedures for Sse4 and Sse2 are included, aswell as a managed C# implementation. There are currently no plans for supporting ARM or other instruction sets in the future, although Burst/LLVM is generally good at vectorizing some of the code for them.
- The "float8" type does not support deterministic compilation across all platforms with Unity.Burst. Split up the vector into two "float4" vectors via the properties "myfloat8.v4_0" and "myfloat8.v4_4" to take advantage of deterministic compilation instead.
How To Use This Library
New Types
Integer
(S)Byte
(U)Short
(U)Int
(U)Long
Floating Point
Float
Half
Quarter (8-bit 1.3.4.-3 IEEE 754 floating point)
Random Number Generators
XOR-Shift
New Functions
Miscellaneous
Arithmetic
Bitwise Operations
Vector Operations
Interpolation and Geometry
Type Conversion
What Kind Of Performance To Expect
Highlights
-
Division and modulo operations of (s)byte and (u)short vectors by other vectors are implemented as either a long division algorithm ((s)byte32, (s)byte16 and (s)byte8 if not compiling for Avx2) or reciprocal multiplication after converting the vectors to float vectors (up to (s)byte8, all (u)short vectors) - it is very fast and, of course, 100% accurate!
-
This library uses Wojciech Mula's SIMD population count algorithm. You can count the amount of set bits of a contiguous block of memory very efficiently using either the (s)byte32 (Avx2) or (s)byte16 (Ssse3) type
Notes
- 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
How To Install This Library
Disclaimer: I firmly believe in open source - being able to copy/modify/understand other people's code is great :) I also want people to be able to step through code with a debugger. For these reasons I usually don't distribute DLLs.
- Download the package and unzip it into your "LocalPackages" folder, which is located at the root folder of your Unity project (where your "Assets" folder resides at).
- Start up Unity. Usually Unity detects new packages and will generate .meta files for you.
- In case that doesn't work, open up the package manager from within Unity and click on the '+' symbol at the upper left corner of the window, further clicking on "Add package from disk..." - "Add package from git URL" should also work.
- Locate the library's "package.json" file
- DONE!