- #ifdef TB_COMPILER_IS_GCC
- #define tb_bits_cl0_u32_be(x) ((x)? (tb_size_t)__builtin_clz((tb_uint32_t)(x)) : 32)
- #else
- static __tb_inline__ tb_size_t tb_bits_cl0_u32_be_inline(tb_uint32_t x)
- {
- tb_check_return_val(x, 32);
- tb_size_t n = 31;
- if (x & 0xffff0000) { n -= 16; x >>= 16; }
- if (x & 0xff00) { n -= 8; x >>= 8; }
- if (x & 0xf0) { n -= 4; x >>= 4; }
- if (x & 0xc) { n -= 2; x >>= 2; }
- if (x & 0x2) { n--; }
- return n;
- }
- #endif
- tb_uint32_t tb_ilog2i(tb_uint32_t x)
- {
- return (31 - tb_bits_cl0_u32_be(x | 1));
- }
- //该片段来自于http://www.codesnippet.cn/detail/1912201411349.html
来源: http://www.codesnippet.cn/detail/1912201411349.html