From patchwork Tue Jan 6 16:57:37 2026 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: 25635 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 19FB8BDCBF for ; Tue, 6 Jan 2026 16:58:13 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id A71D261FD6; Tue, 6 Jan 2026 17:58:04 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="ngl+TCMg"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 393FC61F9F for ; Tue, 6 Jan 2026 17:57:59 +0100 (CET) Received: from pb-laptop.local (185.221.143.114.nat.pool.zt.hu [185.221.143.114]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 2DCDD16CD; Tue, 6 Jan 2026 17:57:38 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1767718658; bh=mkcW4kOYD2jP66GKK3ji8SaLitSezNm7hJvxYbBYpC0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ngl+TCMgutPonxKXPhXK/PTSS1GuqGqmSESqTEg4MGyDlqtPfVmvnLh+UjEySgqtP 2cJVJnFPgoi6gZuW8i3kE5uqJC277NQka5oj6+23quUs1tXEFUJ6X1b7NzH+B1GTbW Yo5k5I5CBQni4Z8uyolLHK8G4XC7H/xTNuYGG5/M= From: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= To: libcamera-devel@lists.libcamera.org Cc: Paul Elder , Kieran Bingham Subject: [PATCH v4 05/22] libcamera: base: cxx20: Add `has_single_bit()` Date: Tue, 6 Jan 2026 17:57:37 +0100 Message-ID: <20260106165754.1759831-6-barnabas.pocze@ideasonboard.com> X-Mailer: git-send-email 2.52.0 In-Reply-To: <20260106165754.1759831-1-barnabas.pocze@ideasonboard.com> References: <20260106165754.1759831-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 1f4caf56f..d47168a70 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 */