csutil/bitops.h
00001 /* 00002 Copyright (C) 2007 by Marten Svanfeldt 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License as published by the Free Software Foundation; either 00007 version 2 of the License, or (at your option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public 00015 License along with this library; if not, write to the Free 00016 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 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 // Generic c++ version 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
Generated for Crystal Space 2.1 by doxygen 1.6.1
