bitmath_func.cpp

Go to the documentation of this file.
00001 /* $Id: bitmath_func.cpp 17248 2009-08-21 20:21:05Z rubidium $ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
00008  */
00009 
00012 #include "../stdafx.h"
00013 #include "bitmath_func.hpp"
00014 
00015 const uint8 _ffb_64[64] = {
00016   0,  0,  1,  0,  2,  0,  1,  0,
00017   3,  0,  1,  0,  2,  0,  1,  0,
00018   4,  0,  1,  0,  2,  0,  1,  0,
00019   3,  0,  1,  0,  2,  0,  1,  0,
00020   5,  0,  1,  0,  2,  0,  1,  0,
00021   3,  0,  1,  0,  2,  0,  1,  0,
00022   4,  0,  1,  0,  2,  0,  1,  0,
00023   3,  0,  1,  0,  2,  0,  1,  0,
00024 };
00025 
00037 uint8 FindFirstBit(uint32 x)
00038 {
00039   if (x == 0) return 0;
00040   /* The macro FIND_FIRST_BIT is better to use when your x is
00041     not more than 128. */
00042 
00043   uint8 pos = 0;
00044 
00045   if ((x & 0x0000ffff) == 0) { x >>= 16; pos += 16; }
00046   if ((x & 0x000000ff) == 0) { x >>= 8;  pos += 8;  }
00047   if ((x & 0x0000000f) == 0) { x >>= 4;  pos += 4;  }
00048   if ((x & 0x00000003) == 0) { x >>= 2;  pos += 2;  }
00049   if ((x & 0x00000001) == 0) { pos += 1; }
00050 
00051   return pos;
00052 }
00053 
00065 uint8 FindLastBit(uint64 x)
00066 {
00067   if (x == 0) return 0;
00068 
00069   uint8 pos = 0;
00070 
00071   if ((x & 0xffffffff00000000ULL) != 0) { x >>= 32; pos += 32; }
00072   if ((x & 0x00000000ffff0000ULL) != 0) { x >>= 16; pos += 16; }
00073   if ((x & 0x000000000000ff00ULL) != 0) { x >>= 8;  pos += 8;  }
00074   if ((x & 0x00000000000000f0ULL) != 0) { x >>= 4;  pos += 4;  }
00075   if ((x & 0x000000000000000cULL) != 0) { x >>= 2;  pos += 2;  }
00076   if ((x & 0x0000000000000002ULL) != 0) { pos += 1; }
00077 
00078   return pos;
00079 }

Generated on Tue Jan 5 21:02:53 2010 for OpenTTD by  doxygen 1.5.6