JD2022-TU1/main/extern/Camcam/CCMath/BitCounter.cpp

116 lines
2.8 KiB
C++

//#include "win.h"
#include "CCMath.h"
unsigned char Bitnum[4096];
unsigned char Bitrev[256];
unsigned char BitContract[256];
unsigned short BitExpand[256];
void ComputeBitNum()
{
unsigned int Counter;
Counter = 4096;
while (Counter --)
{
unsigned int C2,Cr,Ce,Cc;
C2 = Counter;
Bitnum[Counter] = 0;
while (C2)
{
Bitnum[Counter] += C2 & 1;
C2>>=1;
}
if (Counter < 256)
{
C2 = Counter;
Ce = Cc = Cr = 0;
for (int i = 0 ; i < 8 ; i ++)
{
Cc <<= i & 1;
Ce <<= 2;
Cr <<= 1;
Cr |= C2 & 1;
Ce |= C2 & 1;
Cc |= C2 & 1;
C2>>=1;
}
Bitrev[Counter] = Cr;
BitExpand[Counter] = Ce;
BitContract[Counter] = Cc;
}
}
}
/* MIT Model !! */
unsigned int GetBitNumberMIT(unsigned int n)
{
// works for 32-bit numbers only
// fix last line for 64-bit numbers
register unsigned int tmp;
tmp = n - ((n >> 1) & 033333333333) - ((n >> 2) & 011111111111);
return ((tmp + (tmp >> 3)) & 030707070707) % 63;
}//*/
unsigned int GetBitNumberT(unsigned int Value)
{
return Bitnum[Value & 0xfff] + Bitnum[(Value>>12) & 0xfff] + Bitnum[(Value>>24) & 0xfff];
}//*/
unsigned int GetBitNumber(unsigned int n)
{
register unsigned int tmpA;
tmpA = (n & 0x55555555) + ((n>>1) & 0x55555555);
tmpA = (tmpA & 0x33333333) + ((tmpA>>2) & 0x33333333);
tmpA = (tmpA & 0x0f0f0f0f) + ((tmpA>>4) & 0x0f0f0f0f);
tmpA = (tmpA & 0x00ff00ff) + ((tmpA>>8) & 0x00ff00ff);
tmpA = (tmpA & 0x0000ffff) + ((tmpA>>16) & 0x0000ffff);
return tmpA;
}//*/
unsigned int GetBitReverse(unsigned int Value)
{
return ((unsigned int)(Bitrev[Value & 0xff])<<24) +
((unsigned int)(Bitrev[(Value >> 8)& 0xff])<<16) +
((unsigned int)(Bitrev[(Value >> 16)& 0xff])<<8) +
((unsigned int)(Bitrev[(Value >> 24)& 0xff])<<0);
}
unsigned int GetBitExpand(unsigned int Value) // Transform 0xFFFF into 0x55555555
{
return ((unsigned int)(BitExpand[Value & 0xff])<<16) +
((unsigned int)(BitExpand[(Value >> 8)& 0xff])<<0);
}
unsigned int GetBitContract(unsigned int Value) // Transform 0x55555555 into 0xFFFF
{
return ((unsigned int)(BitContract[Value & 0xff])<<12) +
((unsigned int)(BitContract[(Value >> 8)& 0xff])<<8) +
((unsigned int)(BitContract[(Value >> 16)& 0xff])<<4) +
((unsigned int)(BitContract[(Value >> 24)& 0xff])<<0);
}
int GetLogUpperPO2(float Value)
{
unsigned int Local;
*(float *)&Local = Value;
Local &= 0x7fffffff;
return (((Local-1) >> 23) - 126);
}
int GetUpperPO2(float Value)
{
return 1<<GetLogUpperPO2(Value);
}
int GetFirstBit(unsigned int Mask)
{
unsigned int Index;
if (!Mask) return -1;
Index = 0;
if ((Mask & 0xffff) == 0) {Mask >>= 16;Index+=16;};
if ((Mask & 0xff) == 0) {Mask >>= 8;Index+=8;};
if ((Mask & 0xf) == 0) {Mask >>= 4;Index+=4;};
if ((Mask & 0x3) == 0) {Mask >>= 2;Index+=2;};
if ((Mask & 0x1) == 0) {Mask >>= 1;Index+=1;};
return Index;
}