Description
Small header-only math library for C++ with the same syntax as the hlsl shading language. It supports any SSE (x86/x64 devices like PC, Mac, PS4, Xbox One) and NEON (ARM devices like Android, iOS, Switch) platforms. It features swizzling and all the operators and functions from the hlsl documentation. The library is aimed mainly at game developers as it's meant to ease the C++ to shader bridge by providing common syntax, but can be used for any application requiring fast, portable math. It also adds some functionality that hlsl doesn't natively provide, such as convenient matrix functions, quaternions and extended vectors such as float8 (8-component float) that take advantage of wide SSE registers.
HLSL++ alternatives and similar libraries
Based on the "Math" category.
Alternatively, view hlslpp alternatives based on common mentions on social networks and blogs.
-
OpenBLAS
OpenBLAS is an optimized BLAS library based on GotoBLAS2 1.13 BSD version. -
Eigen
A high-level C++ library of template headers for linear algebra, matrix and vector operations, numerical solvers and related algorithms. [MPL2] -
TinyExpr
tiny recursive descent expression parser, compiler, and evaluation engine for math expressions -
linmath.h
a lean linear math library, aimed at graphics programming. Supports vec3, vec4, mat4x4 and quaternions -
MIRACL
MIRACL Cryptographic SDK: Multiprecision Integer and Rational Arithmetic Cryptographic Library is a C software library that is widely regarded by developers as the gold standard open source SDK for elliptic curve cryptography (ECC). -
NT2
A SIMD-optimized numerical template library that provides an interface with MATLAB-like syntax. [Boost] -
ExprTK
C++ Mathematical Expression Parsing And Evaluation Library https://www.partow.net/programming/exprtk/index.html -
LibTomMath
LibTomMath is a free open source portable number theoretic multiple-precision integer library written entirely in C. -
muparser
muparser is a fast math parser library for C/C++ with (optional) OpenMP support. -
GMTL
Generic Math Template Library (forked from http://ggt.sourceforge.net/) -
safe_numerics
Replacements to standard numeric types which throw exceptions on errors -
blaze
high-performance C++ math library for dense and sparse arithmetic. [BSD] -
Versor
[older version] Versor 1.0 C++ library for conformal geometric algebra draw routines -
Xerus
A general purpose library for numerical calculations with higher order tensors, Tensor-Train Decompositions / Matrix Product States and other Tensor Networks -
SLIMCPP
Simple Long Integer Math for C++. Lightweight cross-platform header-only library what implements big integer arithmetic in С++17. -
Mission : Impossible (AutoDiff)
A concise C++17 implementation of automatic differentiation (operator overloading) -
Armadillo
A high quality C++ linear algebra library, aiming towards a good balance between speed and ease of use. The syntax (API) is deliberately similar to Matlab. [MPL2] -
GMP
A C/C++ library for arbitrary precision arithmetic, operating on signed integers, rational numbers, and floating-point numbers. [LGPL3 & GPL2]
ONLYOFFICE Docs — document collaboration in your environment
* Code Quality Rankings and insights are calculated and provided by Lumnify.
They vary from L1 to L5 with "L5" being the highest.
Do you think we are missing an alternative of HLSL++ or a related project?
Popular Comparisons
README
HLSL++
Small header-only math library for C++ with the same syntax as the hlsl shading language. It supports any SSE (x86/x64 devices like PC, Mac, PS4, Xbox One) and NEON (ARM devices like Android, iOS, Switch) platforms. It features swizzling and all the operators and functions from the hlsl documentation. The library is aimed mainly at game developers as it's meant to ease the C++ to shader bridge by providing common syntax, but can be used for any application requiring fast, portable math. It also adds some functionality that hlsl doesn't natively provide, such as convenient matrix functions, quaternions and extended vectors such as float8 (8-component float) that take advantage of wide SSE registers.
Example
hlsl++ allows you to be as expressive in C++ as when programming in the shader language. Constructs such as the following are possible.
float4 foo4 = float4(1, 2, 3, 4);
float3 bar3 = foo4.xzy;
float2 logFoo2 = log(bar3.xz);
foo4.wx = logFoo2.yx;
float4 baz4 = float4(logFoo2, foo4.zz);
float4x4 fooMatrix4x4 = float4x4( 1, 2, 3, 4,
5, 6, 7, 8,
8, 7, 6, 5,
4, 3, 2, 1);
float4 myTransformedVector = mul(fooMatrix4x4, baz4);
int2 ifoo2 = int2(1, 2);
int4 ifoo4 = int4(1, 2, 3, 4) + ifoo2.xyxy;
float4 fooCast4 = ifoo4.wwyx;
float8 foo8 = float8(1, 2, 3, 4, 5, 6, 7, 8);
float8 bar8 = float8(1, 2, 3, 4, 5, 6, 7, 8);
float8 add8 = foo8 + bar8;
The natvis files provided for Visual Studio debugging allow you to see both vectors and the result of the swizzling in the debugging window in a programmer-friendly way.
Requirements
The only required features are a C++ compiler supporting anonymous unions, and SSE or NEON depending on your target platform. If your target platform does not have SIMD support, it can also fall back to a scalar implementation. As a curiosity it also includes an Xbox 360 implementation.
How to use
#include "hlsl++.h"
Remember to also add an include path to "hlslpp/include". hlsl++.h pulls in other headers that live in the same folder. To force the scalar version of the library, define HLSLPP_SCALAR.
Features
- SSE/AVX/AVX2, NEON, Xbox360, and scalar versions
- float1, float2, float3, float4, float8
- int1, int2, int3, int4
- uint1, uint2, uint3, uint4
- double1, double2, double3, double4
- floatNxM
- quaternion
- Conversion construction and assignment, e.g. float4(float2, float2) and int4(float2, int2)
- Efficient swizzling for all vector types
- Basic operators +, *, -, / for all vector and matrix types
- Per-component comparison operators ==, !=, >, <, >=, <= (no ternary operator as overloading is disallowed in C++)
- hlsl vector functions: abs, acos, all, any, asin, atan, atan2, ceil, clamp, cos, cosh, cross, degrees, dot, floor, fmod, frac, exp, exp2, isfinite, isinf, isnan, length, lerp, log, log2, log10, max, mad, min, modf, normalize, pow, radians, reflect, refract, round, rsqrt, saturate, sign, sin, sincos, sinh, smoothstep, sqrt, step, trunc, tan, tanh
- Additional matrix functions: determinant, transpose, inverse (not in hlsl but very useful)
- Matrix multiplication for all NxM matrix combinations
- Transformation matrices for scale, rotation and translation, as well as world-to-view look_at and view-to-projection orthographic/perspective coordinate transformations. These static functions are optionally available for matrix types float2x2, float3x3, float4x4 when hlsl++.h is compiled with HLSLPP_FEATURE_TRANSFORM definition.
- Native visualizers for Visual Studio (.natvis files) which correctly parse with both MSVC and Clang in Windows
Missing/planned:
- floatNxM _m00_m01 style swizzling (rows implemented but missing columns)
- boolN types
*Note that all licence references and agreements mentioned in the HLSL++ README section above
are relevant to that project's source code only.