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:
Dan Gohman 2013-08-19 12:32:22 -07:00
parent baec7c49b6
commit 6fbf2016aa
3 changed files with 59 additions and 5 deletions

View File

@ -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<T> &other) const {
return other.bits_ == bits_;

View File

@ -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)

View 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;
}