[{"id":33794,"web_url":"https://patchwork.libcamera.org/comment/33794/","msgid":"<174344092293.3394313.1704010105580098312@ping.linuxembedded.co.uk>","date":"2025-03-31T17:08:42","subject":"Re: [PATCH v2 07/17] libcamera: matrix: Extend multiplication\n\toperator to heterogenous types","submitter":{"id":4,"url":"https://patchwork.libcamera.org/api/people/4/","name":"Kieran Bingham","email":"kieran.bingham@ideasonboard.com"},"content":"Quoting Stefan Klug (2025-03-19 16:11:12)\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\nPosted by Stefan, so should probably have your SoB - (or at least RB tag\nnow :D)\n\nBut this sounds like something that will be helpful later in this\nseries...\n\n\nAcked-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n\n> \n> ---\n> \n> Changes in v2:\n> - Added this patch\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 6e3c190286fe..688c6fd498f3 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> @@ -129,15 +130,16 @@ constexpr Matrix<U, Rows, Cols> operator*(const Matrix<U, Rows, Cols> &m, T d)\n>         return 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> +                                                              const Matrix<T2, R2, C2> &m2)\n>  {\n>         static_assert(C1 == R2, \"Matrix dimensions must match for multiplication\");\n> -       Matrix<T, R1, C2> result;\n> +       Matrix<std::common_type_t<T1, T2>, R1, C2> result;\n>  \n>         for (unsigned int i = 0; i < R1; i++) {\n>                 for (unsigned int j = 0; j < C2; j++) {\n> -                       T sum = 0;\n> +                       std::common_type_t<T1, T2> sum = 0;\n>  \n>                         for (unsigned int k = 0; k < C1; k++)\n>                                 sum += m1[i][k] * m2[k][j];\n> diff --git a/src/libcamera/matrix.cpp b/src/libcamera/matrix.cpp\n> index 8590f8efeff3..f9117357dc7e 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 69239C323E\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon, 31 Mar 2025 17:08:47 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 2489868985;\n\tMon, 31 Mar 2025 19:08:47 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 984186897A\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 31 Mar 2025 19:08:45 +0200 (CEST)","from pendragon.ideasonboard.com\n\t(cpc89244-aztw30-2-0-cust6594.18-1.cable.virginm.net [86.31.185.195])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 1342A703;\n\tMon, 31 Mar 2025 19:06:54 +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=\"hMdCyKxd\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1743440814;\n\tbh=U9VH5nCAsGYXs0cFbIN6FXJO9d79AhL2EYsYlE3p2A0=;\n\th=In-Reply-To:References:Subject:From:Cc:To:Date:From;\n\tb=hMdCyKxdeir/1JZih46sQ/GZO/NSjyKHoaq0mgkApx/kVEuQ3rny+6f8J4zKcwWST\n\tT8yeZ3mvFW2QxfAyS41kDGgKUmhm97dN5pAPEfsWnGPuBe8nbhTeTMRUE0q7P7fLAG\n\tNo8ISf8jNrxvoqKitsvg39xMbbCOwwrzoUlGs6fY=","Content-Type":"text/plain; charset=\"utf-8\"","MIME-Version":"1.0","Content-Transfer-Encoding":"quoted-printable","In-Reply-To":"<20250319161152.63625-8-stefan.klug@ideasonboard.com>","References":"<20250319161152.63625-1-stefan.klug@ideasonboard.com>\n\t<20250319161152.63625-8-stefan.klug@ideasonboard.com>","Subject":"Re: [PATCH v2 07/17] libcamera: matrix: Extend multiplication\n\toperator to heterogenous types","From":"Kieran Bingham <kieran.bingham@ideasonboard.com>","Cc":"Stefan Klug <stefan.klug@ideasonboard.com>,\n\tLaurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Stefan Klug <stefan.klug@ideasonboard.com>,\n\tlibcamera-devel@lists.libcamera.org","Date":"Mon, 31 Mar 2025 18:08:42 +0100","Message-ID":"<174344092293.3394313.1704010105580098312@ping.linuxembedded.co.uk>","User-Agent":"alot/0.10","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>"}}]