[{"id":21611,"web_url":"https://patchwork.libcamera.org/comment/21611/","msgid":"<163880564656.995700.8333679699947672619@Monstersaurus>","date":"2021-12-06T15:47:26","subject":"Re: [libcamera-devel] [PATCH 1/2] libcamera: base: utils: Add\n\tabs_diff() utility function","submitter":{"id":4,"url":"https://patchwork.libcamera.org/api/people/4/","name":"Kieran Bingham","email":"kieran.bingham@ideasonboard.com"},"content":"Quoting Laurent Pinchart (2021-12-06 15:26:43)\n> The abs_diff() function computes the absolute difference of two\n> elements. This may seem trivial at first, but can lead to unexpected\n> results when operating on unsigned operands. A common implementation\n> of the absolute difference of two unsigned int (used through the\n> libcamera code base) is\n> \n>         std::abs(static_cast<int>(a - b))\n> \n> but doesn't return the expected result when either a or b is larger than\n> UINT_MAX / 2 due to overflows. The abs_diff() function offers a safe\n> alternative.\n> \n> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n> ---\n>  include/libcamera/base/utils.h |  9 +++++++++\n>  src/libcamera/base/utils.cpp   | 17 +++++++++++++++++\n>  2 files changed, 26 insertions(+)\n> \n> diff --git a/include/libcamera/base/utils.h b/include/libcamera/base/utils.h\n> index 3a803176693d..9ab18101cf27 100644\n> --- a/include/libcamera/base/utils.h\n> +++ b/include/libcamera/base/utils.h\n> @@ -346,6 +346,15 @@ public:\n>         }\n>  };\n>  \n> +template<typename T>\n> +decltype(auto) abs_diff(const T &a, const T &b)\n> +{\n> +       if (a < b)\n> +               return b - a;\n> +       else\n> +               return a - b;\n> +}\n> +\n>  } /* namespace utils */\n>  \n>  #ifndef __DOXYGEN__\n> diff --git a/src/libcamera/base/utils.cpp b/src/libcamera/base/utils.cpp\n> index 45b92b670837..8cf8a5b2c104 100644\n> --- a/src/libcamera/base/utils.cpp\n> +++ b/src/libcamera/base/utils.cpp\n> @@ -437,6 +437,23 @@ std::string toAscii(const std::string &str)\n>   * \\return True if \\a Duration is a non-zero time value, False otherwise\n>   */\n>  \n> +/**\n> + * \\fn abs_diff(const T& a, const T& b)\n> + * \\brief Calculates the absolute value of the difference between two elements\n> + * \\param[in] a The first element\n> + * \\param[in] b The second element\n> + *\n> + * This function calculates the absolute value of the difference between two\n> + * elements of the same type, in such a way that a negative value will never\n> + * occur during the calculation.\n> + *\n> + * This is inspired by the std::abs_diff() candidate proposed in N4318\n> + * (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4318.pdf).\n\nIt's unfortunate that this proposal hasn't progressed.\nI'm happy to see an implementation in our utils though.\n\nReviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n\n> + *\n> + * \\return The absolute value of the difference of the two parameters \\a a and\n> + * \\a b\n> + */\n> +\n>  } /* namespace utils */\n>  \n>  #ifndef __DOXYGEN__\n> -- \n> Regards,\n> \n> Laurent Pinchart\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 DB14CBDB13\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon,  6 Dec 2021 15:47:30 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 3DC0560876;\n\tMon,  6 Dec 2021 16:47:30 +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 A175F60725\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon,  6 Dec 2021 16:47:29 +0100 (CET)","from pendragon.ideasonboard.com\n\t(cpc89244-aztw30-2-0-cust3082.18-1.cable.virginm.net [86.31.172.11])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 40CCE547;\n\tMon,  6 Dec 2021 16:47:29 +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=\"nel7pNYg\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1638805649;\n\tbh=Y9i3ly832VdBQ4m40rBlhUtfZ/JDG3wchrRYdr6YHG0=;\n\th=In-Reply-To:References:Subject:From:To:Date:From;\n\tb=nel7pNYgwf3Su1tN5Nx8w1VEt+pn+h7sjpzhBsHIthYec5IXLfnQlujhc/vF+UPeY\n\tt/upRnEXY/ykjsDnoqhl6h6H/P/x2DO8dsKTUMeWSrTOexJtOzSE2szaWDDfH3ma3p\n\tY1rhOwUgLU/v3rqMp8ynFO46Z8d1zYuH+NMNorfA=","Content-Type":"text/plain; charset=\"utf-8\"","MIME-Version":"1.0","Content-Transfer-Encoding":"quoted-printable","In-Reply-To":"<20211206152644.4863-2-laurent.pinchart@ideasonboard.com>","References":"<20211206152644.4863-1-laurent.pinchart@ideasonboard.com>\n\t<20211206152644.4863-2-laurent.pinchart@ideasonboard.com>","From":"Kieran Bingham <kieran.bingham@ideasonboard.com>","To":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>,\n\tlibcamera-devel@lists.libcamera.org","Date":"Mon, 06 Dec 2021 15:47:26 +0000","Message-ID":"<163880564656.995700.8333679699947672619@Monstersaurus>","User-Agent":"alot/0.10","Subject":"Re: [libcamera-devel] [PATCH 1/2] libcamera: base: utils: Add\n\tabs_diff() utility function","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":21647,"web_url":"https://patchwork.libcamera.org/comment/21647/","msgid":"<fa1c60db-c550-be79-72c3-cc4539c5b1ff@ideasonboard.com>","date":"2021-12-07T16:08:48","subject":"Re: [libcamera-devel] [PATCH 1/2] libcamera: base: utils: Add\n\tabs_diff() utility function","submitter":{"id":86,"url":"https://patchwork.libcamera.org/api/people/86/","name":"Umang Jain","email":"umang.jain@ideasonboard.com"},"content":"Hi Laurent,\n\nThanks for the patch\n\nOn 12/6/21 8:56 PM, Laurent Pinchart wrote:\n> The abs_diff() function computes the absolute difference of two\n> elements. This may seem trivial at first, but can lead to unexpected\n> results when operating on unsigned operands. A common implementation\n> of the absolute difference of two unsigned int (used through the\n> libcamera code base) is\n>\n> \tstd::abs(static_cast<int>(a - b))\n>\n> but doesn't return the expected result when either a or b is larger than\n> UINT_MAX / 2 due to overflows. The abs_diff() function offers a safe\n> alternative.\n>\n> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\n\nReviewed-by: Umang Jain <umang.jain@ideasonboard.com>\n\n> ---\n>   include/libcamera/base/utils.h |  9 +++++++++\n>   src/libcamera/base/utils.cpp   | 17 +++++++++++++++++\n>   2 files changed, 26 insertions(+)\n>\n> diff --git a/include/libcamera/base/utils.h b/include/libcamera/base/utils.h\n> index 3a803176693d..9ab18101cf27 100644\n> --- a/include/libcamera/base/utils.h\n> +++ b/include/libcamera/base/utils.h\n> @@ -346,6 +346,15 @@ public:\n>   \t}\n>   };\n>   \n> +template<typename T>\n> +decltype(auto) abs_diff(const T &a, const T &b)\n> +{\n> +\tif (a < b)\n> +\t\treturn b - a;\n> +\telse\n> +\t\treturn a - b;\n> +}\n> +\n>   } /* namespace utils */\n>   \n>   #ifndef __DOXYGEN__\n> diff --git a/src/libcamera/base/utils.cpp b/src/libcamera/base/utils.cpp\n> index 45b92b670837..8cf8a5b2c104 100644\n> --- a/src/libcamera/base/utils.cpp\n> +++ b/src/libcamera/base/utils.cpp\n> @@ -437,6 +437,23 @@ std::string toAscii(const std::string &str)\n>    * \\return True if \\a Duration is a non-zero time value, False otherwise\n>    */\n>   \n> +/**\n> + * \\fn abs_diff(const T& a, const T& b)\n> + * \\brief Calculates the absolute value of the difference between two elements\n> + * \\param[in] a The first element\n> + * \\param[in] b The second element\n> + *\n> + * This function calculates the absolute value of the difference between two\n> + * elements of the same type, in such a way that a negative value will never\n> + * occur during the calculation.\n> + *\n> + * This is inspired by the std::abs_diff() candidate proposed in N4318\n> + * (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4318.pdf).\n> + *\n> + * \\return The absolute value of the difference of the two parameters \\a a and\n> + * \\a b\n> + */\n> +\n>   } /* namespace utils */\n>   \n>   #ifndef __DOXYGEN__","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 5C5C3BDB13\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue,  7 Dec 2021 16:08:58 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 873C46086A;\n\tTue,  7 Dec 2021 17:08:57 +0100 (CET)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id B501260592\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue,  7 Dec 2021 17:08:55 +0100 (CET)","from [IPv6:2401:4900:1f3e:16f1:4e1b:f869:54d4:8c0c] (unknown\n\t[IPv6:2401:4900:1f3e:16f1:4e1b:f869:54d4:8c0c])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 1FA49556;\n\tTue,  7 Dec 2021 17:08:53 +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=\"VKFYi/up\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1638893335;\n\tbh=V8v/jprAV1ItafxazAGJxWw95rNsNSa8MJZbgUZiZsU=;\n\th=Subject:To:References:From:Date:In-Reply-To:From;\n\tb=VKFYi/up3+7x4EZk31VDKhm6XhTaaUrNB0htb659ecmbiKtUhqmqGHbEt3Xx8sXxW\n\tEvXKsj7JuLRaPUKJOfrn059Ue456cXbkEC9Aq3r4pFgrXZvkzbK8YwdKOYWJ64Z3h5\n\tvGpd/utZ6FkgX4YFmtaXirs76j9vRLDPAl2A6Pe8=","To":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>,\n\tlibcamera-devel@lists.libcamera.org","References":"<20211206152644.4863-1-laurent.pinchart@ideasonboard.com>\n\t<20211206152644.4863-2-laurent.pinchart@ideasonboard.com>","From":"Umang Jain <umang.jain@ideasonboard.com>","Message-ID":"<fa1c60db-c550-be79-72c3-cc4539c5b1ff@ideasonboard.com>","Date":"Tue, 7 Dec 2021 21:38:48 +0530","User-Agent":"Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101\n\tThunderbird/78.10.2","MIME-Version":"1.0","In-Reply-To":"<20211206152644.4863-2-laurent.pinchart@ideasonboard.com>","Content-Type":"text/plain; charset=utf-8; format=flowed","Content-Transfer-Encoding":"7bit","Content-Language":"en-US","Subject":"Re: [libcamera-devel] [PATCH 1/2] libcamera: base: utils: Add\n\tabs_diff() utility function","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>"}}]