proxygen
folly::compression::detail::UpperBitsReader< Encoder, Instructions, SizeType > Class Template Reference

#include <EliasFanoCoding.h>

Inheritance diagram for folly::compression::detail::UpperBitsReader< Encoder, Instructions, SizeType >:
folly::compression::detail::ForwardPointers< Encoder::forwardQuantum > folly::compression::detail::SkipPointers< Encoder::skipQuantum >

Public Types

typedef Encoder::ValueType ValueType
 

Public Member Functions

 UpperBitsReader (const typename Encoder::CompressedList &list)
 
void reset ()
 
SizeType position () const
 
ValueType value () const
 
ValueType previous ()
 
ValueType next ()
 
ValueType skip (SizeType n)
 
ValueType skipToNext (ValueType v)
 
SizeType prepareSkipTo (ValueType v) const
 
ValueType jump (size_t n)
 
ValueType jumpToNext (ValueType v)
 
ValueType previousValue () const
 
void setDone (SizeType endPos)
 

Private Types

typedef Encoder::SkipValueType SkipValueType
 
using block_t = uint64_t
 
using OuterType = typename std::common_type< ValueType, SizeType >::type
 

Private Member Functions

ValueType setValue (size_t inner)
 
void reposition (SizeType dest)
 
void getPreviousInfo (block_t &block, size_t &inner, OuterType &outer) const
 
- Private Member Functions inherited from folly::compression::detail::ForwardPointers< Encoder::forwardQuantum >
 ForwardPointers (const unsigned char *ptr)
 
- Private Member Functions inherited from folly::compression::detail::SkipPointers< Encoder::skipQuantum >
 SkipPointers (const unsigned char *ptr)
 

Private Attributes

const unsigned char *const start_
 
block_t block_
 
SizeType position_
 
OuterType outer_
 
ValueType value_
 
- Private Attributes inherited from folly::compression::detail::ForwardPointers< Encoder::forwardQuantum >
const unsigned char *const forwardPointers_
 
- Private Attributes inherited from folly::compression::detail::SkipPointers< Encoder::skipQuantum >
const unsigned char *const skipPointers_
 

Detailed Description

template<class Encoder, class Instructions, class SizeType>
class folly::compression::detail::UpperBitsReader< Encoder, Instructions, SizeType >

Definition at line 337 of file EliasFanoCoding.h.

Member Typedef Documentation

template<class Encoder, class Instructions, class SizeType>
using folly::compression::detail::UpperBitsReader< Encoder, Instructions, SizeType >::block_t = uint64_t
private

Definition at line 538 of file EliasFanoCoding.h.

template<class Encoder, class Instructions, class SizeType>
using folly::compression::detail::UpperBitsReader< Encoder, Instructions, SizeType >::OuterType = typename std::common_type<ValueType, SizeType>::type
private

Definition at line 541 of file EliasFanoCoding.h.

template<class Encoder, class Instructions, class SizeType>
typedef Encoder::SkipValueType folly::compression::detail::UpperBitsReader< Encoder, Instructions, SizeType >::SkipValueType
private

Definition at line 339 of file EliasFanoCoding.h.

template<class Encoder, class Instructions, class SizeType>
typedef Encoder::ValueType folly::compression::detail::UpperBitsReader< Encoder, Instructions, SizeType >::ValueType

Definition at line 342 of file EliasFanoCoding.h.

Constructor & Destructor Documentation

template<class Encoder, class Instructions, class SizeType>
folly::compression::detail::UpperBitsReader< Encoder, Instructions, SizeType >::UpperBitsReader ( const typename Encoder::CompressedList &  list)
inlineexplicit

Definition at line 344 of file EliasFanoCoding.h.

345  : ForwardPointers<Encoder::forwardQuantum>(list.forwardPointers),
346  SkipPointers<Encoder::skipQuantum>(list.skipPointers),
347  start_(list.upper) {
348  reset();
349  }
Encoder::MutableCompressedList list

Member Function Documentation

