先頭から続く0の数 Count Leading Zeros
#ビット演算 で、値の先頭から続く0のビットの数を数えたい。 組み込み関数を使う
code:c++
__builtin_clzll(x)
__builtin_clz(x)
_lzcnt_u64(x)
_lzcnt_u32(x)
ビット演算で書く
code:c++
int clz(int64_t x){
x = x | (x >> 1);
x = x | (x >> 2);
x = x | (x >> 4);
x = x | (x >> 8);
x = x | (x >> 16);
x = x | (x >> 32);
return pop_count_ull(~x);
}
↑1のビットを右に伝播させて、0のビットの数を数える(反転させて1のビットを数える)
CUDA C++で書く
code:cuda c++
__clzll(x)