From 6849b541e9fd4b251e5a4bbaa241f68fb965588b Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Wed, 3 Sep 2025 22:28:18 -0700 Subject: [PATCH] [common] move bit-related macros to `bit_utils.hpp` (#11890) This change moves the `kBitsPerByte` constant and the `BitSizeOf()` and `BytesForBitSize()` macros from `numeric_limits.hpp` to the more specialized `bit_utils.hpp` header. This consolidation places common bit-utility definitions into a more appropriate, dedicated header, improving code organization and logical grouping. Headers that relied on these definitions are updated accordingly. --- src/core/common/bit_set.hpp | 2 +- src/core/common/bit_utils.hpp | 20 ++++++++++++++++++++ src/core/common/numeric_limits.hpp | 20 -------------------- src/core/common/time_ticker.hpp | 2 +- src/core/net/ip6_address.hpp | 3 +-- 5 files changed, 23 insertions(+), 24 deletions(-) diff --git a/src/core/common/bit_set.hpp b/src/core/common/bit_set.hpp index 78dc4676f..c26777342 100644 --- a/src/core/common/bit_set.hpp +++ b/src/core/common/bit_set.hpp @@ -36,9 +36,9 @@ #include "openthread-core-config.h" +#include "common/bit_utils.hpp" #include "common/clearable.hpp" #include "common/equatable.hpp" -#include "common/numeric_limits.hpp" namespace ot { diff --git a/src/core/common/bit_utils.hpp b/src/core/common/bit_utils.hpp index cc269c08e..8169b53d9 100644 --- a/src/core/common/bit_utils.hpp +++ b/src/core/common/bit_utils.hpp @@ -40,6 +40,26 @@ namespace ot { +static constexpr uint8_t kBitsPerByte = 8; ///< Number of bits in a byte. + +/** + * Returns the bit-size (number of bits) of a given type or variable. + * + * @param[in] aItem The item (type or variable or expression) to get the bit-size of. + * + * @returns Number of bits of @p aItem. + */ +#define BitSizeOf(aItem) (sizeof(aItem) * kBitsPerByte) + +/** + * Determines number of bytes to represent a given number of bits. + * + * @param[in] aBitSize The bit-size (number of bits). + * + * @returns Number of bytes to represent @p aBitSize. + */ +#define BytesForBitSize(aBitSize) static_cast(((aBitSize) + (kBitsPerByte - 1)) / kBitsPerByte) + /** * Counts the number of `1` bits in the binary representation of a given unsigned int bit-mask value. * diff --git a/src/core/common/numeric_limits.hpp b/src/core/common/numeric_limits.hpp index 122cdc35e..c104f5381 100644 --- a/src/core/common/numeric_limits.hpp +++ b/src/core/common/numeric_limits.hpp @@ -38,26 +38,6 @@ namespace ot { -static constexpr uint8_t kBitsPerByte = 8; ///< Number of bits in a byte. - -/** - * Returns the bit-size (number of bits) of a given type or variable. - * - * @param[in] aItem The item (type or variable or expression) to get the bit-size of. - * - * @returns Number of bits of @p aItem. - */ -#define BitSizeOf(aItem) (sizeof(aItem) * kBitsPerByte) - -/** - * Determines number of byes to represent a given number of bits. - * - * @param[in] aBitSize The bit-size (number of bits). - * - * @returns Number of bytes to represent @p aBitSize. - */ -#define BytesForBitSize(aBitSize) static_cast(((aBitSize) + (kBitsPerByte - 1)) / kBitsPerByte) - /** * Provides a way to query properties of arithmetic types. * diff --git a/src/core/common/time_ticker.hpp b/src/core/common/time_ticker.hpp index 55873a3b7..f05fde339 100644 --- a/src/core/common/time_ticker.hpp +++ b/src/core/common/time_ticker.hpp @@ -36,9 +36,9 @@ #include "openthread-core-config.h" +#include "common/bit_utils.hpp" #include "common/locator.hpp" #include "common/non_copyable.hpp" -#include "common/numeric_limits.hpp" #include "common/time.hpp" #include "common/timer.hpp" diff --git a/src/core/net/ip6_address.hpp b/src/core/net/ip6_address.hpp index 16b197ce2..aeb0e11f0 100644 --- a/src/core/net/ip6_address.hpp +++ b/src/core/net/ip6_address.hpp @@ -39,11 +39,10 @@ #include #include "common/as_core_type.hpp" +#include "common/bit_utils.hpp" #include "common/clearable.hpp" #include "common/encoding.hpp" #include "common/equatable.hpp" -#include "common/num_utils.hpp" -#include "common/numeric_limits.hpp" #include "common/string.hpp" #include "mac/mac_types.hpp"