template<class Encoder, class Instructions, class SizeType>
void folly::compression::detail::UpperBitsReader< Encoder, Instructions, SizeType >::getPreviousInfo ( block_t block,
size_t &  inner,
OuterType outer 
) const
inlineprivate

Definition at line 543 of file EliasFanoCoding.h.

References max, start_, and UNLIKELY.

543  {
545  DCHECK_GT(position(), 0);
546 
547  outer = outer_;
548  block = folly::loadUnaligned<block_t>(start_ + outer);
549  inner = size_t(value_) - 8 * outer_ + position_;
550  block &= (block_t(1) << inner) - 1;
551  while (UNLIKELY(block == 0)) {
552  DCHECK_GT(outer, 0);
553  outer -= std::min<OuterType>(sizeof(block_t), outer);
554  block = folly::loadUnaligned<block_t>(start_ + outer);
555  }
556  inner = 8 * sizeof(block_t) - 1 - Instructions::clz(block);
557  }
LogLevel max
Definition: LogLevel.cpp:31
#define UNLIKELY(x)
Definition: Likely.h:48
template<class Encoder, class Instructions, class SizeType>
ValueType folly::compression::detail::UpperBitsReader< Encoder, Instructions, SizeType >::jump ( size_t  n)
inline

Definition at line 495 of file EliasFanoCoding.h.

References max, and folly::gen::skip().

495  {
496  if (Encoder::forwardQuantum == 0 || n <= Encoder::forwardQuantum) {
497  reset();
498  } else {
499  // Avoid reading the head, skip() will reposition.
501  }
502  return skip(n);
503  }
LogLevel max
Definition: LogLevel.cpp:31
template<class Encoder, class Instructions, class SizeType>
ValueType folly::compression::detail::UpperBitsReader< Encoder, Instructions, SizeType >::jumpToNext ( ValueType  v)
inline

Definition at line 505 of file EliasFanoCoding.h.

505  {
506  if (Encoder::skipQuantum == 0 || v < Encoder::skipQuantum) {
507  reset();
508  } else {
509  value_ = 0; // Avoid reading the head, skipToNext() will reposition.
510  }
511  return skipToNext(v);
512  }
template<class Encoder, class Instructions, class SizeType>
ValueType folly::compression::detail::UpperBitsReader< Encoder, Instructions, SizeType >::next ( )
inline

Definition at line 375 of file EliasFanoCoding.h.

References start_.

375  {
376  // Skip to the first non-zero block.
377  while (block_ == 0) {
378  outer_ += sizeof(block_t);
379  block_ = folly::loadUnaligned<block_t>(start_ + outer_);
380  }
381 
382  ++position_;
383  size_t inner = Instructions::ctz(block_);
384  block_ = Instructions::blsr(block_);
385 
386  return setValue(inner);
387  }
template<class Encoder, class Instructions, class SizeType>
SizeType folly::compression::detail::UpperBitsReader< Encoder, Instructions, SizeType >::position ( ) const
inline

Definition at line 358 of file EliasFanoCoding.h.

358  {
359  return position_;
360  }
template<class Encoder, class Instructions, class SizeType>
SizeType folly::compression::detail::UpperBitsReader< Encoder, Instructions, SizeType >::prepareSkipTo ( ValueType  v) const
inline

Prepare to skip to value. This is a constant-time operation that will prefetch memory required for a skipTo(value) call.

Returns
position of reader

Definition at line 469 of file EliasFanoCoding.h.

References addr, upload::dest, start_, and shell_builder::steps.

