00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef __CSUTIL_BITOPS_H__
00020 #define __CSUTIL_BITOPS_H__
00021
00022
00023 namespace CS
00024 {
00025 namespace Utility
00026 {
00030 namespace BitOps
00031 {
00038 CS_FORCEINLINE bool ScanBitForward (uint32 value, unsigned long& index)
00039 {
00040 #if defined(CS_HAVE___BUILTIN_CTZ)
00041 index = __builtin_ctz (value);
00042 return value != 0;
00043 #elif defined(CS_HAVE_BITSCAN_INTRINSICS)
00044 return _BitScanForward (&index, value) != 0;
00045 #else
00046
00047 index = 0;
00048
00049 while (value)
00050 {
00051 if (value & 0x01)
00052 {
00053 return true;
00054 }
00055 value >>= 0x01;
00056 index++;
00057 }
00058
00059 return false;
00060 #endif
00061 }
00062
00069 CS_FORCEINLINE bool ScanBitReverse (uint32 value, unsigned long& index)
00070 {
00071 #if defined(CS_HAVE___BUILTIN_CLZ)
00072 index = __builtin_clz (value);
00073 return value != 0;
00074 #elif defined(CS_HAVE_BITSCAN_INTRINSICS)
00075 return _BitScanReverse (&index, value) != 0;
00076 #else
00077 index = 0;
00078
00079 while (value)
00080 {
00081 if (value & 0x80000000)
00082 {
00083 return true;
00084 }
00085 value <<= 0x01;
00086 index++;
00087 }
00088
00089 return false;
00090 #endif
00091 }
00092
00096 CS_FORCEINLINE uint32 ComputeBitsSet (uint32 v)
00097 {
00098 #if defined(CS_HAVE___BUILTIN_POPCOUNT)
00099 return __builtin_popcount (v);
00100 #else
00101 v = v - ((v >> 1) & 0x55555555);
00102 v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
00103 return (((v + (v >> 4)) & 0x0f0f0f0f) * 0x01010101) >> 24;
00104 #endif
00105 }
00106
00107 }
00108 }
00109
00110 }
00111
00112 #endif