{"id":15052,"url":"https://patchwork.libcamera.org/api/1.1/patches/15052/?format=json","web_url":"https://patchwork.libcamera.org/patch/15052/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20211206152644.4863-2-laurent.pinchart@ideasonboard.com>","date":"2021-12-06T15:26:43","name":"[libcamera-devel,1/2] libcamera: base: utils: Add abs_diff() utility function","commit_ref":"f413f944d789911c9ded831ac45a7674c129ceba","pull_url":null,"state":"accepted","archived":false,"hash":"8007949c1797616ca40253daab929b5b531048d1","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/1.1/people/2/?format=json","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"delegate":{"id":14,"url":"https://patchwork.libcamera.org/api/1.1/users/14/?format=json","username":"pinchartl","first_name":"Laurent","last_name":"Pinchart","email":"laurent.pinchart@ideasonboard.com"},"mbox":"https://patchwork.libcamera.org/patch/15052/mbox/","series":[{"id":2819,"url":"https://patchwork.libcamera.org/api/1.1/series/2819/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=2819","date":"2021-12-06T15:26:42","name":"libcamera: Introduce utils::abs_diff()","version":1,"mbox":"https://patchwork.libcamera.org/series/2819/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/15052/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/15052/checks/","tags":{},"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 69D4FBDB13\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon,  6 Dec 2021 15:27:19 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 222216087E;\n\tMon,  6 Dec 2021 16:27:19 +0100 (CET)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 1677760725\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon,  6 Dec 2021 16:27:17 +0100 (CET)","from pendragon.lan (62-78-145-57.bb.dnainternet.fi [62.78.145.57])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id A86E5547\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon,  6 Dec 2021 16:27:16 +0100 (CET)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"KdGy/93S\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1638804436;\n\tbh=43LZ5Gae5XpndtQ7zkW9WZrDi1BhsCVfMxoiB4RbMzg=;\n\th=From:To:Subject:Date:In-Reply-To:References:From;\n\tb=KdGy/93SMsdINo/rj6g58g1YM1svy/aDq9ZRgrY3gk6k2uoxXX7BPakiZgZ6wCFGX\n\tfKQrwMWyQTG6AK7t8kMdO8GXGr3U4io4Xi3fgSNN1u/IV9deMtI6qTafJ2gNBD038l\n\t5VyXyxjqMJThK77noat6usEtZYsbiTBi8KMNGBq8=","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Mon,  6 Dec 2021 17:26:43 +0200","Message-Id":"<20211206152644.4863-2-laurent.pinchart@ideasonboard.com>","X-Mailer":"git-send-email 2.32.0","In-Reply-To":"<20211206152644.4863-1-laurent.pinchart@ideasonboard.com>","References":"<20211206152644.4863-1-laurent.pinchart@ideasonboard.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[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>"},"content":"The abs_diff() function computes the absolute difference of two\nelements. This may seem trivial at first, but can lead to unexpected\nresults when operating on unsigned operands. A common implementation\nof the absolute difference of two unsigned int (used through the\nlibcamera code base) is\n\n\tstd::abs(static_cast<int>(a - b))\n\nbut doesn't return the expected result when either a or b is larger than\nUINT_MAX / 2 due to overflows. The abs_diff() function offers a safe\nalternative.\n\nSigned-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(+)","diff":"diff --git a/include/libcamera/base/utils.h b/include/libcamera/base/utils.h\nindex 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__\ndiff --git a/src/libcamera/base/utils.cpp b/src/libcamera/base/utils.cpp\nindex 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__\n","prefixes":["libcamera-devel","1/2"]}