Add some more core math and SIMD functions Diffs= 466f68e3a Add some more core math and SIMD functions
diff --git a/.rive_head b/.rive_head index f5e1000..ed79e9f 100644 --- a/.rive_head +++ b/.rive_head
@@ -1 +1 @@ -3bfd9917f7683625726ba0ad0540b50d86830968 +466f68e3a09af0e4d1a3d391eb5a1bcff64840b9
diff --git a/include/rive/math/math_types.hpp b/include/rive/math/math_types.hpp index 4785fc9..187b338 100644 --- a/include/rive/math/math_types.hpp +++ b/include/rive/math/math_types.hpp
@@ -14,8 +14,35 @@ namespace math { constexpr float PI = 3.14159265f; + +// Performs a floating point division with conformant IEEE 754 behavior for NaN and Inf. +// +// Returns +/-Inf if b == 0. +// Returns 0 if b == +/-Inf. +// Returns NaN if a and b are both zero. +// Returns NaN if a and b are both infinite. +// Returns NaN a or b is NaN. +// +// Reference: +// https://stackoverflow.com/questions/42926763/the-behaviour-of-floating-point-division-by-zero +[[maybe_unused]] static __attribute__((no_sanitize("float-divide-by-zero"), always_inline)) float +ieee_float_divide(float a, float b) +{ + static_assert(std::numeric_limits<float>::is_iec559, + "Conformant IEEE 754 behavior for NaN and Inf is required."); + return a / b; } +// Reinterprets the underlying bits of src as the given type. +template <typename Dst, typename Src> Dst bit_cast(const Src& src) +{ + static_assert(sizeof(Dst) == sizeof(Src)); + Dst dst; + __builtin_memcpy(&dst, &src, sizeof(Dst)); + return dst; +} +} // namespace math + template <typename T> T lerp(const T& a, const T& b, float t) { return a + (b - a) * t; } } // namespace rive
diff --git a/include/rive/math/simd.hpp b/include/rive/math/simd.hpp index 3d4e5a5..726a5c7 100644 --- a/include/rive/math/simd.hpp +++ b/include/rive/math/simd.hpp
@@ -14,15 +14,20 @@ #ifndef _RIVE_SIMD_HPP_ #define _RIVE_SIMD_HPP_ +#include <cassert> +#include <limits> #include <stdint.h> #define SIMD_ALWAYS_INLINE inline __attribute__((always_inline)) +// SIMD math can expect conformant IEEE 754 behavior for NaN and Inf. +static_assert(std::numeric_limits<float>::is_iec559, + "Conformant IEEE 754 behavior for NaN and Inf is required."); + namespace rive { namespace simd { - // The GLSL spec uses "gvec" to denote a vector of unspecified type. template <typename T, int N> using gvec = T __attribute__((ext_vector_type(N))) __attribute__((aligned(sizeof(T) * N))); @@ -43,6 +48,18 @@ return __builtin_elementwise_max(a, b); } +// Unlike std::clamp(), simd::clamp() always returns a value between lo and hi. +// +// Returns lo if x == NaN, but std::clamp() returns NaN. +// Returns hi if hi <= lo. +// Ignores hi and/or lo if they are NaN. +// +template <typename T, int N> +SIMD_ALWAYS_INLINE gvec<T, N> clamp(gvec<T, N> x, gvec<T, N> lo, gvec<T, N> hi) +{ + return min(max(lo, x), hi); +} + // Returns the absolute value of x per element, with one exception: // If x[i] is an integer type and equal to the minimum representable value, returns x[i]. template <typename T, int N> SIMD_ALWAYS_INLINE gvec<T, N> abs(gvec<T, N> x) @@ -79,24 +96,85 @@ ////// Loading and storing ////// -template <typename T, int N> SIMD_ALWAYS_INLINE gvec<T, N> load(const T* ptr) +template <typename T, int N> SIMD_ALWAYS_INLINE gvec<T, N> load(const void* ptr) { gvec<T, N> vec; __builtin_memcpy(&vec, ptr, sizeof(vec)); return vec; } -SIMD_ALWAYS_INLINE gvec<float, 2> load2f(const float* ptr) { return load<float, 2>(ptr); } -SIMD_ALWAYS_INLINE gvec<float, 4> load4f(const float* ptr) { return load<float, 4>(ptr); } -SIMD_ALWAYS_INLINE gvec<int32_t, 2> load2i(const int32_t* ptr) { return load<int32_t, 2>(ptr); } -SIMD_ALWAYS_INLINE gvec<int32_t, 4> load4i(const int32_t* ptr) { return load<int32_t, 4>(ptr); } -SIMD_ALWAYS_INLINE gvec<uint32_t, 2> load2ui(const uint32_t* ptr) { return load<uint32_t, 2>(ptr); } -SIMD_ALWAYS_INLINE gvec<uint32_t, 4> load4ui(const uint32_t* ptr) { return load<uint32_t, 4>(ptr); } +SIMD_ALWAYS_INLINE gvec<float, 2> load2f(const void* ptr) { return load<float, 2>(ptr); } +SIMD_ALWAYS_INLINE gvec<float, 4> load4f(const void* ptr) { return load<float, 4>(ptr); } +SIMD_ALWAYS_INLINE gvec<int32_t, 2> load2i(const void* ptr) { return load<int32_t, 2>(ptr); } +SIMD_ALWAYS_INLINE gvec<int32_t, 4> load4i(const void* ptr) { return load<int32_t, 4>(ptr); } +SIMD_ALWAYS_INLINE gvec<uint32_t, 2> load2ui(const void* ptr) { return load<uint32_t, 2>(ptr); } +SIMD_ALWAYS_INLINE gvec<uint32_t, 4> load4ui(const void* ptr) { return load<uint32_t, 4>(ptr); } -template <typename T, int N> SIMD_ALWAYS_INLINE void store(T* ptr, gvec<T, N> vec) +template <typename T, int N> SIMD_ALWAYS_INLINE void store(void* ptr, gvec<T, N> vec) { __builtin_memcpy(ptr, &vec, sizeof(vec)); } +template <typename T, int M, int N> +SIMD_ALWAYS_INLINE gvec<T, M + N> join(gvec<T, M> a, gvec<T, N> b) +{ + T data[M + N]; + __builtin_memcpy(data, &a, sizeof(T) * M); + __builtin_memcpy(data + M, &b, sizeof(T) * N); + return load<T, M + N>(data); +} + +////// Basic linear algebra ////// + +template <typename T, int N> SIMD_ALWAYS_INLINE T dot(gvec<T, N> a, gvec<T, N> b) +{ + gvec<T, N> d = a * b; + if constexpr (N == 2) + { + return d.x + d.y; + } + else if constexpr (N == 3) + { + return d.x + d.y + d.z; + } + else if constexpr (N == 4) + { + return d.x + d.y + d.z + d.w; + } + else + { + T s = d[0]; + for (int i = 1; i < N; ++i) + { + s += d[i]; + } + return s; + } +} + +SIMD_ALWAYS_INLINE float cross(gvec<float, 2> a, gvec<float, 2> b) +{ + gvec<float, 2> c = a * b.yx; + return c.x - c.y; +} + +// Linearly interpolates between a and b. +// +// NOTE: mix(a, b, 1) !== b (!!) +// +// The floating point numerics are not precise in the case where t === 1. But overall, this +// structure seems to get better precision for things like chopping cubics on exact cusp points than +// "a*(1 - t) + b*t" (which would return exactly b when t == 1). +template <int N> SIMD_ALWAYS_INLINE gvec<float, N> mix(gvec<float, N> a, gvec<float, N> b, float t) +{ + assert(0 <= t && t < 1); + return (b - a) * t + a; +} +template <int N> +SIMD_ALWAYS_INLINE gvec<float, N> mix(gvec<float, N> a, gvec<float, N> b, gvec<float, N> t) +{ + assert(simd::all(0 <= t && t < 1)); + return (b - a) * t + a; +} } // namespace simd } // namespace rive @@ -104,7 +182,6 @@ namespace rive { - template <int N> using vec = simd::gvec<float, N>; using float2 = vec<2>; using float4 = vec<4>; @@ -116,7 +193,6 @@ template <int N> using uvec = simd::gvec<uint32_t, N>; using uint2 = uvec<2>; using uint4 = uvec<4>; - } // namespace rive #endif
diff --git a/test/math_test.cpp b/test/math_test.cpp new file mode 100644 index 0000000..7057b84 --- /dev/null +++ b/test/math_test.cpp
@@ -0,0 +1,62 @@ +#include <catch.hpp> +#include <rive/math/math_types.hpp> + +using namespace rive; + +constexpr float kInf = std::numeric_limits<float>::infinity(); +constexpr float kNaN = std::numeric_limits<float>::quiet_NaN(); + +// Check math::ieee_float_divide. +TEST_CASE("ieee_float_divide", "[math]") +{ + // Make sure we're doing division. + CHECK(math::ieee_float_divide(100, 10) == 10); + + // Returns +/-Inf if b == 0. + CHECK(math::ieee_float_divide(5, 0) == kInf); + CHECK(math::ieee_float_divide(5, -0) == kInf); + CHECK(math::ieee_float_divide(-3, 0) == -kInf); + CHECK(math::ieee_float_divide(-3, -0) == -kInf); + CHECK(math::ieee_float_divide(kInf, 0) == kInf); + CHECK(math::ieee_float_divide(-kInf, 0) == -kInf); + CHECK(math::ieee_float_divide(kInf, -0) == kInf); + CHECK(math::ieee_float_divide(-kInf, -0) == -kInf); + + // Returns 0 if b == +/-Inf. + CHECK(math::ieee_float_divide(1, kInf) == 0); + CHECK(math::ieee_float_divide(-100, kInf) == 0); + CHECK(math::ieee_float_divide(std::numeric_limits<float>::max(), kInf) == 0); + CHECK(math::ieee_float_divide(std::numeric_limits<float>::max(), -kInf) == 0); + CHECK(math::ieee_float_divide(-std::numeric_limits<float>::max(), -kInf) == 0); + CHECK(math::ieee_float_divide(-std::numeric_limits<float>::max(), kInf) == 0); + CHECK(math::ieee_float_divide(0, kInf) == 0); + CHECK(math::ieee_float_divide(0, -kInf) == 0); + CHECK(math::ieee_float_divide(-0, -kInf) == 0); + CHECK(math::ieee_float_divide(-0, kInf) == 0); + + // Returns NaN if a and b are both zero. + CHECK(std::isnan(math::ieee_float_divide(0, 0))); + CHECK(std::isnan(math::ieee_float_divide(0, -0))); + CHECK(std::isnan(math::ieee_float_divide(-0, 0))); + CHECK(std::isnan(math::ieee_float_divide(-0, -0))); + + // Returns NaN if b and are both infinite. + CHECK(std::isnan(math::ieee_float_divide(kInf, kInf))); + CHECK(std::isnan(math::ieee_float_divide(kInf, -kInf))); + CHECK(std::isnan(math::ieee_float_divide(-kInf, kInf))); + CHECK(std::isnan(math::ieee_float_divide(kInf, -kInf))); + + // Returns NaN a or b is NaN. + CHECK(std::isnan(math::ieee_float_divide(kNaN, 1))); + CHECK(std::isnan(math::ieee_float_divide(kInf, kNaN))); +} + +// Check math::bit_cast. +TEST_CASE("bit_cast", "[math]") +{ + CHECK(math::bit_cast<float>(0x3f800000) == 1); + CHECK(math::bit_cast<float>((1u << 31) | 0x3f800000) == -1); + CHECK(math::bit_cast<float>(0x7f800000) == kInf); + CHECK(math::bit_cast<float>((1u << 31) | 0x7f800000) == -kInf); + CHECK(std::isnan(math::bit_cast<float>(0x7fc00000))); +}
diff --git a/test/simd_test.cpp b/test/simd_test.cpp index 66199e7..fadef68 100644 --- a/test/simd_test.cpp +++ b/test/simd_test.cpp
@@ -4,6 +4,7 @@ #include <catch.hpp> +#include "rive/math/math_types.hpp" #include "rive/math/simd.hpp" #include <limits> @@ -104,10 +105,42 @@ CHECK(simd::max<float, 1>(1, kNaN).x == std::max<float>(1, kNaN)); } +// Check simd::clamp. +TEST_CASE("clamp", "[simd]") +{ + CHECK(simd::all(simd::clamp(float4{1, 2, kInf, -kInf}, + float4{2, 1, kInf, 0}, + float4{3, 1, kInf, kInf}) == float4{2, 1, kInf, 0})); + CHECK(simd::all(simd::clamp(float4{1, kNaN, kInf, -kInf}, + float4{kNaN, 2, kNaN, 0}, + float4{kNaN, 3, kInf, kNaN}) == float4{1, 2, kInf, 0})); + float4 f4 = simd::clamp(float4{1, kNaN, kNaN, kNaN}, + float4{kNaN, 1, kNaN, kNaN}, + float4{kNaN, kNaN, 1, kNaN}); + CHECK(simd::all(f4.xyz == 1)); + CHECK(std::isnan(f4.w)); + + // Returns lo if x == NaN, but std::clamp() returns NaN. + CHECK(simd::clamp<float, 1>(kNaN, 1, 2).x == 1); + CHECK(std::clamp<float>(kNaN, 1, 2) != 1); + CHECK(std::isnan(std::clamp<float>(kNaN, 1, 2))); + + // Returns hi if hi <= lo. + CHECK(simd::clamp<float, 1>(3, 2, 1).x == 1); + CHECK(simd::clamp<float, 1>(kNaN, 2, 1).x == 1); + CHECK(simd::clamp<float, 1>(kNaN, kNaN, 1).x == 1); + + // Ignores hi and/or lo if they are NaN. + CHECK(simd::clamp<float, 1>(3, 4, kNaN).x == 4); + CHECK(simd::clamp<float, 1>(3, 2, kNaN).x == 3); + CHECK(simd::clamp<float, 1>(3, kNaN, 2).x == 2); + CHECK(simd::clamp<float, 1>(3, kNaN, 4).x == 3); + CHECK(simd::clamp<float, 1>(3, kNaN, kNaN).x == 3); +} + // Check simd::abs. TEST_CASE("abs", "[simd]") { - CHECK(simd::all(simd::abs(float4{-1, 2, -3, 4}) == float4{1, 2, 3, 4})); CHECK(simd::all(simd::abs(float2{-5, 6}) == float2{5, 6})); CHECK(simd::all(float4{-std::numeric_limits<float>::epsilon(), @@ -130,4 +163,86 @@ int2{std::numeric_limits<int32_t>::max(), std::numeric_limits<int32_t>::min()})); } +// Check simd::dot. +TEST_CASE("dot", "[simd]") +{ + CHECK(simd::dot(int2{0, 1}, int2{1, 0}) == 0); + CHECK(simd::dot(int2{1, 0}, int2{0, 1}) == 0); + CHECK(simd::dot(int2{1, 1}, int2{1, -1}) == 0); + CHECK(simd::dot(int2{1, 1}, int2{1, 1}) == 2); + CHECK(simd::dot(int2{1, 1}, int2{-1, -1}) == -2); + CHECK(simd::dot(simd::gvec<int, 3>{1, 2, 3}, simd::gvec<int, 3>{1, 2, 3}) == 14); + CHECK(simd::dot(int4{1, 2, 3, 4}, int4{1, 2, 3, 4}) == 30); + CHECK(simd::dot(ivec<5>{1, 2, 3, 4, 5}, ivec<5>{1, 2, 3, 4, 5}) == 55); +} + +// Check simd::cross. +TEST_CASE("cross", "[simd]") +{ + CHECK(simd::cross({0, 1}, {0, 1}) == 0); + CHECK(simd::cross({1, 0}, {1, 0}) == 0); + CHECK(simd::cross({1, 1}, {1, 1}) == 0); + CHECK(simd::cross({1, 1}, {1, -1}) == -2); + CHECK(simd::cross({1, 1}, {-1, 1}) == 2); +} + +// Check simd::join +TEST_CASE("join", "[simd]") +{ + CHECK(simd::all(simd::join(int2{1, 2}, int4{3, 4, 5, 6}) == ivec<6>{1, 2, 3, 4, 5, 6})); + CHECK(simd::all(simd::join(vec<1>{1}, vec<3>{2, 3, 4}) == float4{1, 2, 3, 4})); +} + +template <int N> static vec<N> mix_reference_impl(vec<N> a, vec<N> b, float t) +{ + return a * (1 - t) + b * t; +} +template <int N> static vec<N> mix_reference_impl(vec<N> a, vec<N> b, vec<N> t) +{ + return a * (1 - t) + b * t; +} + +template <typename T, int N> static bool fuzzy_equal(simd::gvec<T, N> a, simd::gvec<T, N> b) +{ + return simd::all(b - a < 1e-4f); +} + +static float frand() +{ + float kMaxBelow1 = math::bit_cast<float>(math::bit_cast<uint32_t>(1.f) - 1); + float f = static_cast<float>(rand()) / RAND_MAX; + return std::min(kMaxBelow1, f); +} +template <int N> vec<N> vrand() +{ + vec<N> vrand{}; + for (int i = 0; i < N; ++i) + { + vrand[i] = frand(); + } + return vrand; +} + +template <int N> void check_mix() +{ + vec<N> a = vrand<N>(); + vec<N> b = vrand<N>(); + float t = frand(); + CHECK(fuzzy_equal(simd::mix(a, b, t), mix_reference_impl(a, b, t))); + vec<N> tt = vrand<N>(); + CHECK(fuzzy_equal(simd::mix(a, b, tt), mix_reference_impl(a, b, tt))); +} + +// Check simd::mix +TEST_CASE("mix", "[simd]") +{ + srand(0); + check_mix<1>(); + check_mix<2>(); + check_mix<3>(); + check_mix<4>(); + check_mix<5>(); + CHECK(simd::all(simd::mix(float4{1, 2, 3, 4}, float4{5, 6, 7, 8}, 0) == float4{1, 2, 3, 4})); +} + } // namespace rive