Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace find_last_bit_set with some intrinsic functions #15

Open
wjcskqygj2015 opened this issue Apr 23, 2020 · 2 comments
Open

Replace find_last_bit_set with some intrinsic functions #15

wjcskqygj2015 opened this issue Apr 23, 2020 · 2 comments

Comments

@wjcskqygj2015
Copy link

wjcskqygj2015 commented Apr 23, 2020

In G++, there exists some intrinsic function that can make the function find_last_bit_set more efficiently, i.e. __builtin_clz
If under g++/clang, maybe we can replace the function like this. Or even simply using the BSR(bit scan reverse) instructions. Besides, the VSStudio also provides _BitScanReverse, _BitScanReverse64 if needed.

  template <typename T>
  constexpr unsigned find_last_bit_set(T val) {
    // Or we can replace it with bit scan reverse with extra 1 plus.
    if constexpr (sizeof(T) == sizeof(unsigned int)) {
      return sizeof(T) * 8 - __builtin_clz(val);
    } else if constexpr (sizeof(T) == sizeof(unsigned long)){
      return sizeof(T) * 8 - __builtin_clzl(val);
    } else if constexpr (sizeof(T) == sizeof(unsigned long long)){
      return sizeof(T) * 8 - __builtin_clzll(val);
    } else {
      unsigned result = 0;
      for (; val != 0; val >>= 1)
        ++result;
      return result;
    }
  }
@mpoeter
Copy link
Owner

mpoeter commented Apr 24, 2020

Thanks for the tip. I know about the intrinsics for bitscan forward/reverse, I thought I even added a TODO to adapt the function. Unfortunately I don't think the intrinsisc are constexpr, but I will look into this.

@wjcskqygj2015
Copy link
Author

It seems it is constexpr under g++ even with O0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants