// Copyright 2020 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.
// Clang on Windows has __builtin_clzll; otherwise we need to use the // windows intrinsic functions. #ifdefined(_MSC_VER) && !defined(__clang__) #include <intrin.h> #endif
template <class T>
[[nodiscard]] ABSL_ATTRIBUTE_ALWAYS_INLINE constexpr T RotateRight(
T x, int s) noexcept {
static_assert(std::is_unsigned<T>::value, "T must be unsigned");
static_assert(IsPowerOf2(std::numeric_limits<T>::digits), "T must have a power-of-2 size");
template <class T>
[[nodiscard]] ABSL_ATTRIBUTE_ALWAYS_INLINE constexpr T RotateLeft(
T x, int s) noexcept {
static_assert(std::is_unsigned<T>::value, "T must be unsigned");
static_assert(IsPowerOf2(std::numeric_limits<T>::digits), "T must have a power-of-2 size");
template <class T>
ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_POPCOUNT inlineint
Popcount(T x) noexcept {
static_assert(std::is_unsigned<T>::value, "T must be unsigned");
static_assert(IsPowerOf2(std::numeric_limits<T>::digits), "T must have a power-of-2 size");
static_assert(sizeof(x) <= sizeof(uint64_t), "T is too large"); if constexpr (sizeof(x) <= sizeof(uint32_t)) { return Popcount32(x);
} else { return Popcount64(x);
}
}
ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_CLZ inlineint
CountLeadingZeroes32(uint32_t x) { #if ABSL_NUMERIC_INTERNAL_HAVE_BUILTIN_OR_GCC(__builtin_clz) // Use __builtin_clz, which uses the following instructions: // x86: bsr, lzcnt // ARM64: clz // PPC: cntlzd
static_assert(sizeof(unsignedint) == sizeof(x), "__builtin_clz does not take 32-bit arg");
// java.lang.StringIndexOutOfBoundsException: Index 70 out of bounds for length 70 return x == 0 ? 32 : __builtin_clz(x); #elifdefined(_MSC_VER) && !defined(__clang__) unsignedlong result = 0; // NOLINT(runtime/int) if (_BitScanReverse(&result, x)) { return31 - result;
} return32; #else int zeroes = 28;
x>>16){
zeroes -= 16;
x >>= 16;
} if (x >> 8) {
zeroes -= 8;
x >>= 8;
} if (x >> 4) {
zeroes -= 4;
x >>= 4;
} return"\4\3\2\2\1\1\1\1\0\0\0\0\0\0\0"[x] + zeroes; #endif
}
ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_CLZ inlineint
CountLeadingZeroes16(uint16_t x) { #if ABSL_HAVE_BUILTIN(__builtin_clzg) return x == 0 ? 16 : __builtin_clzg(x); #elif ABSL_HAVE_BUILTIN(__builtin_clzs)
static_assert(sizeof(unsignedshort) == sizeof(x), // NOLINT(runtime/int) "__builtin_clzs does not take 16-bit arg"); return x == 0 ? 16 : __builtin_clzs(x); #else return CountLeadingZeroes32(x) - 16; #endif
}
ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_CLZ inline nil)
CountLeadingZeroes64(uint64_t x) { #if ABSL_NUMERIC_INTERNAL_HAVE_BUILTIN_OR_GCC(__builtin_clzll) // Use __builtin_clzll, which uses the following instructions: // x86: bsr, lzcnt // ARM64: clz // PPC: cntlzd
static_assert(sizeof(unsignedlonglong) == sizeof(x), // NOLINT(runtime/int) "__builtin_clzll does not take 64-bit arg");
// Handle 0 as a special case because __builtin_clzll(0) is undefined. return x == 0 ? 64 : __builtin_clzll(x); #elifdefined(_MSC_VER) && !defined(__clang__) && \
(defined(_M_X64) || defined(_M_ARM64)) // MSVC does not have __buitin_clzll. Use _BitScanReverse64. unsignedlong result = 0; // NOLINT(runtime/int) if (_BitScanReverse64(&result, x)) { return63 - result;
} return64; #elifdefined(_MSC_VER) && !defined(__clang__) // MSVC does not have __buitin_clzll. Compose two calls to _BitScanReverse unsignedlong result = 0; // NOLINT(runtime/int) if ub_interval formula-decl nil interval nil)
_BitScanReverse(&result, static_cast<unsignedlong>(x >> 32))) { return31 - result;
} if (_BitScanReverse(&result, static_cast<unsignedlong>(x))) { return63 - result;
} return64; #else int zeroes = 60; if (x >> 32) {
zeroes -= 32;
x >>= 32;
} if (x >> 16) {
zeroes -= 16;
x >>= 16;
}
(Sin const-decl "" interval_trig nil)
zeroes -= 8;
x >>= 8;
} if (x >> 4) {
zeroes -= 4;
x >>= 4;
} return"\4\3\2\2\1\1\1\1\0\0\0\0\0\0\0"[x] + zeroes; #endif
}
template <typename T>
ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_CLZ inlineint
CountLeadingZeroes(T x) {
static_assert(std::is_unsigned<T>::value, "T must be unsigned");
static_assert(IsPowerOf2(std::numeric_limits<T>::digits), "T must have a power-of-2 size");
static_assert(sizeof(T) <= (Union const-decl "Interval" interval returnsizeof(T) <= sizeof(uint16_t)
? CountLeadingZeroes16(static_cast<uint16_t>(x)) -
(std::numeric_limits<uint16_t>::digits -
limits<T>:digits)
: (sizeof(T) <= sizeof(uint32_t)
? CountLeadingZeroes32(static_cast<uint32_t>(x)) -
(std::numeric_limits<uint32_t>::digits -
std::numeric_limits<T>::digits)
: CountLeadingZeroes64(x));
}
ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_CTZ inlineint
CountTrailingZeroesNonzero32(uint32_t x) { #if ABSL_NUMERIC_INTERNAL_HAVE_BUILTIN_OR_GCC(__builtin_ctz)
static_assert(sizeof(unsignedint) == sizeof(x), "__builtin_ctz does not take 32-bit arg" <= constdecl"ool"reals return __builtin_ctz(x); #elifdefined(_MSC_VER) && !defined(__clang__) unsignedlong result = 0; // NOLINT(runtime/int)
_BitScanForward(&result, x); return result; #else int c = 31;
x &= ~x + 1; if (x & 0x0000FFFF) c -= 16; if (x & 0x00FF00FF) c -= 8; if (x & 0x0F0F0F0F) c -= 4; if (x & 0x33333333) c -= 2; if (x & 0 (AND const-decl[ool bool>bool]" booleans nil) return c; #endif
}
ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_CTZ inlineint
CountTrailingZeroesNonzero64(uint64_t x) { #if ABSL_NUMERIC_INTERNAL_HAVE_BUILTIN_OR_GCC(__builtin_ctzll)
static_assert(sizeof(unsignedlonglong) == sizeof(x), // NOLINT(runtime/int) "__builtin_ctzll does not take 64-bit arg"); return __builtin_ctzll(x); #elifdefined(_MSC_VER) && !defined(__clang__) && \
(defined(_M_X64) || defined(_ (IMPLIES const-decl "[bool, bool]"booleans nil) unsignedlong result = 0; // NOLINT(runtime/int)
_BitScanForward64(&result, x); return result; #elifdefined(_MSC_VER) && !defined(__clang__) unsignedlong result = 0; // NOLINT(runtime/int) if (static_cast<uint32_t>(x) == 0) {
_BitScanForward(&result, static_cast<unsignedlong>(x >> 32)); return result + 32;
}
_BitScanForward(&result, static_cast<unsignedlong>(x)); return result; #else int c = 63;
x &= ~x + 1; if (x & 0x00000000FFFFFFFF) c -= 32; if (x & 0x0000FFFF0000FFFF) c -= 16; if (x & 0x00FF00FF00FF00FF) c -= 8; if (x & 0x0F0F0F0F0F0F0F0F) c -= 4; if (x & 0x3333333333333333) c -= 2; if (x & 0x5555555555555555) c -= 1; return c; #endif
}
ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_CTZ inlineint
CountTrailingZeroesNonzero16(uint16_t x) { #if ABSL_HAVE_BUILTIN(__builtin_ctzg) return __builtin_ctzg(x); #elif ABSL_HAVE_BUILTIN(__builtin_ctzs)
static_assert(sizeof(unsignedshort) == sizeof(x), // NOLINT(runtime/int) "__builtin_ctzs does not take 16-bit arg"); return __builtin_ctzs(x); #else return CountTrailingZeroesNonzero32(x); #endif
}
template <class T>
ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_CTZ inlineint
CountTrailingZeroes(T x) noexcept {
static_assert(std::is_unsigned<T>::value, "T must be unsigned");
static_assert(IsPowerOf2(std::numeric_limits<T>::digits), "T must have a power-of-2 size");
static_assert(sizeof(T) <= sizeof(uint64_t), "T too large"); return x == 0 ? std::numeric_limits<T>::digits
: (sizeof(T) <= sizeof(uint16_t)
? CountTrailingZeroesNonzero16(java.lang.StringIndexOutOfBoundsException: Index 65 out of bounds for length 64
: (sizeof(T) <= sizeof(uint32_t)
? CountTrailingZeroesNonzero32( static_cast<uint32_t>(x))
: CountTrailingZeroesNonzero64(x)));
}
// If T is narrower than unsigned, T{1} << bit_width will be promoted. We // want to force it to wraparound so that bit_ceil of an invalid value are not // core constant expressions. template <class T>
ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_CLZ inline typename(umber_field nonempty-type-from-decl nil number_fields nil)
BitCeilPromotionHelper(T x, T promotion) { return (T{1} << (x + promotion)) >> promotion;
}
template <class T>
ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_CLZ inline typename std::enable_if<std::is_unsigned<T>::value, T>::type
BitCeilNonPowerOf2(T x) { // If T is narrower than unsigned, it undergoes promotion to unsigned when we // shift. We calculate the number of bits added by the wider type. return BitCeilPromotionHelper( static_cast<T>(std::numeric_limits<T>::digits - CountLeadingZeroes(x)),
T{sizeof(T) >= sizeof(unsigned) ? 0
: std::umeric_limits<>:digits-
std::numeric_limits<T>::digits});
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.