From ba79de0439ff797c3e922c5908d3ad60eafbdb43 Mon Sep 17 00:00:00 2001 From: Zaggy1024 Date: Fri, 30 Sep 2022 17:38:24 -0500 Subject: [PATCH] LibGfx: Make Matrix and VectorN more constexpr-friendly This allows the copy constructor of Matrix to be called constexpr, which should allow more values to be compile-time calculated. Likewise, VectorN.data() is now constexpr so that it can be compile-time evaluated. --- Userland/Libraries/LibGfx/Matrix.h | 16 +++++++++++++--- Userland/Libraries/LibGfx/VectorN.h | 4 ++-- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/Userland/Libraries/LibGfx/Matrix.h b/Userland/Libraries/LibGfx/Matrix.h index 638ced76f5..1d7d0a7d94 100644 --- a/Userland/Libraries/LibGfx/Matrix.h +++ b/Userland/Libraries/LibGfx/Matrix.h @@ -36,13 +36,23 @@ public: { } - Matrix(Matrix const& other) + constexpr Matrix(Matrix const& other) { - __builtin_memcpy(m_elements, other.elements(), sizeof(T) * N * N); + *this = other; } - Matrix& operator=(Matrix const& other) + constexpr Matrix& operator=(Matrix const& other) { +#ifndef __clang__ + if (is_constant_evaluated()) { + for (size_t i = 0; i < N; i++) { + for (size_t j = 0; j < N; j++) { + m_elements[i][j] = other.elements()[i][j]; + } + } + return *this; + } +#endif __builtin_memcpy(m_elements, other.elements(), sizeof(T) * N * N); return *this; } diff --git a/Userland/Libraries/LibGfx/VectorN.h b/Userland/Libraries/LibGfx/VectorN.h index 0f3f5f38ca..66c70d7eea 100644 --- a/Userland/Libraries/LibGfx/VectorN.h +++ b/Userland/Libraries/LibGfx/VectorN.h @@ -251,8 +251,8 @@ public: return result; } - auto& data() { return m_data; } - auto const& data() const { return m_data; } + constexpr auto& data() { return m_data; } + constexpr auto const& data() const { return m_data; } private: AK::Array m_data;