bitmath_func.hpp

Go to the documentation of this file.
00001 /* $Id: bitmath_func.hpp 11694 2007-12-25 13:28:09Z rubidium $ */
00002 
00005 #ifndef BITMATH_FUNC_HPP
00006 #define BITMATH_FUNC_HPP
00007 
00024 template<typename T> static inline uint GB(const T x, const uint8 s, const uint8 n)
00025 {
00026   return (x >> s) & ((1U << n) - 1);
00027 }
00028 
00046 template<typename T, typename U> static inline T SB(T& x, const uint8 s, const uint8 n, const U d)
00047 {
00048   x &= (T)(~(((1U << n) - 1) << s));
00049   x |= (T)(d << s);
00050   return x;
00051 }
00052 
00067 template<typename T, typename U> static inline T AB(T& x, const uint8 s, const uint8 n, const U i)
00068 {
00069   const T mask = (T)(((1U << n) - 1) << s);
00070   x = (T)((x & ~mask) | ((x + (i << s)) & mask));
00071   return x;
00072 }
00073 
00085 template<typename T> static inline bool HasBit(const T x, const uint8 y)
00086 {
00087   return (x & ((T)1U << y)) != 0;
00088 }
00089 
00100 #define HASBITS(x, y) ((x) & (y))
00101 
00113 template<typename T> static inline T SetBit(T& x, const uint8 y)
00114 {
00115   return x = (T)(x | (T)(1U << y));
00116 }
00117 
00128 #define SETBITS(x, y) ((x) |= (y))
00129 
00141 template<typename T> static inline T ClrBit(T& x, const uint8 y)
00142 {
00143   return x = (T)(x & ~((T)1U << y));
00144 }
00145 
00156 #define CLRBITS(x, y) ((x) &= ~(y))
00157 
00169 template<typename T> static inline T ToggleBit(T& x, const uint8 y)
00170 {
00171   return x = (T)(x ^ (T)(1U << y));
00172 }
00173 
00174 
00176 extern const uint8 _ffb_64[64];
00177 
00188 #define FIND_FIRST_BIT(x) _ffb_64[(x)]
00189 
00204 static inline uint8 FindFirstBit2x64(const int value)
00205 {
00206   if ((value & 0xFF) == 0) {
00207     return FIND_FIRST_BIT((value >> 8) & 0x3F) + 8;
00208   } else {
00209     return FIND_FIRST_BIT(value & 0x3F);
00210   }
00211 }
00212 
00213 uint8 FindFirstBit(uint32 x);
00214 uint8 FindLastBit(uint64 x);
00215 
00226 template<typename T> static inline T KillFirstBit(T value)
00227 {
00228   return value &= (T)(value - 1);
00229 }
00230 
00237 template<typename T> static inline uint CountBits(T value)
00238 {
00239   uint num;
00240 
00241   /* This loop is only called once for every bit set by clearing the lowest
00242    * bit in each loop. The number of bits is therefore equal to the number of
00243    * times the loop was called. It was found at the following website:
00244    * http://graphics.stanford.edu/~seander/bithacks.html */
00245 
00246   for (num = 0; value != 0; num++) {
00247     value &= (T)(value - 1);
00248   }
00249 
00250   return num;
00251 }
00252 
00261 template<typename T> static inline T ROL(const T x, const uint8 n)
00262 {
00263   return (T)(x << n | x >> (sizeof(x) * 8 - n));
00264 }
00265 
00274 template<typename T> static inline T ROR(const T x, const uint8 n)
00275 {
00276   return (T)(x >> n | x << (sizeof(x) * 8 - n));
00277 }
00278 
00290 #define FOR_EACH_SET_BIT(i, b)      \
00291   for (i = 0; b != 0; i++, b >>= 1) \
00292     if (b & 1)
00293 
00294 
00295 #if defined(__APPLE__)
00296   /* Make endian swapping use Apple's macros to increase speed
00297    * (since it will use hardware swapping if available).
00298    * Even though they should return uint16 and uint32, we get
00299    * warnings if we don't cast those (why?) */
00300   #define BSWAP32(x) ((uint32)Endian32_Swap(x))
00301   #define BSWAP16(x) ((uint16)Endian16_Swap(x))
00302 #else
00303 
00308   static inline uint32 BSWAP32(uint32 x)
00309   {
00310     return ((x >> 24) & 0xFF) | ((x >> 8) & 0xFF00) | ((x << 8) & 0xFF0000) | ((x << 24) & 0xFF000000);
00311   }
00312 
00318   static inline uint16 BSWAP16(uint16 x)
00319   {
00320     return (x >> 8) | (x << 8);
00321   }
00322 #endif /* __APPLE__ */
00323 
00324 #endif /* BITMATH_FUNC_HPP */

Generated on Mon Sep 22 20:34:15 2008 for openttd by  doxygen 1.5.6