[{"id":2240,"web_url":"https://patchwork.libcamera.org/comment/2240/","msgid":"<20190713115319.GC4839@pendragon.ideasonboard.com>","date":"2019-07-13T11:53:19","subject":"Re: [libcamera-devel] [PATCH] libcamera: utils: Add clamp()","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Niklas,\n\nThank you for the patch.\n\nOn Sat, Jul 13, 2019 at 08:39:46PM +0900, Niklas Söderlund wrote:\n> C++11 does not support std::clamp(), add a custom implementation in\n> utils.\n> \n> Signed-off-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>\n> ---\n>  src/libcamera/include/utils.h | 7 +++++++\n>  src/libcamera/utils.cpp       | 9 +++++++++\n>  2 files changed, 16 insertions(+)\n> \n> diff --git a/src/libcamera/include/utils.h b/src/libcamera/include/utils.h\n> index 97bd470a45b050ef..beb2ce3ff7da9352 100644\n> --- a/src/libcamera/include/utils.h\n> +++ b/src/libcamera/include/utils.h\n> @@ -7,6 +7,7 @@\n>  #ifndef __LIBCAMERA_UTILS_H__\n>  #define __LIBCAMERA_UTILS_H__\n>  \n> +#include <algorithm>\n>  #include <memory>\n>  \n>  #define ARRAY_SIZE(a)\t(sizeof(a) / sizeof(a[0]))\n> @@ -45,6 +46,12 @@ unsigned int set_overlap(InputIt1 first1, InputIt1 last1,\n>  \treturn count;\n>  }\n>  \n> +/* C++11 doesn't provide std::clamp */\n> +template <typename T>\n> +T clamp(const T& v, const T& lo, const T& hi) {\n\nThe { should go to the next line.\n\nThe function should return a const T&. Ideally it should even be a\nconstexpr, but std::max and std::min are only constexpr starting in\nC++14, so you would have to reimplement that, and it's likely not worth\nit.\n\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\nwith this fixed.\n\n> +\treturn std::max(lo, std::min(v, hi));\n> +}\n> +\n>  } /* namespace utils */\n>  \n>  } /* namespace libcamera */\n> diff --git a/src/libcamera/utils.cpp b/src/libcamera/utils.cpp\n> index ef365366f29b426e..b0ca3565ee5a0d83 100644\n> --- a/src/libcamera/utils.cpp\n> +++ b/src/libcamera/utils.cpp\n> @@ -85,6 +85,15 @@ char *secure_getenv(const char *name)\n>   * \\return The number of elements in the intersection of the two ranges\n>   */\n>  \n> +/**\n> + * \\fn libcamera::utils::clamp(const T& v, const T& lo, const T& hi)\n> + * \\param[in] v The value to clamp\n> + * \\param[in] lo The lower boundary to clamp v to\n> + * \\param[in] hi The higher boundary to clamp v to\n> + *\n> + * \\return lo if v is less than lo, hi if v is greater than hi, otherwise v\n> + */\n> +\n>  } /* namespace utils */\n>  \n>  } /* namespace libcamera */","headers":{"Return-Path":"<laurent.pinchart@ideasonboard.com>","Received":["from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 1235F60E40\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSat, 13 Jul 2019 13:53:48 +0200 (CEST)","from pendragon.ideasonboard.com (softbank126209254147.bbtec.net\n\t[126.209.254.147])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 125D12B2;\n\tSat, 13 Jul 2019 13:53:46 +0200 (CEST)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1563018827;\n\tbh=8YYEpMwVs+9Y3TK7bM6iZs1CLfistAJy8fKlD67SgHY=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=hDjGTEmK0+O196QlCiGHHU6hIiIZJozDEHqOaBkZLOBNqCE+18g+L/wVZwGrmc95s\n\tSYhC94p/0FGVeQ+cadhYoA3NAMw50HAVu5VEURzLc26omWWPC368Aun4tcSxCLcIxQ\n\ttwqhvFJIEFCwwHZJE0DLIx+4CaI7REuTx9c6ckY4=","Date":"Sat, 13 Jul 2019 14:53:19 +0300","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Niklas =?utf-8?q?S=C3=B6derlund?= <niklas.soderlund@ragnatech.se>","Cc":"libcamera-devel@lists.libcamera.org","Message-ID":"<20190713115319.GC4839@pendragon.ideasonboard.com>","References":"<20190713113946.25744-1-niklas.soderlund@ragnatech.se>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","Content-Transfer-Encoding":"8bit","In-Reply-To":"<20190713113946.25744-1-niklas.soderlund@ragnatech.se>","User-Agent":"Mutt/1.10.1 (2018-07-13)","Subject":"Re: [libcamera-devel] [PATCH] libcamera: utils: Add clamp()","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.23","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>","X-List-Received-Date":"Sat, 13 Jul 2019 11:53:48 -0000"}},{"id":2241,"web_url":"https://patchwork.libcamera.org/comment/2241/","msgid":"<20190713133837.GC15070@wyvern>","date":"2019-07-13T13:38:37","subject":"Re: [libcamera-devel] [PATCH] libcamera: utils: Add clamp()","submitter":{"id":5,"url":"https://patchwork.libcamera.org/api/people/5/","name":"Niklas Söderlund","email":"niklas.soderlund@ragnatech.se"},"content":"Pushed with Laurent's tag.\n\nOn 2019-07-13 14:53:19 +0300, Laurent Pinchart wrote:\n> Hi Niklas,\n> \n> Thank you for the patch.\n> \n> On Sat, Jul 13, 2019 at 08:39:46PM +0900, Niklas Söderlund wrote:\n> > C++11 does not support std::clamp(), add a custom implementation in\n> > utils.\n> > \n> > Signed-off-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>\n> > ---\n> >  src/libcamera/include/utils.h | 7 +++++++\n> >  src/libcamera/utils.cpp       | 9 +++++++++\n> >  2 files changed, 16 insertions(+)\n> > \n> > diff --git a/src/libcamera/include/utils.h b/src/libcamera/include/utils.h\n> > index 97bd470a45b050ef..beb2ce3ff7da9352 100644\n> > --- a/src/libcamera/include/utils.h\n> > +++ b/src/libcamera/include/utils.h\n> > @@ -7,6 +7,7 @@\n> >  #ifndef __LIBCAMERA_UTILS_H__\n> >  #define __LIBCAMERA_UTILS_H__\n> >  \n> > +#include <algorithm>\n> >  #include <memory>\n> >  \n> >  #define ARRAY_SIZE(a)\t(sizeof(a) / sizeof(a[0]))\n> > @@ -45,6 +46,12 @@ unsigned int set_overlap(InputIt1 first1, InputIt1 last1,\n> >  \treturn count;\n> >  }\n> >  \n> > +/* C++11 doesn't provide std::clamp */\n> > +template <typename T>\n> > +T clamp(const T& v, const T& lo, const T& hi) {\n> \n> The { should go to the next line.\n> \n> The function should return a const T&. Ideally it should even be a\n> constexpr, but std::max and std::min are only constexpr starting in\n> C++14, so you would have to reimplement that, and it's likely not worth\n> it.\n> \n> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n> \n> with this fixed.\n> \n> > +\treturn std::max(lo, std::min(v, hi));\n> > +}\n> > +\n> >  } /* namespace utils */\n> >  \n> >  } /* namespace libcamera */\n> > diff --git a/src/libcamera/utils.cpp b/src/libcamera/utils.cpp\n> > index ef365366f29b426e..b0ca3565ee5a0d83 100644\n> > --- a/src/libcamera/utils.cpp\n> > +++ b/src/libcamera/utils.cpp\n> > @@ -85,6 +85,15 @@ char *secure_getenv(const char *name)\n> >   * \\return The number of elements in the intersection of the two ranges\n> >   */\n> >  \n> > +/**\n> > + * \\fn libcamera::utils::clamp(const T& v, const T& lo, const T& hi)\n> > + * \\param[in] v The value to clamp\n> > + * \\param[in] lo The lower boundary to clamp v to\n> > + * \\param[in] hi The higher boundary to clamp v to\n> > + *\n> > + * \\return lo if v is less than lo, hi if v is greater than hi, otherwise v\n> > + */\n> > +\n> >  } /* namespace utils */\n> >  \n> >  } /* namespace libcamera */\n> \n> -- \n> Regards,\n> \n> Laurent Pinchart","headers":{"Return-Path":"<niklas.soderlund@ragnatech.se>","Received":["from mail-pf1-x441.google.com (mail-pf1-x441.google.com\n\t[IPv6:2607:f8b0:4864:20::441])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 8F5156156A\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSat, 13 Jul 2019 15:38:43 +0200 (CEST)","by mail-pf1-x441.google.com with SMTP id u14so5522269pfn.2\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSat, 13 Jul 2019 06:38:43 -0700 (PDT)","from localhost (softbank126209254147.bbtec.net. [126.209.254.147])\n\tby smtp.gmail.com with ESMTPSA id\n\t65sm12650666pff.148.2019.07.13.06.38.39\n\t(version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256);\n\tSat, 13 Jul 2019 06:38:40 -0700 (PDT)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=ragnatech-se.20150623.gappssmtp.com; s=20150623;\n\th=date:from:to:cc:subject:message-id:references:mime-version\n\t:content-disposition:content-transfer-encoding:in-reply-to\n\t:user-agent; bh=os+ly9SLTJERdPGFWi8RW9MZxP0C0eCiVpUAFWH0TeA=;\n\tb=AHxpPL2GP0smBpW/h4unzlWjYSIIuNeLvEyAdmf0Yadm7Ia9dSwRbZQvuZPv7EFJvA\n\t4S3m/qGvhRzY4rCU6OkobQOPuYj90CiPREVwpf0U7+quSf5YnFEFLYhHRWVkHHO2MnuR\n\tgqS899tyj5n8CziZH4dMfAKkq4UMGaTd2bsRLT3YsefK8TtzBa4NBnXr100qe612PRr5\n\tVPDKOF0RX/s7mSozPDUwWMjE/vxipf/5annxwr9PPpzAds57DN7DALr9ZTXJhdnllki6\n\t9WpF8jF/e6UFJ4RudYv38agIrDJZvM5oKJ0dB9ko8Mq0x5oCIRNlU0sdipkkbZ/Ce+sH\n\tEjsA==","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20161025;\n\th=x-gm-message-state:date:from:to:cc:subject:message-id:references\n\t:mime-version:content-disposition:content-transfer-encoding\n\t:in-reply-to:user-agent;\n\tbh=os+ly9SLTJERdPGFWi8RW9MZxP0C0eCiVpUAFWH0TeA=;\n\tb=JmutXanyLA3Sn3MpWaoiF4C1UqyG6Ac3XsghcOrdZPA8f9fLANQFLq0xbFXrhC6JL0\n\tq1A8hsghRux0dmkzV0ikxoUdhvebXfUv2mqcriCFQoVzcYS70Dt19RkqB5mf4NaIKOu5\n\tYZonLCUcWT6VAxKmBpaMFBZk1vZBHjtSq5UiDjjbM0A5vKDF2+MCip6pj6dHpxCVIVRW\n\tZ0E2/ePcF0jN+krf1lizxcxaEGx3l0t08hRZPa7DY2H73W/QokUhVwh+Wx4Vcul0bvJa\n\thyEx1TumfRKpAJBsc2w/YysISUvpYebVdEbOMVnnd01bdKS9JsJ1IJyS9PzKfMJz4iMa\n\t2vTw==","X-Gm-Message-State":"APjAAAWioW6zWaYm5xbJra1OAWhoyOEuKa7MVQxd27MxeyVbMkSLgELW\n\tjqRU8agH7ov0Qwt7bJn+gP0=","X-Google-Smtp-Source":"APXvYqxNv5klo9JIIaj2ybvDnDzswt6sfIHJJZXDNEMwMwuY8k4vMFrEb0UWpnZ6xH8MTz0WdFvZaA==","X-Received":"by 2002:a63:eb56:: with SMTP id\n\tb22mr17057669pgk.355.1563025121470; \n\tSat, 13 Jul 2019 06:38:41 -0700 (PDT)","Date":"Sat, 13 Jul 2019 22:38:37 +0900","From":"Niklas =?iso-8859-1?q?S=F6derlund?= <niklas.soderlund@ragnatech.se>","To":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org","Message-ID":"<20190713133837.GC15070@wyvern>","References":"<20190713113946.25744-1-niklas.soderlund@ragnatech.se>\n\t<20190713115319.GC4839@pendragon.ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=iso-8859-1","Content-Disposition":"inline","Content-Transfer-Encoding":"8bit","In-Reply-To":"<20190713115319.GC4839@pendragon.ideasonboard.com>","User-Agent":"Mutt/1.12.1 (2019-06-15)","Subject":"Re: [libcamera-devel] [PATCH] libcamera: utils: Add clamp()","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.23","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>","X-List-Received-Date":"Sat, 13 Jul 2019 13:38:44 -0000"}}]