469  {
470  auto position = position_;
471 
472  if (Encoder::skipQuantum > 0 && v >= value_ + Encoder::skipQuantum) {
473  auto outer = outer_;
474  const size_t steps = v / Encoder::skipQuantum;
475  const size_t dest = folly::loadUnaligned<SkipValueType>(
476  this->skipPointers_ + (steps - 1) * sizeof(SkipValueType));
477 
478  position = dest - 1;
479  outer = (dest + Encoder::skipQuantum * steps) / 8;
480 
481  // Prefetch up to the beginning of where we linear search. After that,
482  // hardware prefetching will outperform our own. In addition, this
483  // simplifies calculating what to prefetch as we don't have to calculate
484  // the entire destination address. Two cache lines are prefetched because
485  // this results in fewer cycles used (based on practical results) than
486  // one. However, three cache lines does not have any additional effect.
487  const auto addr = start_ + outer;
488  __builtin_prefetch(addr);
489  __builtin_prefetch(addr + kCacheLineSize);
490  }
491 
492  return position;
493  }
dest
Definition: upload.py:394
constexpr size_t kCacheLineSize
ThreadPoolListHook * addr
template<class Encoder, class Instructions, class SizeType>
ValueType folly::compression::detail::UpperBitsReader< Encoder, Instructions, SizeType >::previous ( )
inline

Definition at line 365 of file EliasFanoCoding.h.

References start_.

365  {
366  size_t inner;
367  block_t block;
368  getPreviousInfo(block, inner, outer_);
369  block_ = folly::loadUnaligned<block_t>(start_ + outer_);
370  block_ ^= block;
371  --position_;
372  return setValue(inner);
373  }
void getPreviousInfo(block_t &block, size_t &inner, OuterType &outer) const
template<class Encoder, class Instructions, class SizeType>
ValueType folly::compression::detail::UpperBitsReader< Encoder, Instructions, SizeType >::previousValue ( ) const
inline

Definition at line 514 of file EliasFanoCoding.h.

514  {
515  block_t block;
516  size_t inner;
517  OuterType outer;
518  getPreviousInfo(block, inner, outer);
519  return static_cast<ValueType>(8 * outer + inner - (position_ - 1));
520  }
void getPreviousInfo(block_t &block, size_t &inner, OuterType &outer) const
typename std::common_type< ValueType, SizeType >::type OuterType
template<class Encoder, class Instructions, class SizeType>
void folly::compression::detail::UpperBitsReader< Encoder, Instructions, SizeType >::reposition ( SizeType  dest)
inlineprivate

Definition at line 532 of file EliasFanoCoding.h.

References start_.

532  {
533  outer_ = dest / 8;
534  block_ = folly::loadUnaligned<block_t>(start_ + outer_);
535  block_ &= ~((block_t(1) << (dest % 8)) - 1);
536  }
dest
Definition: upload.py:394
template<class Encoder, class Instructions, class SizeType>
void folly::compression::detail::UpperBitsReader< Encoder, Instructions, SizeType >::reset ( )
inline

Definition at line 351 of file EliasFanoCoding.h.

References max, and start_.

template<class Encoder, class Instructions, class SizeType>
void folly::compression::detail::UpperBitsReader< Encoder, Instructions, SizeType >::setDone ( SizeType  endPos)
inline

Definition at line 522 of file EliasFanoCoding.h.

522  {
523  position_ = endPos;
524  }
template<class Encoder, class Instructions, class SizeType>
ValueType folly::compression::detail::UpperBitsReader< Encoder, Instructions, SizeType >::setValue ( size_t  inner)
inlineprivate
template<class Encoder, class Instructions, class SizeType>
ValueType folly::compression::detail::UpperBitsReader< Encoder, Instructions, SizeType >::skip ( SizeType  n)
inline

Definition at line 389 of file EliasFanoCoding.h.

References upload::dest, folly::popcount(), start_, and shell_builder::steps.

