Refactor the newly created <bit> header. Still (almost) NFC. Reviewed as https://reviews.llvm.org/D50876

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@340049 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Marshall Clow
2018-08-17 17:27:25 +00:00
parent 6c372355ba
commit f017e1e2a4
2 changed files with 72 additions and 83 deletions

View File

@@ -12,6 +12,7 @@
#define _LIBCPP___BIT_REFERENCE #define _LIBCPP___BIT_REFERENCE
#include <__config> #include <__config>
#include <bit>
#include <algorithm> #include <algorithm>
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
@@ -254,18 +255,18 @@ __count_bool_true(__bit_iterator<_Cp, _IsConst> __first, typename _Cp::size_type
__storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_); __storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_);
__storage_type __dn = _VSTD::min(__clz_f, __n); __storage_type __dn = _VSTD::min(__clz_f, __n);
__storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn)); __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn));
__r = _VSTD::__pop_count(*__first.__seg_ & __m); __r = _VSTD::__popcount(*__first.__seg_ & __m);
__n -= __dn; __n -= __dn;
++__first.__seg_; ++__first.__seg_;
} }
// do middle whole words // do middle whole words
for (; __n >= __bits_per_word; ++__first.__seg_, __n -= __bits_per_word) for (; __n >= __bits_per_word; ++__first.__seg_, __n -= __bits_per_word)
__r += _VSTD::__pop_count(*__first.__seg_); __r += _VSTD::__popcount(*__first.__seg_);
// do last partial word // do last partial word
if (__n > 0) if (__n > 0)
{ {
__storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n); __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
__r += _VSTD::__pop_count(*__first.__seg_ & __m); __r += _VSTD::__popcount(*__first.__seg_ & __m);
} }
return __r; return __r;
} }
@@ -285,18 +286,18 @@ __count_bool_false(__bit_iterator<_Cp, _IsConst> __first, typename _Cp::size_typ
__storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_); __storage_type __clz_f = static_cast<__storage_type>(__bits_per_word - __first.__ctz_);
__storage_type __dn = _VSTD::min(__clz_f, __n); __storage_type __dn = _VSTD::min(__clz_f, __n);
__storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn)); __storage_type __m = (~__storage_type(0) << __first.__ctz_) & (~__storage_type(0) >> (__clz_f - __dn));
__r = _VSTD::__pop_count(~*__first.__seg_ & __m); __r = _VSTD::__popcount(~*__first.__seg_ & __m);
__n -= __dn; __n -= __dn;
++__first.__seg_; ++__first.__seg_;
} }
// do middle whole words // do middle whole words
for (; __n >= __bits_per_word; ++__first.__seg_, __n -= __bits_per_word) for (; __n >= __bits_per_word; ++__first.__seg_, __n -= __bits_per_word)
__r += _VSTD::__pop_count(~*__first.__seg_); __r += _VSTD::__popcount(~*__first.__seg_);
// do last partial word // do last partial word
if (__n > 0) if (__n > 0)
{ {
__storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n); __storage_type __m = ~__storage_type(0) >> (__bits_per_word - __n);
__r += _VSTD::__pop_count(~*__first.__seg_ & __m); __r += _VSTD::__popcount(~*__first.__seg_ & __m);
} }
return __r; return __r;
} }

View File

