parlaylib icon indicating copy to clipboard operation
parlaylib copied to clipboard

log2_up

Open WhateverLiu opened this issue 11 months ago • 2 comments

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 :)

WhateverLiu avatar Jan 03 '25 18:01 WhateverLiu