proxygen
folly::Bits< T, Traits > Struct Template Reference

#include <Bits.h>

Public Types

typedef Traits::UnderlyingType UnderlyingType
 
typedef T type
 

Static Public Member Functions

static constexpr size_t blockIndex (size_t bit)
 
static constexpr size_t bitOffset (size_t bit)
 
static constexpr size_t blockCount (size_t nbits)
 
static void set (T *p, size_t bit)
 
static void clear (T *p, size_t bit)
 
static bool test (const T *p, size_t bit)
 
static void set (T *p, size_t bitStart, size_t count, UnderlyingType value)
 
static UnderlyingType get (const T *p, size_t bitStart, size_t count)
 
static size_t count (const T *begin, const T *end)
 

Static Public Attributes

static constexpr size_t bitsPerBlock
 

Private Types

using UnsignedType = typename std::make_unsigned< UnderlyingType >::type
 

Static Private Member Functions

static void innerSet (T *p, size_t bitStart, size_t count, UnderlyingType value)
 
static UnderlyingType innerGet (const T *p, size_t bitStart, size_t count)
 
static constexpr UnderlyingType ones (size_t count)
 

Static Private Attributes

static constexpr UnderlyingType zero = UnderlyingType(0)
 
static constexpr UnderlyingType one = UnderlyingType(1)
 

Detailed Description

template<class T, class Traits = detail::BitsTraits<T>>
struct folly::Bits< T, Traits >

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<X>, where X is an unsigned integral type)

Definition at line 125 of file Bits.h.

Member Typedef Documentation

template<class T, class Traits = detail::BitsTraits<T>>
typedef T folly::Bits< T, Traits >::type

Definition at line 127 of file Bits.h.

template<class T, class Traits = detail::BitsTraits<T>>
typedef Traits::UnderlyingType folly::Bits< T, Traits >::UnderlyingType

Definition at line 126 of file Bits.h.

template<class T, class Traits = detail::BitsTraits<T>>
using folly::Bits< T, Traits >::UnsignedType = typename std::make_unsigned<UnderlyingType>::type
private

Definition at line 205 of file Bits.h.

Member Function Documentation

template<class T, class Traits = detail::BitsTraits<T>>
static constexpr size_t folly::Bits< T, Traits >::bitOffset ( size_t  bit)
inlinestatic

Offset in block of the given bit.

Definition at line 146 of file Bits.h.

146  {
147  return bit % bitsPerBlock;
148  }
static constexpr size_t bitsPerBlock
Definition: Bits.h:133
template<class T, class Traits = detail::BitsTraits<T>>
static constexpr size_t folly::Bits< T, Traits >::blockCount ( size_t  nbits)
inlinestatic

Number of blocks used by the given number of bits.

Definition at line 153 of file Bits.h.

References folly::test::begin(), count, folly::test::end(), folly::T, test(), and folly::value().

153  {
154  return nbits / bitsPerBlock + (nbits % bitsPerBlock != 0);
155  }
static constexpr size_t bitsPerBlock
Definition: Bits.h:133
template<class T, class Traits = detail::BitsTraits<T>>
static constexpr size_t folly::Bits< T, Traits >::blockIndex ( size_t  bit)
inlinestatic

Byte index of the given bit.

Definition at line 139 of file Bits.h.

139  {
140  return bit / bitsPerBlock;
141  }
static constexpr size_t bitsPerBlock
Definition: Bits.h:133
template<class T , class Traits >
void folly::Bits< T, Traits >::clear ( T p,
size_t  bit 
)
inlinestatic

Clear the given bit.

Definition at line 226 of file Bits.h.

References folly::T.

Referenced by runSimpleTest64(), and runSimpleTest8().

226  {
227  T& block = p[blockIndex(bit)];
228  Traits::store(block, Traits::loadRMW(block) & ~(one << bitOffset(bit)));
229 }
folly::std T
static constexpr UnderlyingType one
Definition: Bits.h:203
static constexpr size_t blockIndex(size_t bit)
Definition: Bits.h:139
static constexpr size_t bitOffset(size_t bit)
Definition: Bits.h:146
template<class T , class Traits >
size_t folly::Bits< T, Traits >::count ( const T begin,
const T end 
)
inlinestatic

Count the number of bits set in a range of blocks.

Definition at line 319 of file Bits.h.

References folly::test::begin(), folly::test::end(), deadlock::load(), and folly::popcount().

