43 |
43 |
44 return pos; |
44 return pos; |
45 } |
45 } |
46 |
46 |
47 /** |
47 /** |
48 * Search the last set bit in a 32 bit variable. |
48 * Search the last set bit in a 64 bit variable. |
49 * |
49 * |
50 * This algorithm is a static implementation of a log |
50 * This algorithm is a static implementation of a log |
51 * conguence search algorithm. It checks the second half |
51 * conguence search algorithm. It checks the second half |
52 * if there is a bit set search there further. And this |
52 * if there is a bit set search there further. And this |
53 * way further. If no bit is set return 0. |
53 * way further. If no bit is set return 0. |
54 * |
54 * |
55 * @param x The value to search |
55 * @param x The value to search |
56 * @return The position of the last bit set |
56 * @return The position of the last bit set |
57 */ |
57 */ |
58 uint8 FindLastBit(uint32 x) |
58 uint8 FindLastBit(uint64 x) |
59 { |
59 { |
60 if (x == 0) return 0; |
60 if (x == 0) return 0; |
61 |
61 |
62 uint8 pos = 0; |
62 uint8 pos = 0; |
63 |
63 |
64 if ((x & 0xffff0000) != 0) { x >>= 16; pos += 16; } |
64 if ((x & 0xffffffff00000000ULL) != 0) { x >>= 32; pos += 32; } |
65 if ((x & 0x0000ff00) != 0) { x >>= 8; pos += 8; } |
65 if ((x & 0x00000000ffff0000ULL) != 0) { x >>= 16; pos += 16; } |
66 if ((x & 0x000000f0) != 0) { x >>= 4; pos += 4; } |
66 if ((x & 0x000000000000ff00ULL) != 0) { x >>= 8; pos += 8; } |
67 if ((x & 0x0000000c) != 0) { x >>= 2; pos += 2; } |
67 if ((x & 0x00000000000000f0ULL) != 0) { x >>= 4; pos += 4; } |
68 if ((x & 0x00000002) != 0) { pos += 1; } |
68 if ((x & 0x000000000000000cULL) != 0) { x >>= 2; pos += 2; } |
|
69 if ((x & 0x0000000000000002ULL) != 0) { pos += 1; } |
69 |
70 |
70 return pos; |
71 return pos; |
71 } |
72 } |