Bug 1413096 - Replace ffs with MathAlgorithms functions. r?njn draft
authorMike Hommey <mh+mozilla@glandium.org>
Mon, 30 Oct 2017 17:44:16 +0900
changeset 689164 0b717b3bdb043c5d68d67e0f5d35baf47d695b1c
parent 689163 ecb05844831d596f8acbfab734fba45999244828
child 689165 2dd706608340e8825eeb71b356298cd16d43c828
push id86942
push userbmo:mh+mozilla@glandium.org
push dateTue, 31 Oct 2017 06:24:30 +0000
reviewersnjn
bugs1413096
milestone58.0a1
Bug 1413096 - Replace ffs with MathAlgorithms functions. r?njn - In the cases where it's used on powers of 2, replace it with FloorLog2 - In the cases where it's used on any kind of number, replace it with CountTrailingZeroes, which is `ffs(x) - 1`.
memory/build/mozjemalloc.cpp
--- a/memory/build/mozjemalloc.cpp
+++ b/memory/build/mozjemalloc.cpp
@@ -159,29 +159,16 @@ using namespace mozilla;
 // Some defines from the CRT internal headers that we need here.
 #define _CRT_SPINCOUNT 5000
 #include <io.h>
 #include <windows.h>
 #include <intrin.h>
 
 #define STDERR_FILENO 2
 
-// Use MSVC intrinsics.
-#pragma intrinsic(_BitScanForward)
-static __forceinline int
-ffs(int x)
-{
-  unsigned long i;
-
-  if (_BitScanForward(&i, x) != 0) {
-    return i + 1;
-  }
-  return 0;
-}
-
 // Implement getenv without using malloc.
 static char mozillaMallocOptionsBuf[64];
 
 #define getenv xgetenv
 static char*
 getenv(const char* name)
 {
 
@@ -226,19 +213,16 @@ typedef long ssize_t;
 #include <pthread.h>
 #include <sched.h>
 #include <stdarg.h>
 #include <stdio.h>
 #include <stdbool.h>
 #include <stdint.h>
 #include <stdlib.h>
 #include <string.h>
-#ifndef XP_DARWIN
-#include <strings.h>
-#endif
 #include <unistd.h>
 
 #ifdef XP_DARWIN
 #include <libkern/OSAtomic.h>
 #include <mach/mach_error.h>
 #include <mach/mach_init.h>
 #include <mach/vm_map.h>
 #include <malloc/malloc.h>
@@ -2279,17 +2263,17 @@ arena_run_reg_alloc(arena_run_t* run, ar
 
   // Move the first check outside the loop, so that run->regs_minelm can
   // be updated unconditionally, without the possibility of updating it
   // multiple times.
   i = run->regs_minelm;
   mask = run->regs_mask[i];
   if (mask != 0) {
     // Usable allocation found.
-    bit = ffs((int)mask) - 1;
+    bit = CountTrailingZeroes32(mask);
 
     regind = ((i << (SIZEOF_INT_2POW + 3)) + bit);
     MOZ_ASSERT(regind < bin->nregs);
     ret =
       (void*)(((uintptr_t)run) + bin->reg0_offset + (bin->reg_size * regind));
 
     // Clear bit.
     mask ^= (1U << bit);
@@ -2297,17 +2281,17 @@ arena_run_reg_alloc(arena_run_t* run, ar
 
     return ret;
   }
 
   for (i++; i < bin->regs_mask_nelms; i++) {
     mask = run->regs_mask[i];
     if (mask != 0) {
       // Usable allocation found.
-      bit = ffs((int)mask) - 1;
+      bit = CountTrailingZeroes32(mask);
 
       regind = ((i << (SIZEOF_INT_2POW + 3)) + bit);
       MOZ_ASSERT(regind < bin->nregs);
       ret =
         (void*)(((uintptr_t)run) + bin->reg0_offset + (bin->reg_size * regind));
 
       // Clear bit.
       mask ^= (1U << bit);
@@ -3054,33 +3038,33 @@ arena_t::MallocSmall(size_t aSize, bool 
 {
   void* ret;
   arena_bin_t* bin;
   arena_run_t* run;
 
   if (aSize < small_min) {
     // Tiny.
     aSize = RoundUpPow2(aSize);
-    bin = &mBins[ffs((int)(aSize >> (TINY_MIN_2POW + 1)))];
+    bin = &mBins[FloorLog2(aSize >> (TINY_MIN_2POW + 1))];
 
     // Bin calculation is always correct, but we may need
     // to fix size for the purposes of assertions and/or
     // stats accuracy.
     if (aSize < (1U << TINY_MIN_2POW)) {
       aSize = 1U << TINY_MIN_2POW;
     }
   } else if (aSize <= small_max) {
     // Quantum-spaced.
     aSize = QUANTUM_CEILING(aSize);
     bin = &mBins[ntbins + (aSize >> QUANTUM_2POW_MIN) - 1];
   } else {
     // Sub-page.
     aSize = RoundUpPow2(aSize);
     bin = &mBins[ntbins + nqbins +
-                 (ffs((int)(aSize >> SMALL_MAX_2POW_DEFAULT)) - 2)];
+                 (FloorLog2(aSize >> SMALL_MAX_2POW_DEFAULT) - 2)];
   }
   MOZ_DIAGNOSTIC_ASSERT(aSize == bin->reg_size);
 
   {
     MutexAutoLock lock(mLock);
     if ((run = bin->runcur) && run->nfree > 0) {
       ret = MallocBinEasy(bin, run);
     } else {
@@ -4177,17 +4161,19 @@ static
     _malloc_message(
       _getprogname(),
       "Compile-time page size does not divide the runtime one.\n");
     MOZ_CRASH();
   }
 #else
   pagesize = (size_t)result;
   pagesize_mask = (size_t)result - 1;
-  pagesize_2pow = ffs((int)result) - 1;
+  pagesize_2pow = FloorLog2(result);
+  MOZ_RELEASE_ASSERT(1ULL << pagesize_2pow == pagesize,
+                     "Page size is not a power of two");
 #endif
 
   // Get runtime configuration.
   if ((opts = getenv("MALLOC_OPTIONS"))) {
     for (i = 0; opts[i] != '\0'; i++) {
       unsigned j, nreps;
       bool nseen;