319  {
320  size_t n = 0;
321  for (; begin != end; ++begin) {
322  n += popcount(Traits::load(*begin));
323  }
324  return n;
325 }
constexpr unsigned int popcount(T const v)
Definition: Bits.h:130
auto begin(TestAdlIterable &instance)
Definition: ForeachTest.cpp:56
def load()
Definition: deadlock.py:441
auto end(TestAdlIterable &instance)
Definition: ForeachTest.cpp:62
template<class T , class Traits >
auto folly::Bits< T, Traits >::get ( const T p,
size_t  bitStart,
size_t  count 
)
inlinestatic

Get count contiguous bits starting at bitStart. Precondition: count <= sizeof(T) * 8

Definition at line 285 of file Bits.h.

References count, and value.

Referenced by benchmarkGet(), and TEST().

286  {
287  if (count == 0) {
288  return UnderlyingType{};
289  }
290 
291  DCHECK_LE(count, sizeof(UnderlyingType) * 8);
292  size_t idx = blockIndex(bitStart);
293  size_t offset = bitOffset(bitStart);
294  UnderlyingType ret;
295  if (offset + count <= bitsPerBlock) {
296  ret = innerGet(p + idx, offset, count);
297  } else {
298  size_t countInThisBlock = bitsPerBlock - offset;
299  size_t countInNextBlock = count - countInThisBlock;
300  UnderlyingType thisBlockValue = innerGet(p + idx, offset, countInThisBlock);
301  UnderlyingType nextBlockValue = innerGet(p + idx + 1, 0, countInNextBlock);
302  ret = (nextBlockValue << countInThisBlock) | thisBlockValue;
303  }
305  size_t emptyBits = bitsPerBlock - count;
306  ret <<= emptyBits;
307  ret >>= emptyBits;
308  }
309  return ret;
310 }
static UnderlyingType innerGet(const T *p, size_t bitStart, size_t count)
Definition: Bits.h:313
static constexpr size_t bitsPerBlock
Definition: Bits.h:133
Traits::UnderlyingType UnderlyingType
Definition: Bits.h:126
static size_t count(const T *begin, const T *end)
Definition: Bits.h:319
static const char *const value
Definition: Conv.cpp:50
static constexpr size_t blockIndex(size_t bit)
Definition: Bits.h:139
static constexpr size_t bitOffset(size_t bit)
Definition: Bits.h:146
template<class T , class Traits >
auto folly::Bits< T, Traits >::innerGet ( const T p,
size_t  bitStart,
size_t  count 
)
inlinestaticprivate

Definition at line 313 of file Bits.h.

References count, and deadlock::load().

314  {
315  return (Traits::load(*p) >> offset) & ones(count);
316 }
static constexpr UnderlyingType ones(size_t count)
Definition: Bits.h:206
def load()
Definition: deadlock.py:441
static size_t count(const T *begin, const T *end)
Definition: Bits.h:319
template<class T , class Traits >
void folly::Bits< T, Traits >::innerSet ( T p,
size_t  bitStart,
size_t  count,
UnderlyingType  value 
)
inlinestaticprivate

Definition at line 265 of file Bits.h.

References FOLLY_POP_WARNING.

269  {
270  // Mask out bits and set new value
271  UnderlyingType v = Traits::loadRMW(*p);
272  v &= ~(ones(count) << offset);
273  v |= (value << offset);
274  Traits::store(*p, v);
275 }
static constexpr UnderlyingType ones(size_t count)
Definition: Bits.h:206
Traits::UnderlyingType UnderlyingType
Definition: Bits.h:126
static size_t count(const T *begin, const T *end)
Definition: Bits.h:319
uint64_t value(const typename LockFreeRingBuffer< T, Atom >::Cursor &rbcursor)
template<class T, class Traits = detail::BitsTraits<T>>
static constexpr UnderlyingType folly::Bits< T, Traits >::ones ( size_t  count)
inlinestaticprivate

Definition at line 206 of file Bits.h.

References count, FOLLY_GCC_DISABLE_WARNING, FOLLY_GNU_DISABLE_WARNING, and FOLLY_PUSH_WARNING.

206  {
207  return (count < bitsPerBlock)
208  ? static_cast<UnderlyingType>((UnsignedType{1} << count) - 1)
209  : ~zero;
210  }
static constexpr UnderlyingType zero
Definition: Bits.h:202
static constexpr size_t bitsPerBlock
Definition: Bits.h:133
typename std::make_unsigned< UnderlyingType >::type UnsignedType
Definition: Bits.h:205
Traits::UnderlyingType UnderlyingType
Definition: Bits.h:126
static size_t count(const T *begin, const T *end)
Definition: Bits.h:319
template<class T , class Traits >
FOLLY_PUSH_WARNING void folly::Bits< T, Traits >::set ( T p,
size_t  bit 
)
inlinestatic

