[{"id":11386,"web_url":"https://patchwork.libcamera.org/comment/11386/","msgid":"<20200714211553.GB5854@pendragon.ideasonboard.com>","date":"2020-07-14T21:15:53","subject":"Re: [libcamera-devel] [PATCH 03/20] libcamera: utils: Add alignUp\n\tand alignDown functions","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Jacopo,\n\nThank you for the patch.\n\nOn Tue, Jul 14, 2020 at 12:41:55PM +0200, Jacopo Mondi wrote:\n> Add to libcamera utils library two functions to round up or down a\n> value to an alignment and add a test in test/utils.cpp for the two\n> new functions.\n> \n> Signed-off-by: Jacopo Mondi <jacopo@jmondi.org>\n> ---\n>  include/libcamera/internal/utils.h |  3 +++\n>  src/libcamera/utils.cpp            | 22 ++++++++++++++++++++++\n>  test/utils.cpp                     | 18 ++++++++++++++++++\n>  3 files changed, 43 insertions(+)\n> \n> diff --git a/include/libcamera/internal/utils.h b/include/libcamera/internal/utils.h\n> index 8d026cc6c0fe..25eb24ec2d16 100644\n> --- a/include/libcamera/internal/utils.h\n> +++ b/include/libcamera/internal/utils.h\n> @@ -200,6 +200,9 @@ details::StringSplitter split(const std::string &str, const std::string &delim);\n>  std::string libcameraBuildPath();\n>  std::string libcameraSourcePath();\n>  \n> +unsigned int alignUp(unsigned int value, unsigned int alignment);\n> +unsigned int alignDown(unsigned int value, unsigned int alignment);\n\nI would inline those two functions, to let the compiler optimize the\ncode. For instance it's common to have a constexpr alignment value,\nwhich can often translate to more efficient code. I would also mark the\nfunctions as constexpr. As constexpr implies inline,\n\nconstexpr unsigned int alignUp(unsigned int value, unsigned int alignment)\n{\n\treturn (value + alignment - 1) / alignment * alignment;\n}\n\nconstexpr unsigned int alignDown(unsigned int value, unsigned int alignment)\n{\n\treturn value / alignment * alignment;\n}\n\n> +\n>  } /* namespace utils */\n>  \n>  } /* namespace libcamera */\n> diff --git a/src/libcamera/utils.cpp b/src/libcamera/utils.cpp\n> index 0567328fe31b..52fb0bb171d8 100644\n> --- a/src/libcamera/utils.cpp\n> +++ b/src/libcamera/utils.cpp\n> @@ -444,6 +444,28 @@ std::string libcameraSourcePath()\n>  \treturn path + \"/\";\n>  }\n>  \n> +/**\n> + * \\brief Align \\a value to \\a alignment\n\nMaybe s/value to/value up to/ ? Same for alignDown().\n\n> + * \\param[in] value The value to align\n> + * \\param[in] alignment The alignment\n> + * \\return The value rounded up to the nearest multiple of \\a alignment\n> + */\n> +unsigned int alignUp(unsigned int value, unsigned int alignment)\n> +{\n> +\treturn (value + alignment - 1) / alignment * alignment;\n> +}\n> +\n> +/**\n> + * \\brief Align \\a value to \\a alignment\n> + * \\param[in] value The value to align\n> + * \\param[in] alignment The alignment\n> + * \\return The value rounded down to the nearest multiple of \\a alignment\n> + */\n> +unsigned int alignDown(unsigned int value, unsigned int alignment)\n> +{\n> +\treturn value / alignment * alignment;\n> +}\n> +\n>  } /* namespace utils */\n>  \n>  } /* namespace libcamera */\n> diff --git a/test/utils.cpp b/test/utils.cpp\n> index f482e6a1d829..02054f46b00e 100644\n> --- a/test/utils.cpp\n> +++ b/test/utils.cpp\n> @@ -7,6 +7,7 @@\n>  \n>  #include <iostream>\n>  #include <map>\n> +#include <random>\n>  #include <sstream>\n>  #include <string>\n>  #include <vector>\n> @@ -166,6 +167,23 @@ protected:\n>  \t\t\treturn TestFail;\n>  \t\t}\n>  \n> +\t\t/* utils::alignUp() and utils::alignDown() tests. */\n> +\t\trandom_device random;\n> +\t\tunsigned int val = random();\n\nUsage of random number in tests makes it difficult to reproduce\nfailures. Additionally, the random alignment, could be 0, leading to a\npotential division by 0. I think you can just hardcode values :\n\n\t\tif (utils::alignUp(6, 3) != 6 || utils::alignUp(7, 3) != 9) {\n\t\t\tcerr << \"utils::alignUp test failed\" << endl;\n\t\t\treturn TestFail;\n\t\t}\n\n\t\tif (utils::alignDown(6, 3) != 6 || utils::alignDown(7, 3) != 6) {\n\t\t\tcerr << \"utils::alignDown test failed\" << endl;\n\t\t\treturn TestFail;\n\t\t}\n\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\n> +\t\tunsigned int align = random() % (val / 10);\n> +\t\tunsigned int roundUp = (val + align - 1) / align;\n> +\t\tunsigned int roundDown = val / align;\n> +\n> +\t\tif (utils::alignUp(val, align) != align * roundUp) {\n> +\t\t\tcerr << \"utils::alignUp test failed\" << endl;\n> +\t\t\treturn TestFail;\n> +\t\t}\n> +\n> +\t\tif (utils::alignDown(val, align) != align * roundDown) {\n> +\t\t\tcerr << \"utils::alignDown test failed\" << endl;\n> +\t\t\treturn TestFail;\n> +\t\t}\n> +\n>  \t\treturn TestPass;\n>  \t}\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 38D14BD792\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 14 Jul 2020 21:16:03 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id A168D6089D;\n\tTue, 14 Jul 2020 23:16:02 +0200 (CEST)","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 55B34605AC\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 14 Jul 2020 23:16:01 +0200 (CEST)","from pendragon.ideasonboard.com (81-175-216-236.bb.dnainternet.fi\n\t[81.175.216.236])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id C348271D;\n\tTue, 14 Jul 2020 23:16:00 +0200 (CEST)"],"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=\"TveHv8rj\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1594761360;\n\tbh=W0UyXwcUIN3jpBNdo+eWMyZTT9Hqq9PMqlhSgbW/X/8=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=TveHv8rj4+ugw58TT0orpnlZfojMtqf+U8DhnTtRdV4d7ACq2n1rn9CeDieGyDpFE\n\tuqJm8xNHpcVMcvFVujDTOqW+DqAasLtmYNr7PBcmcoFjh1o+zFUoRuyK3gHgG4YJHY\n\t35CBdtYeVD6+nX07uJpgLLduq3OPB8CLXe+MuBWs=","Date":"Wed, 15 Jul 2020 00:15:53 +0300","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Jacopo Mondi <jacopo@jmondi.org>","Message-ID":"<20200714211553.GB5854@pendragon.ideasonboard.com>","References":"<20200714104212.48683-1-jacopo@jmondi.org>\n\t<20200714104212.48683-4-jacopo@jmondi.org>","MIME-Version":"1.0","Content-Disposition":"inline","In-Reply-To":"<20200714104212.48683-4-jacopo@jmondi.org>","Subject":"Re: [libcamera-devel] [PATCH 03/20] libcamera: utils: Add alignUp\n\tand alignDown functions","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>","Cc":"libcamera-devel@lists.libcamera.org","Content-Type":"text/plain; charset=\"us-ascii\"","Content-Transfer-Encoding":"7bit","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]