/usr/include/osl/misc/bitOp.h is in libosl-dev 0.6.0-3.3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | #ifndef OSL_MISC_BITOP_H
#define OSL_MISC_BITOP_H
#include "osl/config.h"
#include <cassert>
namespace osl
{
namespace misc
{
/**
* x86 bsf 命令
*/
template <class Integer> struct Bsf;
template <>
struct Bsf<unsigned int>
{
static int bsf(unsigned int mask)
{
assert(mask);
#ifdef __x86_64__
int ret;
__asm__("bsfl %1,%0" : "=r"(ret) : "r"(mask));
return ret;
#else /* これ以下は 32bit 環境 */
# ifdef __i386__
int ret;
__asm__("bsfl %1,%0" : "=r"(ret) : "r"(mask));
return ret;
# elif defined __GNUC__
return __builtin_ctzl(mask);
# else
for (int i=0;i<32;i++)
if (mask & (1<<i))
return i;
# endif
#endif
assert(0);
return -1;
}
};
template <>
struct Bsf<unsigned short>
{
static unsigned short bsf(unsigned short mask)
{
assert(mask);
#ifdef __x86_64__
unsigned short ret;
__asm__("bsfw %1,%0" : "=r"(ret) : "r"(mask));
return ret;
#else /* これ以下は 32bit 環境 */
# ifdef __i386__
unsigned short ret;
__asm__("bsfw %1,%0" : "=r"(ret) : "r"(mask));
return ret;
# else
return __builtin_ctzl(mask);
# endif
#endif
}
};
template <>
struct Bsf<unsigned long long>
{
static int bsf(unsigned long long mask)
{
assert(mask);
#ifdef __x86_64__
long long ret;
__asm__("bsfq %1,%0" : "=r"(ret) : "r"(mask));
return static_cast<int>(ret);
#else /* これ以下は 32bit 環境 */
unsigned int mask32 = static_cast<unsigned int>(mask);
if (mask32)
return Bsf<unsigned int>::bsf(mask32);
mask32 = static_cast<unsigned int>(mask >> 32);
return 32+Bsf<unsigned int>::bsf(mask32);
#endif
}
};
template <class Integer> struct Bsr;
template <>
struct Bsr<unsigned int>
{
static int bsr(unsigned int mask)
{
assert(mask);
#ifdef __x86_64__
int ret;
__asm__("bsrl %1,%0" : "=r"(ret) : "r"(mask));
return ret;
#else /* これ以下は 32bit 環境 */
# ifdef __i386__
int ret;
__asm__("bsrl %1,%0" : "=r"(ret) : "r"(mask));
return ret;
# elif __GNUC__
return __builtin_clzl(mask);
# else
for (int i=31; i>=0; --i)
if (mask & (1<<i))
return i;
# endif
#endif
assert(0);
return -1;
}
};
template <>
struct Bsr<unsigned long long>
{
static int bsr(unsigned long long mask)
{
assert(mask);
#ifdef __x86_64__
long long ret;
__asm__("bsrq %1,%0" : "=r"(ret) : "r"(mask));
return static_cast<int>(ret);
#else /* これ以下は 32bit 環境 */
unsigned int mask32 = static_cast<unsigned int>(mask >> 32);
if (mask32)
return 32+Bsr<unsigned int>::bsr(mask32);
mask32 = static_cast<unsigned int>(mask);
return Bsr<unsigned int>::bsr(mask32);
#endif
}
};
struct BitOp
{
template <class Integer>
static int bsf(Integer mask)
{
return Bsf<Integer>::bsf(mask);
}
template <class Integer>
static int bsr(Integer mask)
{
return Bsr<Integer>::bsr(mask);
}
template <class Integer>
static int takeOneBit(Integer& mask){
assert(mask);
const int num=bsf(mask);
mask &= mask-1;
return num;
}
template <class Integer>
static int
#ifdef __GNUC__
__attribute__ ((const))
#endif
countBit(Integer mask)
{
int count=0;
while (mask)
{
++count;
mask &= (mask-1);
}
return count;
}
template <class Integer>
static bool hasMultipleBit(Integer mask){
assert(mask);
return (mask & (mask-1));
}
/**
* non-zeroのmaskのsetされているビットをLSBから探し,そのビットだけがsetされたmaskを返す.
*/
template <class Integer>
static Integer lowestBit(Integer mask){
assert(mask);
return static_cast<Integer>(mask & (-mask));
}
};
#if 0
/**
* bit数が多い時は?
* population countは
* Cray, CDC Cyber series, Alliant FX/80.
* UltraSPARC, Alpha 21264
* Intel x86 has the parity of a byte
*#define bitcount(n) \
* (tmp = n - ((n >> 1) & 033333333333) - ((n >> 2) & 011111111111), \
* tmp = ((tmp + (tmp >> 3)) & 030707070707), \
* tmp = (tmp + (tmp >> 6)), \
* tmp = (tmp + (tmp >> 12) + (tmp >> 24)) & 077)
*
*/
inline int countBitDense(unsigned int mask)
{
mask = ((mask>>1)&0x55555555)+(mask&0x55555555);
mask = ((mask>>2)&0x33333333)+(mask&0x33333333);
mask = ((mask>>4)+mask)&0xf0f0f0f;
mask = (mask>>8)+mask;
return ((mask>>16)+mask)&0x3f;
}
#endif
} // namespace misc
} // namespace osl
#endif // _BITOP_H
// ;;; Local Variables:
// ;;; mode:c++
// ;;; c-basic-offset:2
// ;;; End:
|