[{"id":34144,"web_url":"https://patchwork.libcamera.org/comment/34144/","msgid":"<aBuOVPOBOj6bPcBo@pyrite.rasen.tech>","date":"2025-05-07T16:46:12","subject":"Re: [PATCH v3 07/16] libcamera: matrix: Extend multiplication\n\toperator to heterogenous types","submitter":{"id":17,"url":"https://patchwork.libcamera.org/api/people/17/","name":"Paul Elder","email":"paul.elder@ideasonboard.com"},"content":"On Thu, Apr 03, 2025 at 05:49:12PM +0200, Stefan Klug wrote:\n> From: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n> \n> It is useful to multiply matrices of heterogneous types, for instance\n> float and double. Extend the multiplication operator to support this,\n> avoiding the need to convert one of the matrices. The type of the\n> returned matrix is selected automatically to avoid loosing precision.\n> \n> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n> Signed-off-by: Stefan Klug <stefan.klug@ideasonboard.com>\n> Acked-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n\nReviewed-by: Paul Elder <paul.elder@ideasonboard.com>\n\n> \n> ---\n> \n> Changes in v2:\n> - Added this patch\n> \n> Changes in v3:\n> - Added my SoB tag\n> - Collected tags\n> ---\n>  include/libcamera/internal/matrix.h | 10 ++++++----\n>  src/libcamera/matrix.cpp            |  5 +++--\n>  2 files changed, 9 insertions(+), 6 deletions(-)\n> \n> diff --git a/include/libcamera/internal/matrix.h b/include/libcamera/internal/matrix.h\n> index a07a47701336..47513b9950e4 100644\n> --- a/include/libcamera/internal/matrix.h\n> +++ b/include/libcamera/internal/matrix.h\n> @@ -8,6 +8,7 @@\n>  \n>  #include <algorithm>\n>  #include <sstream>\n> +#include <type_traits>\n>  #include <vector>\n>  \n>  #include <libcamera/base/log.h>\n> @@ -152,15 +153,16 @@ Matrix<U, Rows, Cols> operator*(const Matrix<U, Rows, Cols> &m, T d)\n>  \treturn d * m;\n>  }\n>  \n> -template<typename T, unsigned int R1, unsigned int C1, unsigned int R2, unsigned int C2>\n> -constexpr Matrix<T, R1, C2> operator*(const Matrix<T, R1, C1> &m1, const Matrix<T, R2, C2> &m2)\n> +template<typename T1, unsigned int R1, unsigned int C1, typename T2, unsigned int R2, unsigned int C2>\n> +constexpr Matrix<std::common_type_t<T1, T2>, R1, C2> operator*(const Matrix<T1, R1, C1> &m1,\n> +\t\t\t\t\t\t\t       const Matrix<T2, R2, C2> &m2)\n>  {\n>  \tstatic_assert(C1 == R2, \"Matrix dimensions must match for multiplication\");\n> -\tMatrix<T, R1, C2> result;\n> +\tMatrix<std::common_type_t<T1, T2>, R1, C2> result;\n>  \n>  \tfor (unsigned int i = 0; i < R1; i++) {\n>  \t\tfor (unsigned int j = 0; j < C2; j++) {\n> -\t\t\tT sum = 0;\n> +\t\t\tstd::common_type_t<T1, T2> sum = 0;\n>  \n>  \t\t\tfor (unsigned int k = 0; k < C1; k++)\n>  \t\t\t\tsum += m1[i][k] * m2[k][j];\n> diff --git a/src/libcamera/matrix.cpp b/src/libcamera/matrix.cpp\n> index 68fc1b7bd5ac..ed22263b58f8 100644\n> --- a/src/libcamera/matrix.cpp\n> +++ b/src/libcamera/matrix.cpp\n> @@ -138,11 +138,12 @@ LOG_DEFINE_CATEGORY(Matrix)\n>   */\n>  \n>  /**\n> - * \\fn Matrix<T, R1, C2> operator*(const Matrix<T, R1, C1> &m1, const Matrix<T, R2, C2> &m2)\n> + * \\fn operator*(const Matrix<T1, R1, C1> &m1, const Matrix<T2, R2, C2> &m2)\n>   * \\brief Matrix multiplication\n> - * \\tparam T Type of numerical values in the matrices\n> + * \\tparam T1 Type of numerical values in the first matrix\n>   * \\tparam R1 Number of rows in the first matrix\n>   * \\tparam C1 Number of columns in the first matrix\n> + * \\tparam T2 Type of numerical values in the secont matrix\n>   * \\tparam R2 Number of rows in the second matrix\n>   * \\tparam C2 Number of columns in the second matrix\n>   * \\param m1 Multiplicand matrix\n> -- \n> 2.43.0\n>","headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id E1876C3226\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed,  7 May 2025 16:46:17 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 2DDD968B36;\n\tWed,  7 May 2025 18:46:17 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 1196168B2F\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed,  7 May 2025 18:46:16 +0200 (CEST)","from pyrite.rasen.tech (unknown\n\t[IPv6:2001:861:3a80:3300:4f2f:8c2c:b3ef:17d4])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 579176D5;\n\tWed,  7 May 2025 18:46:04 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"A62wOAaq\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1746636364;\n\tbh=VwsRmNtD6P2niUqM0TUpz2slela3XDUeuJ5FLd+0KG4=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=A62wOAaqizlhxF2UQ4yA/KGiXY+IcNtBhZaeTGCOfNzLvdgyr6pL0HGOwoaUNnxfe\n\td+RsgTyd6fJvUfgBcjOpTC7cJWjdmiAwS7BmrEBcfVOcLDGM7XPQWFFNscv1X7zdmg\n\tkyDBtH5oBCIes9bjI/AVSHuy31YzalY6Epc1U8tE=","Date":"Wed, 7 May 2025 18:46:12 +0200","From":"Paul Elder <paul.elder@ideasonboard.com>","To":"Stefan Klug <stefan.klug@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org,\n\tLaurent Pinchart <laurent.pinchart@ideasonboard.com>,\n\tKieran Bingham <kieran.bingham@ideasonboard.com>","Subject":"Re: [PATCH v3 07/16] libcamera: matrix: Extend multiplication\n\toperator to heterogenous types","Message-ID":"<aBuOVPOBOj6bPcBo@pyrite.rasen.tech>","References":"<20250403154925.382973-1-stefan.klug@ideasonboard.com>\n\t<20250403154925.382973-8-stefan.klug@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=us-ascii","Content-Disposition":"inline","In-Reply-To":"<20250403154925.382973-8-stefan.klug@ideasonboard.com>","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]