[{"id":32219,"web_url":"https://patchwork.libcamera.org/comment/32219/","msgid":"<87frnoiueg.fsf@redhat.com>","date":"2024-11-18T12:44:07","subject":"Re: [RFC PATCH v2 05/12] ipa: libipa: vector: Generalize arithmetic\n\toperators","submitter":{"id":177,"url":"https://patchwork.libcamera.org/api/people/177/","name":"Milan Zamazal","email":"mzamazal@redhat.com"},"content":"Laurent Pinchart <laurent.pinchart@ideasonboard.com> writes:\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\nI always welcome deduplication!\n\n> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n> ---\n>  src/ipa/libipa/vector.h | 48 ++++++++++++++++++++++++-----------------\n>  1 file changed, 28 insertions(+), 20 deletions(-)\n>\n> diff --git a/src/ipa/libipa/vector.h b/src/ipa/libipa/vector.h\n> index 9dabdc28a540..8c4edfbaf85f 100644\n> --- a/src/ipa/libipa/vector.h\n> +++ b/src/ipa/libipa/vector.h\n> @@ -74,36 +74,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, [](T a, T b) { return a - b; });\n>  \t}\n\nCould std::minus be used here (and similarly for the other methods)?\n\nWith or without this:\n\nReviewed-by: Milan Zamazal <mzamazal@redhat.com>\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, [](T a, T b) { return a + b; });\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, [](T a, T b) { return a * b; });\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, [](T a, T b) { return a / b; });\n>  \t}\n>  \n>  \tconstexpr T dot(const Vector<T, Rows> &other) const\n> @@ -178,6 +166,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>  \tstd::array<T, Rows> data_;\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 3E75FC32DD\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon, 18 Nov 2024 12:44:16 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 5D983658CF;\n\tMon, 18 Nov 2024 13:44:15 +0100 (CET)","from us-smtp-delivery-124.mimecast.com\n\t(us-smtp-delivery-124.mimecast.com [170.10.129.124])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id B0E0760532\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 18 Nov 2024 13:44:13 +0100 (CET)","from mail-wm1-f72.google.com (mail-wm1-f72.google.com\n\t[209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS\n\t(version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id\n\tus-mta-157-JD6mmHQzPliKDchUNngYog-1; Mon, 18 Nov 2024 07:44:11 -0500","by mail-wm1-f72.google.com with SMTP id\n\t5b1f17b1804b1-4316655b2f1so20175205e9.0\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 18 Nov 2024 04:44:11 -0800 (PST)","from nuthatch ([2a00:102a:400a:489a:ffe9:aa41:63cd:fc2b])\n\tby smtp.gmail.com with ESMTPSA id\n\t5b1f17b1804b1-432dac1fb7asm154539995e9.42.2024.11.18.04.44.08\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tMon, 18 Nov 2024 04:44:09 -0800 (PST)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=redhat.com header.i=@redhat.com\n\theader.b=\"KNAQEcXo\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com;\n\ts=mimecast20190719; t=1731933852;\n\th=from:from:reply-to:subject:subject:date:date:message-id:message-id:\n\tto:to:cc:cc:mime-version:mime-version:content-type:content-type:\n\tin-reply-to:in-reply-to:references:references;\n\tbh=mCgh7aNDvbGD0owQDjhvrO6+G4Fo/dwtkcksRobs91k=;\n\tb=KNAQEcXoCcCyIcmwEuUqsnLK4SDopMDfDnMAg2tWz6bzO4Y8JTO1rfMnNUyHQC28Dq5P5J\n\txhldvdXEjyaXVoTT+tNi7ScrkWb6fCJ+f9AWkoY2At+jkkqfO6oDKLMtTkaoh2G44MOhYM\n\t25xyNxqv6fNsotQ3d9fTiwiDuzvkvj0=","X-MC-Unique":"JD6mmHQzPliKDchUNngYog-1","X-Mimecast-MFC-AGG-ID":"JD6mmHQzPliKDchUNngYog","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20230601; t=1731933850; x=1732538650;\n\th=mime-version:user-agent:message-id:date:references:in-reply-to\n\t:subject:cc:to:from:x-gm-message-state:from:to:cc:subject:date\n\t:message-id:reply-to;\n\tbh=mCgh7aNDvbGD0owQDjhvrO6+G4Fo/dwtkcksRobs91k=;\n\tb=sL5gI2m/oQwRELROfRmbRmhuMuSRVTdxqJACWUdR1KV00g3RXELZ7OaEmK/aCf2ddB\n\tM+vQmsZ8nWzpJj1j2rHQ3GdnsZjMO7mYM1rPPzirmZdyWatnpPIx2Fbw4AdGKgfAkeOD\n\t2fzfYjy9/D+zd0Nje9M49tfiYKSSApvben+wcAgrpaXiqMf9OeH1df8RHilXMgMH/z3i\n\tUG9lZHDWngegNnF/kMq5hRnMG0LQ+l85QRoIkI3i8itdEkjUhTKphEvTcWMMtj4jUqXB\n\t1goVV4Ubm0EXOAoydHX1o7qL0th1J8HXU0nTZVSKy6LpZ4zjYNB806CTWYsYS5vzwSCo\n\tH87w==","X-Gm-Message-State":"AOJu0Ywb8pifdbsIcjUa0Q5YnNgdHN9hZYGHwJvVl8qgL5ui2v28WNLA\n\tmrjZvWeosJoMswawtB2cNcbtkZ01WSM8A/8ciN9fhWgDJzy457DrGQhsSSlmNRKDJPFB1H/DqiQ\n\tcJGX628QHmMeJoSGM8nEASSYrRJgA+Um3i8nou7Eg7RV83barQqN4MojJ8oIjhJJVSFXKsFceqG\n\t9shMsMaJF/MQholzS4lyMw9tt/KR1Us70sDTxxVTDYKFRQWAazIlto0XY=","X-Received":["by 2002:a05:600c:4693:b0:431:55af:a22f with SMTP id\n\t5b1f17b1804b1-432df72c9e7mr107417075e9.13.1731933850025; \n\tMon, 18 Nov 2024 04:44:10 -0800 (PST)","by 2002:a05:600c:4693:b0:431:55af:a22f with SMTP id\n\t5b1f17b1804b1-432df72c9e7mr107416775e9.13.1731933849652; \n\tMon, 18 Nov 2024 04:44:09 -0800 (PST)"],"X-Google-Smtp-Source":"AGHT+IEw4VtWN4fdKXguqP8taXgK11jZAOUKRxQCu0XEQTWgPhWlTrxzCM3wf95TPgA32Sl8luAQlw==","From":"Milan Zamazal <mzamazal@redhat.com>","To":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org","Subject":"Re: [RFC PATCH v2 05/12] ipa: libipa: vector: Generalize arithmetic\n\toperators","In-Reply-To":"<20241118000738.18977-6-laurent.pinchart@ideasonboard.com>\n\t(Laurent Pinchart's message of \"Mon, 18 Nov 2024 02:07:31 +0200\")","References":"<20241118000738.18977-1-laurent.pinchart@ideasonboard.com>\n\t<20241118000738.18977-6-laurent.pinchart@ideasonboard.com>","Date":"Mon, 18 Nov 2024 13:44:07 +0100","Message-ID":"<87frnoiueg.fsf@redhat.com>","User-Agent":"Gnus/5.13 (Gnus v5.13)","MIME-Version":"1.0","X-Mimecast-Spam-Score":"0","X-Mimecast-MFC-PROC-ID":"wbsRaa0d4qZaE07hFagxCrH4oe_bKTPwE9AoMsckfO4_1731933850","X-Mimecast-Originator":"redhat.com","Content-Type":"text/plain","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":32224,"web_url":"https://patchwork.libcamera.org/comment/32224/","msgid":"<20241118135052.GO31681@pendragon.ideasonboard.com>","date":"2024-11-18T13:50:52","subject":"Re: [RFC PATCH v2 05/12] 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":"On Mon, Nov 18, 2024 at 01:44:07PM +0100, Milan Zamazal wrote:\n> Laurent Pinchart <laurent.pinchart@ideasonboard.com> writes:\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> I always welcome deduplication!\n> \n> > Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n> > ---\n> >  src/ipa/libipa/vector.h | 48 ++++++++++++++++++++++++-----------------\n> >  1 file changed, 28 insertions(+), 20 deletions(-)\n> >\n> > diff --git a/src/ipa/libipa/vector.h b/src/ipa/libipa/vector.h\n> > index 9dabdc28a540..8c4edfbaf85f 100644\n> > --- a/src/ipa/libipa/vector.h\n> > +++ b/src/ipa/libipa/vector.h\n> > @@ -74,36 +74,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, [](T a, T b) { return a - b; });\n> >  \t}\n> \n> Could std::minus be used here (and similarly for the other methods)?\n\nYes it can. I'll fix that.\n\n> With or without this:\n> \n> Reviewed-by: Milan Zamazal <mzamazal@redhat.com>\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, [](T a, T b) { return a + b; });\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, [](T a, T b) { return a * b; });\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, [](T a, T b) { return a / b; });\n> >  \t}\n> >  \n> >  \tconstexpr T dot(const Vector<T, Rows> &other) const\n> > @@ -178,6 +166,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> >  \tstd::array<T, Rows> data_;\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 CD0B1C32EA\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon, 18 Nov 2024 13:51:03 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 7D62060532;\n\tMon, 18 Nov 2024 14:51:03 +0100 (CET)","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 F301D60532\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 18 Nov 2024 14:51:01 +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 1349F316;\n\tMon, 18 Nov 2024 14:50:45 +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=\"caTfCkIu\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1731937845;\n\tbh=P7rw9iXVO2w7JU/4BVw8gG7SLlcHv/qFpASUDtBK2s0=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=caTfCkIu2CmL6n9tZamIHpXX0bwamR4AEbZLrxpLsVb6nx3nmAbr+SMhbZl/aCnjm\n\tx5HFW88an1KduDLn8bZc+r7Pp96obNJdRoXD7Cuq67HQ7y36oLSlewEWMHFOYjquuv\n\tY7TjL8vfX2lUkB7vnO804Dmj1D81eL6nizyolQOA=","Date":"Mon, 18 Nov 2024 15:50:52 +0200","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Milan Zamazal <mzamazal@redhat.com>","Cc":"libcamera-devel@lists.libcamera.org","Subject":"Re: [RFC PATCH v2 05/12] ipa: libipa: vector: Generalize arithmetic\n\toperators","Message-ID":"<20241118135052.GO31681@pendragon.ideasonboard.com>","References":"<20241118000738.18977-1-laurent.pinchart@ideasonboard.com>\n\t<20241118000738.18977-6-laurent.pinchart@ideasonboard.com>\n\t<87frnoiueg.fsf@redhat.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<87frnoiueg.fsf@redhat.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>"}}]