@@ -35,135 +35,123 @@ namespace std {
_LIBCPP_BEGIN_NAMESPACE_STD _LIBCPP_BEGIN_NAMESPACE_STD
#ifndef _LIBCPP_COMPILER_MSVC
inline _LIBCPP_INLINE_VISIBILITY
int __ctz(unsigned __x) { return __builtin_ctz(__x); }
inline _LIBCPP_INLINE_VISIBILITY
int __ctz(unsigned long __x) { return __builtin_ctzl(__x); }
inline _LIBCPP_INLINE_VISIBILITY
int __ctz(unsigned long long __x) { return __builtin_ctzll(__x); }
inline _LIBCPP_INLINE_VISIBILITY
int __clz(unsigned __x) { return __builtin_clz(__x); }
inline _LIBCPP_INLINE_VISIBILITY
int __clz(unsigned long __x) { return __builtin_clzl(__x); }
inline _LIBCPP_INLINE_VISIBILITY
int __clz(unsigned long long __x) { return __builtin_clzll(__x); }
inline _LIBCPP_INLINE_VISIBILITY
int __popcount(unsigned __x) { return __builtin_popcount(__x); }
inline _LIBCPP_INLINE_VISIBILITY
int __popcount(unsigned long __x) { return __builtin_popcountl(__x); }
inline _LIBCPP_INLINE_VISIBILITY
int __popcount(unsigned long long __x) { return __builtin_popcountll(__x); }
#else // _LIBCPP_COMPILER_MSVC
// Precondition: __x != 0 // Precondition: __x != 0
inline _LIBCPP_INLINE_VISIBILITY inline _LIBCPP_INLINE_VISIBILITY
unsigned __ctz(unsigned __x) { int __ctz(unsigned __x) {
#ifndef _LIBCPP_COMPILER_MSVC
return static_cast<unsigned>(__builtin_ctz(__x));
#else
static_assert(sizeof(unsigned) == sizeof(unsigned long), ""); static_assert(sizeof(unsigned) == sizeof(unsigned long), "");
static_assert(sizeof(unsigned long) == 4, ""); static_assert(sizeof(unsigned long) == 4, "");
unsigned long where; unsigned long __where;
// Search from LSB to MSB for first set bit. if (_BitScanForward(&__where, __x))
// Returns zero if no set bit is found. return static_cast<int>(__where);
if (_BitScanForward(&where, __x))
return where;
return 32; return 32;
#endif
} }
inline _LIBCPP_INLINE_VISIBILITY inline _LIBCPP_INLINE_VISIBILITY
unsigned long __ctz(unsigned long __x) { int __ctz(unsigned long __x) {
#ifndef _LIBCPP_COMPILER_MSVC
return static_cast<unsigned long>(__builtin_ctzl(__x));
#else
static_assert(sizeof(unsigned long) == sizeof(unsigned), ""); static_assert(sizeof(unsigned long) == sizeof(unsigned), "");
return __ctz(static_cast<unsigned>(__x)); return __ctz(static_cast<unsigned>(__x));
#endif
} }
inline _LIBCPP_INLINE_VISIBILITY inline _LIBCPP_INLINE_VISIBILITY
unsigned long long __ctz(unsigned long long __x) { int __ctz(unsigned long long __x) {
#ifndef _LIBCPP_COMPILER_MSVC unsigned long __where;
return static_cast<unsigned long long>(__builtin_ctzll(__x));
#else
unsigned long where;
// Search from LSB to MSB for first set bit.
// Returns zero if no set bit is found.
#if defined(_LIBCPP_HAS_BITSCAN64) #if defined(_LIBCPP_HAS_BITSCAN64)
(defined(_M_AMD64) || defined(__x86_64__)) (defined(_M_AMD64) || defined(__x86_64__))
if (_BitScanForward64(&where, __x)) if (_BitScanForward64(&__where, __x))
return static_cast<int>(where); return static_cast<int>(__where);
#else #else
// Win32 doesn't have _BitScanForward64 so emulate it with two 32 bit calls. // Win32 doesn't have _BitScanForward64 so emulate it with two 32 bit calls.
// Scan the Low Word. if (_BitScanForward(&__where, static_cast<unsigned long>(__x)))
if (_BitScanForward(&where, static_cast<unsigned long>(__x))) return static_cast<int>(__where);
return where; if (_BitScanForward(&__where, static_cast<unsigned long>(__x >> 32)))
// Scan the High Word. return static_cast<int>(__where + 32);
if (_BitScanForward(&where, static_cast<unsigned long>(__x >> 32)))
return where + 32; // Create a bit offset from the LSB.
#endif #endif
return 64; return 64;
#endif // _LIBCPP_COMPILER_MSVC
} }
// Precondition: __x != 0 // Precondition: __x != 0
inline _LIBCPP_INLINE_VISIBILITY inline _LIBCPP_INLINE_VISIBILITY
unsigned __clz(unsigned __x) { int __clz(unsigned __x) {
#ifndef _LIBCPP_COMPILER_MSVC
return static_cast<unsigned>(__builtin_clz(__x));
#else
static_assert(sizeof(unsigned) == sizeof(unsigned long), ""); static_assert(sizeof(unsigned) == sizeof(unsigned long), "");
static_assert(sizeof(unsigned long) == 4, ""); static_assert(sizeof(unsigned long) == 4, "");
unsigned long where; unsigned long __where;
// Search from LSB to MSB for first set bit. if (_BitScanReverse(&__where, __x))
// Returns zero if no set bit is found. return static_cast<int>(31 - __where);
if (_BitScanReverse(&where, __x))
return 31 - where;
return 32; // Undefined Behavior. return 32; // Undefined Behavior.
#endif
} }
inline _LIBCPP_INLINE_VISIBILITY inline _LIBCPP_INLINE_VISIBILITY
unsigned long __clz(unsigned long __x) { int __clz(unsigned long __x) {
#ifndef _LIBCPP_COMPILER_MSVC
return static_cast<unsigned long>(__builtin_clzl (__x));
#else
static_assert(sizeof(unsigned) == sizeof(unsigned long), ""); static_assert(sizeof(unsigned) == sizeof(unsigned long), "");
return __clz(static_cast<unsigned>(__x)); return __clz(static_cast<unsigned>(__x));
#endif
} }
inline _LIBCPP_INLINE_VISIBILITY inline _LIBCPP_INLINE_VISIBILITY
unsigned long long __clz(unsigned long long __x) { int __clz(unsigned long long __x) {
#ifndef _LIBCPP_COMPILER_MSVC unsigned long __where;
return static_cast<unsigned long long>(__builtin_clzll(__x));
#else
unsigned long where;
// BitScanReverse scans from MSB to LSB for first set bit.
// Returns 0 if no set bit is found.
#if defined(_LIBCPP_HAS_BITSCAN64) #if defined(_LIBCPP_HAS_BITSCAN64)
if (_BitScanReverse64(&where, __x)) if (_BitScanReverse64(&__where, __x))
return static_cast<int>(63 - where); return static_cast<int>(63 - __where);
#else #else
// Scan the high 32 bits. // Win32 doesn't have _BitScanReverse64 so emulate it with two 32 bit calls.
if (_BitScanReverse(&where, static_cast<unsigned long>(__x >> 32))) if (_BitScanReverse(&__where, static_cast<unsigned long>(__x >> 32)))
return 63 - (where + 32); // Create a bit offset from the MSB. return static_cast<int>(63 - (__where + 32));
// Scan the low 32 bits. if (_BitScanReverse(&__where, static_cast<unsigned long>(__x)))
if (_BitScanReverse(&where, static_cast<unsigned long>(__x))) return static_cast<int>(63 - __where);
return 63 - where;
#endif #endif
return 64; // Undefined Behavior. return 64; // Undefined Behavior.
#endif // _LIBCPP_COMPILER_MSVC
} }
inline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned __x) { inline _LIBCPP_INLINE_VISIBILITY int __popcount(unsigned __x) {
#ifndef _LIBCPP_COMPILER_MSVC
return __builtin_popcount (__x);
#else
static_assert(sizeof(unsigned) == 4, ""); static_assert(sizeof(unsigned) == 4, "");
return __popcnt(__x); return __popcnt(__x);
#endif
} }
inline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned long __x) { inline _LIBCPP_INLINE_VISIBILITY int __popcount(unsigned long __x) {
#ifndef _LIBCPP_COMPILER_MSVC
return __builtin_popcountl (__x);
#else
static_assert(sizeof(unsigned long) == 4, ""); static_assert(sizeof(unsigned long) == 4, "");
return __popcnt(__x); return __popcnt(__x);
#endif
} }
inline _LIBCPP_INLINE_VISIBILITY int __pop_count(unsigned long long __x) { inline _LIBCPP_INLINE_VISIBILITY int __popcount(unsigned long long __x) {
#ifndef _LIBCPP_COMPILER_MSVC
return __builtin_popcountll(__x);
#else
static_assert(sizeof(unsigned long long) == 8, ""); static_assert(sizeof(unsigned long long) == 8, "");
return __popcnt64(__x); return __popcnt64(__x);
#endif
} }
#endif // _LIBCPP_COMPILER_MSVC
_LIBCPP_END_NAMESPACE_STD _LIBCPP_END_NAMESPACE_STD
#endif // _LIBCPP_BIT #endif // _LIBCPP_BIT