Set the given bit.

Definition at line 220 of file Bits.h.

References folly::T.

Referenced by benchmarkSet(), runMultiBitTest64(), runMultiBitTest8(), runSignedMultiBitTest8(), runSimpleTest64(), runSimpleTest8(), and TEST().

220  {
221  T& block = p[blockIndex(bit)];
222  Traits::store(block, Traits::loadRMW(block) | (one << bitOffset(bit)));
223 }
folly::std T
static constexpr UnderlyingType one
Definition: Bits.h:203
static constexpr size_t blockIndex(size_t bit)
Definition: Bits.h:139
static constexpr size_t bitOffset(size_t bit)
Definition: Bits.h:146
template<class T , class Traits >
void folly::Bits< T, Traits >::set ( T p,
size_t  bitStart,
size_t  count,
UnderlyingType  value 
)
inlinestatic

Set count contiguous bits starting at bitStart to the values from the least significant count bits of value; little endian. (value & 1 becomes the bit at bitStart, etc) Precondition: count <= sizeof(T) * 8 Precondition: value can fit in 'count' bits

Definition at line 232 of file Bits.h.

References count, type, and value.

236  {
237  DCHECK_LE(count, sizeof(UnderlyingType) * 8);
238  size_t cut = bitsPerBlock - count;
239  if (cut != 8 * sizeof(UnderlyingType)) {
240  using U = typename std::make_unsigned<UnderlyingType>::type;
241  DCHECK_EQ(value, UnderlyingType(U(value) << cut) >> cut);
242  }
243  size_t idx = blockIndex(bitStart);
244  size_t offset = bitOffset(bitStart);
246  value &= ones(count);
247  }
248  if (offset + count <= bitsPerBlock) {
249  innerSet(p + idx, offset, count, value);
250  } else {
251  size_t countInThisBlock = bitsPerBlock - offset;
252  size_t countInNextBlock = count - countInThisBlock;
253 
254  UnderlyingType thisBlock = UnderlyingType(value & ones(countInThisBlock));
255  UnderlyingType nextBlock = UnderlyingType(value >> countInThisBlock);
257  nextBlock &= ones(countInNextBlock);
258  }
259  innerSet(p + idx, offset, countInThisBlock, thisBlock);
260  innerSet(p + idx + 1, 0, countInNextBlock, nextBlock);
261  }
262 }
PskType type
static constexpr UnderlyingType ones(size_t count)
Definition: Bits.h:206
static constexpr size_t bitsPerBlock
Definition: Bits.h:133
static void innerSet(T *p, size_t bitStart, size_t count, UnderlyingType value)
Definition: Bits.h:265
Traits::UnderlyingType UnderlyingType
Definition: Bits.h:126
static size_t count(const T *begin, const T *end)
Definition: Bits.h:319
static const char *const value
Definition: Conv.cpp:50
static constexpr size_t blockIndex(size_t bit)
Definition: Bits.h:139
uint64_t value(const typename LockFreeRingBuffer< T, Atom >::Cursor &rbcursor)
static constexpr size_t bitOffset(size_t bit)
Definition: Bits.h:146
template<class T , class Traits >
FOLLY_POP_WARNING bool folly::Bits< T, Traits >::test ( const T p,
size_t  bit 
)
inlinestatic

Test the given bit.

Definition at line 280 of file Bits.h.

References deadlock::load().

280  {
281  return Traits::load(p[blockIndex(bit)]) & (one << bitOffset(bit));
282 }
def load()
Definition: deadlock.py:441
static constexpr UnderlyingType one
Definition: Bits.h:203
static constexpr size_t blockIndex(size_t bit)
Definition: Bits.h:139
static constexpr size_t bitOffset(size_t bit)
Definition: Bits.h:146

Member Data Documentation

template<class T, class Traits = detail::BitsTraits<T>>
constexpr size_t folly::Bits< T, Traits >::bitsPerBlock
static
Initial value:
= std::numeric_limits<

Number of bits in a block.

Definition at line 133 of file Bits.h.

template<class T, class Traits = detail::BitsTraits<T>>
constexpr UnderlyingType folly::Bits< T, Traits >::one = UnderlyingType(1)
staticprivate

Definition at line 203 of file Bits.h.

template<class T, class Traits = detail::BitsTraits<T>>
constexpr UnderlyingType folly::Bits< T, Traits >::zero = UnderlyingType(0)
staticprivate

Definition at line 202 of file Bits.h.


The documentation for this struct was generated from the following file: