]> git.sesse.net Git - x264/blob - common/cpu.c
762f3c88ab2b91a265962660c5faeac1bba18d89
[x264] / common / cpu.c
1 /*****************************************************************************
2  * cpu.c: h264 encoder library
3  *****************************************************************************
4  * Copyright (C) 2003-2008 x264 project
5  *
6  * Authors: Loren Merritt <lorenm@u.washington.edu>
7  *          Laurent Aimar <fenrir@via.ecp.fr>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 #if defined(HAVE_PTHREAD) && defined(SYS_LINUX)
25 #define _GNU_SOURCE
26 #include <sched.h>
27 #endif
28 #ifdef SYS_BEOS
29 #include <kernel/OS.h>
30 #endif
31 #if defined(SYS_MACOSX) || defined(SYS_FREEBSD)
32 #include <sys/types.h>
33 #include <sys/sysctl.h>
34 #endif
35
36 #include "common.h"
37 #include "cpu.h"
38
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.1",  X264_CPU_MMX|X264_CPU_MMXEXT|X264_CPU_SSE|X264_CPU_SSE2|X264_CPU_SSE3|X264_CPU_SSSE3|X264_CPU_SSE4},
52     {"SSE4.2",  X264_CPU_MMX|X264_CPU_MMXEXT|X264_CPU_SSE|X264_CPU_SSE2|X264_CPU_SSE3|X264_CPU_SSSE3|X264_CPU_SSE4|X264_CPU_SSE42},
53     {"Cache32", X264_CPU_CACHELINE_32},
54     {"Cache64", X264_CPU_CACHELINE_64},
55     {"SSEMisalign", X264_CPU_SSE_MISALIGN},
56     {"Slow_mod4_stack", X264_CPU_STACK_MOD4},
57     {"", 0},
58 };
59
60
61 #ifdef HAVE_MMX
62 extern int  x264_cpu_cpuid_test( void );
63 extern uint32_t  x264_cpu_cpuid( uint32_t op, uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx );
64
65 uint32_t x264_cpu_detect( void )
66 {
67     uint32_t cpu = 0;
68     uint32_t eax, ebx, ecx, edx;
69     uint32_t vendor[4] = {0};
70     int max_extended_cap;
71     int cache;
72
73 #ifndef ARCH_X86_64
74     if( !x264_cpu_cpuid_test() )
75         return 0;
76 #endif
77
78     x264_cpu_cpuid( 0, &eax, vendor+0, vendor+2, vendor+1 );
79     if( eax == 0 )
80         return 0;
81
82     x264_cpu_cpuid( 1, &eax, &ebx, &ecx, &edx );
83     if( edx&0x00800000 )
84         cpu |= X264_CPU_MMX;
85     else
86         return 0;
87     if( edx&0x02000000 )
88         cpu |= X264_CPU_MMXEXT|X264_CPU_SSE;
89     if( edx&0x04000000 )
90         cpu |= X264_CPU_SSE2;
91     if( ecx&0x00000001 )
92         cpu |= X264_CPU_SSE3;
93     if( ecx&0x00000200 )
94         cpu |= X264_CPU_SSSE3;
95     if( ecx&0x00080000 )
96         cpu |= X264_CPU_SSE4;
97     if( ecx&0x00100000 )
98         cpu |= X264_CPU_SSE42;
99
100     if( cpu & X264_CPU_SSSE3 )
101         cpu |= X264_CPU_SSE2_IS_FAST;
102     if( cpu & X264_CPU_SSE4 )
103         cpu |= X264_CPU_PHADD_IS_FAST;
104
105     x264_cpu_cpuid( 0x80000000, &eax, &ebx, &ecx, &edx );
106     max_extended_cap = eax;
107
108     if( !strcmp((char*)vendor, "AuthenticAMD") && max_extended_cap >= 0x80000001 )
109     {
110         x264_cpu_cpuid( 0x80000001, &eax, &ebx, &ecx, &edx );
111         if( edx&0x00400000 )
112             cpu |= X264_CPU_MMXEXT;
113         if( cpu & X264_CPU_SSE2 )
114         {
115             if( ecx&0x00000040 ) /* SSE4a */
116             {
117                 cpu |= X264_CPU_SSE2_IS_FAST;
118                 cpu |= X264_CPU_SSE_MISALIGN;
119                 x264_cpu_mask_misalign_sse();
120             }
121             else
122                 cpu |= X264_CPU_SSE2_IS_SLOW;
123         }
124     }
125
126     if( !strcmp((char*)vendor, "GenuineIntel") )
127     {
128         int family, model, stepping;
129         x264_cpu_cpuid( 1, &eax, &ebx, &ecx, &edx );
130         family = ((eax>>8)&0xf) + ((eax>>20)&0xff);
131         model  = ((eax>>4)&0xf) + ((eax>>12)&0xf0);
132         stepping = eax&0xf;
133         /* 6/9 (pentium-m "banias"), 6/13 (pentium-m "dothan"), and 6/14 (core1 "yonah")
134          * theoretically support sse2, but it's significantly slower than mmx for
135          * almost all of x264's functions, so let's just pretend they don't. */
136         if( family==6 && (model==9 || model==13 || model==14) )
137         {
138             cpu &= ~(X264_CPU_SSE2|X264_CPU_SSE3);
139             assert(!(cpu&(X264_CPU_SSSE3|X264_CPU_SSE4)));
140         }
141     }
142
143     if( (!strcmp((char*)vendor, "GenuineIntel") || !strcmp((char*)vendor, "CyrixInstead")) && !(cpu&X264_CPU_SSE42))
144     {
145         /* cacheline size is specified in 3 places, any of which may be missing */
146         x264_cpu_cpuid( 1, &eax, &ebx, &ecx, &edx );
147         cache = (ebx&0xff00)>>5; // cflush size
148         if( !cache && max_extended_cap >= 0x80000006 )
149         {
150             x264_cpu_cpuid( 0x80000006, &eax, &ebx, &ecx, &edx );
151             cache = ecx&0xff; // cacheline size
152         }
153         if( !cache )
154         {
155             // Cache and TLB Information
156             static const char cache32_ids[] = { 0x0a, 0x0c, 0x41, 0x42, 0x43, 0x44, 0x45, 0x82, 0x83, 0x84, 0x85, 0 };
157             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 };
158             uint32_t buf[4];
159             int max, i=0, j;
160             do {
161                 x264_cpu_cpuid( 2, buf+0, buf+1, buf+2, buf+3 );
162                 max = buf[0]&0xff;
163                 buf[0] &= ~0xff;
164                 for(j=0; j<4; j++)
165                     if( !(buf[j]>>31) )
166                         while( buf[j] )
167                         {
168                             if( strchr( cache32_ids, buf[j]&0xff ) )
169                                 cache = 32;
170                             if( strchr( cache64_ids, buf[j]&0xff ) )
171                                 cache = 64;
172                             buf[j] >>= 8;
173                         }
174             } while( ++i < max );
175         }
176
177         if( cache == 32 )
178             cpu |= X264_CPU_CACHELINE_32;
179         else if( cache == 64 )
180             cpu |= X264_CPU_CACHELINE_64;
181         else
182             fprintf( stderr, "x264 [warning]: unable to determine cacheline size\n" );
183     }
184
185 #ifdef BROKEN_STACK_ALIGNMENT
186     cpu |= X264_CPU_STACK_MOD4;
187 #endif
188
189     return cpu;
190 }
191
192 #elif defined( ARCH_PPC )
193
194 #ifdef SYS_MACOSX
195 #include <sys/sysctl.h>
196 uint32_t x264_cpu_detect( void )
197 {
198     /* Thank you VLC */
199     uint32_t cpu = 0;
200     int      selectors[2] = { CTL_HW, HW_VECTORUNIT };
201     int      has_altivec = 0;
202     size_t   length = sizeof( has_altivec );
203     int      error = sysctl( selectors, 2, &has_altivec, &length, NULL, 0 );
204
205     if( error == 0 && has_altivec != 0 )
206     {
207         cpu |= X264_CPU_ALTIVEC;
208     }
209
210     return cpu;
211 }
212
213 #elif defined( SYS_LINUX )
214 #include <signal.h>
215 #include <setjmp.h>
216 static sigjmp_buf jmpbuf;
217 static volatile sig_atomic_t canjump = 0;
218
219 static void sigill_handler( int sig )
220 {
221     if( !canjump )
222     {
223         signal( sig, SIG_DFL );
224         raise( sig );
225     }
226
227     canjump = 0;
228     siglongjmp( jmpbuf, 1 );
229 }
230
231 uint32_t x264_cpu_detect( void )
232 {
233     static void (* oldsig)( int );
234
235     oldsig = signal( SIGILL, sigill_handler );
236     if( sigsetjmp( jmpbuf, 1 ) )
237     {
238         signal( SIGILL, oldsig );
239         return 0;
240     }
241
242     canjump = 1;
243     asm volatile( "mtspr 256, %0\n\t"
244                   "vand 0, 0, 0\n\t"
245                   :
246                   : "r"(-1) );
247     canjump = 0;
248
249     signal( SIGILL, oldsig );
250
251     return X264_CPU_ALTIVEC;
252 }
253 #endif
254
255 #else
256
257 uint32_t x264_cpu_detect( void )
258 {
259     return 0;
260 }
261
262 #endif
263
264 #ifndef HAVE_MMX
265 void x264_emms( void )
266 {
267 }
268 #endif
269
270
271 int x264_cpu_num_processors( void )
272 {
273 #if !defined(HAVE_PTHREAD)
274     return 1;
275
276 #elif defined(_WIN32)
277     return pthread_num_processors_np();
278
279 #elif defined(SYS_LINUX)
280     unsigned int bit;
281     int np;
282     cpu_set_t p_aff;
283     memset( &p_aff, 0, sizeof(p_aff) );
284     sched_getaffinity( 0, sizeof(p_aff), &p_aff );
285     for( np = 0, bit = 0; bit < sizeof(p_aff); bit++ )
286         np += (((uint8_t *)&p_aff)[bit / 8] >> (bit % 8)) & 1;
287     return np;
288
289 #elif defined(SYS_BEOS)
290     system_info info;
291     get_system_info( &info );
292     return info.cpu_count;
293
294 #elif defined(SYS_MACOSX) || defined(SYS_FREEBSD)
295     int numberOfCPUs;
296     size_t length = sizeof( numberOfCPUs );
297     if( sysctlbyname("hw.ncpu", &numberOfCPUs, &length, NULL, 0) )
298     {
299         numberOfCPUs = 1;
300     }
301     return numberOfCPUs;
302
303 #else
304     return 1;
305 #endif
306 }