38 using boost::uint16_t;
39 using boost::uint32_t;
40 using boost::uint64_t;
42 namespace lsst {
namespace ap {
namespace detail {
namespace {
44 #if LSST_AP_HAVE_BUILTIN_POPCOUNT
46 inline int populationCount(uint64_t
const val) {
47 # if ULLONG_MAX == 0xffffffffffffffff
48 return __builtin_popcountll(val);
49 # elif ULONG_MAX == 0xffffffffffffffff
50 return __builtin_popcountl(val);
52 # error Unable to map between uint64_t and built-in integral types
56 inline int populationCount(uint32_t
const val) {
57 # if ULONG_MAX == 0xffffffff
58 return __builtin_popcountl(val);
59 # elif UINT_MAX == 0xffffffff
60 return __builtin_popcount(val);
62 # error Unable to map between uint32_t and built-in integral types
66 inline int populationCount(uint16_t
const val) {
return __builtin_popcount(val); }
67 inline int populationCount(uint8_t
const val) {
return __builtin_popcount(val); }
71 uint64_t
const sFreedMagic64[6] = {
72 UINT64_C(0x5555555555555555),
73 UINT64_C(0x3333333333333333),
74 UINT64_C(0x0F0F0F0F0F0F0F0F),
75 UINT64_C(0x00FF00FF00FF00FF),
76 UINT64_C(0x0000FFFF0000FFFF),
77 UINT64_C(0x00000000FFFFFFFF)
80 uint32_t
const sFreedMagic32[5] = {
90 inline int populationCount(uint64_t
const val) {
92 v = ((v >> 1) & sFreedMagic64[0]) + (v & sFreedMagic64[0]);
93 v = ((v >> 2) & sFreedMagic64[1]) + (v & sFreedMagic64[1]);
94 v = ((v >> 4) & sFreedMagic64[2]) + (v & sFreedMagic64[2]);
95 v = ((v >> 8) & sFreedMagic64[3]) + (v & sFreedMagic64[3]);
96 v = ((v >> 16) & sFreedMagic64[4]) + (v & sFreedMagic64[4]);
97 v = ((v >> 32) & sFreedMagic64[5]) + (v & sFreedMagic64[5]);
101 inline int populationCount(uint32_t
const val) {
103 v = ((v >> 1) & sFreedMagic32[0]) + (v & sFreedMagic32[0]);
104 v = ((v >> 2) & sFreedMagic32[1]) + (v & sFreedMagic32[1]);
105 v = ((v >> 4) & sFreedMagic32[2]) + (v & sFreedMagic32[2]);
106 v = ((v >> 8) & sFreedMagic32[3]) + (v & sFreedMagic32[3]);
107 v = ((v >> 16) & sFreedMagic32[4]) + (v & sFreedMagic32[4]);
111 inline int populationCount(uint16_t
const val) {
113 v = ((v >> 1) & sFreedMagic32[0]) + (v & sFreedMagic32[0]);
114 v = ((v >> 2) & sFreedMagic32[1]) + (v & sFreedMagic32[1]);
115 v = ((v >> 4) & sFreedMagic32[2]) + (v & sFreedMagic32[2]);
116 v = ((v >> 8) & sFreedMagic32[3]) + (v & sFreedMagic32[3]);
120 inline int populationCount(uint8_t
const val) {
122 v = ((v >> 1) & sFreedMagic32[0]) + (v & sFreedMagic32[0]);
123 v = ((v >> 2) & sFreedMagic32[1]) + (v & sFreedMagic32[1]);
124 v = ((v >> 4) & sFreedMagic32[2]) + (v & sFreedMagic32[2]);
148 template <
typename WordT>
152 int const numBitsToSet,
155 assert(words != 0 && numBits > 0 &&
"null or empty bitset");
156 assert(indexes != 0 && numBitsToSet > 0 &&
"null or empty index array");
159 WordT
const last = ~(maskForBit<WordT>(numBits) - 1);
163 int zeroes = numBitsToSet;
165 for (i = 0; i < nwords - special && zeroes > 0; ++i) {
169 if (zeroes > 0 && special) {
179 for (
int i = 0; zeroes < numBitsToSet; ++i) {
181 if (w == BitTraits<WordT>::WORD_MASK) {
185 for (
int j = 0; j < BitTraits<WordT>::BITS_PER_WORD; ++j, mask <<= 1) {
186 if ((w & mask) == 0) {
187 indexes[zeroes++] = (i << BitTraits<WordT>::BITS_PER_WORD_LOG2) + j;
189 if (zeroes == numBitsToSet) {
211 template <
typename WordT>
214 int const *
const indexes,
215 int const numBitsToReset,
218 assert(words != 0 && numBits > 0 &&
"null or empty bitset");
219 assert(indexes != 0 && numBitsToReset >= 0 &&
"null or empty index array");
221 for (
int i = 0; i < numBitsToReset; ++i) {
222 int const j = indexes[i];
223 assert(j >= 0 && j < numBits);
224 words[wordForBit<WordT>(j)] &= ~ maskForBit<WordT>(j);
230 #define INSTANTIATE(t) \
231 template bool lsst::ap::detail::setBits(int * const, t * const, int const, int const); \
232 template void lsst::ap::detail::resetBits(t * const, int const * const, int const, int const);
A class for manipulating a fixed set of bits at the individual bit level.
#define INSTANTIATE(TYPE)
void resetBits(WordT *const words, int const *const indexes, int const numBitsToReset, int const numBits)
bool setBits(int *const indexes, WordT *const words, int const numBitsToSet, int const numBits)