diff --git a/mfbt/MathAlgorithms.h b/mfbt/MathAlgorithms.h index 7b98c7fc1966..c61be1f3f5f2 100644 --- a/mfbt/MathAlgorithms.h +++ b/mfbt/MathAlgorithms.h @@ -189,11 +189,9 @@ namespace detail { 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 diff --git a/mfbt/tests/moz.build b/mfbt/tests/moz.build index 2163a5f32795..706540fa5dec 100644 --- a/mfbt/tests/moz.build +++ b/mfbt/tests/moz.build @@ -10,6 +10,7 @@ CPP_UNIT_TESTS += [ 'TestCasting.cpp', 'TestCeilingFloor.cpp', 'TestCheckedInt.cpp', + 'TestCountPopulation.cpp', 'TestCountZeroes.cpp', 'TestEndian.cpp', 'TestEnumSet.cpp',