From patchwork Tue Jul 14 10:41:55 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jacopo Mondi X-Patchwork-Id: 8778 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 00910BD792 for ; Tue, 14 Jul 2020 10:38:52 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 90E6860889; Tue, 14 Jul 2020 12:38:51 +0200 (CEST) Received: from relay10.mail.gandi.net (relay10.mail.gandi.net [217.70.178.230]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id B937E60486 for ; Tue, 14 Jul 2020 12:38:49 +0200 (CEST) Received: from uno.lan (93-34-118-233.ip49.fastwebnet.it [93.34.118.233]) (Authenticated sender: jacopo@jmondi.org) by relay10.mail.gandi.net (Postfix) with ESMTPSA id 5C15A240011; Tue, 14 Jul 2020 10:38:49 +0000 (UTC) From: Jacopo Mondi To: libcamera-devel@lists.libcamera.org Date: Tue, 14 Jul 2020 12:41:55 +0200 Message-Id: <20200714104212.48683-4-jacopo@jmondi.org> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20200714104212.48683-1-jacopo@jmondi.org> References: <20200714104212.48683-1-jacopo@jmondi.org> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 03/20] libcamera: utils: Add alignUp and alignDown functions 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" Add to libcamera utils library two functions to round up or down a value to an alignment and add a test in test/utils.cpp for the two new functions. Signed-off-by: Jacopo Mondi Reviewed-by: Laurent Pinchart --- include/libcamera/internal/utils.h | 3 +++ src/libcamera/utils.cpp | 22 ++++++++++++++++++++++ test/utils.cpp | 18 ++++++++++++++++++ 3 files changed, 43 insertions(+) diff --git a/include/libcamera/internal/utils.h b/include/libcamera/internal/utils.h index 8d026cc6c0fe..25eb24ec2d16 100644 --- a/include/libcamera/internal/utils.h +++ b/include/libcamera/internal/utils.h @@ -200,6 +200,9 @@ details::StringSplitter split(const std::string &str, const std::string &delim); std::string libcameraBuildPath(); std::string libcameraSourcePath(); +unsigned int alignUp(unsigned int value, unsigned int alignment); +unsigned int alignDown(unsigned int value, unsigned int alignment); + } /* namespace utils */ } /* namespace libcamera */ diff --git a/src/libcamera/utils.cpp b/src/libcamera/utils.cpp index 0567328fe31b..52fb0bb171d8 100644 --- a/src/libcamera/utils.cpp +++ b/src/libcamera/utils.cpp @@ -444,6 +444,28 @@ std::string libcameraSourcePath() return path + "/"; } +/** + * \brief Align \a value to \a alignment + * \param[in] value The value to align + * \param[in] alignment The alignment + * \return The value rounded up to the nearest multiple of \a alignment + */ +unsigned int alignUp(unsigned int value, unsigned int alignment) +{ + return (value + alignment - 1) / alignment * alignment; +} + +/** + * \brief Align \a value to \a alignment + * \param[in] value The value to align + * \param[in] alignment The alignment + * \return The value rounded down to the nearest multiple of \a alignment + */ +unsigned int alignDown(unsigned int value, unsigned int alignment) +{ + return value / alignment * alignment; +} + } /* namespace utils */ } /* namespace libcamera */ diff --git a/test/utils.cpp b/test/utils.cpp index f482e6a1d829..02054f46b00e 100644 --- a/test/utils.cpp +++ b/test/utils.cpp @@ -7,6 +7,7 @@ #include #include +#include #include #include #include @@ -166,6 +167,23 @@ protected: return TestFail; } + /* utils::alignUp() and utils::alignDown() tests. */ + random_device random; + unsigned int val = random(); + unsigned int align = random() % (val / 10); + unsigned int roundUp = (val + align - 1) / align; + unsigned int roundDown = val / align; + + if (utils::alignUp(val, align) != align * roundUp) { + cerr << "utils::alignUp test failed" << endl; + return TestFail; + } + + if (utils::alignDown(val, align) != align * roundDown) { + cerr << "utils::alignDown test failed" << endl; + return TestFail; + } + return TestPass; } };