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