Bug 978397 - Fix the implementation of CountPopulation_32 on Windows. r=nbp
authorDan Gohman <sunfish@mozilla.com>
Tue, 11 Mar 2014 08:09:55 -0400
changeset 190144 4d6f4e6bba2172d05f6cf9cee1dc7b8e7c0bf972
parent 190143 404edd4f2130a34d00ffbbe5491f94e34b5537fd
child 190145 016e7ab40c285d069f3e6b8624c9fa375f193eee
push id3503
push userraliiev@mozilla.com
push dateMon, 28 Apr 2014 18:51:11 +0000
treeherdermozilla-beta@c95ac01e332e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersnbp
bugs978397
milestone30.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 978397 - Fix the implementation of CountPopulation_32 on Windows. r=nbp
mfbt/MathAlgorithms.h
mfbt/tests/moz.build
--- a/mfbt/MathAlgorithms.h
+++ b/mfbt/MathAlgorithms.h
@@ -184,21 +184,19 @@ namespace detail {
     unsigned long index;
     _BitScanForward(&index, static_cast<unsigned long>(u));
     return uint_fast8_t(index);
   }
 
   inline uint_fast8_t
   CountPopulation32(uint32_t u)
   {
-     uint32_t sum2  = (u     & 0x55555555) + ((u     & 0xaaaaaaaa) >> 1);
-     uint32_t sum4  = (sum2  & 0x33333333) + ((sum2  & 0xcccccccc) >> 2);
-     uint32_t sum8  = (sum4  & 0x0f0f0f0f) + ((sum4  & 0xf0f0f0f0) >> 4);
-     uint32_t sum16 = (sum8  & 0x00ff00ff) + ((sum8  & 0xff00ff00) >> 8);
-     return sum16;
+    uint32_t x = u - ((u >> 1) & 0x55555555);
+    x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
+    return (((x + (x >> 4)) & 0xf0f0f0f) * 0x1010101) >> 24;
   }
 
   inline uint_fast8_t
   CountLeadingZeroes64(uint64_t u)
   {
 #  if defined(MOZ_BITSCAN_WINDOWS64)
     unsigned long index;
     _BitScanReverse64(&index, static_cast<unsigned __int64>(u));
--- a/mfbt/tests/moz.build
+++ b/mfbt/tests/moz.build
@@ -5,16 +5,17 @@
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 CPP_UNIT_TESTS += [
     'TestAtomics.cpp',
     'TestBloomFilter.cpp',
     'TestCasting.cpp',
     'TestCeilingFloor.cpp',
     'TestCheckedInt.cpp',
+    'TestCountPopulation.cpp',
     'TestCountZeroes.cpp',
     'TestEndian.cpp',
     'TestEnumSet.cpp',
     'TestFloatingPoint.cpp',
     'TestIntegerPrintfMacros.cpp',
     'TestRollingMean.cpp',
     'TestSHA1.cpp',
     'TestTypedEnum.cpp',