1 /*****************************************************************************
2 * cpu.c: h264 encoder library
3 *****************************************************************************
4 * Copyright (C) 2003-2008 x264 project
6 * Authors: Loren Merritt <lorenm@u.washington.edu>
7 * Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
22 *****************************************************************************/
24 #if defined(HAVE_PTHREAD) && defined(SYS_LINUX)
29 #include <kernel/OS.h>
31 #if defined(SYS_MACOSX) || defined(SYS_FREEBSD)
32 #include <sys/types.h>
33 #include <sys/sysctl.h>
39 const x264_cpu_name_t x264_cpu_names[] = {
40 {"Altivec", X264_CPU_ALTIVEC},
41 // {"MMX", X264_CPU_MMX}, // we don't support asm on mmx1 cpus anymore
42 {"MMX2", X264_CPU_MMX|X264_CPU_MMXEXT},
43 {"MMXEXT", X264_CPU_MMX|X264_CPU_MMXEXT},
44 // {"SSE", X264_CPU_MMX|X264_CPU_MMXEXT|X264_CPU_SSE}, // there are no sse1 functions in x264
45 {"SSE2Slow",X264_CPU_MMX|X264_CPU_MMXEXT|X264_CPU_SSE|X264_CPU_SSE2|X264_CPU_SSE2_IS_SLOW},
46 {"SSE2", X264_CPU_MMX|X264_CPU_MMXEXT|X264_CPU_SSE|X264_CPU_SSE2},
47 {"SSE2Fast",X264_CPU_MMX|X264_CPU_MMXEXT|X264_CPU_SSE|X264_CPU_SSE2|X264_CPU_SSE2_IS_FAST},
48 {"SSE3", X264_CPU_MMX|X264_CPU_MMXEXT|X264_CPU_SSE|X264_CPU_SSE2|X264_CPU_SSE3},
49 {"SSSE3", X264_CPU_MMX|X264_CPU_MMXEXT|X264_CPU_SSE|X264_CPU_SSE2|X264_CPU_SSE3|X264_CPU_SSSE3},
50 {"PHADD", X264_CPU_MMX|X264_CPU_MMXEXT|X264_CPU_SSE|X264_CPU_SSE2|X264_CPU_SSE3|X264_CPU_SSSE3|X264_CPU_PHADD_IS_FAST},
51 {"SSE4", X264_CPU_MMX|X264_CPU_MMXEXT|X264_CPU_SSE|X264_CPU_SSE2|X264_CPU_SSE3|X264_CPU_SSSE3|X264_CPU_SSE4},
52 {"Cache32", X264_CPU_CACHELINE_32},
53 {"Cache64", X264_CPU_CACHELINE_64},
54 {"Slow_mod4_stack", X264_CPU_STACK_MOD4},
59 extern int x264_cpu_cpuid_test( void );
60 extern uint32_t x264_cpu_cpuid( uint32_t op, uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx );
62 uint32_t x264_cpu_detect( void )
65 uint32_t eax, ebx, ecx, edx;
66 uint32_t vendor[4] = {0};
71 if( !x264_cpu_cpuid_test() )
75 x264_cpu_cpuid( 0, &eax, vendor+0, vendor+2, vendor+1 );
79 x264_cpu_cpuid( 1, &eax, &ebx, &ecx, &edx );
85 cpu |= X264_CPU_MMXEXT|X264_CPU_SSE;
91 cpu |= X264_CPU_SSSE3;
95 if( cpu & X264_CPU_SSSE3 )
96 cpu |= X264_CPU_SSE2_IS_FAST;
97 if( cpu & X264_CPU_SSE4 )
98 cpu |= X264_CPU_PHADD_IS_FAST;
100 x264_cpu_cpuid( 0x80000000, &eax, &ebx, &ecx, &edx );
101 max_extended_cap = eax;
103 if( !strcmp((char*)vendor, "AuthenticAMD") && max_extended_cap >= 0x80000001 )
105 x264_cpu_cpuid( 0x80000001, &eax, &ebx, &ecx, &edx );
107 cpu |= X264_CPU_MMXEXT;
108 if( cpu & X264_CPU_SSE2 )
110 if( ecx&0x00000040 ) /* SSE4a */
111 cpu |= X264_CPU_SSE2_IS_FAST;
113 cpu |= X264_CPU_SSE2_IS_SLOW;
117 if( !strcmp((char*)vendor, "GenuineIntel") )
119 int family, model, stepping;
120 x264_cpu_cpuid( 1, &eax, &ebx, &ecx, &edx );
121 family = ((eax>>8)&0xf) + ((eax>>20)&0xff);
122 model = ((eax>>4)&0xf) + ((eax>>12)&0xf0);
124 /* 6/9 (pentium-m "banias"), 6/13 (pentium-m "dothan"), and 6/14 (core1 "yonah")
125 * theoretically support sse2, but it's significantly slower than mmx for
126 * almost all of x264's functions, so let's just pretend they don't. */
127 if( family==6 && (model==9 || model==13 || model==14) )
129 cpu &= ~(X264_CPU_SSE2|X264_CPU_SSE3);
130 assert(!(cpu&(X264_CPU_SSSE3|X264_CPU_SSE4)));
134 if( !strcmp((char*)vendor, "GenuineIntel") || !strcmp((char*)vendor, "CyrixInstead") )
136 /* cacheline size is specified in 3 places, any of which may be missing */
137 x264_cpu_cpuid( 1, &eax, &ebx, &ecx, &edx );
138 cache = (ebx&0xff00)>>5; // cflush size
139 if( !cache && max_extended_cap >= 0x80000006 )
141 x264_cpu_cpuid( 0x80000006, &eax, &ebx, &ecx, &edx );
142 cache = ecx&0xff; // cacheline size
146 // Cache and TLB Information
147 static const char cache32_ids[] = { 0x0a, 0x0c, 0x41, 0x42, 0x43, 0x44, 0x45, 0x82, 0x83, 0x84, 0x85, 0 };
148 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 };
152 x264_cpu_cpuid( 2, buf+0, buf+1, buf+2, buf+3 );
159 if( strchr( cache32_ids, buf[j]&0xff ) )
161 if( strchr( cache64_ids, buf[j]&0xff ) )
165 } while( ++i < max );
169 cpu |= X264_CPU_CACHELINE_32;
170 else if( cache == 64 )
171 cpu |= X264_CPU_CACHELINE_64;
173 fprintf( stderr, "x264 [warning]: unable to determine cacheline size\n" );
176 #ifdef BROKEN_STACK_ALIGNMENT
177 cpu |= X264_CPU_STACK_MOD4;
183 #elif defined( ARCH_PPC )
186 #include <sys/sysctl.h>
187 uint32_t x264_cpu_detect( void )
191 int selectors[2] = { CTL_HW, HW_VECTORUNIT };
193 size_t length = sizeof( has_altivec );
194 int error = sysctl( selectors, 2, &has_altivec, &length, NULL, 0 );
196 if( error == 0 && has_altivec != 0 )
198 cpu |= X264_CPU_ALTIVEC;
204 #elif defined( SYS_LINUX )
207 static sigjmp_buf jmpbuf;
208 static volatile sig_atomic_t canjump = 0;
210 static void sigill_handler( int sig )
214 signal( sig, SIG_DFL );
219 siglongjmp( jmpbuf, 1 );
222 uint32_t x264_cpu_detect( void )
224 static void (* oldsig)( int );
226 oldsig = signal( SIGILL, sigill_handler );
227 if( sigsetjmp( jmpbuf, 1 ) )
229 signal( SIGILL, oldsig );
234 asm volatile( "mtspr 256, %0\n\t"
240 signal( SIGILL, oldsig );
242 return X264_CPU_ALTIVEC;
248 uint32_t x264_cpu_detect( void )
256 void x264_emms( void )
262 int x264_cpu_num_processors( void )
264 #if !defined(HAVE_PTHREAD)
267 #elif defined(_WIN32)
268 return pthread_num_processors_np();
270 #elif defined(SYS_LINUX)
274 memset( &p_aff, 0, sizeof(p_aff) );
275 sched_getaffinity( 0, sizeof(p_aff), &p_aff );
276 for( np = 0, bit = 0; bit < sizeof(p_aff); bit++ )
277 np += (((uint8_t *)&p_aff)[bit / 8] >> (bit % 8)) & 1;
280 #elif defined(SYS_BEOS)
282 get_system_info( &info );
283 return info.cpu_count;
285 #elif defined(SYS_MACOSX) || defined(SYS_FREEBSD)
287 size_t length = sizeof( numberOfCPUs );
288 if( sysctlbyname("hw.ncpu", &numberOfCPUs, &length, NULL, 0) )