From: Tudor Bosman Date: Wed, 11 Jul 2012 21:28:00 +0000 (-0700) Subject: Make Bits work with T = Unaligned (X is unsigned integral type) X-Git-Tag: v0.22.0~1244 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=7488db1c43eaae6fd903a0172c394a4d390f430e;p=folly.git Make Bits work with T = Unaligned (X is unsigned integral type) Test Plan: all folly tests Reviewed By: andrei.alexandrescu@fb.com FB internal diff: D517118 --- diff --git a/folly/Bits.h b/folly/Bits.h index 5a131f23..6413252f 100644 --- a/folly/Bits.h +++ b/folly/Bits.h @@ -530,10 +530,15 @@ BitIterator findFirstSet(BitIterator begin, template struct Unaligned; +/** + * Representation of an unaligned value of a POD type. + */ template struct Unaligned< - T, - typename std::enable_if::value>::type> { + T, + typename std::enable_if::value>::type> { + Unaligned() { } // uninitialized + /* implicit */ Unaligned(T v) : value(v) { } T value; } __attribute__((packed)); @@ -542,6 +547,7 @@ struct Unaligned< */ template inline T loadUnaligned(const void* p) { + static_assert(sizeof(Unaligned) == sizeof(T), "Invalid unaligned size"); static_assert(alignof(Unaligned) == 1, "Invalid alignment"); return static_cast*>(p)->value; } @@ -551,8 +557,9 @@ inline T loadUnaligned(const void* p) { */ template inline void storeUnaligned(void* p, T value) { + static_assert(sizeof(Unaligned) == sizeof(T), "Invalid unaligned size"); static_assert(alignof(Unaligned) == 1, "Invalid alignment"); - static_cast*>(p)->value = value; + new (p) Unaligned(value); } } // namespace folly diff --git a/folly/experimental/Bits.h b/folly/experimental/Bits.h index 249450eb..a18f4f95 100644 --- a/folly/experimental/Bits.h +++ b/folly/experimental/Bits.h @@ -21,10 +21,15 @@ #include #include +#include "folly/Bits.h" #include "folly/Range.h" namespace folly { +// As a general rule, bit operations work on unsigned values only; +// right-shift is arithmetic for signed values, and that can lead to +// unpleasant bugs. + /** * Population count (number of bits set), using __builtin_popcount or * __builtin_popcountll, depending on size. @@ -50,14 +55,51 @@ inline typename std::enable_if< return __builtin_popcountll(x); } +namespace detail { + +/** + * Helper class to make Bits (below) work with both aligned values + * (T, where T is an unsigned integral type) or unaligned values + * (Unaligned, where T is an unsigned integral type) + */ +template struct BitsTraits; + +// Partial specialization for Unaligned, where T is unsigned integral template -struct Bits { - static_assert(std::is_integral::value && - std::is_unsigned::value, - "Unsigned integral type required"); +struct BitsTraits, typename std::enable_if< + (std::is_integral::value && std::is_unsigned::value)>::type> { + typedef T UnderlyingType; + static T load(const Unaligned& x) { return x.value; } + static void store(Unaligned& x, T v) { x.value = v; } +}; + +// Partial specialization for T, where T is unsigned integral +template +struct BitsTraits::value && std::is_unsigned::value)>::type> { + typedef T UnderlyingType; + static T load(const T& x) { return x; } + static void store(T& x, T v) { x = v; } +}; +} // namespace detail +/** + * Wrapper class with static methods for various bit-level operations, + * treating an array of T as an array of bits (in little-endian order). + * (T is either an unsigned integral type or Unaligned, where X is + * an unsigned integral type) + */ +template > +struct Bits { + typedef typename Traits::UnderlyingType UnderlyingType; typedef T type; - static constexpr size_t bitsPerBlock = std::numeric_limits::digits; + static_assert(sizeof(T) == sizeof(UnderlyingType), "Size mismatch"); + + /** + * Number of bits in a block. + */ + static constexpr size_t bitsPerBlock = + std::numeric_limits::digits; /** * Byte index of the given bit. @@ -101,13 +143,13 @@ struct Bits { * (value & 1 becomes the bit at bitStart, etc) * Precondition: count <= sizeof(T) * 8 */ - static void set(T* p, size_t bitStart, size_t count, T value); + static void set(T* p, size_t bitStart, size_t count, UnderlyingType value); /** * Get count contiguous bits starting at bitStart. * Precondition: count <= sizeof(T) * 8 */ - static T get(const T* p, size_t bitStart, size_t count); + static UnderlyingType get(const T* p, size_t bitStart, size_t count); /** * Count the number of bits set in a range of blocks. @@ -117,34 +159,38 @@ struct Bits { private: // Same as set, assumes all bits are in the same block. // (bitStart < sizeof(T) * 8, bitStart + count <= sizeof(T) * 8) - static void innerSet(T* p, size_t bitStart, size_t count, T value); + static void innerSet(T* p, size_t bitStart, size_t count, + UnderlyingType value); // Same as get, assumes all bits are in the same block. // (bitStart < sizeof(T) * 8, bitStart + count <= sizeof(T) * 8) - static T innerGet(const T* p, size_t bitStart, size_t count); + static UnderlyingType innerGet(const T* p, size_t bitStart, size_t count); - static constexpr T one = T(1); + static constexpr UnderlyingType one = UnderlyingType(1); }; -template -inline void Bits::set(T* p, size_t bit) { - p[blockIndex(bit)] |= (one << bitOffset(bit)); +template +inline void Bits::set(T* p, size_t bit) { + T& block = p[blockIndex(bit)]; + Traits::store(block, Traits::load(block) | (one << bitOffset(bit))); } -template -inline void Bits::clear(T* p, size_t bit) { - p[blockIndex(bit)] &= ~(one << bitOffset(bit)); +template +inline void Bits::clear(T* p, size_t bit) { + T& block = p[blockIndex(bit)]; + Traits::store(block, Traits::load(block) & ~(one << bitOffset(bit))); } -template -inline bool Bits::test(const T* p, size_t bit) { - return p[blockIndex(bit)] & (one << bitOffset(bit)); +template +inline bool Bits::test(const T* p, size_t bit) { + return Traits::load(p[blockIndex(bit)]) & (one << bitOffset(bit)); } -template -inline void Bits::set(T* p, size_t bitStart, size_t count, T value) { - assert(count <= sizeof(T) * 8); - assert(count == sizeof(T) || +template +inline void Bits::set(T* p, size_t bitStart, size_t count, + UnderlyingType value) { + assert(count <= sizeof(UnderlyingType) * 8); + assert(count == sizeof(UnderlyingType) || (value & ~((one << count) - 1)) == 0); size_t idx = blockIndex(bitStart); size_t offset = bitOffset(bitStart); @@ -159,9 +205,10 @@ inline void Bits::set(T* p, size_t bitStart, size_t count, T value) { } } -template -inline T Bits::get(const T* p, size_t bitStart, size_t count) { - assert(count <= sizeof(T) * 8); +template +inline auto Bits::get(const T* p, size_t bitStart, size_t count) + -> UnderlyingType { + assert(count <= sizeof(UnderlyingType) * 8); size_t idx = blockIndex(bitStart); size_t offset = bitOffset(bitStart); if (offset + count <= bitsPerBlock) { @@ -169,28 +216,33 @@ inline T Bits::get(const T* p, size_t bitStart, size_t count) { } else { size_t countInThisBlock = bitsPerBlock - offset; size_t countInNextBlock = count - countInThisBlock; - T thisBlockValue = innerGet(p + idx, offset, countInThisBlock); - T nextBlockValue = innerGet(p + idx + 1, 0, countInNextBlock); + UnderlyingType thisBlockValue = innerGet(p + idx, offset, countInThisBlock); + UnderlyingType nextBlockValue = innerGet(p + idx + 1, 0, countInNextBlock); return (nextBlockValue << countInThisBlock) | thisBlockValue; } } -template -inline void Bits::innerSet(T* p, size_t offset, size_t count, T value) { +template +inline void Bits::innerSet(T* p, size_t offset, size_t count, + UnderlyingType value) { // Mask out bits and set new value - *p = (*p & ~(((one << count) - 1) << offset)) | (value << offset); + UnderlyingType v = Traits::load(*p); + v &= ~(((one << count) - 1) << offset); + v |= (value << offset); + Traits::store(*p, v); } -template -inline T Bits::innerGet(const T* p, size_t offset, size_t count) { - return (*p >> offset) & ((one << count) - 1); +template +inline auto Bits::innerGet(const T* p, size_t offset, size_t count) + -> UnderlyingType { + return (Traits::load(*p) >> offset) & ((one << count) - 1); } -template -inline size_t Bits::count(const T* begin, const T* end) { +template +inline size_t Bits::count(const T* begin, const T* end) { size_t n = 0; for (; begin != end; ++begin) { - n += popcount(*begin); + n += popcount(Traits::load(*begin)); } return n; } diff --git a/folly/experimental/test/BitsTest.cpp b/folly/experimental/test/BitsTest.cpp index 09eb4ae6..83dc8d06 100644 --- a/folly/experimental/test/BitsTest.cpp +++ b/folly/experimental/test/BitsTest.cpp @@ -21,60 +21,149 @@ using namespace folly; -TEST(Bits, Simple) { - EXPECT_EQ(0, Bits::blockCount(0)); - EXPECT_EQ(1, Bits::blockCount(1)); - EXPECT_EQ(1, Bits::blockCount(8)); - EXPECT_EQ(2, Bits::blockCount(9)); - EXPECT_EQ(256, Bits::blockCount(2048)); - EXPECT_EQ(257, Bits::blockCount(2049)); - - EXPECT_EQ(4, Bits::blockIndex(39)); - EXPECT_EQ(7, Bits::bitOffset(39)); - EXPECT_EQ(5, Bits::blockIndex(40)); - EXPECT_EQ(0, Bits::bitOffset(40)); - - uint8_t buf[256]; - memset(buf, 0, 256); - - Bits::set(buf, 36); - Bits::set(buf, 39); - EXPECT_EQ((1 << 7) | (1 << 4), buf[4]); - EXPECT_EQ(0, buf[5]); - Bits::clear(buf, 39); - EXPECT_EQ(1 << 4, buf[4]); - EXPECT_EQ(0, buf[5]); - Bits::set(buf, 40); - EXPECT_EQ(1 << 4, buf[4]); - EXPECT_EQ(1, buf[5]); - - EXPECT_EQ(2, Bits::count(buf, buf + 256)); +template +void runSimpleTest8() { + auto load = detail::BitsTraits::load; + + EXPECT_EQ(0, Bits::blockCount(0)); + EXPECT_EQ(1, Bits::blockCount(1)); + EXPECT_EQ(1, Bits::blockCount(8)); + EXPECT_EQ(2, Bits::blockCount(9)); + EXPECT_EQ(256, Bits::blockCount(2048)); + EXPECT_EQ(257, Bits::blockCount(2049)); + + EXPECT_EQ(4, Bits::blockIndex(39)); + EXPECT_EQ(7, Bits::bitOffset(39)); + EXPECT_EQ(5, Bits::blockIndex(40)); + EXPECT_EQ(0, Bits::bitOffset(40)); + + T buf[256]; + std::fill(buf, buf + 256, T(0)); + + Bits::set(buf, 36); + Bits::set(buf, 39); + EXPECT_EQ((1 << 7) | (1 << 4), load(buf[4])); + EXPECT_EQ(0, load(buf[5])); + Bits::clear(buf, 39); + EXPECT_EQ(1 << 4, load(buf[4])); + EXPECT_EQ(0, load(buf[5])); + Bits::set(buf, 40); + EXPECT_EQ(1 << 4, load(buf[4])); + EXPECT_EQ(1, load(buf[5])); + + EXPECT_EQ(2, Bits::count(buf, buf + 256)); +} + +TEST(Bits, Simple8) { + runSimpleTest8(); +} + +TEST(Bits, SimpleUnaligned8) { + runSimpleTest8>(); +} + +template +void runSimpleTest64() { + auto load = detail::BitsTraits::load; + + EXPECT_EQ(0, Bits::blockCount(0)); + EXPECT_EQ(1, Bits::blockCount(1)); + EXPECT_EQ(1, Bits::blockCount(8)); + EXPECT_EQ(1, Bits::blockCount(9)); + EXPECT_EQ(1, Bits::blockCount(64)); + EXPECT_EQ(2, Bits::blockCount(65)); + EXPECT_EQ(32, Bits::blockCount(2048)); + EXPECT_EQ(33, Bits::blockCount(2049)); + + EXPECT_EQ(0, Bits::blockIndex(39)); + EXPECT_EQ(39, Bits::bitOffset(39)); + EXPECT_EQ(4, Bits::blockIndex(319)); + EXPECT_EQ(63, Bits::bitOffset(319)); + EXPECT_EQ(5, Bits::blockIndex(320)); + EXPECT_EQ(0, Bits::bitOffset(320)); + + T buf[256]; + std::fill(buf, buf + 256, T(0)); + + Bits::set(buf, 300); + Bits::set(buf, 319); + EXPECT_EQ((uint64_t(1) << 44) | (uint64_t(1) << 63), load(buf[4])); + EXPECT_EQ(0, load(buf[5])); + Bits::clear(buf, 319); + EXPECT_EQ(uint64_t(1) << 44, load(buf[4])); + EXPECT_EQ(0, load(buf[5])); + Bits::set(buf, 320); + EXPECT_EQ(uint64_t(1) << 44, load(buf[4])); + EXPECT_EQ(1, load(buf[5])); + + EXPECT_EQ(2, Bits::count(buf, buf + 256)); +} + +TEST(Bits, Simple64) { + runSimpleTest64(); +} + +TEST(Bits, SimpleUnaligned64) { + runSimpleTest64>(); +} + +template +void runMultiBitTest8() { + auto load = detail::BitsTraits::load; + T buf[] = {0x12, 0x34, 0x56, 0x78}; + + EXPECT_EQ(0x02, load(Bits::get(buf, 0, 4))); + EXPECT_EQ(0x1a, load(Bits::get(buf, 9, 5))); + EXPECT_EQ(0xb1, load(Bits::get(buf, 13, 8))); + + Bits::set(buf, 0, 4, 0x0b); + EXPECT_EQ(0x1b, load(buf[0])); + EXPECT_EQ(0x34, load(buf[1])); + EXPECT_EQ(0x56, load(buf[2])); + EXPECT_EQ(0x78, load(buf[3])); + + Bits::set(buf, 9, 5, 0x0e); + EXPECT_EQ(0x1b, load(buf[0])); + EXPECT_EQ(0x1c, load(buf[1])); + EXPECT_EQ(0x56, load(buf[2])); + EXPECT_EQ(0x78, load(buf[3])); + + Bits::set(buf, 13, 8, 0xaa); + EXPECT_EQ(0x1b, load(buf[0])); + EXPECT_EQ(0x5c, load(buf[1])); + EXPECT_EQ(0x55, load(buf[2])); + EXPECT_EQ(0x78, load(buf[3])); +} + +TEST(Bits, MultiBit8) { + runMultiBitTest8(); +} + +TEST(Bits, MultiBitUnaligned8) { + runMultiBitTest8>(); +} + +template +void runMultiBitTest64() { + auto load = detail::BitsTraits::load; + T buf[] = {0x123456789abcdef0, 0x13579bdf2468ace0}; + + EXPECT_EQ(0xf0, load(Bits::get(buf, 0, 8))); + EXPECT_EQ(0x89abcdef, load(Bits::get(buf, 4, 32))); + EXPECT_EQ(0x189abcdef, load(Bits::get(buf, 4, 33))); + + Bits::set(buf, 4, 31, 0x55555555); + EXPECT_EQ(0xd5555555, load(Bits::get(buf, 4, 32))); + EXPECT_EQ(0x1d5555555, load(Bits::get(buf, 4, 33))); + EXPECT_EQ(0xd55555550, load(Bits::get(buf, 0, 36))); +} + +TEST(Bits, MultiBit64) { + runMultiBitTest64(); } -TEST(Bits, MultiBit) { - uint8_t buf[] = {0x12, 0x34, 0x56, 0x78}; - - EXPECT_EQ(0x02, Bits::get(buf, 0, 4)); - EXPECT_EQ(0x1a, Bits::get(buf, 9, 5)); - EXPECT_EQ(0xb1, Bits::get(buf, 13, 8)); - - Bits::set(buf, 0, 4, 0x0b); - EXPECT_EQ(0x1b, buf[0]); - EXPECT_EQ(0x34, buf[1]); - EXPECT_EQ(0x56, buf[2]); - EXPECT_EQ(0x78, buf[3]); - - Bits::set(buf, 9, 5, 0x0e); - EXPECT_EQ(0x1b, buf[0]); - EXPECT_EQ(0x1c, buf[1]); - EXPECT_EQ(0x56, buf[2]); - EXPECT_EQ(0x78, buf[3]); - - Bits::set(buf, 13, 8, 0xaa); - EXPECT_EQ(0x1b, buf[0]); - EXPECT_EQ(0x5c, buf[1]); - EXPECT_EQ(0x55, buf[2]); - EXPECT_EQ(0x78, buf[3]); +TEST(Bits, MultiBitUnaligned64) { + runMultiBitTest64>(); } int main(int argc, char *argv[]) {