[{"id":34441,"web_url":"https://patchwork.libcamera.org/comment/34441/","msgid":"<ls56pgp5swu4xrg2gvqohewuaq5uvulnlvzkionfloiaaqosu2@asqdel3y47zu>","date":"2025-06-10T16:31:40","subject":"Re: [RFC PATCH v1 05/23] libcamera: base: cxx20: Add\n\t`has_single_bit()`","submitter":{"id":143,"url":"https://patchwork.libcamera.org/api/people/143/","name":"Jacopo Mondi","email":"jacopo.mondi@ideasonboard.com"},"content":"On Fri, Jun 06, 2025 at 06:41:38PM +0200, Barnabás Pőcze wrote:\n> `has_single_bit()` checks if the given unsigned integer has a single\n> bit set, that is, whether the number is a power of two or not.\n>\n> Not all requirements of the C++20 standard are implemented.\n>\n> See https://en.cppreference.com/w/cpp/numeric/has_single_bit\n>\n> Signed-off-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com>\n> ---\n>  include/libcamera/base/details/cxx20.h | 10 ++++++++++\n>  1 file changed, 10 insertions(+)\n>\n> diff --git a/include/libcamera/base/details/cxx20.h b/include/libcamera/base/details/cxx20.h\n> index 2d26db53e..8bf45ff11 100644\n> --- a/include/libcamera/base/details/cxx20.h\n> +++ b/include/libcamera/base/details/cxx20.h\n> @@ -7,9 +7,19 @@\n>\n>  #pragma once\n>\n> +#include <type_traits>\n> +\n>  namespace libcamera::details::cxx20 {\n>\n>  template<typename T> struct type_identity { using type = T; };\n>  template<typename T> using type_identity_t = typename type_identity<T>::type;\n>\n> +template<typename T>\n> +constexpr bool has_single_bit(T x) noexcept\n> +{\n> +\tstatic_assert(std::is_unsigned_v<T>);\n> +\n> +\treturn x != 0 && (x & (x - 1)) == 0;\n\nOr, as suggested by cppreference\n\n        return x && !(x & (x - 1));\n\n> +}\n> +\n>  } /* namespace libcamera::details::cxx20 */\n> --\n> 2.49.0\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 7E9C4BDE6B\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 10 Jun 2025 16:31:48 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 5B14268DBD;\n\tTue, 10 Jun 2025 18:31:47 +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 9479061869\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 10 Jun 2025 18:31:45 +0200 (CEST)","from ideasonboard.com (93-61-96-190.ip145.fastwebnet.it\n\t[93.61.96.190])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id EC235982;\n\tTue, 10 Jun 2025 18:31:37 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"oh2NyQph\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1749573098;\n\tbh=7O8g0Cz8GjpRiA01VglHAkxLrcxIM8CcYtfwM/jmmCs=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=oh2NyQphI88dDga5dtFW7QyAhgga1tJUzil/l8diqlOaTFNkWiS2EC6TiXdwG7GIO\n\tLkfk4rc2PSixYXgjTSPCxnEAX7blfUKHqRJwBaqcsJST2aa+3+lK6GglqB9Yml5gzc\n\tv4MMX/bXKQ41vU0klEJq/5zorCRYjk2C+/E8V+QE=","Date":"Tue, 10 Jun 2025 18:31:40 +0200","From":"Jacopo Mondi <jacopo.mondi@ideasonboard.com>","To":"=?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= <barnabas.pocze@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org","Subject":"Re: [RFC PATCH v1 05/23] libcamera: base: cxx20: Add\n\t`has_single_bit()`","Message-ID":"<ls56pgp5swu4xrg2gvqohewuaq5uvulnlvzkionfloiaaqosu2@asqdel3y47zu>","References":"<20250606164156.1442682-1-barnabas.pocze@ideasonboard.com>\n\t<20250606164156.1442682-6-barnabas.pocze@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","Content-Transfer-Encoding":"8bit","In-Reply-To":"<20250606164156.1442682-6-barnabas.pocze@ideasonboard.com>","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>"}}]