]> git.sesse.net Git - x264/blob - common/cpu.c
Add speedcontrol file.
[x264] / common / cpu.c
1 /*****************************************************************************
2  * cpu.c: cpu detection
3  *****************************************************************************
4  * Copyright (C) 2003-2016 x264 project
5  *
6  * Authors: Loren Merritt <lorenm@u.washington.edu>
7  *          Laurent Aimar <fenrir@via.ecp.fr>
8  *          Fiona Glaser <fiona@x264.com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
23  *
24  * This program is also available under a commercial proprietary license.
25  * For more information, contact us at licensing@x264.com.
26  *****************************************************************************/
27
28 #include "common.h"
29
30 #if HAVE_POSIXTHREAD && SYS_LINUX
31 #include <sched.h>
32 #endif
33 #if SYS_BEOS
34 #include <kernel/OS.h>
35 #endif
36 #if SYS_MACOSX || SYS_FREEBSD
37 #include <sys/types.h>
38 #include <sys/sysctl.h>
39 #endif
40 #if SYS_OPENBSD
41 #include <sys/param.h>
42 #include <sys/sysctl.h>
43 #include <machine/cpu.h>
44 #endif
45
46 const x264_cpu_name_t x264_cpu_names[] =
47 {
48 #if HAVE_MMX
49 //  {"MMX",         X264_CPU_MMX},  // we don't support asm on mmx1 cpus anymore
50 //  {"CMOV",        X264_CPU_CMOV}, // we require this unconditionally, so don't print it
51 #define MMX2 X264_CPU_MMX|X264_CPU_MMX2|X264_CPU_CMOV
52     {"MMX2",        MMX2},
53     {"MMXEXT",      MMX2},
54     {"SSE",         MMX2|X264_CPU_SSE},
55 #define SSE2 MMX2|X264_CPU_SSE|X264_CPU_SSE2
56     {"SSE2Slow",    SSE2|X264_CPU_SSE2_IS_SLOW},
57     {"SSE2",        SSE2},
58     {"SSE2Fast",    SSE2|X264_CPU_SSE2_IS_FAST},
59     {"SSE3",        SSE2|X264_CPU_SSE3},
60     {"SSSE3",       SSE2|X264_CPU_SSE3|X264_CPU_SSSE3},
61     {"SSE4.1",      SSE2|X264_CPU_SSE3|X264_CPU_SSSE3|X264_CPU_SSE4},
62     {"SSE4",        SSE2|X264_CPU_SSE3|X264_CPU_SSSE3|X264_CPU_SSE4},
63     {"SSE4.2",      SSE2|X264_CPU_SSE3|X264_CPU_SSSE3|X264_CPU_SSE4|X264_CPU_SSE42},
64 #define AVX SSE2|X264_CPU_SSE3|X264_CPU_SSSE3|X264_CPU_SSE4|X264_CPU_SSE42|X264_CPU_AVX
65     {"AVX",         AVX},
66     {"XOP",         AVX|X264_CPU_XOP},
67     {"FMA4",        AVX|X264_CPU_FMA4},
68     {"FMA3",        AVX|X264_CPU_FMA3},
69     {"AVX2",        AVX|X264_CPU_FMA3|X264_CPU_AVX2},
70 #undef AVX
71 #undef SSE2
72 #undef MMX2
73     {"Cache32",         X264_CPU_CACHELINE_32},
74     {"Cache64",         X264_CPU_CACHELINE_64},
75     {"LZCNT",           X264_CPU_LZCNT},
76     {"BMI1",            X264_CPU_BMI1},
77     {"BMI2",            X264_CPU_BMI1|X264_CPU_BMI2},
78     {"SlowCTZ",         X264_CPU_SLOW_CTZ},
79     {"SlowAtom",        X264_CPU_SLOW_ATOM},
80     {"SlowPshufb",      X264_CPU_SLOW_PSHUFB},
81     {"SlowPalignr",     X264_CPU_SLOW_PALIGNR},
82     {"SlowShuffle",     X264_CPU_SLOW_SHUFFLE},
83     {"UnalignedStack",  X264_CPU_STACK_MOD4},
84 #elif ARCH_PPC
85     {"Altivec",         X264_CPU_ALTIVEC},
86 #elif ARCH_ARM
87     {"ARMv6",           X264_CPU_ARMV6},
88     {"NEON",            X264_CPU_NEON},
89     {"FastNeonMRC",     X264_CPU_FAST_NEON_MRC},
90 #elif ARCH_AARCH64
91     {"ARMv8",           X264_CPU_ARMV8},
92     {"NEON",            X264_CPU_NEON},
93 #elif ARCH_MIPS
94     {"MSA",             X264_CPU_MSA},
95 #endif
96     {"", 0},
97 };
98
99 #if (ARCH_PPC && SYS_LINUX) || (ARCH_ARM && !HAVE_NEON)
100 #include <signal.h>
101 #include <setjmp.h>
102 static sigjmp_buf jmpbuf;
103 static volatile sig_atomic_t canjump = 0;
104
105 static void sigill_handler( int sig )
106 {
107     if( !canjump )
108     {
109         signal( sig, SIG_DFL );
110         raise( sig );
111     }
112
113     canjump = 0;
114     siglongjmp( jmpbuf, 1 );
115 }
116 #endif
117
118 #if HAVE_MMX
119 int x264_cpu_cpuid_test( void );
120 void x264_cpu_cpuid( uint32_t op, uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx );
121 void x264_cpu_xgetbv( uint32_t op, uint32_t *eax, uint32_t *edx );
122
123 uint32_t x264_cpu_detect( void )
124 {
125     uint32_t cpu = 0;
126     uint32_t eax, ebx, ecx, edx;
127     uint32_t vendor[4] = {0};
128     uint32_t max_extended_cap, max_basic_cap;
129     int cache;
130
131 #if !ARCH_X86_64
132     if( !x264_cpu_cpuid_test() )
133         return 0;
134 #endif
135
136     x264_cpu_cpuid( 0, &eax, vendor+0, vendor+2, vendor+1 );
137     max_basic_cap = eax;
138     if( max_basic_cap == 0 )
139         return 0;
140
141     x264_cpu_cpuid( 1, &eax, &ebx, &ecx, &edx );
142     if( edx&0x00800000 )
143         cpu |= X264_CPU_MMX;
144     else
145         return cpu;
146     if( edx&0x02000000 )
147         cpu |= X264_CPU_MMX2|X264_CPU_SSE;
148     if( edx&0x00008000 )
149         cpu |= X264_CPU_CMOV;
150     else
151         return cpu;
152     if( edx&0x04000000 )
153         cpu |= X264_CPU_SSE2;
154     if( ecx&0x00000001 )
155         cpu |= X264_CPU_SSE3;
156     if( ecx&0x00000200 )
157         cpu |= X264_CPU_SSSE3;
158     if( ecx&0x00080000 )
159         cpu |= X264_CPU_SSE4;
160     if( ecx&0x00100000 )
161         cpu |= X264_CPU_SSE42;
162     /* Check OXSAVE and AVX bits */
163     if( (ecx&0x18000000) == 0x18000000 )
164     {
165         /* Check for OS support */
166         x264_cpu_xgetbv( 0, &eax, &edx );
167         if( (eax&0x6) == 0x6 )
168         {
169             cpu |= X264_CPU_AVX;
170             if( ecx&0x00001000 )
171                 cpu |= X264_CPU_FMA3;
172         }
173     }
174
175     if( max_basic_cap >= 7 )
176     {
177         x264_cpu_cpuid( 7, &eax, &ebx, &ecx, &edx );
178         /* AVX2 requires OS support, but BMI1/2 don't. */
179         if( (cpu&X264_CPU_AVX) && (ebx&0x00000020) )
180             cpu |= X264_CPU_AVX2;
181         if( ebx&0x00000008 )
182         {
183             cpu |= X264_CPU_BMI1;
184             if( ebx&0x00000100 )
185                 cpu |= X264_CPU_BMI2;
186         }
187     }
188
189     if( cpu & X264_CPU_SSSE3 )
190         cpu |= X264_CPU_SSE2_IS_FAST;
191
192     x264_cpu_cpuid( 0x80000000, &eax, &ebx, &ecx, &edx );
193     max_extended_cap = eax;
194
195     if( max_extended_cap >= 0x80000001 )
196     {
197         x264_cpu_cpuid( 0x80000001, &eax, &ebx, &ecx, &edx );
198
199         if( ecx&0x00000020 )
200             cpu |= X264_CPU_LZCNT;             /* Supported by Intel chips starting with Haswell */
201         if( ecx&0x00000040 ) /* SSE4a, AMD only */
202         {
203             int family = ((eax>>8)&0xf) + ((eax>>20)&0xff);
204             cpu |= X264_CPU_SSE2_IS_FAST;      /* Phenom and later CPUs have fast SSE units */
205             if( family == 0x14 )
206             {
207                 cpu &= ~X264_CPU_SSE2_IS_FAST; /* SSSE3 doesn't imply fast SSE anymore... */
208                 cpu |= X264_CPU_SSE2_IS_SLOW;  /* Bobcat has 64-bit SIMD units */
209                 cpu |= X264_CPU_SLOW_PALIGNR;  /* palignr is insanely slow on Bobcat */
210             }
211             if( family == 0x16 )
212             {
213                 cpu |= X264_CPU_SLOW_PSHUFB;   /* Jaguar's pshufb isn't that slow, but it's slow enough
214                                                 * compared to alternate instruction sequences that this
215                                                 * is equal or faster on almost all such functions. */
216             }
217         }
218
219         if( cpu & X264_CPU_AVX )
220         {
221             if( ecx&0x00000800 ) /* XOP */
222                 cpu |= X264_CPU_XOP;
223             if( ecx&0x00010000 ) /* FMA4 */
224                 cpu |= X264_CPU_FMA4;
225         }
226
227         if( !strcmp((char*)vendor, "AuthenticAMD") )
228         {
229             if( edx&0x00400000 )
230                 cpu |= X264_CPU_MMX2;
231             if( !(cpu&X264_CPU_LZCNT) )
232                 cpu |= X264_CPU_SLOW_CTZ;
233             if( (cpu&X264_CPU_SSE2) && !(cpu&X264_CPU_SSE2_IS_FAST) )
234                 cpu |= X264_CPU_SSE2_IS_SLOW; /* AMD CPUs come in two types: terrible at SSE and great at it */
235         }
236     }
237
238     if( !strcmp((char*)vendor, "GenuineIntel") )
239     {
240         x264_cpu_cpuid( 1, &eax, &ebx, &ecx, &edx );
241         int family = ((eax>>8)&0xf) + ((eax>>20)&0xff);
242         int model  = ((eax>>4)&0xf) + ((eax>>12)&0xf0);
243         if( family == 6 )
244         {
245             /* 6/9 (pentium-m "banias"), 6/13 (pentium-m "dothan"), and 6/14 (core1 "yonah")
246              * theoretically support sse2, but it's significantly slower than mmx for
247              * almost all of x264's functions, so let's just pretend they don't. */
248             if( model == 9 || model == 13 || model == 14 )
249             {
250                 cpu &= ~(X264_CPU_SSE2|X264_CPU_SSE3);
251                 assert(!(cpu&(X264_CPU_SSSE3|X264_CPU_SSE4)));
252             }
253             /* Detect Atom CPU */
254             else if( model == 28 )
255             {
256                 cpu |= X264_CPU_SLOW_ATOM;
257                 cpu |= X264_CPU_SLOW_CTZ;
258                 cpu |= X264_CPU_SLOW_PSHUFB;
259             }
260             /* Conroe has a slow shuffle unit. Check the model number to make sure not
261              * to include crippled low-end Penryns and Nehalems that don't have SSE4. */
262             else if( (cpu&X264_CPU_SSSE3) && !(cpu&X264_CPU_SSE4) && model < 23 )
263                 cpu |= X264_CPU_SLOW_SHUFFLE;
264         }
265     }
266
267     if( (!strcmp((char*)vendor, "GenuineIntel") || !strcmp((char*)vendor, "CyrixInstead")) && !(cpu&X264_CPU_SSE42))
268     {
269         /* cacheline size is specified in 3 places, any of which may be missing */
270         x264_cpu_cpuid( 1, &eax, &ebx, &ecx, &edx );
271         cache = (ebx&0xff00)>>5; // cflush size
272         if( !cache && max_extended_cap >= 0x80000006 )
273         {
274             x264_cpu_cpuid( 0x80000006, &eax, &ebx, &ecx, &edx );
275             cache = ecx&0xff; // cacheline size
276         }
277         if( !cache && max_basic_cap >= 2 )
278         {
279             // Cache and TLB Information
280             static const char cache32_ids[] = { 0x0a, 0x0c, 0x41, 0x42, 0x43, 0x44, 0x45, 0x82, 0x83, 0x84, 0x85, 0 };
281             static const char cache64_ids[] = { 0x22, 0x23, 0x25, 0x29, 0x2c, 0x46, 0x47, 0x49, 0x60, 0x66, 0x67,
282                                                 0x68, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7c, 0x7f, 0x86, 0x87, 0 };
283             uint32_t buf[4];
284             int max, i = 0;
285             do {
286                 x264_cpu_cpuid( 2, buf+0, buf+1, buf+2, buf+3 );
287                 max = buf[0]&0xff;
288                 buf[0] &= ~0xff;
289                 for( int j = 0; j < 4; j++ )
290                     if( !(buf[j]>>31) )
291                         while( buf[j] )
292                         {
293                             if( strchr( cache32_ids, buf[j]&0xff ) )
294                                 cache = 32;
295                             if( strchr( cache64_ids, buf[j]&0xff ) )
296                                 cache = 64;
297                             buf[j] >>= 8;
298                         }
299             } while( ++i < max );
300         }
301
302         if( cache == 32 )
303             cpu |= X264_CPU_CACHELINE_32;
304         else if( cache == 64 )
305             cpu |= X264_CPU_CACHELINE_64;
306         else
307             x264_log( NULL, X264_LOG_WARNING, "unable to determine cacheline size\n" );
308     }
309
310 #if STACK_ALIGNMENT < 16
311     cpu |= X264_CPU_STACK_MOD4;
312 #endif
313
314     return cpu;
315 }
316
317 #elif ARCH_PPC && HAVE_ALTIVEC
318
319 #if SYS_MACOSX || SYS_OPENBSD || SYS_FREEBSD
320 #include <sys/sysctl.h>
321 uint32_t x264_cpu_detect( void )
322 {
323     /* Thank you VLC */
324     uint32_t cpu = 0;
325 #if SYS_OPENBSD
326     int      selectors[2] = { CTL_MACHDEP, CPU_ALTIVEC };
327 #elif SYS_MACOSX
328     int      selectors[2] = { CTL_HW, HW_VECTORUNIT };
329 #endif
330     int      has_altivec = 0;
331     size_t   length = sizeof( has_altivec );
332 #if SYS_MACOSX || SYS_OPENBSD
333     int      error = sysctl( selectors, 2, &has_altivec, &length, NULL, 0 );
334 #else
335     int      error = sysctlbyname( "hw.altivec", &has_altivec, &length, NULL, 0 );
336 #endif
337
338     if( error == 0 && has_altivec != 0 )
339         cpu |= X264_CPU_ALTIVEC;
340
341     return cpu;
342 }
343
344 #elif SYS_LINUX
345
346 uint32_t x264_cpu_detect( void )
347 {
348 #ifdef __NO_FPRS__
349     return 0;
350 #else
351     static void (*oldsig)( int );
352
353     oldsig = signal( SIGILL, sigill_handler );
354     if( sigsetjmp( jmpbuf, 1 ) )
355     {
356         signal( SIGILL, oldsig );
357         return 0;
358     }
359
360     canjump = 1;
361     asm volatile( "mtspr 256, %0\n\t"
362                   "vand 0, 0, 0\n\t"
363                   :
364                   : "r"(-1) );
365     canjump = 0;
366
367     signal( SIGILL, oldsig );
368
369     return X264_CPU_ALTIVEC;
370 #endif
371 }
372 #endif
373
374 #elif ARCH_ARM
375
376 void x264_cpu_neon_test( void );
377 int x264_cpu_fast_neon_mrc_test( void );
378
379 uint32_t x264_cpu_detect( void )
380 {
381     int flags = 0;
382 #if HAVE_ARMV6
383     flags |= X264_CPU_ARMV6;
384
385     // don't do this hack if compiled with -mfpu=neon
386 #if !HAVE_NEON
387     static void (* oldsig)( int );
388     oldsig = signal( SIGILL, sigill_handler );
389     if( sigsetjmp( jmpbuf, 1 ) )
390     {
391         signal( SIGILL, oldsig );
392         return flags;
393     }
394
395     canjump = 1;
396     x264_cpu_neon_test();
397     canjump = 0;
398     signal( SIGILL, oldsig );
399 #endif
400
401     flags |= X264_CPU_NEON;
402
403     // fast neon -> arm (Cortex-A9) detection relies on user access to the
404     // cycle counter; this assumes ARMv7 performance counters.
405     // NEON requires at least ARMv7, ARMv8 may require changes here, but
406     // hopefully this hacky detection method will have been replaced by then.
407     // Note that there is potential for a race condition if another program or
408     // x264 instance disables or reinits the counters while x264 is using them,
409     // which may result in incorrect detection and the counters stuck enabled.
410     // right now Apple does not seem to support performance counters for this test
411 #ifndef __MACH__
412     flags |= x264_cpu_fast_neon_mrc_test() ? X264_CPU_FAST_NEON_MRC : 0;
413 #endif
414     // TODO: write dual issue test? currently it's A8 (dual issue) vs. A9 (fast mrc)
415 #endif
416     return flags;
417 }
418
419 #elif ARCH_AARCH64
420
421 uint32_t x264_cpu_detect( void )
422 {
423     return X264_CPU_ARMV8 | X264_CPU_NEON;
424 }
425
426 #elif ARCH_MIPS
427
428 uint32_t x264_cpu_detect( void )
429 {
430     uint32_t flags = 0;
431 #if HAVE_MSA
432     flags |= X264_CPU_MSA;
433 #endif
434     return flags;
435 }
436
437 #else
438
439 uint32_t x264_cpu_detect( void )
440 {
441     return 0;
442 }
443
444 #endif
445
446 int x264_cpu_num_processors( void )
447 {
448 #if !HAVE_THREAD
449     return 1;
450
451 #elif SYS_WINDOWS
452     return x264_pthread_num_processors_np();
453
454 #elif SYS_CYGWIN || SYS_SunOS
455     return sysconf( _SC_NPROCESSORS_ONLN );
456
457 #elif SYS_LINUX
458 #ifdef __ANDROID__
459     // Android NDK does not expose sched_getaffinity
460     return sysconf( _SC_NPROCESSORS_CONF );
461 #else
462     cpu_set_t p_aff;
463     memset( &p_aff, 0, sizeof(p_aff) );
464     if( sched_getaffinity( 0, sizeof(p_aff), &p_aff ) )
465         return 1;
466 #if HAVE_CPU_COUNT
467     return CPU_COUNT(&p_aff);
468 #else
469     int np = 0;
470     for( unsigned int bit = 0; bit < 8 * sizeof(p_aff); bit++ )
471         np += (((uint8_t *)&p_aff)[bit / 8] >> (bit % 8)) & 1;
472     return np;
473 #endif
474 #endif
475
476 #elif SYS_BEOS
477     system_info info;
478     get_system_info( &info );
479     return info.cpu_count;
480
481 #elif SYS_MACOSX || SYS_FREEBSD || SYS_OPENBSD
482     int ncpu;
483     size_t length = sizeof( ncpu );
484 #if SYS_OPENBSD
485     int mib[2] = { CTL_HW, HW_NCPU };
486     if( sysctl(mib, 2, &ncpu, &length, NULL, 0) )
487 #else
488     if( sysctlbyname("hw.ncpu", &ncpu, &length, NULL, 0) )
489 #endif
490     {
491         ncpu = 1;
492     }
493     return ncpu;
494
495 #else
496     return 1;
497 #endif
498 }