1#ifndef MARISA_POPCOUNT_H_
2#define MARISA_POPCOUNT_H_
3
4#include "base.h"
5
6namespace marisa {
7
8class PopCount {
9 public:
10  PopCount(UInt32 x) : value_() {
11    x = (x & 0x55555555U) + ((x & 0xAAAAAAAAU) >> 1);
12    x = (x & 0x33333333U) + ((x & 0xCCCCCCCCU) >> 2);
13    x = (x + (x >> 4)) & 0x0F0F0F0FU;
14    x += x << 8;
15    x += x << 16;
16    value_ = x;
17  }
18
19  UInt32 lo8() const {
20    return value_ & 0xFFU;
21  }
22  UInt32 lo16() const {
23    return (value_ >> 8) & 0xFFU;
24  }
25  UInt32 lo24() const {
26    return (value_ >> 16) & 0xFFU;
27  }
28  UInt32 lo32() const {
29    return value_ >> 24;
30  }
31
32 private:
33  UInt32 value_;
34};
35
36}  // namespace marisa
37
38#endif  // MARISA_POPCOUNT_H_
39