00001
00002
00003
00004
00005
00006
00007
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
00041
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 }