Finds the first bit set in a 32-bits numeral and returns its index, or -1 if no bit is set.
| 12 | { |
| 13 | // Finds the first bit set in a 32-bits numeral and returns its index, or -1 if no bit is set. |
| 14 | int32_t bitScanForward(uint32_t source) |
| 15 | { |
| 16 | #if defined(_MSC_VER) && (_MSC_VER >= 1400) // Visual Studio 2005 |
| 17 | unsigned long i; |
| 18 | uint8_t success = _BitScanForward(&i, source); |
| 19 | return success != 0 ? i : -1; |
| 20 | #elif defined(__GNUC__) |
| 21 | auto result = __builtin_ffs(source); |
| 22 | return result - 1; |
| 23 | #else |
| 24 | #pragma message "Falling back to iterative bitscan forward, consider using intrinsics" |
| 25 | if (source != 0) |
| 26 | { |
| 27 | for (int32_t i = 0; i < 32; i++) |
| 28 | { |
| 29 | if (source & (1u << i)) |
| 30 | { |
| 31 | return i; |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | return -1; |
| 36 | #endif |
| 37 | } |
| 38 | |
| 39 | int32_t bitScanReverse(uint32_t source) |
| 40 | { |
no outgoing calls