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#ifdef BSLS_PLATFORM_CMP_IBM
146# define BDLB_BITUTIL_USE_IBM_INTRINSICS 1
151#if defined(BSLS_PLATFORM_CMP_GNU) || defined(BSLS_PLATFORM_CMP_CLANG)
154# define BDLB_BITUTIL_USE_GNU_INTRINSICS 1
157#ifdef BSLS_PLATFORM_CMP_MSVC
159# define BDLB_BITUTIL_USE_MSVC_INTRINSICS 1
162# if defined(BSLS_PLATFORM_CPU_ARM)
163# define BDLB_BITUTIL_USE_MSVC_COUNT_ONE_BITS 1
184 k_BITS_PER_INT32 = 32,
185 k_BITS_PER_INT64 = 64
206 static int privateNumBitsSet(
unsigned int value);
207 static int privateNumBitsSet(
unsigned long value);
209 static int privateNumBitsSet(
unsigned long long value);
211 static int privateNumLeadingUnsetBits(
unsigned int value);
212 static int privateNumLeadingUnsetBits(
unsigned long value);
215 static int privateNumLeadingUnsetBits(
unsigned long long value);
217 static int privateNumTrailingUnsetBits(
unsigned int value);
218 static int privateNumTrailingUnsetBits(
unsigned long value);
221 static int privateNumTrailingUnsetBits(
unsigned long long value);
226 static bool isBitSet(
unsigned int value,
int index);
227 static bool isBitSet(
unsigned long value,
int index);
231 static bool isBitSet(
unsigned long long value,
int index);
233 static int log2(
unsigned int value);
234 static int log2(
unsigned long value);
237 static int log2(
unsigned long long value);
242 static int numBitsSet(
unsigned long long value);
256 static unsigned int roundUp(
unsigned int value,
unsigned int boundary);
257 static unsigned long roundUp(
unsigned long value,
unsigned long boundary);
264 static unsigned long long roundUp(
unsigned long long value,
265 unsigned long long boundary);
277 template <
class INTEGER>
280 static unsigned int withBitCleared(
unsigned int value,
int index);
281 static unsigned long withBitCleared(
unsigned long value,
int index);
286 static unsigned long long withBitCleared(
unsigned long long value,
289 static unsigned int withBitSet(
unsigned int value,
int index);
290 static unsigned long withBitSet(
unsigned long value,
int index);
295 static unsigned long long withBitSet(
unsigned long long value,
int index);
310 return static_cast<ULongLikeType
>(value);
319 return ((1 << index) & value) != 0;
328 return ((1ULL << index) & value) != 0;
334 return isBitSet(normalize(value), index);
356 return log2(normalize(value));
362#if defined(BDLB_BITUTIL_USE_IBM_INTRINSICS)
363 return __popcnt4(value);
364#elif 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_IBM_INTRINSICS)
381 return __popcnt8(value);
382#elif defined(BDLB_BITUTIL_USE_GNU_INTRINSICS)
383 return __builtin_popcountll(value);
384#elif defined(BDLB_BITUTIL_USE_MSVC_INTRINSICS)
385 #if !defined(BDLB_BITUTIL_USE_MSVC_COUNT_ONE_BITS)
386 #if defined(BSLS_PLATFORM_CPU_64_BIT)
387 return static_cast<int>(__popcnt64(value));
390 return __popcnt(
static_cast<unsigned int>(value)) +
391 __popcnt(
static_cast<unsigned int>(value >>
395 return _CountOneBits64(value);
398 return privateNumBitsSet(value);
411#if defined(BDLB_BITUTIL_USE_IBM_INTRINSICS)
412 return __cntlz4(value);
413#elif defined(BDLB_BITUTIL_USE_GNU_INTRINSICS)
415 return __builtin_clz(value | 1) +
static_cast<int>(!value);
416#elif defined(BDLB_BITUTIL_USE_MSVC_INTRINSICS)
419 return _BitScanReverse(&index, value)
420 ? k_BITS_PER_INT32 - 1 - index
423 return privateNumLeadingUnsetBits(value);
430#if defined(BDLB_BITUTIL_USE_IBM_INTRINSICS)
431 return __cntlz8(value);
432#elif defined(BDLB_BITUTIL_USE_GNU_INTRINSICS)
434 return __builtin_clzll(value | 1) +
static_cast<int>(!value);
435#elif defined(BDLB_BITUTIL_USE_MSVC_INTRINSICS)
436 #if defined(BSLS_PLATFORM_CPU_64_BIT)
439 return _BitScanReverse64(&index, value)
440 ? k_BITS_PER_INT64 - 1 - index
444 return value > 0xffffffff
446 value >> k_BITS_PER_INT32))
451 return privateNumLeadingUnsetBits(value);
464#if defined(BDLB_BITUTIL_USE_IBM_INTRINSICS)
465 return __cnttz4(value);
466#elif defined(BDLB_BITUTIL_USE_GNU_INTRINSICS)
468 k_INT32_MASK = k_BITS_PER_INT32 - 1
470 const unsigned int a = __builtin_ffs(value) - 1;
471 return (a & k_INT32_MASK) + (a >> k_INT32_MASK);
477#elif defined(BDLB_BITUTIL_USE_MSVC_INTRINSICS)
480 return _BitScanForward(&index, value) ? index : k_BITS_PER_INT32;
482 return privateNumTrailingUnsetBits(value);
489#if defined(BDLB_BITUTIL_USE_IBM_INTRINSICS)
490 return __cnttz8(value);
491#elif defined(BDLB_BITUTIL_USE_GNU_INTRINSICS)
493 k_INT64_MASK = k_BITS_PER_INT64 - 1,
494 k_INT32_MASK = k_BITS_PER_INT32 - 1
496 const unsigned int a = __builtin_ffsll(value) - 1;
497 return (a & k_INT64_MASK) + (a >> k_INT32_MASK);
503#elif defined(BDLB_BITUTIL_USE_MSVC_INTRINSICS)
504 #if defined(BSLS_PLATFORM_CPU_64_BIT)
507 return _BitScanForward64(&index, value) ? index : k_BITS_PER_INT64;
510 return 0 != (value & 0xffffffff)
513 value >> k_BITS_PER_INT32)) + k_BITS_PER_INT32;
516 return privateNumTrailingUnsetBits(value);
531 return ((value - 1) | (boundary - 1)) + 1;
536 unsigned long long boundary)
540 return ((value - 1) | (boundary - 1)) + 1;
546 return roundUp(normalize(value), normalize(boundary));
554 ? 1U << (k_BITS_PER_INT32 - index)
563 ? 1ULL << (k_BITS_PER_INT64 - index)
577 return static_cast<int>(CHAR_BIT *
sizeof(TYPE));
586 return value & ~(1 << index);
595 return value & ~(1ULL << index);
610 return value | (1 << index);
619 return value | (1ULL << index);
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1804
#define BSLS_ASSERT_SAFE(X)
Definition bsls_assert.h:1762
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bdlb_algorithmworkaroundutil.h:74
Definition bdlb_bitutil.h:179
static int numTrailingUnsetBits(unsigned int value)
Definition bdlb_bitutil.h:462
static unsigned int withBitSet(unsigned int value, int index)
Definition bdlb_bitutil.h:605
static int numLeadingUnsetBits(unsigned int value)
Definition bdlb_bitutil.h:409
static unsigned int withBitCleared(unsigned int value, int index)
Definition bdlb_bitutil.h:581
static int log2(unsigned int value)
Definition bdlb_bitutil.h:338
static int numBitsSet(unsigned int value)
Definition bdlb_bitutil.h:360
static unsigned int roundUp(unsigned int value, unsigned int boundary)
Definition bdlb_bitutil.h:527
bsl::uint64_t uint64_t
Definition bdlb_bitutil.h:191
static unsigned int roundUpToBinaryPower(unsigned int value)
Definition bdlb_bitutil.h:550
bsl::uint32_t uint32_t
Definition bdlb_bitutil.h:190
static bool isBitSet(unsigned int value, int index)
Definition bdlb_bitutil.h:314
static int sizeInBits(INTEGER value=0)
Definition bslmf_conditional.h:120
Definition bslmf_integralconstant.h:244