]> git.sesse.net Git - x264/blob - common/cpu.c
Update file headers throughout x264
[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",    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},
55     {"", 0},
56 };
57
58 #ifdef HAVE_MMX
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 );
61
62 uint32_t x264_cpu_detect( void )
63 {
64     uint32_t cpu = 0;
65     uint32_t eax, ebx, ecx, edx;
66     uint32_t vendor[4] = {0};
67     int max_extended_cap;
68     int cache;
69
70 #ifndef ARCH_X86_64
71     if( !x264_cpu_cpuid_test() )
72         return 0;
73 #endif
74
75     x264_cpu_cpuid( 0, &eax, vendor+0, vendor+2, vendor+1 );
76     if( eax == 0 )
77         return 0;
78
79     x264_cpu_cpuid( 1, &eax, &ebx, &ecx, &edx );
80     if( edx&0x00800000 )
81         cpu |= X264_CPU_MMX;
82     else
83         return 0;
84     if( edx&0x02000000 )
85         cpu |= X264_CPU_MMXEXT|X264_CPU_SSE;
86     if( edx&0x04000000 )
87         cpu |= X264_CPU_SSE2;
88     if( ecx&0x00000001 )
89         cpu |= X264_CPU_SSE3;
90     if( ecx&0x00000200 )
91         cpu |= X264_CPU_SSSE3;
92     if( ecx&0x00080000 )
93         cpu |= X264_CPU_SSE4;
94
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;
99
100     x264_cpu_cpuid( 0x80000000, &eax, &ebx, &ecx, &edx );
101     max_extended_cap = eax;
102
103     if( !strcmp((char*)vendor, "AuthenticAMD") && max_extended_cap >= 0x80000001 )
104     {
105         x264_cpu_cpuid( 0x80000001, &eax, &ebx, &ecx, &edx );
106         if( edx&0x00400000 )
107             cpu |= X264_CPU_MMXEXT;
108         if( cpu & X264_CPU_SSE2 )
109         {
110             if( ecx&0x00000040 ) /* SSE4a */
111                 cpu |= X264_CPU_SSE2_IS_FAST;
112             else
113                 cpu |= X264_CPU_SSE2_IS_SLOW;
114         }
115     }
116
117     if( !strcmp((char*)vendor, "GenuineIntel") )
118     {
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);
123         stepping = eax&0xf;
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) )
128         {
129             cpu &= ~(X264_CPU_SSE2|X264_CPU_SSE3);
130             assert(!(cpu&(X264_CPU_SSSE3|X264_CPU_SSE4)));
131         }
132     }
133
134     if( !strcmp((char*)vendor, "GenuineIntel") || !strcmp((char*)vendor, "CyrixInstead") )
135     {
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 )
140         {
141             x264_cpu_cpuid( 0x80000006, &eax, &ebx, &ecx, &edx );
142             cache = ecx&0xff; // cacheline size
143         }
144         if( !cache )
145         {
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 };
149             uint32_t buf[4];
150             int max, i=0, j;
151             do {
152                 x264_cpu_cpuid( 2, buf+0, buf+1, buf+2, buf+3 );
153                 max = buf[0]&0xff;
154                 buf[0] &= ~0xff;
155                 for(j=0; j<4; j++)
156                     if( !(buf[j]>>31) )
157                         while( buf[j] )
158                         {
159                             if( strchr( cache32_ids, buf[j]&0xff ) )
160                                 cache = 32;
161                             if( strchr( cache64_ids, buf[j]&0xff ) )
162                                 cache = 64;
163                             buf[j] >>= 8;
164                         }
165             } while( ++i < max );
166         }
167
168         if( cache == 32 )
169             cpu |= X264_CPU_CACHELINE_32;
170         else if( cache == 64 )
171             cpu |= X264_CPU_CACHELINE_64;
172         else
173             fprintf( stderr, "x264 [warning]: unable to determine cacheline size\n" );
174     }
175
176 #ifdef BROKEN_STACK_ALIGNMENT
177     cpu |= X264_CPU_STACK_MOD4;
178 #endif
179
180     return cpu;
181 }
182
183 #elif defined( ARCH_PPC )
184
185 #ifdef SYS_MACOSX
186 #include <sys/sysctl.h>
187 uint32_t x264_cpu_detect( void )
188 {
189     /* Thank you VLC */
190     uint32_t cpu = 0;
191     int      selectors[2] = { CTL_HW, HW_VECTORUNIT };
192     int      has_altivec = 0;
193     size_t   length = sizeof( has_altivec );
194     int      error = sysctl( selectors, 2, &has_altivec, &length, NULL, 0 );
195
196     if( error == 0 && has_altivec != 0 )
197     {
198         cpu |= X264_CPU_ALTIVEC;
199     }
200
201     return cpu;
202 }
203
204 #elif defined( SYS_LINUX )
205 #include <signal.h>
206 #include <setjmp.h>
207 static sigjmp_buf jmpbuf;
208 static volatile sig_atomic_t canjump = 0;
209
210 static void sigill_handler( int sig )
211 {
212     if( !canjump )
213     {
214         signal( sig, SIG_DFL );
215         raise( sig );
216     }
217
218     canjump = 0;
219     siglongjmp( jmpbuf, 1 );
220 }
221
222 uint32_t x264_cpu_detect( void )
223 {
224     static void (* oldsig)( int );
225
226     oldsig = signal( SIGILL, sigill_handler );
227     if( sigsetjmp( jmpbuf, 1 ) )
228     {
229         signal( SIGILL, oldsig );
230         return 0;
231     }
232
233     canjump = 1;
234     asm volatile( "mtspr 256, %0\n\t"
235                   "vand 0, 0, 0\n\t"
236                   :
237                   : "r"(-1) );
238     canjump = 0;
239
240     signal( SIGILL, oldsig );
241
242     return X264_CPU_ALTIVEC;
243 }
244 #endif
245
246 #else
247
248 uint32_t x264_cpu_detect( void )
249 {
250     return 0;
251 }
252
253 #endif
254
255 #ifndef HAVE_MMX
256 void x264_emms( void )
257 {
258 }
259 #endif
260
261
262 int x264_cpu_num_processors( void )
263 {
264 #if !defined(HAVE_PTHREAD)
265     return 1;
266
267 #elif defined(_WIN32)
268     return pthread_num_processors_np();
269
270 #elif defined(SYS_LINUX)
271     unsigned int bit;
272     int np;
273     cpu_set_t p_aff;
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;
278     return np;
279
280 #elif defined(SYS_BEOS)
281     system_info info;
282     get_system_info( &info );
283     return info.cpu_count;
284
285 #elif defined(SYS_MACOSX) || defined(SYS_FREEBSD)
286     int numberOfCPUs;
287     size_t length = sizeof( numberOfCPUs );
288     if( sysctlbyname("hw.ncpu", &numberOfCPUs, &length, NULL, 0) )
289     {
290         numberOfCPUs = 1;
291     }
292     return numberOfCPUs;
293
294 #else
295     return 1;
296 #endif
297 }