参考自:http://blog.csdn.net/eagler_hzh/article/details/6550841
其实要抽出的函数是x264\common\cpu.c里的void x264_cpu_detect(void),源文件
int x264_cpu_cpuid_test( void );
void x264_cpu_cpuid( uint32_t op, uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx );
void x264_cpu_xgetbv( uint32_t op, uint32_t *eax, uint32_t *edx );
uint32_t x264_cpu_detect( void )
{
uint32_t cpu = 0;
uint32_t eax, ebx, ecx, edx;
uint32_t vendor[4] = {0};
uint32_t max_extended_cap;
int cache;
#if !ARCH_X86_64
if( !x264_cpu_cpuid_test() )
return 0;
#endif
x264_cpu_cpuid( 0, &eax, vendor+0, vendor+2, vendor+1 );
if( eax == 0 )
return 0;
x264_cpu_cpuid( 1, &eax, &ebx, &ecx, &edx );
if( edx&0x00800000 )
cpu |= X264_CPU_MMX;
else
return 0;
if( edx&0x02000000 )
cpu |= X264_CPU_MMXEXT|X264_CPU_SSE;
if( edx&0x04000000 )
cpu |= X264_CPU_SSE2;
if( ecx&0x00000001 )
cpu |= X264_CPU_SSE3;
if( ecx&0x00000200 )
cpu |= X264_CPU_SSSE3;
if( ecx&0x00080000 )
cpu |= X264_CPU_SSE4;
if( ecx&0x00100000 )
cpu |= X264_CPU_SSE42;
/* Check OXSAVE and AVX bits */
if( (ecx&0x18000000) == 0x18000000 )
{
/* Check for OS support */
x264_cpu_xgetbv( 0, &eax, &edx );
if( (eax&0x6) == 0x6 )
cpu |= X264_CPU_AVX;
}
if( cpu & X264_CPU_SSSE3 )
cpu |= X264_CPU_SSE2_IS_FAST;
if( cpu & X264_CPU_SSE4 )
cpu |= X264_CPU_SHUFFLE_IS_FAST;
x264_cpu_cpuid( 0x80000000, &eax, &ebx, &ecx, &edx );
max_extended_cap = eax;
if( !strcmp((char*)vendor, "AuthenticAMD") && max_extended_cap >= 0x80000001 )
{
cpu |= X264_CPU_SLOW_CTZ;
x264_cpu_cpuid( 0x80000001, &eax, &ebx, &ecx, &edx );
if( edx&0x00400000 )
cpu |= X264_CPU_MMXEXT;
if( cpu & X264_CPU_SSE2 )
{
if( ecx&0x00000040 ) /* SSE4a */
{
cpu |= X264_CPU_SSE2_IS_FAST;
cpu |= X264_CPU_LZCNT;
cpu |= X264_CPU_SHUFFLE_IS_FAST;
cpu &= ~X264_CPU_SLOW_CTZ;
}
else
cpu |= X264_CPU_SSE2_IS_SLOW;
if( ecx&0x00000080 ) /* Misalign SSE */
{
cpu |= X264_CPU_SSE_MISALIGN;
x264_cpu_mask_misalign_sse();
}
}
}
if( !strcmp((char*)vendor, "GenuineIntel") )
{
x264_cpu_cpuid( 1, &eax, &ebx, &ecx, &edx );
int family = ((eax>>8)&0xf) + ((eax>>20)&0xff);
int model = ((eax>>4)&0xf) + ((eax>>12)&0xf0);
if( family == 6 )
{
/* 6/9 (pentium-m "banias"), 6/13 (pentium-m "dothan"), and 6/14 (core1 "yonah")
* theoretically support sse2, but it's significantly slower than mmx for
* almost all of x264's functions, so let's just pretend they don't. */
if( model == 9 || model == 13 || model == 14 )
{
cpu &= ~(X264_CPU_SSE2|X264_CPU_SSE3);
assert(!(cpu&(X264_CPU_SSSE3|X264_CPU_SSE4)));
}
/* Detect Atom CPU */
else if( model == 28 )
{
cpu |= X264_CPU_SLOW_ATOM;
cpu |= X264_CPU_SLOW_CTZ;
}
/* Some Penryns and Nehalems are pointlessly crippled (SSE4 disabled), so
* detect them here. */
else if( model >= 23 )
cpu |= X264_CPU_SHUFFLE_IS_FAST;
}
}
if( (!strcmp((char*)vendor, "GenuineIntel") || !strcmp((char*)vendor, "CyrixInstead")) && !(cpu&X264_CPU_SSE42))
{
/* cacheline size is specified in 3 places, any of which may be missing */
x264_cpu_cpuid( 1, &eax, &ebx, &ecx, &edx );
cache = (ebx&0xff00)>>5; // cflush size
if( !cache && max_extended_cap >= 0x80000006 )
{
x264_cpu_cpuid( 0x80000006, &eax, &ebx, &ecx, &edx );
cache = ecx&0xff; // cacheline size
}
if( !cache )
{
// Cache and TLB Information
static const char cache32_ids[] = { 0x0a, 0x0c, 0x41, 0x42, 0x43, 0x44, 0x45, 0x82, 0x83, 0x84, 0x85, 0 };
static const char cache64_ids[] = { 0x22, 0x23, 0x25, 0x29, 0x2c, 0x46, 0x47, 0x49, 0x60, 0x66, 0x67,
0x68, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7c, 0x7f, 0x86, 0x87, 0 };
uint32_t buf[4];
int max, i = 0;
do {
x264_cpu_cpuid( 2, buf+0, buf+1, buf+2, buf+3 );
max = buf[0]&0xff;
buf[0] &= ~0xff;
for( int j = 0; j < 4; j++ )
if( !(buf[j]>>31) )
while( buf[j] )
{
if( strchr( cache32_ids, buf[j]&0xff ) )
cache = 32;
if( strchr( cache64_ids, buf[j]&0xff ) )
cache = 64;
buf[j] >>= 8;
}
} while( ++i < max );
}
if( cache == 32 )
cpu |= X264_CPU_CACHELINE_32;
else if( cache == 64 )
cpu |= X264_CPU_CACHELINE_64;
else
x264_log( NULL, X264_LOG_WARNING, "unable to determine cacheline size\n" );
}
#if BROKEN_STACK_ALIGNMENT
cpu |= X264_CPU_STACK_MOD4;
#endif
return cpu;
}
int x264_cpu_cpuid_test( void );
void x264_cpu_cpuid( uint32_t op, uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx );
void x264_cpu_xgetbv( uint32_t op, uint32_t *eax, uint32_t *edx );
这三个函数实现是在asm里面,位置x264\common\x86\cpu-a.asm
而cpu-a.asm引用了x264\common\x86\x86inc.asm,直接把x86inc.asm的内容全部复制到cpu-a.asm里面。
然后把cpu-a.asm添加到vc工程,安装好nasm(http://www.nasm.us/),在vc解决方案文件列表里右键cpu-a.asm文件选择属性,如下设置:
我的是vc的对话框工程,x264里的是c函数,所以使用函数的时候加上extern "C"。cpu-a里的一个函数定义是
;-----------------------------------------------------------------------------
; void cpu_cpuid( int op, int *eax, int *ebx, int *ecx, int *edx )
;-----------------------------------------------------------------------------
cglobal cpu_cpuid, 5,7
push rbx
push r4
push r3
push r2
push r1
mov eax, r0d
cpuid
pop rsi
mov [rsi], eax
pop rsi
mov [rsi], ebx
pop rsi
mov [rsi], ecx
pop rsi
mov [rsi], edx
pop rbx
RET
但外面要使用则是void x264_cpu_cpuid(...),cglobal这个宏改了名字,最开头有%define program_name x264,反正最后是什么名字用dumpbin /all xxx.obj看看就清楚了。
调用:
//检测CPU代码
extern "C" void x264_cpu_cpuid( int op, int *eax, int *ebx, int *ecx, int *edx );
void CdialogDlg::OnBnClickedButtonCpuDec()
{
// TODO: 在此添加控件通知处理程序代码
int cpu = 0;
int eax, ebx, ecx, edx;
int vendor[4] = {0};
int max_extended_cap;
int cache;
x264_cpu_cpuid( 0, &eax, vendor+0, vendor+2, vendor+1 );
if( eax == 0 )
return ;
//继续操作
}
nasm -f win32 -DPREFIX cpu-a.asm
就出来cpu-a.obj了,然后使用vc提供的工具
lib cpu-a.obj
就出来cpu-a.lib了,然后把lib扔去vc工程里链接一下,在extern "C"声明一下函数也可以用了。