[{"id":32246,"web_url":"https://patchwork.libcamera.org/comment/32246/","msgid":"<OLXNo2tSn3EWe5aXyNiAiGhRyr3gAgpne-7JaztWkCtnOtALPkEsrk3QqevyJxhWh8_8yBF3tAs7wTXHo80dMXCbdlYDBevix_R9Lt5YgIM=@protonmail.com>","date":"2024-11-18T23:40:16","subject":"Re: [PATCH v3 06/17] ipa: libipa: vector: Generalize arithmetic\n\toperators","submitter":{"id":133,"url":"https://patchwork.libcamera.org/api/people/133/","name":"Pőcze Barnabás","email":"pobrn@protonmail.com"},"content":"Hi\n\n\n2024. november 18., hétfő 23:16 keltezéssel, Laurent Pinchart <laurent.pinchart@ideasonboard.com> írta:\n\n> Instead of hand-coding all arithmetic operators, implement them based on\n> a generic apply() function that takes an operator-specific\n> std::function. This will simplify adding missing arithmetic operators.\n> \n> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n> Reviewed-by: Milan Zamazal <mzamazal@redhat.com>\n> ---\n> Changes since v2:\n> \n> - Include <algorithm>\n> - Use std::plus, std::minus, std::multiplies and std::divides\n> ---\n>  src/ipa/libipa/vector.h | 50 ++++++++++++++++++++++++-----------------\n>  1 file changed, 30 insertions(+), 20 deletions(-)\n> \n> diff --git a/src/ipa/libipa/vector.h b/src/ipa/libipa/vector.h\n> index 2abb5536361c..76fd2934b870 100644\n> --- a/src/ipa/libipa/vector.h\n> +++ b/src/ipa/libipa/vector.h\n> @@ -6,8 +6,10 @@\n>   */\n>  #pragma once\n> \n> +#include <algorithm>\n>  #include <array>\n>  #include <cmath>\n> +#include <functional>\n>  #include <optional>\n>  #include <ostream>\n> \n> @@ -78,36 +80,24 @@ public:\n>  \t\treturn ret;\n>  \t}\n> \n> -\tconstexpr Vector<T, Rows> operator-(const Vector<T, Rows> &other) const\n> +\tconstexpr Vector operator-(const Vector &other) const\n>  \t{\n> -\t\tVector<T, Rows> ret;\n> -\t\tfor (unsigned int i = 0; i < Rows; i++)\n> -\t\t\tret[i] = data_[i] - other[i];\n> -\t\treturn ret;\n> +\t\treturn apply(*this, other, std::minus<>{});\n>  \t}\n> \n> -\tconstexpr Vector<T, Rows> operator+(const Vector<T, Rows> &other) const\n> +\tconstexpr Vector operator+(const Vector &other) const\n>  \t{\n> -\t\tVector<T, Rows> ret;\n> -\t\tfor (unsigned int i = 0; i < Rows; i++)\n> -\t\t\tret[i] = data_[i] + other[i];\n> -\t\treturn ret;\n> +\t\treturn apply(*this, other, std::plus<>{});\n>  \t}\n> \n> -\tconstexpr Vector<T, Rows> operator*(T factor) const\n> +\tconstexpr Vector operator*(T factor) const\n>  \t{\n> -\t\tVector<T, Rows> ret;\n> -\t\tfor (unsigned int i = 0; i < Rows; i++)\n> -\t\t\tret[i] = data_[i] * factor;\n> -\t\treturn ret;\n> +\t\treturn apply(*this, factor, std::multiplies<>{});\n>  \t}\n> \n> -\tconstexpr Vector<T, Rows> operator/(T factor) const\n> +\tconstexpr Vector operator/(T factor) const\n>  \t{\n> -\t\tVector<T, Rows> ret;\n> -\t\tfor (unsigned int i = 0; i < Rows; i++)\n> -\t\t\tret[i] = data_[i] / factor;\n> -\t\treturn ret;\n> +\t\treturn apply(*this, factor, std::divides<>{});\n>  \t}\n> \n>  \tconstexpr T dot(const Vector<T, Rows> &other) const\n> @@ -182,6 +172,26 @@ public:\n>  \t}\n> \n>  private:\n> +\tstatic constexpr Vector apply(const Vector &lhs, const Vector &rhs, std::function<T(T, T)> func)\n> +\t{\n> +\t\tVector result;\n> +\t\tstd::transform(lhs.data_.begin(), lhs.data_.end(),\n> +\t\t\t       rhs.data_.begin(), result.data_.begin(),\n> +\t\t\t       func);\n> +\n> +\t\treturn result;\n> +\t}\n> +\n> +\tstatic constexpr Vector apply(const Vector &lhs, T rhs, std::function<T(T, T)> func)\n> +\t{\n> +\t\tVector result;\n> +\t\tstd::transform(lhs.data_.begin(), lhs.data_.end(),\n> +\t\t\t       result.data_.begin(),\n> +\t\t\t       [&func, rhs](T v) { return func(v, rhs); });\n> +\n> +\t\treturn result;\n> +\t}\n\n`std::function` is quite heavy. I think making `apply()` a template would be\nbeneficial wrt. performance.\n\n\nRegards,\nBarnabás Pőcze\n\n> +\n>  \tstd::array<T, Rows> data_;\n>  };\n> \n> --\n> Regards,\n> \n> Laurent Pinchart","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 E818FC3260\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon, 18 Nov 2024 23:40:23 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id C8290658FA;\n\tTue, 19 Nov 2024 00:40:22 +0100 (CET)","from mail-4316.protonmail.ch (mail-4316.protonmail.ch\n\t[185.70.43.16])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 66C126586D\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 19 Nov 2024 00:40:21 +0100 (CET)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (2048-bit key;\n\tunprotected) header.d=protonmail.com header.i=@protonmail.com\n\theader.b=\"eC3Vk6CR\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com;\n\ts=protonmail3; t=1731973220; x=1732232420;\n\tbh=TVBOtrgxazUS4c/2GaTqEhijVzC2ir73PnkZWBp+ip4=;\n\th=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References:\n\tFeedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID:\n\tMessage-ID:BIMI-Selector:List-Unsubscribe:List-Unsubscribe-Post;\n\tb=eC3Vk6CRHqbc21RKfnzEPu7EyMa3fuh/notD7Ca5T3jJePmYh+O37mEoH41140hZ+\n\tpuf8FErxV7+3gYwERbgBbM3krFEDAUsN55ULp55BiRqMnplaPzjUeRZPkc/oqAFule\n\tmFuq7G7AuigTXwwE1SaEIMZwRyCfYW2p5SByDkG8fIW9itLMgfPkTVt5o4YLPJ4PL7\n\t6HkCWshLB1QDRKB6p/cd7KRvkJiVZJbZY4S3xaMMwp0mIIf4E+yDW6r2ZU0S/BL0WQ\n\tDygZFDC1zGUPqIRi+OS/v21IWVDH49e/gcujWZtIHSz7oguZicdku5rj5oVHDurojl\n\ttCJQWjz8JSZug==","Date":"Mon, 18 Nov 2024 23:40:16 +0000","To":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","From":"=?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= <pobrn@protonmail.com>","Cc":"libcamera-devel@lists.libcamera.org","Subject":"Re: [PATCH v3 06/17] ipa: libipa: vector: Generalize arithmetic\n\toperators","Message-ID":"<OLXNo2tSn3EWe5aXyNiAiGhRyr3gAgpne-7JaztWkCtnOtALPkEsrk3QqevyJxhWh8_8yBF3tAs7wTXHo80dMXCbdlYDBevix_R9Lt5YgIM=@protonmail.com>","In-Reply-To":"<20241118221618.13953-7-laurent.pinchart@ideasonboard.com>","References":"<20241118221618.13953-1-laurent.pinchart@ideasonboard.com>\n\t<20241118221618.13953-7-laurent.pinchart@ideasonboard.com>","Feedback-ID":"20568564:user:proton","X-Pm-Message-ID":"9789615cc407d456c619936cca079b162f1a9889","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Transfer-Encoding":"quoted-printable","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>"}},{"id":32255,"web_url":"https://patchwork.libcamera.org/comment/32255/","msgid":"<20241119083953.GE31681@pendragon.ideasonboard.com>","date":"2024-11-19T08:39:53","subject":"Re: [PATCH v3 06/17] ipa: libipa: vector: Generalize arithmetic\n\toperators","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Barnabás,\n\nOn Mon, Nov 18, 2024 at 11:40:16PM +0000, Barnabás Pőcze wrote:\n> 2024. november 18., hétfő 23:16 keltezéssel, Laurent Pinchart írta:\n> \n> > Instead of hand-coding all arithmetic operators, implement them based on\n> > a generic apply() function that takes an operator-specific\n> > std::function. This will simplify adding missing arithmetic operators.\n> > \n> > Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n> > Reviewed-by: Milan Zamazal <mzamazal@redhat.com>\n> > ---\n> > Changes since v2:\n> > \n> > - Include <algorithm>\n> > - Use std::plus, std::minus, std::multiplies and std::divides\n> > ---\n> >  src/ipa/libipa/vector.h | 50 ++++++++++++++++++++++++-----------------\n> >  1 file changed, 30 insertions(+), 20 deletions(-)\n> > \n> > diff --git a/src/ipa/libipa/vector.h b/src/ipa/libipa/vector.h\n> > index 2abb5536361c..76fd2934b870 100644\n> > --- a/src/ipa/libipa/vector.h\n> > +++ b/src/ipa/libipa/vector.h\n> > @@ -6,8 +6,10 @@\n> >   */\n> >  #pragma once\n> > \n> > +#include <algorithm>\n> >  #include <array>\n> >  #include <cmath>\n> > +#include <functional>\n> >  #include <optional>\n> >  #include <ostream>\n> > \n> > @@ -78,36 +80,24 @@ public:\n> >  \t\treturn ret;\n> >  \t}\n> > \n> > -\tconstexpr Vector<T, Rows> operator-(const Vector<T, Rows> &other) const\n> > +\tconstexpr Vector operator-(const Vector &other) const\n> >  \t{\n> > -\t\tVector<T, Rows> ret;\n> > -\t\tfor (unsigned int i = 0; i < Rows; i++)\n> > -\t\t\tret[i] = data_[i] - other[i];\n> > -\t\treturn ret;\n> > +\t\treturn apply(*this, other, std::minus<>{});\n> >  \t}\n> > \n> > -\tconstexpr Vector<T, Rows> operator+(const Vector<T, Rows> &other) const\n> > +\tconstexpr Vector operator+(const Vector &other) const\n> >  \t{\n> > -\t\tVector<T, Rows> ret;\n> > -\t\tfor (unsigned int i = 0; i < Rows; i++)\n> > -\t\t\tret[i] = data_[i] + other[i];\n> > -\t\treturn ret;\n> > +\t\treturn apply(*this, other, std::plus<>{});\n> >  \t}\n> > \n> > -\tconstexpr Vector<T, Rows> operator*(T factor) const\n> > +\tconstexpr Vector operator*(T factor) const\n> >  \t{\n> > -\t\tVector<T, Rows> ret;\n> > -\t\tfor (unsigned int i = 0; i < Rows; i++)\n> > -\t\t\tret[i] = data_[i] * factor;\n> > -\t\treturn ret;\n> > +\t\treturn apply(*this, factor, std::multiplies<>{});\n> >  \t}\n> > \n> > -\tconstexpr Vector<T, Rows> operator/(T factor) const\n> > +\tconstexpr Vector operator/(T factor) const\n> >  \t{\n> > -\t\tVector<T, Rows> ret;\n> > -\t\tfor (unsigned int i = 0; i < Rows; i++)\n> > -\t\t\tret[i] = data_[i] / factor;\n> > -\t\treturn ret;\n> > +\t\treturn apply(*this, factor, std::divides<>{});\n> >  \t}\n> > \n> >  \tconstexpr T dot(const Vector<T, Rows> &other) const\n> > @@ -182,6 +172,26 @@ public:\n> >  \t}\n> > \n> >  private:\n> > +\tstatic constexpr Vector apply(const Vector &lhs, const Vector &rhs, std::function<T(T, T)> func)\n> > +\t{\n> > +\t\tVector result;\n> > +\t\tstd::transform(lhs.data_.begin(), lhs.data_.end(),\n> > +\t\t\t       rhs.data_.begin(), result.data_.begin(),\n> > +\t\t\t       func);\n> > +\n> > +\t\treturn result;\n> > +\t}\n> > +\n> > +\tstatic constexpr Vector apply(const Vector &lhs, T rhs, std::function<T(T, T)> func)\n> > +\t{\n> > +\t\tVector result;\n> > +\t\tstd::transform(lhs.data_.begin(), lhs.data_.end(),\n> > +\t\t\t       result.data_.begin(),\n> > +\t\t\t       [&func, rhs](T v) { return func(v, rhs); });\n> > +\n> > +\t\treturn result;\n> > +\t}\n> \n> `std::function` is quite heavy. I think making `apply()` a template would be\n> beneficial wrt. performance.\n\nI'll try it out in v4.\n\n> > +\n> >  \tstd::array<T, Rows> data_;\n> >  };\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 436BBC326B\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 19 Nov 2024 08:40:03 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id CD71965EDE;\n\tTue, 19 Nov 2024 09:40:02 +0100 (CET)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 2D93065905\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 19 Nov 2024 09:40:02 +0100 (CET)","from pendragon.ideasonboard.com (81-175-209-231.bb.dnainternet.fi\n\t[81.175.209.231])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id B7CE3D52;\n\tTue, 19 Nov 2024 09:39:44 +0100 (CET)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"h/q3nbJe\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1732005584;\n\tbh=J/WPv+AWVnuazGJhlJRkHVwUkJ2v5ZCKAdAE0YqC7Fw=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=h/q3nbJeGh6WmjhYE3NWQj51JYrcd2fDcXyNtLkQmjxuYhi25O2nGivyKfdYqGnDN\n\tbn0fAOhR/TyQpGWEjjeMA2ls4KJqNH0nRqjdyk3HbgSe5buw7qa9P2Q5I8KkQ3N7nK\n\tr+7paroimdMGHeoix9wsoMdAVzaC6vIHVrMBXMUg=","Date":"Tue, 19 Nov 2024 10:39:53 +0200","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"=?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= <pobrn@protonmail.com>","Cc":"libcamera-devel@lists.libcamera.org","Subject":"Re: [PATCH v3 06/17] ipa: libipa: vector: Generalize arithmetic\n\toperators","Message-ID":"<20241119083953.GE31681@pendragon.ideasonboard.com>","References":"<20241118221618.13953-1-laurent.pinchart@ideasonboard.com>\n\t<20241118221618.13953-7-laurent.pinchart@ideasonboard.com>\n\t<OLXNo2tSn3EWe5aXyNiAiGhRyr3gAgpne-7JaztWkCtnOtALPkEsrk3QqevyJxhWh8_8yBF3tAs7wTXHo80dMXCbdlYDBevix_R9Lt5YgIM=@protonmail.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","Content-Transfer-Encoding":"8bit","In-Reply-To":"<OLXNo2tSn3EWe5aXyNiAiGhRyr3gAgpne-7JaztWkCtnOtALPkEsrk3QqevyJxhWh8_8yBF3tAs7wTXHo80dMXCbdlYDBevix_R9Lt5YgIM=@protonmail.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>"}}]