|
39 | 39 | static INLINE unsigned int
|
40 | 40 | bitcount(BITMASK_W n)
|
41 | 41 | {
|
42 |
| - const int bitmask_len = BITMASK_W_LEN; |
43 |
| - if (bitmask_len == 32) { |
| 42 | +#if BITMASK_W_LEN == 32 |
44 | 43 | #ifdef GILLIES
|
45 |
| - /* (C) Donald W. Gillies, 1992. All rights reserved. You may reuse |
46 |
| - this bitcount() function anywhere you please as long as you retain |
47 |
| - this Copyright Notice. */ |
48 |
| - register unsigned long tmp; |
49 |
| - return (tmp = (n) - (((n) >> 1) & 033333333333) - |
50 |
| - (((n) >> 2) & 011111111111), |
51 |
| - tmp = ((tmp + (tmp >> 3)) & 030707070707), |
52 |
| - tmp = (tmp + (tmp >> 6)), |
53 |
| - tmp = (tmp + (tmp >> 12) + (tmp >> 24)) & 077); |
| 44 | + /* (C) Donald W. Gillies, 1992. All rights reserved. You may reuse |
| 45 | + this bitcount() function anywhere you please as long as you retain |
| 46 | + this Copyright Notice. */ |
| 47 | + register unsigned long tmp; |
| 48 | + return ( |
| 49 | + tmp = (n) - (((n) >> 1) & 033333333333) - (((n) >> 2) & 011111111111), |
| 50 | + tmp = ((tmp + (tmp >> 3)) & 030707070707), tmp = (tmp + (tmp >> 6)), |
| 51 | + tmp = (tmp + (tmp >> 12) + (tmp >> 24)) & 077); |
54 | 52 | /* End of Donald W. Gillies bitcount code */
|
55 |
| -#else |
56 |
| - /* This piece taken from Jorg Arndt's "Algorithms for Programmers" */ |
57 |
| - n = ((n >> 1) & 0x55555555) + (n & 0x55555555); // 0-2 in 2 bits |
58 |
| - n = ((n >> 2) & 0x33333333) + (n & 0x33333333); // 0-4 in 4 bits |
59 |
| - n = ((n >> 4) + n) & 0x0f0f0f0f; // 0-8 in 4 bits |
60 |
| - n += n >> 8; // 0-16 in 8 bits |
61 |
| - n += n >> 16; // 0-32 in 8 bits |
62 |
| - return n & 0xff; |
63 |
| -#endif |
64 |
| - } |
65 |
| - else if (bitmask_len == 64) { |
66 |
| - n = ((n >> 1) & 0x5555555555555555) + (n & 0x5555555555555555); |
67 |
| - n = ((n >> 2) & 0x3333333333333333) + (n & 0x3333333333333333); |
68 |
| - n = ((n >> 4) + n) & 0x0f0f0f0f0f0f0f0f; |
69 |
| - n += n >> 8; |
70 |
| - n += n >> 16; |
71 |
| -#ifdef _WIN32 |
72 |
| - /* Use explicit typecast to silence MSVC warning about large bitshift, |
73 |
| - * even though this part code does not run on windows */ |
74 |
| - n += (long long)n >> 32; |
75 |
| -#else |
76 |
| - n += n >> 32; |
77 |
| -#endif |
78 |
| - return n & 0xff; |
79 |
| - } |
80 |
| - else { |
81 |
| - /* Handle non-32 or 64 bit case the slow way */ |
82 |
| - unsigned int nbits = 0; |
83 |
| - while (n) { |
84 |
| - if (n & 1) |
85 |
| - nbits++; |
86 |
| - n = n >> 1; |
87 |
| - } |
88 |
| - return nbits; |
| 53 | +#else /* ~GILLIES */ |
| 54 | + /* This piece taken from Jorg Arndt's "Algorithms for Programmers" */ |
| 55 | + n = ((n >> 1) & 0x55555555) + (n & 0x55555555); // 0-2 in 2 bits |
| 56 | + n = ((n >> 2) & 0x33333333) + (n & 0x33333333); // 0-4 in 4 bits |
| 57 | + n = ((n >> 4) + n) & 0x0f0f0f0f; // 0-8 in 4 bits |
| 58 | + n += n >> 8; // 0-16 in 8 bits |
| 59 | + n += n >> 16; // 0-32 in 8 bits |
| 60 | + return n & 0xff; |
| 61 | +#endif /* ~GILLIES */ |
| 62 | +#elif BITMASK_W_LEN == 64 |
| 63 | + n = ((n >> 1) & 0x5555555555555555) + (n & 0x5555555555555555); |
| 64 | + n = ((n >> 2) & 0x3333333333333333) + (n & 0x3333333333333333); |
| 65 | + n = ((n >> 4) + n) & 0x0f0f0f0f0f0f0f0f; |
| 66 | + n += n >> 8; |
| 67 | + n += n >> 16; |
| 68 | + n += n >> 32; |
| 69 | + return n & 0xff; |
| 70 | +#else /* BITMASK_W_LEN */ |
| 71 | + /* Handle non-32 or 64 bit case the slow way */ |
| 72 | + unsigned int nbits = 0; |
| 73 | + while (n) { |
| 74 | + if (n & 1) |
| 75 | + nbits++; |
| 76 | + n = n >> 1; |
89 | 77 | }
|
| 78 | + return nbits; |
| 79 | +#endif /* BITMASK_W_LEN */ |
90 | 80 | }
|
91 | 81 |
|
| 82 | +__builtin__popcount |
| 83 | + |
92 | 84 | /* Positive modulo of the given dividend and divisor (dividend % divisor).
|
93 | 85 | *
|
94 | 86 | * Params:
|
|
0 commit comments