]> git.sesse.net Git - x264/blob - common/cpu.c
3ebe970f530d3e7ec0fc886f6d0c29d0b924e97f
[x264] / common / cpu.c
1 /*****************************************************************************
2  * cpu.c: h264 encoder library
3  *****************************************************************************
4  * Copyright (C) 2003 Laurent Aimar
5  * $Id: cpu.c,v 1.1 2004/06/03 19:27:06 fenrir Exp $
6  *
7  * Authors: 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., 59 Temple Place - Suite 330, 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
38 const struct {
39     const char name[8];
40     int flags;
41 } x264_cpu_names[] = {
42     {"MMX",     X264_CPU_MMX},
43     {"MMX2",    X264_CPU_MMX|X264_CPU_MMXEXT},
44     {"MMXEXT",  X264_CPU_MMX|X264_CPU_MMXEXT},
45     {"SSE",     X264_CPU_MMX|X264_CPU_MMXEXT|X264_CPU_SSE},
46     {"SSE1",    X264_CPU_MMX|X264_CPU_MMXEXT|X264_CPU_SSE},
47     {"SSE2",    X264_CPU_MMX|X264_CPU_MMXEXT|X264_CPU_SSE|X264_CPU_SSE2},
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     {"SSE4",    X264_CPU_MMX|X264_CPU_MMXEXT|X264_CPU_SSE|X264_CPU_SSE2|X264_CPU_SSE3|X264_CPU_SSSE3|X264_CPU_SSE4},
51     {"3DNow",   X264_CPU_3DNOW},
52     {"Altivec", X264_CPU_ALTIVEC},
53     {"Cache32", X264_CPU_CACHELINE_SPLIT|X264_CPU_CACHELINE_32},
54     {"Cache64", X264_CPU_CACHELINE_SPLIT|X264_CPU_CACHELINE_64},
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     x264_cpu_cpuid( 0x80000000, &eax, &ebx, &ecx, &edx );
96     max_extended_cap = eax;
97
98     if( !strcmp((char*)vendor, "AuthenticAMD") && max_extended_cap >= 0x80000001 )
99     {
100         x264_cpu_cpuid( 0x80000001, &eax, &ebx, &ecx, &edx );
101         if( edx&0x80000000 )
102             cpu |= X264_CPU_3DNOW;
103         if( edx&0x00400000 )
104             cpu |= X264_CPU_MMXEXT;
105     }
106
107     if( !strcmp((char*)vendor, "GenuineIntel") || !strcmp((char*)vendor, "CyrixInstead") )
108         cpu |= X264_CPU_CACHELINE_SPLIT;
109     /* cacheline size is specified in 3 places, any of which may be missing */
110     x264_cpu_cpuid( 1, &eax, &ebx, &ecx, &edx );
111     cache = (ebx&0xff00)>>5; // cflush size
112     if( !cache && max_extended_cap >= 0x80000006 )
113     {
114         x264_cpu_cpuid( 0x80000006, &eax, &ebx, &ecx, &edx );
115         cache = ecx&0xff; // cacheline size
116     }
117     if( !cache )
118     {
119         // Cache and TLB Information
120         static const char cache32_ids[] = { 0x0a, 0x0c, 0x41, 0x42, 0x43, 0x44, 0x45, 0x82, 0x83, 0x84, 0x85, 0 };
121         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 };
122         uint32_t buf[4];
123         int max, i=0, j;
124         do {
125             x264_cpu_cpuid( 2, buf+0, buf+1, buf+2, buf+3 );
126             max = buf[0]&0xff;
127             buf[0] &= ~0xff;
128             for(j=0; j<4; j++)
129                 if( !(buf[j]>>31) )
130                     while( buf[j] )
131                     {
132                         if( strchr( cache32_ids, buf[j]&0xff ) )
133                             cache = 32;
134                         if( strchr( cache64_ids, buf[j]&0xff ) )
135                             cache = 64;
136                         buf[j] >>= 8;
137                     }
138         } while( ++i < max );
139     }
140
141     if( cache == 32 )
142         cpu |= X264_CPU_CACHELINE_32;
143     if( cache == 64 )
144         cpu |= X264_CPU_CACHELINE_64;
145
146     return cpu;
147 }
148
149 #elif defined( ARCH_PPC )
150
151 #ifdef SYS_MACOSX
152 #include <sys/sysctl.h>
153 uint32_t x264_cpu_detect( void )
154 {
155     /* Thank you VLC */
156     uint32_t cpu = 0;
157     int      selectors[2] = { CTL_HW, HW_VECTORUNIT };
158     int      has_altivec = 0;
159     size_t   length = sizeof( has_altivec );
160     int      error = sysctl( selectors, 2, &has_altivec, &length, NULL, 0 );
161
162     if( error == 0 && has_altivec != 0 )
163     {
164         cpu |= X264_CPU_ALTIVEC;
165     }
166
167     return cpu;
168 }
169
170 #elif defined( SYS_LINUX )
171 #include <signal.h>
172 #include <setjmp.h>
173 static sigjmp_buf jmpbuf;
174 static volatile sig_atomic_t canjump = 0;
175
176 static void sigill_handler( int sig )
177 {
178     if( !canjump )
179     {
180         signal( sig, SIG_DFL );
181         raise( sig );
182     }
183
184     canjump = 0;
185     siglongjmp( jmpbuf, 1 );
186 }
187
188 uint32_t x264_cpu_detect( void )
189 {
190     static void (* oldsig)( int );
191
192     oldsig = signal( SIGILL, sigill_handler );
193     if( sigsetjmp( jmpbuf, 1 ) )
194     {
195         signal( SIGILL, oldsig );
196         return 0;
197     }
198
199     canjump = 1;
200     asm volatile( "mtspr 256, %0\n\t"
201                   "vand 0, 0, 0\n\t"
202                   :
203                   : "r"(-1) );
204     canjump = 0;
205
206     signal( SIGILL, oldsig );
207
208     return X264_CPU_ALTIVEC;
209 }
210 #endif
211
212 #else
213
214 uint32_t x264_cpu_detect( void )
215 {
216     return 0;
217 }
218
219 #endif
220
221 #ifndef HAVE_MMX
222 void x264_emms( void )
223 {
224 }
225 #endif
226
227
228 int x264_cpu_num_processors( void )
229 {
230 #if !defined(HAVE_PTHREAD)
231     return 1;
232
233 #elif defined(_WIN32)
234     return pthread_num_processors_np();
235
236 #elif defined(SYS_LINUX)
237     unsigned int bit;
238     int np;
239     cpu_set_t p_aff;
240     memset( &p_aff, 0, sizeof(p_aff) );
241     sched_getaffinity( 0, sizeof(p_aff), &p_aff );
242     for( np = 0, bit = 0; bit < sizeof(p_aff); bit++ )
243         np += (((uint8_t *)&p_aff)[bit / 8] >> (bit % 8)) & 1;
244     return np;
245
246 #elif defined(SYS_BEOS)
247     system_info info;
248     get_system_info( &info );
249     return info.cpu_count;
250
251 #elif defined(SYS_MACOSX) || defined(SYS_FREEBSD)
252     int numberOfCPUs;
253     size_t length = sizeof( numberOfCPUs );
254     if( sysctlbyname("hw.ncpu", &numberOfCPUs, &length, NULL, 0) )
255     {
256         numberOfCPUs = 1;
257     }
258     return numberOfCPUs;
259
260 #else
261     return 1;
262 #endif
263 }