| 
注册时间2008-2-6最后登录1970-1-1威望 星金币 枚贡献 分经验 点鲜花 朵魅力 点上传 次下载 次积分51605在线时间 小时 
 | 
 
 楼主|
发表于 2008-4-12 14:10:43
|
显示全部楼层 
| #include 
#include 
#define TestCount 100000000
__declspec (naked)
DWORD __fastcall bitInv(DWORD b, DWORD n)
{
  __asm
  {	
  bswap ecx
  mov eax, ecx
  and ecx, 0x55555555
  and eax, 0xAAAAAAAA
  shl ecx, 1
  shr eax, 1
  or ecx, eax
  mov eax, ecx
  and ecx, 0x33333333
  and eax, 0xCCCCCCCC
  shl ecx, 2
  shr eax, 2
  or ecx, eax
  mov eax, ecx
  and ecx, 0x0F0F0F0F
  and eax, 0xF0F0F0F0
  shl ecx, 4
  shr eax, 4
  or eax, ecx
  mov ecx, 32
  sub ecx, edx
  shr eax, cl
  ret
  }
}
int main(void)
{
	UINT64 s_u64Frequency = 1;
    UINT64 s_u64Start, s_u64End;
	unsigned long i, TestSize;
	SetThreadPriority(GetCurrentThread(),   THREAD_PRIORITY_TIME_CRITICAL); 
	QueryPerformanceFrequency((LARGE_INTEGER *)&s_u64Frequency );
	QueryPerformanceCounter((LARGE_INTEGER *)&s_u64Start );
	for (i = 0; i < TestCount; i ++)
		bitInv(0x12345678, 31);
	QueryPerformanceCounter((LARGE_INTEGER *)&s_u64End );
	printf( "bitInv Elapsed time: %.3f ms\n",
		(double)(( s_u64End - s_u64Start ) * 1000.0 / (double)s_u64Frequency ));
	return 0;
}
//调试好了
//1050ms/1亿次
//全使用逻辑指令为1750ms
//证明BSWAP有很好效果
合21时钟/次
感觉有极大的优化余地 | 
 |