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, size_t& index) 00039 { 00040 #ifdef CS_HAVE_BITSCAN_INTRINSICS 00041 return _BitScanForward ((unsigned long*)&index, value) != 0; 00042 #else 00043 // Generic c++ version 00044 index = 0; 00045 00046 while (value) 00047 { 00048 if (value & 0x01) 00049 { 00050 return true; 00051 } 00052 value >>= 0x01; 00053 index++; 00054 } 00055 00056 return false; 00057 #endif 00058 } 00059 00066 CS_FORCEINLINE bool ScanBitReverse (uint32 value, size_t& index) 00067 { 00068 #ifdef CS_HAVE_BITSCAN_INTRINSICS 00069 return _BitScanReverse ((unsigned long*)&index, value) != 0; 00070 #else 00071 index = 0; 00072 00073 while (value) 00074 { 00075 if (value & 0x80000000) 00076 { 00077 return true; 00078 } 00079 value <<= 0x01; 00080 index++; 00081 } 00082 00083 return false; 00084 #endif 00085 } 00086 00090 CS_FORCEINLINE uint32 ComputeBitsSet (uint32 v) 00091 { 00092 v = v - ((v >> 1) & 0x55555555); 00093 v = (v & 0x33333333) + ((v >> 2) & 0x33333333); 00094 return ((v + (v >> 4) & 0x0f0f0f0f) * 0x01010101) >> 24; 00095 } 00096 00097 } 00098 } 00099 00100 } 00101 00102 #endif
Generated for Crystal Space 1.2 by doxygen 1.4.7