From patchwork Wed Mar 19 16:11:12 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Klug X-Patchwork-Id: 22985 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 5C756C32FE for ; Wed, 19 Mar 2025 16:12:19 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 1D02F6895B; Wed, 19 Mar 2025 17:12:19 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="tm/mIn4/"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 6B3026895E for ; Wed, 19 Mar 2025 17:12:17 +0100 (CET) Received: from ideasonboard.com (unknown [IPv6:2a00:6020:448c:6c00:760:e5ca:4814:99c7]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 7B21D55A; Wed, 19 Mar 2025 17:10:34 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1742400634; bh=mhubXdXAgTxwsfjurIPZD4zCKZWHheK7trJwbcR4wEk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=tm/mIn4/a4gi8JiVHcLg+WfCp+k888vW+ILWhmc1SEc9sl2ffzoB+lsm0D/px7AqI IGtPNJ+PfSToQd85hjtL9Eo+dt4z3j11O1YnXQQPJod+tKdw7IMKIj3JxIAwU1jIK5 2uQXoH8DpTUEL060w2wq+C9/V0TX5lYwDzPIvUmk= From: Stefan Klug To: libcamera-devel@lists.libcamera.org Cc: Stefan Klug , Laurent Pinchart Subject: [PATCH v2 07/17] libcamera: matrix: Extend multiplication operator to heterogenous types Date: Wed, 19 Mar 2025 17:11:12 +0100 Message-ID: <20250319161152.63625-8-stefan.klug@ideasonboard.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250319161152.63625-1-stefan.klug@ideasonboard.com> References: <20250319161152.63625-1-stefan.klug@ideasonboard.com> MIME-Version: 1.0 X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" From: Laurent Pinchart 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 --- Changes in v2: - Added this patch --- 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 6e3c190286fe..688c6fd498f3 100644 --- a/include/libcamera/internal/matrix.h +++ b/include/libcamera/internal/matrix.h @@ -8,6 +8,7 @@ #include #include +#include #include #include @@ -129,15 +130,16 @@ constexpr Matrix operator*(const Matrix &m, T d) return d * m; } -template -constexpr Matrix operator*(const Matrix &m1, const Matrix &m2) +template +constexpr Matrix, R1, C2> operator*(const Matrix &m1, + const Matrix &m2) { static_assert(C1 == R2, "Matrix dimensions must match for multiplication"); - Matrix result; + Matrix, 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 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 8590f8efeff3..f9117357dc7e 100644 --- a/src/libcamera/matrix.cpp +++ b/src/libcamera/matrix.cpp @@ -138,11 +138,12 @@ LOG_DEFINE_CATEGORY(Matrix) */ /** - * \fn Matrix operator*(const Matrix &m1, const Matrix &m2) + * \fn operator*(const Matrix &m1, const Matrix &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