bitmath_func.cpp

Go to the documentation of this file.
00001 /* $Id: bitmath_func.cpp 15299 2009-01-31 20:16:06Z smatz $ */
00002 
00005 #include "../stdafx.h"
00006 #include "bitmath_func.hpp"
00007 
00008 const uint8 _ffb_64[64] = {
00009   0,  0,  1,  0,  2,  0,  1,  0,
00010   3,  0,  1,  0,  2,  0,  1,  0,
00011   4,  0,  1,  0,  2,  0,  1,  0,
00012   3,  0,  1,  0,  2,  0,  1,  0,
00013   5,  0,  1,  0,  2,  0,  1,  0,
00014   3,  0,  1,  0,  2,  0,  1,  0,
00015   4,  0,  1,  0,  2,  0,  1,  0,
00016   3,  0,  1,  0,  2,  0,  1,  0,
00017 };
00018 
00030 uint8 FindFirstBit(uint32 x)
00031 {
00032   if (x == 0) return 0;
00033   /* The macro FIND_FIRST_BIT is better to use when your x is
00034     not more than 128. */
00035 
00036   uint8 pos = 0;
00037 
00038   if ((x & 0x0000ffff) == 0) { x >>= 16; pos += 16; }
00039   if ((x & 0x000000ff) == 0) { x >>= 8;  pos += 8;  }
00040   if ((x & 0x0000000f) == 0) { x >>= 4;  pos += 4;  }
00041   if ((x & 0x00000003) == 0) { x >>= 2;  pos += 2;  }
00042   if ((x & 0x00000001) == 0) { pos += 1; }
00043 
00044   return pos;
00045 }
00046 
00058 uint8 FindLastBit(uint64 x)
00059 {
00060   if (x == 0) return 0;
00061 
00062   uint8 pos = 0;
00063 
00064   if ((x & 0xffffffff00000000ULL) != 0) { x >>= 32; pos += 32; }
00065   if ((x & 0x00000000ffff0000ULL) != 0) { x >>= 16; pos += 16; }
00066   if ((x & 0x000000000000ff00ULL) != 0) { x >>= 8;  pos += 8;  }
00067   if ((x & 0x00000000000000f0ULL) != 0) { x >>= 4;  pos += 4;  }
00068   if ((x & 0x000000000000000cULL) != 0) { x >>= 2;  pos += 2;  }
00069   if ((x & 0x0000000000000002ULL) != 0) { pos += 1; }
00070 
00071   return pos;
00072 }

Generated on Thu Sep 24 19:35:01 2009 for OpenTTD by  doxygen 1.5.6