Skip to content

v3.1.0

Latest

Choose a tag to compare

@MrUnbelievable92 MrUnbelievable92 released this 25 Aug 14:52
· 2 commits to master since this release
4c4722f

Known Issues

  • In order for MaxMath to replace/wrap Unity.Mathematics fully, compile time access to the current FloatMode, FloatPrecision, and OptimizeFor through Unity.Burst is required. These features are, for the most part, currently unsupported
  • Defining each possible parameter combination for math functions for implicitly convertible types, like for instance distance(float4, int4), turned out to be unfeasible. Generating code for float4 alone resulted in a 529MB .cs file with ~16 million lines of code. Explicit casts will be necessary in most cases.
  • RandomX.New can only be executed in a managed context

Fixes

  • fixed mask16x8 and mask8x8 constructors from smaller mask vectors not differentiating between managed and native execution contexts, often discarding upper boolean values in a managed context. These constructors are used often within MaxMath itself and thus errors cascaded through many areas of MaxMath.
  • fixed potential intermediate overflow within integer floormultiple, ceilmultiple, roundmultiple and truncmultiple and replaced non-floormultiple algorithms with more efficient ones (up to ~15% faster)
  • fixed quadruple.[Try]Parse ignoring leading + or -
  • fixed quadruple.[Try]Parse not rounding the least significant mantissa bit correctly in ~33% of all cases

Additions

  • added ceildiv and floordiv for all integer types, performing rounded integer division
  • added none and notall to the all and any family of vector evaluation functions
  • added isint for all floating point types, returning true if the argument is a finite integer with no fractional part
  • added ispow2 overloads for all floating point types, returning true if the argument is in { …, 2⁻², 2⁻¹, 2⁰, 2¹, 2², … }
  • added ispow2mag for all types, returning true if the argument is in { …, ±2⁻², ±2⁻¹, ±2⁰, ±2¹, ±2², … } for floating point types and in { ±2⁰, ±2¹, ±2², … } for integer types
  • added lngamma - the natural logarithm of the absolute value of math.gamma (without overflow of math.gamma(x) itself) - for float and double scalars and vectors as well as quadruple, both with and without the sign of the gamma function as an out parameter
  • added select overloads that blend between boolean vectors
  • added all missing quadruple overloads for any MaxMath.math function, namely pow, exp, exp2, exp10, log, log2, log10, sin, cos, sincos, tan, asin, acos, atan, atan2, sinh, cosh, tanh asinh, acosh, atanh, erf, erfc, gamma, hypot

