From patchwork Thu Oct 30 16:57:59 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: 24908 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 15D33C3259 for ; Thu, 30 Oct 2025 16:58:35 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 828DE608F4; Thu, 30 Oct 2025 17:58:29 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="I6ClU1co"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 0CC26608DD for ; Thu, 30 Oct 2025 17:58:22 +0100 (CET) Received: from pb-laptop.local (185.221.140.239.nat.pool.zt.hu [185.221.140.239]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id DDE2218E5; Thu, 30 Oct 2025 17:56:31 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1761843392; bh=HciZSJ8c9rrJbGOx9JzK+X8ACBgX7TlUyIG8STiQ8As=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=I6ClU1cor3yL5GN0TOgC+N4PzT3L0hWZyvBN4O9rvJ3hA6X1z6x2fxvdlhEaCRYFG 2US9fOcspPUHmR2GrW4pndlkFvb0z+kw173chh/tw5iuCQJev/l86d8mJPAVhc1Q8H TN/G2ntXlXE6EAaHtLY0VlHoAzNBKjlIOS6ROxnQ= From: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= To: libcamera-devel@lists.libcamera.org Cc: Paul Elder Subject: [RFC PATCH v3 05/22] libcamera: base: cxx20: Add `has_single_bit()` Date: Thu, 30 Oct 2025 17:57:59 +0100 Message-ID: <20251030165816.1095180-6-barnabas.pocze@ideasonboard.com> X-Mailer: git-send-email 2.51.1 In-Reply-To: <20251030165816.1095180-1-barnabas.pocze@ideasonboard.com> References: <20251030165816.1095180-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 Reviewed-by: Paul Elder Reviewed-by: Kieran Bingham --- include/libcamera/base/internal/cxx20.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/include/libcamera/base/internal/cxx20.h b/include/libcamera/base/internal/cxx20.h index 1f4caf56f8..d47168a70a 100644 --- a/include/libcamera/base/internal/cxx20.h +++ b/include/libcamera/base/internal/cxx20.h @@ -7,6 +7,8 @@ #pragma once +#include + /** * \internal * \file cxx20.h @@ -39,4 +41,18 @@ template struct type_identity { */ template using type_identity_t = typename type_identity::type; +/** + * \internal + * \brief std::has_single_bit() + * + * Implementation of std::has_single_bit() for C++17. + */ +template +constexpr bool has_single_bit(T x) noexcept +{ + static_assert(std::is_unsigned_v); + + return x != 0 && (x & (x - 1)) == 0; +} + } /* namespace libcamera::internal::cxx20 */