Skip to content

log2_up #82

@WhateverLiu

Description

@WhateverLiu

This is not an issue but more of a remark.

The <bit> library in C++20 contains std::bit_width(), which by my testing is at least 1.6x faster than log2_up in utilities.h. I modified log2_up() as follows, passing all tests:

template <class T>
size_t log2_up(T i) {
  assert(i > 0);
#if defined(__cplusplus) && __cplusplus >= 202002L
#include <bit>
  return std::bit_width ( i - 1 ); 
#else
  size_t a = 0;
  T b = i - 1;
  while (b > 0) {
    b = b >> 1;
    a++;
  }
  return a;
#endif
}

Since log2_up() is key to the pool allocator, it shouldn't hurt to squeeze every bit of speed out of it :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions