Skip to content

Commit 32d6f31

Browse files
perf(core/bit_utils.h): optimize MostSignificantBit default impl (#1064)
1 parent b283ba3 commit 32d6f31

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

src/draco/core/bit_utils.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,19 @@ inline int MostSignificantBit(uint32_t n) {
5959
#if defined(__GNUC__)
6060
return 31 ^ __builtin_clz(n);
6161
#elif defined(_MSC_VER)
62-
6362
unsigned long where;
6463
_BitScanReverse(&where, n);
6564
return (int)where;
6665
#else
67-
// TODO(fgalligan): Optimize this code.
68-
int msb = -1;
69-
while (n != 0) {
70-
msb++;
71-
n >>= 1;
66+
uint32_t msb = 0;
67+
if (n) {
68+
if (0xFFFF0000 & n) { n >>= (1 << 4); msb |= (1 << 4); }
69+
if (0x0000FF00 & n) { n >>= (1 << 3); msb |= (1 << 3); }
70+
if (0x000000F0 & n) { n >>= (1 << 2); msb |= (1 << 2); }
71+
if (0x0000000C & n) { n >>= (1 << 1); msb |= (1 << 1); }
72+
if (0x00000002 & n) { msb |= (1 << 0); }
73+
} else {
74+
msb = -1;
7275
}
7376
return msb;
7477
#endif

0 commit comments

Comments
 (0)