Bug 906368 - IonMonkey: Define a proper CountPopulation32 function, and use it in place of manual code in RegisterSets.h. r=nbp
This commit is contained in:
@@ -449,11 +449,7 @@ class TypedRegisterSet
|
|||||||
return bits_;
|
return bits_;
|
||||||
}
|
}
|
||||||
uint32_t size() const {
|
uint32_t size() const {
|
||||||
uint32_t sum2 = (bits_ & 0x55555555) + ((bits_ & 0xaaaaaaaa) >> 1);
|
return mozilla::CountPopulation32(bits_);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
bool operator ==(const TypedRegisterSet<T> &other) const {
|
bool operator ==(const TypedRegisterSet<T> &other) const {
|
||||||
return other.bits_ == bits_;
|
return other.bits_ == bits_;
|
||||||
|
|||||||
@@ -186,6 +186,16 @@ namespace detail {
|
|||||||
return uint_fast8_t(index);
|
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
|
inline uint_fast8_t
|
||||||
CountLeadingZeroes64(uint64_t u)
|
CountLeadingZeroes64(uint64_t u)
|
||||||
{
|
{
|
||||||
@@ -242,6 +252,12 @@ namespace detail {
|
|||||||
return __builtin_ctz(u);
|
return __builtin_ctz(u);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline uint_fast8_t
|
||||||
|
CountPopulation32(uint32_t u)
|
||||||
|
{
|
||||||
|
return __builtin_popcount(u);
|
||||||
|
}
|
||||||
|
|
||||||
inline uint_fast8_t
|
inline uint_fast8_t
|
||||||
CountLeadingZeroes64(uint64_t u)
|
CountLeadingZeroes64(uint64_t u)
|
||||||
{
|
{
|
||||||
@@ -258,6 +274,7 @@ namespace detail {
|
|||||||
# error "Implement these!"
|
# error "Implement these!"
|
||||||
inline uint_fast8_t CountLeadingZeroes32(uint32_t u) MOZ_DELETE;
|
inline uint_fast8_t CountLeadingZeroes32(uint32_t u) MOZ_DELETE;
|
||||||
inline uint_fast8_t CountTrailingZeroes32(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 CountLeadingZeroes64(uint64_t u) MOZ_DELETE;
|
||||||
inline uint_fast8_t CountTrailingZeroes64(uint64_t u) MOZ_DELETE;
|
inline uint_fast8_t CountTrailingZeroes64(uint64_t u) MOZ_DELETE;
|
||||||
#endif
|
#endif
|
||||||
@@ -300,6 +317,15 @@ CountTrailingZeroes32(uint32_t u)
|
|||||||
return detail::CountTrailingZeroes32(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. */
|
/** Analogous to CountLeadingZeroes32, but for 64-bit numbers. */
|
||||||
inline uint_fast8_t
|
inline uint_fast8_t
|
||||||
CountLeadingZeroes64(uint64_t u)
|
CountLeadingZeroes64(uint64_t u)
|
||||||
|
|||||||
32
mfbt/tests/TestCountPopulation.cpp
Normal file
32
mfbt/tests/TestCountPopulation.cpp
Normal file
@@ -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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user