8#ifndef INCLUDED_BDLB_BITUTIL
9#define INCLUDED_BDLB_BITUTIL
132#include <bdlscm_version.h>
141#include <bsl_climits.h>
142#include <bsl_cstdint.h>
144#if defined(BSLS_PLATFORM_CMP_GNU) || defined(BSLS_PLATFORM_CMP_CLANG)
147# define BDLB_BITUTIL_USE_GNU_INTRINSICS 1
150#ifdef BSLS_PLATFORM_CMP_MSVC
152# define BDLB_BITUTIL_USE_MSVC_INTRINSICS 1
155# if defined(BSLS_PLATFORM_CPU_ARM)
156# define BDLB_BITUTIL_USE_MSVC_COUNT_ONE_BITS 1
179 k_BITS_PER_INT32 = 32,
180 k_BITS_PER_INT64 = 64
202 static int privateNumBitsSet(
unsigned int value);
203 static int privateNumBitsSet(
unsigned long value);
204 static int privateNumBitsSet(
unsigned long long value);
208 static int privateNumLeadingUnsetBits(
unsigned int value);
209 static int privateNumLeadingUnsetBits(
unsigned long value);
210 static int privateNumLeadingUnsetBits(
unsigned long long value);
214 static int privateNumTrailingUnsetBits(
unsigned int value);
215 static int privateNumTrailingUnsetBits(
unsigned long value);
216 static int privateNumTrailingUnsetBits(
unsigned long long value);
225 static bool isBitSet(
unsigned int value,
int index);
226 static bool isBitSet(
unsigned long value,
int index);
227 static bool isBitSet(
unsigned long long value,
int index);
232 static int log2(
unsigned int value);
233 static int log2(
unsigned long value);
234 static int log2(
unsigned long long value);
239 static int numBitsSet(
unsigned long long value);
261 static unsigned int roundUp(
unsigned int value,
unsigned int boundary);
262 static unsigned long roundUp(
unsigned long value,
unsigned long boundary);
263 static unsigned long long roundUp(
unsigned long long value,
264 unsigned long long boundary);
277 template <
class INTEGER>
285 static unsigned int withBitCleared(
unsigned int value,
int index);
286 static unsigned long withBitCleared(
unsigned long value,
int index);
287 static unsigned long long withBitCleared(
unsigned long long value,
295 static unsigned int withBitSet(
unsigned int value,
int index);
296 static unsigned long withBitSet(
unsigned long value,
int index);
297 static unsigned long long withBitSet(
unsigned long long value,
int index);
312 return static_cast<ULongLikeType
>(value);
321 return ((1 << index) & value) != 0;
330 return ((1ULL << index) & value) != 0;
336 return isBitSet(normalize(value), index);
358 return log2(normalize(value));
364#if defined(BDLB_BITUTIL_USE_GNU_INTRINSICS)
365 return __builtin_popcount(value);
366#elif defined(BDLB_BITUTIL_USE_MSVC_INTRINSICS)
367# if !defined(BDLB_BITUTIL_USE_MSVC_COUNT_ONE_BITS)
368 return __popcnt(value);
370 return _CountOneBits(value);
373 return privateNumBitsSet(value);
380#if defined(BDLB_BITUTIL_USE_GNU_INTRINSICS)
381 return __builtin_popcountll(value);
382#elif defined(BDLB_BITUTIL_USE_MSVC_INTRINSICS)
383 #if !defined(BDLB_BITUTIL_USE_MSVC_COUNT_ONE_BITS)
384 #if defined(BSLS_PLATFORM_CPU_64_BIT)
385 return static_cast<int>(__popcnt64(value));
388 return __popcnt(
static_cast<unsigned int>(value)) +
389 __popcnt(
static_cast<unsigned int>(value >>
393 return _CountOneBits64(value);
396 return privateNumBitsSet(value);
409#if defined(BDLB_BITUTIL_USE_GNU_INTRINSICS)
411 return __builtin_clz(value | 1) +
static_cast<int>(!value);
412#elif defined(BDLB_BITUTIL_USE_MSVC_INTRINSICS)
415 return _BitScanReverse(&index, value)
416 ? k_BITS_PER_INT32 - 1 - index
419 return privateNumLeadingUnsetBits(value);
426#if defined(BDLB_BITUTIL_USE_GNU_INTRINSICS)
428 return __builtin_clzll(value | 1) +
static_cast<int>(!value);
429#elif defined(BDLB_BITUTIL_USE_MSVC_INTRINSICS)
430 #if defined(BSLS_PLATFORM_CPU_64_BIT)
433 return _BitScanReverse64(&index, value)
434 ? k_BITS_PER_INT64 - 1 - index
438 return value > 0xffffffff
440 value >> k_BITS_PER_INT32))
445 return privateNumLeadingUnsetBits(value);
458#if defined(BDLB_BITUTIL_USE_GNU_INTRINSICS)
460 k_INT32_MASK = k_BITS_PER_INT32 - 1
462 const unsigned int a = __builtin_ffs(value) - 1;
463 return (a & k_INT32_MASK) + (a >> k_INT32_MASK);
469#elif defined(BDLB_BITUTIL_USE_MSVC_INTRINSICS)
472 return _BitScanForward(&index, value) ? index : k_BITS_PER_INT32;
474 return privateNumTrailingUnsetBits(value);
481#if defined(BDLB_BITUTIL_USE_GNU_INTRINSICS)
483 k_INT64_MASK = k_BITS_PER_INT64 - 1,
484 k_INT32_MASK = k_BITS_PER_INT32 - 1
486 const unsigned int a = __builtin_ffsll(value) - 1;
487 return (a & k_INT64_MASK) + (a >> k_INT32_MASK);
493#elif defined(BDLB_BITUTIL_USE_MSVC_INTRINSICS)
494 #if defined(BSLS_PLATFORM_CPU_64_BIT)
497 return _BitScanForward64(&index, value) ? index : k_BITS_PER_INT64;
500 return 0 != (value & 0xffffffff)
503 value >> k_BITS_PER_INT32)) + k_BITS_PER_INT32;
506 return privateNumTrailingUnsetBits(value);
521 return ((value - 1) | (boundary - 1)) + 1;
526 unsigned long long boundary)
530 return ((value - 1) | (boundary - 1)) + 1;
536 return roundUp(normalize(value), normalize(boundary));
544 ? 1U << (k_BITS_PER_INT32 - index)
553 ? 1ULL << (k_BITS_PER_INT64 - index)
567 return static_cast<int>(CHAR_BIT *
sizeof(TYPE));
576 return value & ~(1 << index);
585 return value & ~(1ULL << index);
600 return value | (1 << index);
609 return value | (1ULL << index);
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1976
#define BSLS_ASSERT_SAFE(X)
Definition bsls_assert.h:1917
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bdlb_algorithmworkaroundutil.h:74
Definition bdlb_bitutil.h:174
static int numTrailingUnsetBits(unsigned int value)
Definition bdlb_bitutil.h:456
static unsigned int withBitSet(unsigned int value, int index)
Definition bdlb_bitutil.h:595
static int numLeadingUnsetBits(unsigned int value)
Definition bdlb_bitutil.h:407
static unsigned int withBitCleared(unsigned int value, int index)
Definition bdlb_bitutil.h:571
static int log2(unsigned int value)
Definition bdlb_bitutil.h:340
static int numBitsSet(unsigned int value)
Return the number of 1 bits in the specified value.
Definition bdlb_bitutil.h:362
static unsigned int roundUp(unsigned int value, unsigned int boundary)
Definition bdlb_bitutil.h:517
bsl::uint64_t uint64_t
Definition bdlb_bitutil.h:186
static unsigned int roundUpToBinaryPower(unsigned int value)
Definition bdlb_bitutil.h:540
bsl::uint32_t uint32_t
Definition bdlb_bitutil.h:185
static bool isBitSet(unsigned int value, int index)
Definition bdlb_bitutil.h:316
static int sizeInBits(INTEGER value=0)
Definition bslmf_conditional.h:123
Definition bslmf_integralconstant.h:261