Improvements

  • MaxMath's existing compile-time analysis has been extended with a nearly complete set of semantic postcondition annotations using Unity.Burst.CompilerServices.Hint.Assume(bool). All functions (including operators) now communicate mathematically proven properties — such as value ranges, arithmetic identities, known-bit information, and other invariants — back to Burst/LLVM, allowing compile-time analyses to propagate across otherwise opaque implementations. For example, custom integer division implementations are now exposed to the compiler as semantically equivalent to the built-in division operator, preserving identities such as dividend = quotient × divisor + remainder and remainder < divisor etc. Likewise, math functions such as intsqrt communicate their exact mathematical semantics and derived properties (value ranges, defining inequalities, etc.), enabling optimization opportunities that would otherwise be lost across abstraction boundaries. LLVM being able to implement this information in its optimization process has been proven to work as intended; it enables additional optimized code path selection, constant folding, dead-code elimination, and more aggressive optimization of chained MaxMath operations (without affecting runtime performance negatively whatsoever). Performance gains are expected to be selective but nevertheless sometimes substantial, especially in large blocks of code heavily utilizing MaxMath.
  • removed stackalloc usage from the intlog10, factorial and integer-to-quarter type conversion, instead relying on statically compiled switch-tables
  • fixed roundto[INTEGER_TYPE] for scalar and vector float and double arguments not constant folding compile time constant arguments
  • added mask (i.e. boolean vector) constructor overloads that pack two wider 256-bit masks into one 256-bit mask, reducing both latency and instruction count by ~50% and removed one constant 128-bit vector loaded from RAM
  • found the optimal semantic expression for (U)Int128 (and thus the internally used __UInt256__) << and >> operators understood by LLVM, removing 2 branches and reducing the latency down to 1 + the latency of double register shifts (usually 2 to 3 cycles). This optimization has cascading effects throughout MaxMath when the shift amount is not a compile-time constant
  • increased the maximum safe integer square root for 64 bit integers when using the i64->sqrt(double)->i64 calculation from ~2.000.000.000.000.000 to the maximum conservative value of 4.503.239.301.588.229. Added a Promise parameter to (u)long intsqrt accordingly.
  • gcd and thus lcm scalar: 128 bit 75% speedup, every other integer ~20% on average, up to 20x faster when numbers differ greatly in magnitude
  • removed back-to-back up and downcasting from the critical path after gcd within lcm for each (s)byte and (u)short vector, reducing latency by up to 7 cycles and reducing code size by removing up to 4 instructions
  • reduced latency of sbyte8, sbyte16 and sbyte32 csum and cavg by 3 to 7 cycles and reduced code size by removing up to 4 instructions. Those particular overloads have had their Promise parameter removed
  • reduced latency of vector (s)byte intsqrt from 17 cycles down to 6 cycles assuming infinite ILP (realistically 7 cycles, a purely throughput based benchmark showed a +25% performance gain) and reduced code size by removing 7 instructions by implementing a 4 * sqrt(hi4bits) + correction term algorithm
  • reduced latency of vector (u)long floormultiple, ceilmultiple, truncmultiple and roundmultiple by 13 cycles and reduced code size by ~20 instructions if compiling for AVX2
  • reduced latency of (s)byte and (u)short ror and rol with vectors as the rotation parameter n by up to 4 cycles and removed up to 6 instructions
  • added compile time evaluated branch to (u)short, (u)int and (u)long ror and rol with vectors as the rotation parameter n if n only contains values that are a multiple of 8. If the branch is selected at compile time and if compiling for either ARM or X86.SSE4 or higher, the operation has a latency of 1 cycle with 1 instruction and 1 constant vector loaded from RAM
  • added compile time evaluated branch to (u)short shl and shrl with vectors as the shift parameter n if n only contains values that are a multiple of 8. If the branch is selected at compile time and if compiling for X86.SSE4 or higher, the operation has a latency of 1 cycle with 1 instruction and 1 constant vector loaded from RAM
  • added compile time evaluated branch to long shra with vectors as the shift parameter n if n only contains values that are a multiple of 8. If the branch is selected at compile time and if compiling for X86.AVX2, the operation has a latency of 2 cycles instead of 3 with no additional instructions and 1 additional constant vector loaded from RAM
  • Divider<T> (and thus the respective / and % operators as well as divrem) for 32, 64 and 128 bit types now uses shiftless magic number multiplication for the prime factors of 2^BITS + 1 as divisors
  • reduced latency of most internal quadruple helper functions by 5% to 55%, reduced code size by 20% to 50% and reduced branches from sometimes 5 branches to at most 1 branch, often made possible by optimiztations regarding UInt128 << and >> operators for non-compile-time-constants. This reduces quadruple code size especially but also decreases latency substantially, although its code size still remains very large
  • reduced latency of quadruple / and % operator (and math.fmod) by ~55% each and decreased code size by ~33% each (~150/~450 instructions) on Windows x64 by implementing the core division logic in x86 assembly (due to missing 128x64 narrowing division as an intrinsic), extending the native dll asm128, resulting in a function call
  • reduced latency of quadruple + and - operator by ~10% and reduced code size by ~50%
  • added specialized handling for compile time constant quadruple powers of 2 ({ …, ±2⁻², ±2⁻¹, ±2⁰, ±2¹, ±2², … }) as arguments to the *, / operators as well as the mad and msub functions, yielding a ~30% performance gain and a ~10% code size reduction, although these gains can potentially be much higher with changes to the relevant COMPILATION_OPTIONS
  • reduced latency of quadruple square and x * x by ~5% and reduced code size by ~5%
  • quadruple mad and msub are now ~25% faster instead of ~35-50% slower than the expressions a * b [+/-] c, due to implementing a new algorithm that does not carry infinite precision throughout the operation before rounding the final result, but rather has the same precision as the expressions a * b [+/-] c instead. Code size has been reduced slightly.
  • quadruple fastrsqrt and fastrcp are no longer wrappers for rsqrt and rcp, respectively, but are now genuinely ~45% faster than the precise implementations, at the cost of the implied precision loss

Changes

  • moved COMPILATION_OPTIONS, constexpr, Uninitialized<T> and UnsafeExtensions from MaxMath and MaxMath.Intrinsics to MaxMath.CompilerServices
  • removed assertions from bitmask8, bitmask16, bitmask32, bitmask64 and bitmask128 in order to enable unconditional evaluation in algorithms that discard the result when it is not valid

Fixed Oversights

  • added overloads to mask matrix types for all and any (and the new none and notall) without implicitly converting to bool matrices

Upcoming

  • v3.2 is going to target Unity's CoreCLR builds, enabling full hardware SIMD intrinsics usage in managed C# while implementing AVX512 functionality, including 512-bit vector types like float16, byte64, long8 etc. with their accompanying math function overloads