[PATCH v3 07/16] libcamera: matrix: Extend multiplication operator to heterogenous types
Paul Elder
paul.elder at ideasonboard.com
Wed May 7 18:46:12 CEST 2025
On Thu, Apr 03, 2025 at 05:49:12PM +0200, Stefan Klug wrote:
> From: Laurent Pinchart <laurent.pinchart at ideasonboard.com>
>
> It is useful to multiply matrices of heterogneous types, for instance
> float and double. Extend the multiplication operator to support this,
> avoiding the need to convert one of the matrices. The type of the
> returned matrix is selected automatically to avoid loosing precision.
>
> Signed-off-by: Laurent Pinchart <laurent.pinchart at ideasonboard.com>
> Signed-off-by: Stefan Klug <stefan.klug at ideasonboard.com>
> Acked-by: Kieran Bingham <kieran.bingham at ideasonboard.com>
Reviewed-by: Paul Elder <paul.elder at ideasonboard.com>
>
> ---
>
> Changes in v2:
> - Added this patch
>
> Changes in v3:
> - Added my SoB tag
> - Collected tags
> ---
> include/libcamera/internal/matrix.h | 10 ++++++----
> src/libcamera/matrix.cpp | 5 +++--
> 2 files changed, 9 insertions(+), 6 deletions(-)
>
> diff --git a/include/libcamera/internal/matrix.h b/include/libcamera/internal/matrix.h
> index a07a47701336..47513b9950e4 100644
> --- a/include/libcamera/internal/matrix.h
> +++ b/include/libcamera/internal/matrix.h
> @@ -8,6 +8,7 @@
>
> #include <algorithm>
> #include <sstream>
> +#include <type_traits>
> #include <vector>
>
> #include <libcamera/base/log.h>
> @@ -152,15 +153,16 @@ Matrix<U, Rows, Cols> operator*(const Matrix<U, Rows, Cols> &m, T d)
> return d * m;
> }
>
> -template<typename T, unsigned int R1, unsigned int C1, unsigned int R2, unsigned int C2>
> -constexpr Matrix<T, R1, C2> operator*(const Matrix<T, R1, C1> &m1, const Matrix<T, R2, C2> &m2)
> +template<typename T1, unsigned int R1, unsigned int C1, typename T2, unsigned int R2, unsigned int C2>
> +constexpr Matrix<std::common_type_t<T1, T2>, R1, C2> operator*(const Matrix<T1, R1, C1> &m1,
> + const Matrix<T2, R2, C2> &m2)
> {
> static_assert(C1 == R2, "Matrix dimensions must match for multiplication");
> - Matrix<T, R1, C2> result;
> + Matrix<std::common_type_t<T1, T2>, R1, C2> result;
>
> for (unsigned int i = 0; i < R1; i++) {
> for (unsigned int j = 0; j < C2; j++) {
> - T sum = 0;
> + std::common_type_t<T1, T2> sum = 0;
>
> for (unsigned int k = 0; k < C1; k++)
> sum += m1[i][k] * m2[k][j];
> diff --git a/src/libcamera/matrix.cpp b/src/libcamera/matrix.cpp
> index 68fc1b7bd5ac..ed22263b58f8 100644
> --- a/src/libcamera/matrix.cpp
> +++ b/src/libcamera/matrix.cpp
> @@ -138,11 +138,12 @@ LOG_DEFINE_CATEGORY(Matrix)
> */
>
> /**
> - * \fn Matrix<T, R1, C2> operator*(const Matrix<T, R1, C1> &m1, const Matrix<T, R2, C2> &m2)
> + * \fn operator*(const Matrix<T1, R1, C1> &m1, const Matrix<T2, R2, C2> &m2)
> * \brief Matrix multiplication
> - * \tparam T Type of numerical values in the matrices
> + * \tparam T1 Type of numerical values in the first matrix
> * \tparam R1 Number of rows in the first matrix
> * \tparam C1 Number of columns in the first matrix
> + * \tparam T2 Type of numerical values in the secont matrix
> * \tparam R2 Number of rows in the second matrix
> * \tparam C2 Number of columns in the second matrix
> * \param m1 Multiplicand matrix
> --
> 2.43.0
>
More information about the libcamera-devel
mailing list