From patchwork Mon Dec 6 15:26:43 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 15052 X-Patchwork-Delegate: laurent.pinchart@ideasonboard.com Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 69D4FBDB13 for ; Mon, 6 Dec 2021 15:27:19 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 222216087E; Mon, 6 Dec 2021 16:27:19 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="KdGy/93S"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 1677760725 for ; Mon, 6 Dec 2021 16:27:17 +0100 (CET) Received: from pendragon.lan (62-78-145-57.bb.dnainternet.fi [62.78.145.57]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id A86E5547 for ; Mon, 6 Dec 2021 16:27:16 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1638804436; bh=43LZ5Gae5XpndtQ7zkW9WZrDi1BhsCVfMxoiB4RbMzg=; h=From:To:Subject:Date:In-Reply-To:References:From; b=KdGy/93SMsdINo/rj6g58g1YM1svy/aDq9ZRgrY3gk6k2uoxXX7BPakiZgZ6wCFGX fKQrwMWyQTG6AK7t8kMdO8GXGr3U4io4Xi3fgSNN1u/IV9deMtI6qTafJ2gNBD038l 5VyXyxjqMJThK77noat6usEtZYsbiTBi8KMNGBq8= From: Laurent Pinchart 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 Subject: [libcamera-devel] [PATCH 1/2] libcamera: base: utils: Add abs_diff() utility function X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" The abs_diff() function computes the absolute difference of two elements. This may seem trivial at first, but can lead to unexpected results when operating on unsigned operands. A common implementation of the absolute difference of two unsigned int (used through the libcamera code base) is std::abs(static_cast(a - b)) but doesn't return the expected result when either a or b is larger than UINT_MAX / 2 due to overflows. The abs_diff() function offers a safe alternative. Signed-off-by: Laurent Pinchart Reviewed-by: Kieran Bingham Reviewed-by: Umang Jain --- include/libcamera/base/utils.h | 9 +++++++++ src/libcamera/base/utils.cpp | 17 +++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/include/libcamera/base/utils.h b/include/libcamera/base/utils.h index 3a803176693d..9ab18101cf27 100644 --- a/include/libcamera/base/utils.h +++ b/include/libcamera/base/utils.h @@ -346,6 +346,15 @@ public: } }; +template +decltype(auto) abs_diff(const T &a, const T &b) +{ + if (a < b) + return b - a; + else + return a - b; +} + } /* namespace utils */ #ifndef __DOXYGEN__ diff --git a/src/libcamera/base/utils.cpp b/src/libcamera/base/utils.cpp index 45b92b670837..8cf8a5b2c104 100644 --- a/src/libcamera/base/utils.cpp +++ b/src/libcamera/base/utils.cpp @@ -437,6 +437,23 @@ std::string toAscii(const std::string &str) * \return True if \a Duration is a non-zero time value, False otherwise */ +/** + * \fn abs_diff(const T& a, const T& b) + * \brief Calculates the absolute value of the difference between two elements + * \param[in] a The first element + * \param[in] b The second element + * + * This function calculates the absolute value of the difference between two + * elements of the same type, in such a way that a negative value will never + * occur during the calculation. + * + * This is inspired by the std::abs_diff() candidate proposed in N4318 + * (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4318.pdf). + * + * \return The absolute value of the difference of the two parameters \a a and + * \a b + */ + } /* namespace utils */ #ifndef __DOXYGEN__