From 5234e4936fab07a3540f8e6ae87d70579a51cfcc Mon Sep 17 00:00:00 2001 From: Stefan Klug Date: Thu, 3 Apr 2025 17:49:07 +0200 Subject: libcamera: matrix: Make most functions constexpr By zero-initializing the data_ member we can make most functions constexpr which will come in handy in upcoming patches. Note that this is due to C++17. In C++20 we will be able to leave data_ uninitialized for constexpr. The Matrix(std::array) version of the constructor can not be constexpr because std::copy only became constexpr in C++20. Signed-off-by: Stefan Klug Reviewed-by: Laurent Pinchart Reviewed-by: Kieran Bingham Reviewed-by: Paul Elder --- include/libcamera/internal/matrix.h | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) (limited to 'include') diff --git a/include/libcamera/internal/matrix.h b/include/libcamera/internal/matrix.h index b9c3d41e..512c1162 100644 --- a/include/libcamera/internal/matrix.h +++ b/include/libcamera/internal/matrix.h @@ -25,9 +25,8 @@ class Matrix static_assert(std::is_arithmetic_v, "Matrix type must be arithmetic"); public: - Matrix() + constexpr Matrix() { - data_.fill(static_cast(0)); } Matrix(const std::array &data) @@ -35,7 +34,7 @@ public: std::copy(data.begin(), data.end(), data_.begin()); } - static Matrix identity() + static constexpr Matrix identity() { Matrix ret; for (size_t i = 0; i < std::min(Rows, Cols); i++) @@ -63,14 +62,14 @@ public: return out.str(); } - Span data() const { return data_; } + constexpr Span data() const { return data_; } - Span operator[](size_t i) const + constexpr Span operator[](size_t i) const { return Span{ &data_.data()[i * Cols], Cols }; } - Span operator[](size_t i) + constexpr Span operator[](size_t i) { return Span{ &data_.data()[i * Cols], Cols }; } @@ -88,7 +87,12 @@ public: } private: - std::array data_; + /* + * \todo The initializer is only necessary for the constructor to be + * constexpr in C++17. Remove the initializer as soon as we are on + * C++20. + */ + std::array data_ = {}; }; #ifndef __DOXYGEN__ @@ -141,7 +145,7 @@ constexpr Matrix operator*(const Matrix &m1, const Matrix< } template -Matrix operator+(const Matrix &m1, const Matrix &m2) +constexpr Matrix operator+(const Matrix &m1, const Matrix &m2) { Matrix result; -- cgit v1.2.1