]> git.sesse.net Git - vlc/blob - src/misc/cpu.c
"#if HAVE_FOO" -> "#ifdef HAVE_FOO"
[vlc] / src / misc / cpu.c
1 /*****************************************************************************
2  * cpu.c: CPU detection code
3  *****************************************************************************
4  * Copyright (C) 1998-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *          Christophe Massiot <massiot@via.ecp.fr>
9  *          Eugenio Jarosiewicz <ej0@cise.ufl.eduEujenio>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc_common.h>
34 #include <vlc_cpu.h>
35
36 #include <sys/types.h>
37 #ifndef WIN32
38 #include <unistd.h>
39 #include <sys/wait.h>
40 #include <signal.h>
41 #else
42 #include <errno.h>
43 #endif
44
45 #include "libvlc.h"
46
47 #if defined(__APPLE__) && (defined(__ppc__) || defined(__ppc64__))
48 #include <sys/sysctl.h>
49 #endif
50
51 #if defined( __i386__ ) || defined( __x86_64__ ) || defined( __powerpc__ ) \
52  || defined( __ppc__ ) || defined( __ppc64__ ) || defined( __powerpc64__ )
53 # ifndef WIN32
54 static bool check_OS_capability( const char *psz_capability, pid_t pid )
55 {
56     int status;
57
58     if( pid == -1 )
59         return false; /* fail safe :-/ */
60
61     while( waitpid( pid, &status, 0 ) == -1 );
62
63     if( WIFEXITED( status ) && WEXITSTATUS( status ) == 0 )
64         return true;
65
66     fprintf( stderr, "warning: your CPU has %s instructions, but not your "
67                      "operating system.\n", psz_capability );
68     fprintf( stderr, "         some optimizations will be disabled unless "
69                      "you upgrade your OS\n" );
70     return false;
71 }
72
73 #  define check_capability(name, flag, code)   \
74      do {                                      \
75         pid_t pid = fork();                    \
76         if( pid == 0 )                         \
77         {                                      \
78             signal(SIGILL, SIG_DFL);           \
79             __asm__ __volatile__ ( code : : ); \
80             _exit(0);                          \
81         }                                      \
82         if( check_OS_capability((name), pid )) \
83             i_capabilities |= (flag);          \
84      } while(0)
85
86 # else /* WIN32 */
87 #  define check_capability(name, flag, code)   \
88         i_capabilities |= (flag);
89 # endif
90 #endif
91
92 /*****************************************************************************
93  * CPUCapabilities: get the CPU capabilities
94  *****************************************************************************
95  * This function is called to list extensions the CPU may have.
96  *****************************************************************************/
97 uint32_t CPUCapabilities( void )
98 {
99     uint32_t i_capabilities = 0;
100
101 #if defined( __i386__ ) || defined( __x86_64__ )
102      unsigned int i_eax, i_ebx, i_ecx, i_edx;
103      bool b_amd;
104
105     /* Needed for x86 CPU capabilities detection */
106 #   if defined( __x86_64__ )
107 #       define cpuid( reg )                    \
108             asm volatile ( "cpuid\n\t"         \
109                            "movl %%ebx,%1\n\t" \
110                          : "=a" ( i_eax ),     \
111                            "=b" ( i_ebx ),     \
112                            "=c" ( i_ecx ),     \
113                            "=d" ( i_edx )      \
114                          : "a"  ( reg )        \
115                          : "cc" );
116 #   else
117 #       define cpuid( reg )                    \
118             asm volatile ( "push %%ebx\n\t"    \
119                            "cpuid\n\t"         \
120                            "movl %%ebx,%1\n\t" \
121                            "pop %%ebx\n\t"     \
122                          : "=a" ( i_eax ),     \
123                            "=r" ( i_ebx ),     \
124                            "=c" ( i_ecx ),     \
125                            "=d" ( i_edx )      \
126                          : "a"  ( reg )        \
127                          : "cc" );
128 #   endif
129      /* Check if the OS really supports the requested instructions */
130 # if defined (__i386__) && !defined (__i486__) && !defined (__i586__) \
131   && !defined (__i686__) && !defined (__pentium4__) \
132   && !defined (__k6__) && !defined (__athlon__) && !defined (__k8__)
133     /* check if cpuid instruction is supported */
134     asm volatile ( "push %%ebx\n\t"
135                    "pushf\n\t"
136                    "pop %%eax\n\t"
137                    "movl %%eax, %%ebx\n\t"
138                    "xorl $0x200000, %%eax\n\t"
139                    "push %%eax\n\t"
140                    "popf\n\t"
141                    "pushf\n\t"
142                    "pop %%eax\n\t"
143                    "movl %%ebx,%1\n\t"
144                    "pop %%ebx\n\t"
145                  : "=a" ( i_eax ),
146                    "=r" ( i_ebx )
147                  :
148                  : "cc" );
149
150     if( i_eax == i_ebx )
151         goto out;
152 # endif
153
154     /* the CPU supports the CPUID instruction - get its level */
155     cpuid( 0x00000000 );
156
157 # if defined (__i386__) && !defined (__i586__) \
158   && !defined (__i686__) && !defined (__pentium4__) \
159   && !defined (__k6__) && !defined (__athlon__) && !defined (__k8__)
160     if( !i_eax )
161         goto out;
162 #endif
163
164     /* borrowed from mpeg2dec */
165     b_amd = ( i_ebx == 0x68747541 ) && ( i_ecx == 0x444d4163 )
166                     && ( i_edx == 0x69746e65 );
167
168     /* test for the MMX flag */
169     cpuid( 0x00000001 );
170 # if !defined (__MMX__)
171     if( ! (i_edx & 0x00800000) )
172         goto out;
173 # endif
174     i_capabilities |= CPU_CAPABILITY_MMX;
175
176 # if defined (__SSE__)
177     i_capabilities |= CPU_CAPABILITY_MMXEXT | CPU_CAPABILITY_SSE;
178 # else
179     if( i_edx & 0x02000000 )
180     {
181         i_capabilities |= CPU_CAPABILITY_MMXEXT;
182
183 #   ifdef CAN_COMPILE_SSE
184         check_capability( "SSE", CPU_CAPABILITY_SSE,
185                           "xorps %%xmm0,%%xmm0\n" );
186 #   endif
187     }
188 # endif
189
190 # if defined (__SSE2__)
191     i_capabilities |= CPU_CAPABILITY_SSE2;
192 # elif defined (CAN_COMPILE_SSE2)
193     if( i_edx & 0x04000000 )
194         check_capability( "SSE2", CPU_CAPABILITY_SSE2,
195                           "movupd %%xmm0, %%xmm0\n" );
196 # endif
197
198 # if defined (__SSE3__)
199     i_capabilities |= CPU_CAPABILITY_SSE3;
200 # elif defined (CAN_COMPILE_SSE3)
201     if( i_ecx & 0x00000001 )
202         check_capability( "SSE3", CPU_CAPABILITY_SSE3,
203                           "movsldup %%xmm1, %%xmm0\n" );
204 # endif
205
206 # if defined (__SSSE3__)
207     i_capabilities |= CPU_CAPABILITY_SSSE3;
208 # elif defined (CAN_COMPILE_SSSE3)
209     if( i_ecx & 0x00000200 )
210         check_capability( "SSSE3", CPU_CAPABILITY_SSSE3,
211                           "pabsw %%xmm1, %%xmm0\n" );
212 # endif
213
214 # if defined (__SSE4_1__)
215     i_capabilities |= CPU_CAPABILITY_SSE4_1;
216 # elif defined (CAN_COMPILE_SSE4_1)
217     if( i_ecx & 0x00080000 )
218         check_capability( "SSE4.1", CPU_CAPABILITY_SSE4_1,
219                           "pmaxsb %%xmm1, %%xmm0\n" );
220 # endif
221
222 # if defined (__SSE4_2__)
223     i_capabilities |= CPU_CAPABILITY_SSE4_2;
224 # elif defined (CAN_COMPILE_SSE4_2)
225     if( i_ecx & 0x00100000 )
226         check_capability( "SSE4.2", CPU_CAPABILITY_SSE4_2,
227                           "pcmpgtq %%xmm1, %%xmm0\n" );
228 # endif
229
230     /* test for additional capabilities */
231     cpuid( 0x80000000 );
232
233     if( i_eax < 0x80000001 )
234         goto out;
235
236     /* list these additional capabilities */
237     cpuid( 0x80000001 );
238
239 # if defined (__3dNOW__)
240     i_capabilities |= CPU_CAPABILITY_3DNOW;
241 # elif defined (CAN_COMPILE_3DNOW)
242     if( i_edx & 0x80000000 )
243         check_capability( "3D Now!", CPU_CAPABILITY_3DNOW,
244                           "pfadd %%mm0,%%mm0\n" "femms\n" );
245 # endif
246
247     if( b_amd && ( i_edx & 0x00400000 ) )
248     {
249         i_capabilities |= CPU_CAPABILITY_MMXEXT;
250     }
251 out:
252
253 #elif defined( __arm__ )
254 #   if defined( __ARM_NEON__ )
255     i_capabilities |= CPU_CAPABILITY_NEON;
256 #   endif
257
258 #elif defined( __powerpc__ ) || defined( __ppc__ ) || defined( __powerpc64__ ) \
259     || defined( __ppc64__ )
260
261 #   if defined(__APPLE__)
262     int selectors[2] = { CTL_HW, HW_VECTORUNIT };
263     int i_has_altivec = 0;
264     size_t i_length = sizeof( i_has_altivec );
265     int i_error = sysctl( selectors, 2, &i_has_altivec, &i_length, NULL, 0);
266
267     if( i_error == 0 && i_has_altivec != 0 )
268         i_capabilities |= CPU_CAPABILITY_ALTIVEC;
269
270 #   elif defined( CAN_COMPILE_ALTIVEC )
271     pid_t pid = fork();
272     if( pid == 0 )
273     {
274         signal(SIGILL, SIG_DFL);
275         asm volatile ("mtspr 256, %0\n\t"
276                       "vand %%v0, %%v0, %%v0"
277                       :
278                       : "r" (-1));
279         _exit(0);
280     }
281
282     if( check_OS_capability( "Altivec", pid ) )
283         i_capabilities |= CPU_CAPABILITY_ALTIVEC;
284
285 #   endif
286
287 #endif
288     return i_capabilities;
289 }
290
291 uint32_t cpu_flags = 0;
292
293
294 /*****************************************************************************
295  * vlc_CPU: get pre-computed CPU capability flags
296  ****************************************************************************/
297 unsigned vlc_CPU (void)
298 {
299     return cpu_flags;
300 }
301
302 const struct
303 {
304     uint32_t value;
305     char name[12];
306 } cap_dirs[] = {
307 #if defined ( __i386__ ) || defined ( __x86_64__ )
308     { CPU_CAPABILITY_MMX,     "mmx" },
309     { CPU_CAPABILITY_MMXEXT,  "mmxext" },
310     { CPU_CAPABILITY_3DNOW,   "3dnow" },
311     { CPU_CAPABILITY_SSE,     "sse" },
312 #endif
313 #if defined (__ppc__) || defined (__ppc64__) || defined (__powerpc__)
314     { CPU_CAPABILITY_ALTIVEC, "altivec" },
315 #endif
316 #if defined (__arm__)
317     { CPU_CAPABILITY_NEON,    "arm_neon" },
318 #endif
319 };
320
321 /**
322  * Return the number of available logical CPU.
323  */
324 unsigned vlc_GetCPUCount(void)
325 {
326 #if defined(WIN32) && !defined(UNDER_CE)
327     DWORD process_mask;
328     DWORD system_mask;
329     if (!GetProcessAffinityMask(GetCurrentProcess(), &process_mask, &system_mask))
330         return 1;
331
332     unsigned count = 0;
333     while (system_mask) {
334         count++;
335         system_mask >>= 1;
336     }
337     return count;
338 #elif defined(HAVE_SCHED_GETAFFINITY)
339     cpu_set_t cpu;
340     CPU_ZERO(&cpu);
341     if (sched_getaffinity(0, sizeof(cpu), &cpu) < 0)
342         return 1;
343     unsigned count = 0;
344     for (unsigned i = 0; i < CPU_SETSIZE; i++)
345         count += CPU_ISSET(i, &cpu) != 0;
346     return count;
347 #elif defined(__APPLE__)
348     int count;
349     size_t size = sizeof(count) ;
350     if (sysctlbyname("hw.ncpu", &count, &size, NULL, 0))
351         return 1; /* Failure */
352     return count;
353 #else
354 #   warning "vlc_GetCPUCount is not implemented for your platform"
355     return 1;
356 #endif
357 }
358
359 /**
360  * Check if a directory name contains usable plugins w.r.t. the hardware
361  * capabilities. Loading a plugin when the hardware has insufficient
362  * capabilities may lead to illegal instructions (SIGILL) and must be avoided.
363  *
364  * @param name the name of the directory (<b>not</b> the path)
365  *
366  * @return true if the hardware has sufficient capabilities or the directory
367  * does not require any special capability; false if the running hardware has
368  * insufficient capabilities.
369  */
370 bool vlc_CPU_CheckPluginDir (const char *name)
371 {
372     const unsigned flags = vlc_CPU ();
373     for (size_t i = 0; i < sizeof (cap_dirs) / sizeof (cap_dirs[0]); i++)
374     {
375         if (strcmp (name, cap_dirs[i].name))
376             continue;
377         return (flags & cap_dirs[i].value) != 0;
378     }
379     return true;
380 }
381
382 static vlc_memcpy_t pf_vlc_memcpy = memcpy;
383 static vlc_memset_t pf_vlc_memset = memset;
384
385 void vlc_fastmem_register (vlc_memcpy_t cpy, vlc_memset_t set)
386 {
387     if (cpy)
388         pf_vlc_memcpy = cpy;
389     if (set)
390         pf_vlc_memset = set;
391 }
392
393 /**
394  * vlc_memcpy: fast CPU-dependent memcpy
395  */
396 void *vlc_memcpy (void *tgt, const void *src, size_t n)
397 {
398     return pf_vlc_memcpy (tgt, src, n);
399 }
400
401 /**
402  * vlc_memset: fast CPU-dependent memset
403  */
404 void *vlc_memset (void *tgt, int c, size_t n)
405 {
406     return pf_vlc_memset (tgt, c, n);
407 }