From patchwork Fri Jun 6 16:41:38 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= X-Patchwork-Id: 23486 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 E179CC3325 for ; Fri, 6 Jun 2025 16:42:35 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 2D9C668DF2; Fri, 6 Jun 2025 18:42:33 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="GYQzkkxQ"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 5A04668DBD for ; Fri, 6 Jun 2025 18:42:16 +0200 (CEST) Received: from pb-laptop.local (185.182.215.79.nat.pool.zt.hu [185.182.215.79]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id DD3126DC for ; Fri, 6 Jun 2025 18:42:11 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1749228132; bh=I154HYs0GO304Y6yaX6MDVD3TR7uKqgqftaAYmVUJcU=; h=From:To:Subject:Date:In-Reply-To:References:From; b=GYQzkkxQG1OTmKCdnb8xQqivNQFrBboJN8Ho59lXcDFr8Nvfu0l37lf8eHQPZRk/R BOxEro7jrIJlixO92EJ3sRgKBmkbfApZkDb3KsniojfDEMEAj2T9HyNVbxvXeZyISZ AR1481VjoFpcoxvIf78c0XwyNX/KEuXPo9iEBpoY= From: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= To: libcamera-devel@lists.libcamera.org Subject: [RFC PATCH v1 05/23] libcamera: base: cxx20: Add `has_single_bit()` Date: Fri, 6 Jun 2025 18:41:38 +0200 Message-ID: <20250606164156.1442682-6-barnabas.pocze@ideasonboard.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250606164156.1442682-1-barnabas.pocze@ideasonboard.com> References: <20250606164156.1442682-1-barnabas.pocze@ideasonboard.com> MIME-Version: 1.0 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" `has_single_bit()` checks if the given unsigned integer has a single bit set, that is, whether the number is a power of two or not. Not all requirements of the C++20 standard are implemented. See https://en.cppreference.com/w/cpp/numeric/has_single_bit Signed-off-by: Barnabás Pőcze --- include/libcamera/base/details/cxx20.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/include/libcamera/base/details/cxx20.h b/include/libcamera/base/details/cxx20.h index 2d26db53e..8bf45ff11 100644 --- a/include/libcamera/base/details/cxx20.h +++ b/include/libcamera/base/details/cxx20.h @@ -7,9 +7,19 @@ #pragma once +#include + namespace libcamera::details::cxx20 { template struct type_identity { using type = T; }; template using type_identity_t = typename type_identity::type; +template +constexpr bool has_single_bit(T x) noexcept +{ + static_assert(std::is_unsigned_v); + + return x != 0 && (x & (x - 1)) == 0; +} + } /* namespace libcamera::details::cxx20 */