Commit e8b6bdc0 authored by Michael Lippautz's avatar Michael Lippautz Committed by Commit Bot

[base] Add templatized CountLeadingZeroBits/CountTrailingZeroBits

Bug: chromium:757440
Change-Id: I59f77f644a900f5fa1480867eb8be22b35bd3ad8
Reviewed-on: https://chromium-review.googlesource.com/758415Reviewed-by: default avatarKentaro Hara <haraken@chromium.org>
Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Commit-Queue: Michael Lippautz <mlippautz@chromium.org>
Cr-Commit-Position: refs/heads/master@{#515947}
parent 78049dc3
......@@ -12,6 +12,7 @@
#include "base/compiler_specific.h"
#include "base/logging.h"
#include "build/build_config.h"
#if defined(COMPILER_MSVC)
#include <intrin.h>
......@@ -54,57 +55,130 @@ inline size_t Align(size_t size, size_t alignment) {
return (size + alignment - 1) & ~(alignment - 1);
}
// These functions count the number of leading zeros in a binary value, starting
// with the most significant bit. C does not have an operator to do this, but
// fortunately the various compilers have built-ins that map to fast underlying
// processor instructions.
// CountLeadingZeroBits(value) returns the number of zero bits following the
// most significant 1 bit in |value| if |value| is non-zero, otherwise it
// returns {sizeof(T) * 8}.
// Example: 00100010 -> 2
//
// CountTrailingZeroBits(value) returns the number of zero bits preceding the
// least significant 1 bit in |value| if |value| is non-zero, otherwise it
// returns {sizeof(T) * 8}.
// Example: 00100010 -> 1
//
// C does not have an operator to do this, but fortunately the various
// compilers have built-ins that map to fast underlying processor instructions.
#if defined(COMPILER_MSVC)
ALWAYS_INLINE uint32_t CountLeadingZeroBits32(uint32_t x) {
template <typename T, unsigned bits = sizeof(T) * 8>
ALWAYS_INLINE
typename std::enable_if<std::is_unsigned<T>::value && sizeof(T) <= 4,
unsigned>::type
CountLeadingZeroBits(T x) {
static_assert(bits > 0, "invalid instantiation");
unsigned long index;
return LIKELY(_BitScanReverse(&index, static_cast<uint32_t>(x)))
? (31 - index - (32 - bits))
: bits;
}
template <typename T, unsigned bits = sizeof(T) * 8>
ALWAYS_INLINE
typename std::enable_if<std::is_unsigned<T>::value && sizeof(T) == 8,
unsigned>::type
CountLeadingZeroBits(T x) {
static_assert(bits > 0, "invalid instantiation");
unsigned long index;
return LIKELY(_BitScanReverse64(&index, static_cast<uint64_t>(x)))
? (63 - index)
: 64;
}
template <typename T, unsigned bits = sizeof(T) * 8>
ALWAYS_INLINE
typename std::enable_if<std::is_unsigned<T>::value && sizeof(T) <= 4,
unsigned>::type
CountTrailingZeroBits(T x) {
static_assert(bits > 0, "invalid instantiation");
unsigned long index;
return LIKELY(_BitScanReverse(&index, x)) ? (31 - index) : 32;
return LIKELY(_BitScanForward(&index, static_cast<uint32_t>(x))) ? index
: bits;
}
template <typename T, unsigned bits = sizeof(T) * 8>
ALWAYS_INLINE
typename std::enable_if<std::is_unsigned<T>::value && sizeof(T) == 8,
unsigned>::type
CountTrailingZeroBits(T x) {
static_assert(bits > 0, "invalid instantiation");
unsigned long index;
return LIKELY(_BitScanForward64(&index, static_cast<uint64_t>(x))) ? index
: 64;
}
ALWAYS_INLINE uint32_t CountLeadingZeroBits32(uint32_t x) {
return CountLeadingZeroBits(x);
}
#if defined(ARCH_CPU_64_BITS)
// MSVC only supplies _BitScanForward64 when building for a 64-bit target.
ALWAYS_INLINE uint64_t CountLeadingZeroBits64(uint64_t x) {
unsigned long index;
return LIKELY(_BitScanReverse64(&index, x)) ? (63 - index) : 64;
return CountLeadingZeroBits(x);
}
#endif
#elif defined(COMPILER_GCC)
// This is very annoying. __builtin_clz has undefined behaviour for an input of
// 0, even though there's clearly a return value that makes sense, and even
// though some processor clz instructions have defined behaviour for 0. We could
// drop to raw __asm__ to do better, but we'll avoid doing that unless we see
// proof that we need to.
ALWAYS_INLINE uint32_t CountLeadingZeroBits32(uint32_t x) {
return LIKELY(x) ? __builtin_clz(x) : 32;
// __builtin_clz has undefined behaviour for an input of 0, even though there's
// clearly a return value that makes sense, and even though some processor clz
// instructions have defined behaviour for 0. We could drop to raw __asm__ to
// do better, but we'll avoid doing that unless we see proof that we need to.
template <typename T, unsigned bits = sizeof(T) * 8>
ALWAYS_INLINE
typename std::enable_if<std::is_unsigned<T>::value && sizeof(T) <= 8,
unsigned>::type
CountLeadingZeroBits(T value) {
static_assert(bits > 0, "invalid instantiation");
return LIKELY(value)
? bits == 64
? __builtin_clzll(static_cast<uint64_t>(value))
: __builtin_clz(static_cast<uint32_t>(value)) - (32 - bits)
: bits;
}
ALWAYS_INLINE uint64_t CountLeadingZeroBits64(uint64_t x) {
return LIKELY(x) ? __builtin_clzll(x) : 64;
template <typename T, unsigned bits = sizeof(T) * 8>
ALWAYS_INLINE
typename std::enable_if<std::is_unsigned<T>::value && sizeof(T) <= 8,
unsigned>::type
CountTrailingZeroBits(T value) {
return LIKELY(value) ? bits == 64
? __builtin_ctzll(static_cast<uint64_t>(value))
: __builtin_ctz(static_cast<uint32_t>(value))
: bits;
}
#endif
ALWAYS_INLINE uint32_t CountLeadingZeroBits32(uint32_t x) {
return CountLeadingZeroBits(x);
}
#if defined(ARCH_CPU_64_BITS)
ALWAYS_INLINE size_t CountLeadingZeroBitsSizeT(size_t x) {
return CountLeadingZeroBits64(x);
ALWAYS_INLINE uint64_t CountLeadingZeroBits64(uint64_t x) {
return CountLeadingZeroBits(x);
}
#else
#endif
#endif
ALWAYS_INLINE size_t CountLeadingZeroBitsSizeT(size_t x) {
return CountLeadingZeroBits32(x);
return CountLeadingZeroBits(x);
}
#endif
ALWAYS_INLINE size_t CountTrailingZeroBitsSizeT(size_t x) {
return CountTrailingZeroBits(x);
}
} // namespace bits
} // namespace base
......
......@@ -5,6 +5,7 @@
// This file contains the unit tests for the bit utilities.
#include "base/bits.h"
#include "build/build_config.h"
#include <stddef.h>
......@@ -61,24 +62,112 @@ TEST(BitsTest, Align) {
EXPECT_EQ(kSizeTMax / 2 + 1, Align(1, kSizeTMax / 2 + 1));
}
TEST(BitsTest, CLZWorks) {
EXPECT_EQ(32u, CountLeadingZeroBits32(0u));
EXPECT_EQ(31u, CountLeadingZeroBits32(1u));
EXPECT_EQ(1u, CountLeadingZeroBits32(1u << 30));
EXPECT_EQ(0u, CountLeadingZeroBits32(1u << 31));
TEST(BitsTest, CountLeadingZeroBits8) {
EXPECT_EQ(8u, CountLeadingZeroBits(uint8_t{0}));
EXPECT_EQ(7u, CountLeadingZeroBits(uint8_t{1}));
for (uint8_t shift = 0; shift <= 7; shift++) {
EXPECT_EQ(7u - shift,
CountLeadingZeroBits(static_cast<uint8_t>(1 << shift)));
}
EXPECT_EQ(4u, CountLeadingZeroBits(uint8_t{0x0f}));
}
TEST(BitsTest, CountLeadingZeroBits16) {
EXPECT_EQ(16u, CountLeadingZeroBits(uint16_t{0}));
EXPECT_EQ(15u, CountLeadingZeroBits(uint16_t{1}));
for (uint16_t shift = 0; shift <= 15; shift++) {
EXPECT_EQ(15u - shift,
CountLeadingZeroBits(static_cast<uint16_t>(1 << shift)));
}
EXPECT_EQ(4u, CountLeadingZeroBits(uint16_t{0x0f0f}));
}
TEST(BitsTest, CountLeadingZeroBits32) {
EXPECT_EQ(32u, CountLeadingZeroBits(uint32_t{0}));
EXPECT_EQ(31u, CountLeadingZeroBits(uint32_t{1}));
for (uint32_t shift = 0; shift <= 31; shift++) {
EXPECT_EQ(31u - shift, CountLeadingZeroBits(uint32_t{1} << shift));
}
EXPECT_EQ(4u, CountLeadingZeroBits(uint32_t{0x0f0f0f0f}));
}
TEST(BitsTest, CountTrailingeZeroBits8) {
EXPECT_EQ(8u, CountTrailingZeroBits(uint8_t{0}));
EXPECT_EQ(7u, CountTrailingZeroBits(uint8_t{128}));
for (uint8_t shift = 0; shift <= 7; shift++) {
EXPECT_EQ(shift, CountTrailingZeroBits(static_cast<uint8_t>(1 << shift)));
}
EXPECT_EQ(4u, CountTrailingZeroBits(uint8_t{0xf0}));
}
TEST(BitsTest, CountTrailingeZeroBits16) {
EXPECT_EQ(16u, CountTrailingZeroBits(uint16_t{0}));
EXPECT_EQ(15u, CountTrailingZeroBits(uint16_t{32768}));
for (uint16_t shift = 0; shift <= 15; shift++) {
EXPECT_EQ(shift, CountTrailingZeroBits(static_cast<uint16_t>(1 << shift)));
}
EXPECT_EQ(4u, CountTrailingZeroBits(uint16_t{0xf0f0}));
}
TEST(BitsTest, CountTrailingeZeroBits32) {
EXPECT_EQ(32u, CountTrailingZeroBits(uint32_t{0}));
EXPECT_EQ(31u, CountTrailingZeroBits(uint32_t{1} << 31));
for (uint32_t shift = 0; shift <= 31; shift++) {
EXPECT_EQ(shift, CountTrailingZeroBits(uint32_t{1} << shift));
}
EXPECT_EQ(4u, CountTrailingZeroBits(uint32_t{0xf0f0f0f0}));
}
#if defined(ARCH_CPU_64_BITS)
TEST(BitsTest, CountLeadingZeroBits64) {
EXPECT_EQ(64u, CountLeadingZeroBits(uint64_t{0}));
EXPECT_EQ(63u, CountLeadingZeroBits(uint64_t{1}));
for (uint64_t shift = 0; shift <= 63; shift++) {
EXPECT_EQ(63u - shift, CountLeadingZeroBits(uint64_t{1} << shift));
}
EXPECT_EQ(4u, CountLeadingZeroBits(uint64_t{0x0f0f0f0f0f0f0f0f}));
}
TEST(BitsTest, CountTrailingeZeroBits64) {
EXPECT_EQ(64u, CountTrailingZeroBits(uint64_t{0}));
EXPECT_EQ(63u, CountTrailingZeroBits(uint64_t{1} << 63));
for (uint64_t shift = 0; shift <= 31; shift++) {
EXPECT_EQ(shift, CountTrailingZeroBits(uint64_t{1} << shift));
}
EXPECT_EQ(4u, CountTrailingZeroBits(uint64_t{0xf0f0f0f0f0f0f0f0}));
}
#endif // ARCH_CPU_64_BITS
TEST(BitsTest, CountLeadingZeroBitsSizeT) {
#if defined(ARCH_CPU_64_BITS)
EXPECT_EQ(64u, CountLeadingZeroBitsSizeT(size_t{0}));
EXPECT_EQ(63u, CountLeadingZeroBitsSizeT(size_t{1}));
EXPECT_EQ(32u, CountLeadingZeroBitsSizeT(size_t{1} << 31));
EXPECT_EQ(1u, CountLeadingZeroBitsSizeT(size_t{1} << 62));
EXPECT_EQ(0u, CountLeadingZeroBitsSizeT(size_t{1} << 63));
#else
EXPECT_EQ(32u, CountLeadingZeroBitsSizeT(size_t{0}));
EXPECT_EQ(31u, CountLeadingZeroBitsSizeT(size_t{1}));
EXPECT_EQ(1u, CountLeadingZeroBitsSizeT(size_t{1} << 30));
EXPECT_EQ(0u, CountLeadingZeroBitsSizeT(size_t{1} << 31));
#endif // ARCH_CPU_64_BITS
}
TEST(BitsTest, CountTrailingZeroBitsSizeT) {
#if defined(ARCH_CPU_64_BITS)
EXPECT_EQ(64u, CountLeadingZeroBitsSizeT(0ull));
EXPECT_EQ(63u, CountLeadingZeroBitsSizeT(1ull));
EXPECT_EQ(32u, CountLeadingZeroBitsSizeT(1ull << 31));
EXPECT_EQ(1u, CountLeadingZeroBitsSizeT(1ull << 62));
EXPECT_EQ(0u, CountLeadingZeroBitsSizeT(1ull << 63));
EXPECT_EQ(64u, CountTrailingZeroBitsSizeT(size_t{0}));
EXPECT_EQ(63u, CountTrailingZeroBitsSizeT(size_t{1} << 63));
EXPECT_EQ(31u, CountTrailingZeroBitsSizeT(size_t{1} << 31));
EXPECT_EQ(1u, CountTrailingZeroBitsSizeT(size_t{2}));
EXPECT_EQ(0u, CountTrailingZeroBitsSizeT(size_t{1}));
#else
EXPECT_EQ(32u, CountLeadingZeroBitsSizeT(0u));
EXPECT_EQ(31u, CountLeadingZeroBitsSizeT(1u));
EXPECT_EQ(1u, CountLeadingZeroBitsSizeT(1u << 30));
EXPECT_EQ(0u, CountLeadingZeroBitsSizeT(1u << 31));
#endif
EXPECT_EQ(32u, CountTrailingZeroBitsSizeT(size_t{0}));
EXPECT_EQ(31u, CountTrailingZeroBitsSizeT(size_t{1} << 31));
EXPECT_EQ(1u, CountTrailingZeroBitsSizeT(size_t{2}));
EXPECT_EQ(0u, CountTrailingZeroBitsSizeT(size_t{1}));
#endif // ARCH_CPU_64_BITS
}
} // namespace bits
......
......@@ -55,7 +55,6 @@ jumbo_component("wtf") {
"AutoReset.h",
"BitVector.cpp",
"BitVector.h",
"BitwiseOperations.h",
"BloomFilter.h",
"ByteOrder.h",
"ByteSwap.h",
......
/*
* Copyright (C) 2013 Google Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// TODO(palmer): The only caller of this code in Blink is PartitionAlloc. When
// PA is moved to base, we can remove this file.
// https://bugs.chromium.org/p/chromium/issues/detail?id=632441
#ifndef WTF_BitwiseOperations_h
#define WTF_BitwiseOperations_h
#include "base/bits.h"
#include "build/build_config.h"
namespace WTF {
using base::bits::CountLeadingZeroBits32;
using base::bits::CountLeadingZeroBitsSizeT;
#if defined(ARCH_CPU_64_BITS)
using base::bits::CountLeadingZeroBits64;
#endif
} // namespace WTF
#endif // WTF_BitwiseOperations_h
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment