--- a/memory/build/mozjemalloc.cpp
+++ b/memory/build/mozjemalloc.cpp
@@ -2338,43 +2338,18 @@ arena_run_reg_dalloc(arena_run_t* run, a
static_assert(((sizeof(size_invs)) / sizeof(unsigned)) + 3 >=
kNumQuantumClasses,
"size_invs doesn't have enough values");
// Avoid doing division with a variable divisor if possible. Using
// actual division here can reduce allocator throughput by over 20%!
diff =
(unsigned)((uintptr_t)ptr - (uintptr_t)run - bin->mRunFirstRegionOffset);
- if ((size & (size - 1)) == 0) {
- // log2_table allows fast division of a power of two in the
- // [1..128] range.
- //
- // (x / divisor) becomes (x >> log2_table[divisor - 1]).
- // clang-format off
- static const unsigned char log2_table[] = {
- 0, 1, 0, 2, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 4,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7
- };
- // clang-format on
-
- if (size <= 128) {
- regind = (diff >> log2_table[size - 1]);
- } else if (size <= 32768) {
- regind = diff >> (8 + log2_table[(size >> 8) - 1]);
- } else {
- // The run size is too large for us to use the lookup
- // table. Use real division.
- regind = diff / size;
- }
+ if (mozilla::IsPowerOfTwo(size)) {
+ regind = diff >> FloorLog2(size);
} else if (size <= ((sizeof(size_invs) / sizeof(unsigned)) * kQuantum) + 2) {
regind = size_invs[(size / kQuantum) - 3] * diff;
regind >>= SIZE_INV_SHIFT;
} else {
// size_invs isn't large enough to handle this size class, so
// calculate regind using actual division. This only happens
// if the user increases small_max via the 'S' runtime
// configuration option.