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 4bb590cbe2
commit f5895e763b
3 changed files with 59 additions and 5 deletions

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)