From patchwork Thu Apr 3 15:49: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: 23124 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 B222FC327D for ; Thu, 3 Apr 2025 15:50:00 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 5F23A68A8E; Thu, 3 Apr 2025 17:50:00 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="SslfHmvS"; 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 21F5468A4C for ; Thu, 3 Apr 2025 17:49:59 +0200 (CEST) Received: from ideasonboard.com (unknown [IPv6:2a00:6020:448c:6c00:6d9d:9854:3fc1:4bb2]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 6DE7D11E9; Thu, 3 Apr 2025 17:48:05 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1743695285; bh=/2ARLNaOLEkA/VETV+UoeUvmFPf2wikIcAnv73CnN5Y=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=SslfHmvSOfuLwaopzUN1qOHfVRu7o34XAJbLI+5RpKdazbJ60Vja+L6iJLdDRn24J FI7YxvxOypWOwL4tfvaSwhQJSDuz7Qk+3Z7kDLRU+IhzU9pfKKWSmNuI6GZRkGqxv4 RCdGe0d5wnGBGTyXiVYyWVxXCxy9EAMeYzZ5cieY= From: Stefan Klug To: libcamera-devel@lists.libcamera.org Cc: Stefan Klug , Laurent Pinchart , Kieran Bingham Subject: [PATCH v3 08/16] libcamera: vector: Extend matrix multiplication operator to heterogenous types Date: Thu, 3 Apr 2025 17:49:13 +0200 Message-ID: <20250403154925.382973-9-stefan.klug@ideasonboard.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250403154925.382973-1-stefan.klug@ideasonboard.com> References: <20250403154925.382973-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 Acked-by: Kieran Bingham --- Changes in v2: - Added this patch Changes in v3: - Collected tag --- 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 4e9ef1ee6853..16b6aef0b38f 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 5567d5b8defb..4dad1b9001c5 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