diff --git a/js/src/jit/RegisterSets.h b/js/src/jit/RegisterSets.h index 2a5e16b792e0..65ba34459363 100644 --- a/js/src/jit/RegisterSets.h +++ b/js/src/jit/RegisterSets.h @@ -449,11 +449,7 @@ class TypedRegisterSet return bits_; } uint32_t size() const { - uint32_t sum2 = (bits_ & 0x55555555) + ((bits_ & 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; + return mozilla::CountPopulation32(bits_); } bool operator ==(const TypedRegisterSet &other) const { return other.bits_ == bits_; diff --git a/mfbt/MathAlgorithms.h b/mfbt/MathAlgorithms.h index 6d58691e0612..941ac812718d 100644 --- a/mfbt/MathAlgorithms.h +++ b/mfbt/MathAlgorithms.h @@ -186,6 +186,16 @@ namespace detail { 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; + } + inline uint_fast8_t CountLeadingZeroes64(uint64_t u) { @@ -242,6 +252,12 @@ namespace detail { return __builtin_ctz(u); } + inline uint_fast8_t + CountPopulation32(uint32_t u) + { + return __builtin_popcount(u); + } + inline uint_fast8_t CountLeadingZeroes64(uint64_t u) { @@ -258,6 +274,7 @@ namespace detail { # error "Implement these!" inline uint_fast8_t CountLeadingZeroes32(uint32_t u) MOZ_DELETE; inline uint_fast8_t CountTrailingZeroes32(uint32_t u) MOZ_DELETE; + inline uint_fast8_t CountPopulation32(uint32_t u) MOZ_DELETE; inline uint_fast8_t CountLeadingZeroes64(uint64_t u) MOZ_DELETE; inline uint_fast8_t CountTrailingZeroes64(uint64_t u) MOZ_DELETE; #endif @@ -300,6 +317,15 @@ CountTrailingZeroes32(uint32_t u) return detail::CountTrailingZeroes32(u); } +/** + * Compute the number of one bits in the number |u|, + */ +inline uint_fast8_t +CountPopulation32(uint32_t u) +{ + return detail::CountPopulation32(u); +} + /** Analogous to CountLeadingZeroes32, but for 64-bit numbers. */ inline uint_fast8_t CountLeadingZeroes64(uint64_t u) diff --git a/mfbt/tests/TestCountPopulation.cpp b/mfbt/tests/TestCountPopulation.cpp new file mode 100644 index 000000000000..e02cb1384538 --- /dev/null +++ b/mfbt/tests/TestCountPopulation.cpp @@ -0,0 +1,32 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "mozilla/MathAlgorithms.h" + +using mozilla::CountPopulation32; + +static void +TestCountPopulation32() +{ + MOZ_ASSERT(CountPopulation32(0xFFFFFFFF) == 32); + MOZ_ASSERT(CountPopulation32(0xF0FF1000) == 13); + MOZ_ASSERT(CountPopulation32(0x7F8F0001) == 13); + MOZ_ASSERT(CountPopulation32(0x3FFF0100) == 15); + MOZ_ASSERT(CountPopulation32(0x1FF50010) == 12); + MOZ_ASSERT(CountPopulation32(0x00800000) == 1); + MOZ_ASSERT(CountPopulation32(0x00400000) == 1); + MOZ_ASSERT(CountPopulation32(0x00008000) == 1); + MOZ_ASSERT(CountPopulation32(0x00004000) == 1); + MOZ_ASSERT(CountPopulation32(0x00000080) == 1); + MOZ_ASSERT(CountPopulation32(0x00000040) == 1); + MOZ_ASSERT(CountPopulation32(0x00000001) == 1); + MOZ_ASSERT(CountPopulation32(0x00000000) == 0); +} + +int main() +{ + TestCountPopulation32(); + return 0; +}