From patchwork Wed Mar 19 16:11:13 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Klug X-Patchwork-Id: 22986 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 E45ACC32FE for ; Wed, 19 Mar 2025 16:12:21 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 97A5568964; Wed, 19 Mar 2025 17:12:21 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="VjCBjuu2"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 3DB9468962 for ; Wed, 19 Mar 2025 17:12:20 +0100 (CET) Received: from ideasonboard.com (unknown [IPv6:2a00:6020:448c:6c00:760:e5ca:4814:99c7]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 569368FA; Wed, 19 Mar 2025 17:10:37 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1742400637; bh=br8rN+1GhOXoViyWSjCXtOrJsJ/Gofo1vO1Isf1ZNqQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=VjCBjuu2ESnSqR/W1VH5VB4waiZhrqCRDVXpnvp/zZXiRHer1m1nt1OXVyqsPt8hm pNwkBVfquzNgnrRM4HKtsM5G3xGCEvDhbaWjDeX66pnDrEMpJqIIMVUEPkYIpuGqTJ T2lGGodi4l2c1fX3gX+IX+Y7wvpPORDvV8NR0VBc= From: Stefan Klug To: libcamera-devel@lists.libcamera.org Cc: Stefan Klug , Laurent Pinchart Subject: [PATCH v2 08/17] libcamera: vector: Extend matrix multiplication operator to heterogenous types Date: Wed, 19 Mar 2025 17:11:13 +0100 Message-ID: <20250319161152.63625-9-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 and vectors of heterogeneous types, for instance float and double. Extend the multiplication operator to support this, avoiding the need to convert one of the operations. The type of the returned vector is selected automatically to avoid loosing precision. Signed-off-by: Laurent Pinchart Signed-off-by: Stefan Klug Changes in v2: - Added this patch --- include/libcamera/internal/vector.h | 9 +++++---- src/libcamera/vector.cpp | 5 +++-- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/include/libcamera/internal/vector.h b/include/libcamera/internal/vector.h index 66cc5ac988c2..d518de9689a3 100644 --- a/include/libcamera/internal/vector.h +++ b/include/libcamera/internal/vector.h @@ -13,6 +13,7 @@ #include #include #include +#include #include #include @@ -295,13 +296,13 @@ private: template using RGB = Vector; -template -Vector operator*(const Matrix &m, const Vector &v) +template +Vector, Rows> operator*(const Matrix &m, const Vector &v) { - Vector result; + Vector, Rows> result; for (unsigned int i = 0; i < Rows; i++) { - T sum = 0; + std::common_type_t sum = 0; for (unsigned int j = 0; j < Cols; j++) sum += m[i][j] * v[j]; result[i] = sum; diff --git a/src/libcamera/vector.cpp b/src/libcamera/vector.cpp index 435f2fc62f8b..c9419da8c663 100644 --- a/src/libcamera/vector.cpp +++ b/src/libcamera/vector.cpp @@ -308,9 +308,10 @@ LOG_DEFINE_CATEGORY(Vector) */ /** - * \fn Vector operator*(const Matrix &m, const Vector &v) + * \fn operator*(const Matrix &m, const Vector &v) * \brief Multiply a matrix by a vector - * \tparam T Numerical type of the contents of the matrix and vector + * \tparam T Numerical type of the contents of the matrix + * \tparam U Numerical type of the contents of the vector * \tparam Rows The number of rows in the matrix * \tparam Cols The number of columns in the matrix (= rows in the vector) * \param m The matrix