Skip to content
This repository has been archived by the owner on Aug 4, 2022. It is now read-only.

Commit

Permalink
Bug 906368 - IonMonkey: Define a proper CountPopulation32 function, a…
Browse files Browse the repository at this point in the history
…nd use it in place of manual code in RegisterSets.h. r=nbp
  • Loading branch information
Dan Gohman committed Aug 19, 2013
1 parent b12649a commit 57a12a5
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 5 deletions.
6 changes: 1 addition & 5 deletions js/src/jit/RegisterSets.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_;
Expand Down
26 changes: 26 additions & 0 deletions mfbt/MathAlgorithms.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
32 changes: 32 additions & 0 deletions mfbt/tests/TestCountPopulation.cpp
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 57a12a5

Please sign in to comment.