Small C++ library with vector math routines, Xorshift random number generator, and simplex noise.
rsqrt(x)Returns 1 / sqrtf(x)fast_rsqrt(x)Returns the approximate reciprocal square root using SSE intrinsics when availablefloortoi(x)Floor a float converting it to an int. This is faster thanstatic_cast<int>(floorf(x))clamp(x, a, b)Clamp a value to be in the the range [a, b]clampi(x, a, b)Same as clamp but for integers instead of floatsclamp01(x)The saturate function, clamps a value to be in [0, 1]lerp(a, b, t)Linearly interpolate between vector a and b at time t where t is in [0, 1]unlerp(a, b, x)The opposite oflerp, returns the position in the curvetfor a given valuex.remap(a, b, c, d, x)Maps a value x in the range [a, b] to be in the range [c, d]signf(x)returns 1 if x is positive, -1 if x is negative, 0 otherwise.next_pow2(x)Returns the next power of two given a unsigned integer x.
-vecvec + vec,vec - vecvec * vec, vec / vecComponent-wise multiply and dividevec * scalarscalar * vec,vec / scalar+=,-=,*=,/=vec(scalar)Initializes all components to a scalar value, can be used to represent the zero vector asfloat3(0)for example. This is an explicit constructor, so scalars will not implicitly convert to vector types. Similar constructors exist to convert between vectors of different types/dimensions.
For 4D vectors, most operators map to their equivalent SSE instructions.
length()Returns length / magnitude / norm of the vector.length_sqr()Returns the length of the vector squared.
Functions defined for all floating point vector types.
dot(a, b)normalize(v)normalize_safe(v)Returns a normalized vector without producing NaN when v is the zero vector.clamp(v, a, b)Clamps each vector component to be in the range [a, b]clamp01(v)Saturate function, clamps the vector have all components in [0, 1]lerp(a, b, t)Linearly interpolate between vector a and b at time t where t is in [0, 1]frac(v)Returns1 - vfloor(v)vstep(edge, v)Returns a vector with each component equal tov[i] >= edge[i]vmin(a, b)Component-wisefminfvmax(a, b)Component-wisefmaxfvabs(a, b)Component-wisefabsfvfloor(a, b)Component-wisefloorfvceil(a, b)Component-wiseceilfvsqrt(a, b)Component-wiseceilfvrsqrt(a, b)Component-wisersqrt, forfloat4thersqrtSSE instrinsic is used.pprint(v)Print a vector
vangle(a, b)Returns the angle between two vectors in degreesvangle_rad(a, b)Returns the angle between two vectors in radiansreflect(v, normal)
cross(a, b)reflect(v, normal)refract(v, normal, eta)
clampi(v, a, b)vabs(v)vmin(a, b)vmax(a, b)
Constructs a new vector by selecting elements from a given vector.
shuffle<0, 0, 1, 1>(float2(1, 2)) // float4(1, 1, 2, 2)
shuffle<3, 2, 1, 0>(float4(1, 2, 3, 4)) // float4(4, 3, 2, 1)
shuffle<1, 2>(float3(1, 2, 3)) // float2(2, 3)The 4D vector types (int4 and float4) use sse instructions when available for most operators. To use vec.m128 and vec.m128i to use with float and integer sse instructions respectively. __m128 can be implicitly be converted to float4 and __m128i to int4.