389  {
390  DCHECK_GT(n, 0);
391 
392  position_ += n; // n 1-bits will be read.
393 
394  // Use forward pointer.
395  if (Encoder::forwardQuantum > 0 && n > Encoder::forwardQuantum) {
396  const size_t steps = position_ / Encoder::forwardQuantum;
397  const size_t dest = folly::loadUnaligned<SkipValueType>(
398  this->forwardPointers_ + (steps - 1) * sizeof(SkipValueType));
399 
400  reposition(dest + steps * Encoder::forwardQuantum);
401  n = position_ + 1 - steps * Encoder::forwardQuantum; // n is > 0.
402  }
403 
404  size_t cnt;
405  // Find necessary block.
406  while ((cnt = Instructions::popcount(block_)) < n) {
407  n -= cnt;
408  outer_ += sizeof(block_t);
409  block_ = folly::loadUnaligned<block_t>(start_ + outer_);
410  }
411 
412  // Skip to the n-th one in the block.
413  DCHECK_GT(n, 0);
414  size_t inner = select64<Instructions>(block_, n - 1);
415  block_ &= (block_t(-1) << inner) << 1;
416 
417  return setValue(inner);
418  }
constexpr unsigned int popcount(T const v)
Definition: Bits.h:130
dest
Definition: upload.py:394
template<class Encoder, class Instructions, class SizeType>
ValueType folly::compression::detail::UpperBitsReader< Encoder, Instructions, SizeType >::skipToNext ( ValueType  v)
inline

Definition at line 422 of file EliasFanoCoding.h.

References upload::dest, LIKELY, cpp.ast::next(), folly::popcount(), folly::gen::skip(), start_, and shell_builder::steps.

422  {
423  DCHECK_GE(v, value_);
424 
425  // Use skip pointer.
426  if (Encoder::skipQuantum > 0 && v >= value_ + Encoder::skipQuantum) {
427  const size_t steps = v / Encoder::skipQuantum;
428  const size_t dest = folly::loadUnaligned<SkipValueType>(
429  this->skipPointers_ + (steps - 1) * sizeof(SkipValueType));
430 
431  reposition(dest + Encoder::skipQuantum * steps);
432  position_ = dest - 1;
433 
434  // Correct value_ will be set during the next() call at the end.
435 
436  // NOTE: Corresponding block of lower bits sequence may be
437  // prefetched here (via __builtin_prefetch), but experiments
438  // didn't show any significant improvements.
439  }
440 
441  // Skip by blocks.
442  size_t cnt;
443  size_t skip = v - (8 * outer_ - position_ - 1);
444 
445  constexpr size_t kBitsPerBlock = 8 * sizeof(block_t);
446  while ((cnt = Instructions::popcount(~block_)) < skip) {
447  skip -= cnt;
448  position_ += kBitsPerBlock - cnt;
449  outer_ += sizeof(block_t);
450  block_ = folly::loadUnaligned<block_t>(start_ + outer_);
451  }
452 
453  if (LIKELY(skip)) {
454  auto inner = select64<Instructions>(~block_, skip - 1);
455  position_ += inner - skip + 1;
456  block_ &= block_t(-1) << inner;
457  }
458 
459  next();
460  return value_;
461  }
constexpr unsigned int popcount(T const v)
Definition: Bits.h:130
#define LIKELY(x)
Definition: Likely.h:47
dest
Definition: upload.py:394
template<class Encoder, class Instructions, class SizeType>
ValueType folly::compression::detail::UpperBitsReader< Encoder, Instructions, SizeType >::value ( ) const
inline

Definition at line 361 of file EliasFanoCoding.h.

361  {
362  return value_;
363  }

Member Data Documentation

template<class Encoder, class Instructions, class SizeType>
block_t folly::compression::detail::UpperBitsReader< Encoder, Instructions, SizeType >::block_
private

Definition at line 560 of file EliasFanoCoding.h.

template<class Encoder, class Instructions, class SizeType>
OuterType folly::compression::detail::UpperBitsReader< Encoder, Instructions, SizeType >::outer_
private

Definition at line 562 of file EliasFanoCoding.h.

template<class Encoder, class Instructions, class SizeType>
SizeType folly::compression::detail::UpperBitsReader< Encoder, Instructions, SizeType >::position_
private

Definition at line 561 of file EliasFanoCoding.h.

template<class Encoder, class Instructions, class SizeType>
const unsigned char* const folly::compression::detail::UpperBitsReader< Encoder, Instructions, SizeType >::start_
private

Definition at line 559 of file EliasFanoCoding.h.

template<class Encoder, class Instructions, class SizeType>
ValueType folly::compression::detail::UpperBitsReader< Encoder, Instructions, SizeType >::value_
private

Definition at line 563 of file EliasFanoCoding.h.


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