2103fd9acd
-- d447fdcb801036cf08197eece193a5a706661120 by Gennadiy Rozental <rogeeff@google.com>: Eliminate the need for static function holding help message. This decreases the cost of ABSL_FLAG abstraction by 120 bytes under clang. PiperOrigin-RevId: 281107806 -- 0aa6b91189f0e8b2381438c33465673a7ae02487 by Derek Mauro <dmauro@google.com>: Disable the weak symbol CCTZ extension in the time test_util on MinGW, which does not support it. PiperOrigin-RevId: 280719769 -- 67322c41c3e776eb541de90fa4526bdb49422eb6 by Abseil Team <absl-team@google.com>: Tune PeriodicSampler implementation (for internal-use only) PiperOrigin-RevId: 280708943 -- 3a48c346340c7ed03816645cd327e1ff07729aa4 by Abseil Team <absl-team@google.com>: Clean up public headers not to have warnings for "-Wcomma" PiperOrigin-RevId: 280695373 -- 981acd1ef3b13a83a84f04f11c8931f4ed4451c9 by Matthew Brown <matthewbr@google.com>: Release absl::int128. PiperOrigin-RevId: 280690817 -- d30fae9d2ec30b81322d2eb5afe7e13e45b4b422 by Derek Mauro <dmauro@google.com>: Fix -Wundef warnings in random platform detection PiperOrigin-RevId: 280669598 GitOrigin-RevId: d447fdcb801036cf08197eece193a5a706661120 Change-Id: Ie5e10e567c54b7de211833607689f233d4ddf734
214 lines
5.8 KiB
C++
214 lines
5.8 KiB
C++
// Copyright 2018 The Abseil Authors.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
#ifndef ABSL_BASE_INTERNAL_BITS_H_
|
|
#define ABSL_BASE_INTERNAL_BITS_H_
|
|
|
|
// This file contains bitwise ops which are implementation details of various
|
|
// absl libraries.
|
|
|
|
#include <cstdint>
|
|
|
|
// Clang on Windows has __builtin_clzll; otherwise we need to use the
|
|
// windows intrinsic functions.
|
|
#if defined(_MSC_VER)
|
|
#include <intrin.h>
|
|
#if defined(_M_X64)
|
|
#pragma intrinsic(_BitScanReverse64)
|
|
#pragma intrinsic(_BitScanForward64)
|
|
#endif
|
|
#pragma intrinsic(_BitScanReverse)
|
|
#pragma intrinsic(_BitScanForward)
|
|
#endif
|
|
|
|
#include "absl/base/attributes.h"
|
|
|
|
#if defined(_MSC_VER)
|
|
// We can achieve something similar to attribute((always_inline)) with MSVC by
|
|
// using the __forceinline keyword, however this is not perfect. MSVC is
|
|
// much less aggressive about inlining, and even with the __forceinline keyword.
|
|
#define ABSL_BASE_INTERNAL_FORCEINLINE __forceinline
|
|
#else
|
|
// Use default attribute inline.
|
|
#define ABSL_BASE_INTERNAL_FORCEINLINE inline ABSL_ATTRIBUTE_ALWAYS_INLINE
|
|
#endif
|
|
|
|
|
|
namespace absl {
|
|
namespace base_internal {
|
|
|
|
ABSL_BASE_INTERNAL_FORCEINLINE int CountLeadingZeros64Slow(uint64_t n) {
|
|
int zeroes = 60;
|
|
if (n >> 32) {
|
|
zeroes -= 32;
|
|
n >>= 32;
|
|
}
|
|
if (n >> 16) {
|
|
zeroes -= 16;
|
|
n >>= 16;
|
|
}
|
|
if (n >> 8) {
|
|
zeroes -= 8;
|
|
n >>= 8;
|
|
}
|
|
if (n >> 4) {
|
|
zeroes -= 4;
|
|
n >>= 4;
|
|
}
|
|
return "\4\3\2\2\1\1\1\1\0\0\0\0\0\0\0"[n] + zeroes;
|
|
}
|
|
|
|
ABSL_BASE_INTERNAL_FORCEINLINE int CountLeadingZeros64(uint64_t n) {
|
|
#if defined(_MSC_VER) && defined(_M_X64)
|
|
// MSVC does not have __buitin_clzll. Use _BitScanReverse64.
|
|
unsigned long result = 0; // NOLINT(runtime/int)
|
|
if (_BitScanReverse64(&result, n)) {
|
|
return 63 - result;
|
|
}
|
|
return 64;
|
|
#elif defined(_MSC_VER)
|
|
// MSVC does not have __buitin_clzll. Compose two calls to _BitScanReverse
|
|
unsigned long result = 0; // NOLINT(runtime/int)
|
|
if ((n >> 32) && _BitScanReverse(&result, n >> 32)) {
|
|
return 31 - result;
|
|
}
|
|
if (_BitScanReverse(&result, n)) {
|
|
return 63 - result;
|
|
}
|
|
return 64;
|
|
#elif defined(__GNUC__)
|
|
// Use __builtin_clzll, which uses the following instructions:
|
|
// x86: bsr
|
|
// ARM64: clz
|
|
// PPC: cntlzd
|
|
static_assert(sizeof(unsigned long long) == sizeof(n), // NOLINT(runtime/int)
|
|
"__builtin_clzll does not take 64-bit arg");
|
|
|
|
// Handle 0 as a special case because __builtin_clzll(0) is undefined.
|
|
if (n == 0) {
|
|
return 64;
|
|
}
|
|
return __builtin_clzll(n);
|
|
#else
|
|
return CountLeadingZeros64Slow(n);
|
|
#endif
|
|
}
|
|
|
|
ABSL_BASE_INTERNAL_FORCEINLINE int CountLeadingZeros32Slow(uint64_t n) {
|
|
int zeroes = 28;
|
|
if (n >> 16) {
|
|
zeroes -= 16;
|
|
n >>= 16;
|
|
}
|
|
if (n >> 8) {
|
|
zeroes -= 8;
|
|
n >>= 8;
|
|
}
|
|
if (n >> 4) {
|
|
zeroes -= 4;
|
|
n >>= 4;
|
|
}
|
|
return "\4\3\2\2\1\1\1\1\0\0\0\0\0\0\0"[n] + zeroes;
|
|
}
|
|
|
|
ABSL_BASE_INTERNAL_FORCEINLINE int CountLeadingZeros32(uint32_t n) {
|
|
#if defined(_MSC_VER)
|
|
unsigned long result = 0; // NOLINT(runtime/int)
|
|
if (_BitScanReverse(&result, n)) {
|
|
return 31 - result;
|
|
}
|
|
return 32;
|
|
#elif defined(__GNUC__)
|
|
// Use __builtin_clz, which uses the following instructions:
|
|
// x86: bsr
|
|
// ARM64: clz
|
|
// PPC: cntlzd
|
|
static_assert(sizeof(int) == sizeof(n),
|
|
"__builtin_clz does not take 32-bit arg");
|
|
|
|
// Handle 0 as a special case because __builtin_clz(0) is undefined.
|
|
if (n == 0) {
|
|
return 32;
|
|
}
|
|
return __builtin_clz(n);
|
|
#else
|
|
return CountLeadingZeros32Slow(n);
|
|
#endif
|
|
}
|
|
|
|
ABSL_BASE_INTERNAL_FORCEINLINE int CountTrailingZerosNonZero64Slow(uint64_t n) {
|
|
int c = 63;
|
|
n &= ~n + 1;
|
|
if (n & 0x00000000FFFFFFFF) c -= 32;
|
|
if (n & 0x0000FFFF0000FFFF) c -= 16;
|
|
if (n & 0x00FF00FF00FF00FF) c -= 8;
|
|
if (n & 0x0F0F0F0F0F0F0F0F) c -= 4;
|
|
if (n & 0x3333333333333333) c -= 2;
|
|
if (n & 0x5555555555555555) c -= 1;
|
|
return c;
|
|
}
|
|
|
|
ABSL_BASE_INTERNAL_FORCEINLINE int CountTrailingZerosNonZero64(uint64_t n) {
|
|
#if defined(_MSC_VER) && defined(_M_X64)
|
|
unsigned long result = 0; // NOLINT(runtime/int)
|
|
_BitScanForward64(&result, n);
|
|
return result;
|
|
#elif defined(_MSC_VER)
|
|
unsigned long result = 0; // NOLINT(runtime/int)
|
|
if (static_cast<uint32_t>(n) == 0) {
|
|
_BitScanForward(&result, n >> 32);
|
|
return result + 32;
|
|
}
|
|
_BitScanForward(&result, n);
|
|
return result;
|
|
#elif defined(__GNUC__)
|
|
static_assert(sizeof(unsigned long long) == sizeof(n), // NOLINT(runtime/int)
|
|
"__builtin_ctzll does not take 64-bit arg");
|
|
return __builtin_ctzll(n);
|
|
#else
|
|
return CountTrailingZerosNonZero64Slow(n);
|
|
#endif
|
|
}
|
|
|
|
ABSL_BASE_INTERNAL_FORCEINLINE int CountTrailingZerosNonZero32Slow(uint32_t n) {
|
|
int c = 31;
|
|
n &= ~n + 1;
|
|
if (n & 0x0000FFFF) c -= 16;
|
|
if (n & 0x00FF00FF) c -= 8;
|
|
if (n & 0x0F0F0F0F) c -= 4;
|
|
if (n & 0x33333333) c -= 2;
|
|
if (n & 0x55555555) c -= 1;
|
|
return c;
|
|
}
|
|
|
|
ABSL_BASE_INTERNAL_FORCEINLINE int CountTrailingZerosNonZero32(uint32_t n) {
|
|
#if defined(_MSC_VER)
|
|
unsigned long result = 0; // NOLINT(runtime/int)
|
|
_BitScanForward(&result, n);
|
|
return result;
|
|
#elif defined(__GNUC__)
|
|
static_assert(sizeof(int) == sizeof(n),
|
|
"__builtin_ctz does not take 32-bit arg");
|
|
return __builtin_ctz(n);
|
|
#else
|
|
return CountTrailingZerosNonZero32Slow(n);
|
|
#endif
|
|
}
|
|
|
|
#undef ABSL_BASE_INTERNAL_FORCEINLINE
|
|
|
|
} // namespace base_internal
|
|
} // namespace absl
|
|
|
|
#endif // ABSL_BASE_INTERNAL_BITS_H_
|