]> git.sesse.net Git - vlc/blob - src/misc/cpu.c
threads: remove legacy #includes
[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 #else
41 #include <errno.h>
42 #endif
43
44 #include "libvlc.h"
45
46 #if defined(__APPLE__) && (defined(__ppc__) || defined(__ppc64__))
47 #include <sys/sysctl.h>
48 #endif
49
50 #if defined( __i386__ ) || defined( __x86_64__ ) || defined( __powerpc__ ) \
51  || defined( __ppc__ ) || defined( __ppc64__ ) || defined( __powerpc64__ )
52 static bool check_OS_capability( const char *psz_capability, pid_t pid )
53 {
54 #ifndef WIN32
55     int status;
56
57     if( pid == -1 )
58         return false; /* fail safe :-/ */
59
60     while( waitpid( pid, &status, 0 ) == -1 );
61
62     if( WIFEXITED( status ) && WEXITSTATUS( status ) == 0 )
63         return true;
64
65     fprintf( stderr, "warning: your CPU has %s instructions, but not your "
66                      "operating system.\n", psz_capability );
67     fprintf( stderr, "         some optimizations will be disabled unless "
68                      "you upgrade your OS\n" );
69     return false;
70 #else
71 # warning FIXME!
72 # define fork() (errno = ENOSYS, -1)
73     (void)pid;
74     (void)psz_capability;
75     return true;
76 #endif
77 }
78 #endif
79
80 /*****************************************************************************
81  * CPUCapabilities: get the CPU capabilities
82  *****************************************************************************
83  * This function is called to list extensions the CPU may have.
84  *****************************************************************************/
85 uint32_t CPUCapabilities( void )
86 {
87     uint32_t i_capabilities = 0;
88
89 #if defined( __i386__ ) || defined( __x86_64__ )
90      unsigned int i_eax, i_ebx, i_ecx, i_edx;
91      bool b_amd;
92
93     /* Needed for x86 CPU capabilities detection */
94 #   if defined( __x86_64__ )
95 #       define cpuid( reg )                    \
96             asm volatile ( "cpuid\n\t"         \
97                            "movl %%ebx,%1\n\t" \
98                          : "=a" ( i_eax ),     \
99                            "=b" ( i_ebx ),     \
100                            "=c" ( i_ecx ),     \
101                            "=d" ( i_edx )      \
102                          : "a"  ( reg )        \
103                          : "cc" );
104 #   else
105 #       define cpuid( reg )                    \
106             asm volatile ( "push %%ebx\n\t"    \
107                            "cpuid\n\t"         \
108                            "movl %%ebx,%1\n\t" \
109                            "pop %%ebx\n\t"     \
110                          : "=a" ( i_eax ),     \
111                            "=r" ( i_ebx ),     \
112                            "=c" ( i_ecx ),     \
113                            "=d" ( i_edx )      \
114                          : "a"  ( reg )        \
115                          : "cc" );
116 #   endif
117      /* Check if the OS really supports the requested instructions */
118 #   define check_capability(name, flag, code)  \
119      do {                                      \
120         pid_t pid = fork();                    \
121         if( pid == 0 )                         \
122         {                                      \
123             __asm__ __volatile__ ( code : : ); \
124             _exit(0);                           \
125         }                                      \
126         if( check_OS_capability((name), pid )) \
127             i_capabilities |= (flag);          \
128      } while(0)
129
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         asm volatile ("mtspr 256, %0\n\t"
275                       "vand %%v0, %%v0, %%v0"
276                       :
277                       : "r" (-1));
278         _exit(0);
279     }
280
281     if( check_OS_capability( "Altivec", pid ) )
282         i_capabilities |= CPU_CAPABILITY_ALTIVEC;
283
284 #   endif
285
286 #endif
287     return i_capabilities;
288 }
289
290 uint32_t cpu_flags = 0;
291
292
293 /*****************************************************************************
294  * vlc_CPU: get pre-computed CPU capability flags
295  ****************************************************************************/
296 unsigned vlc_CPU (void)
297 {
298     return cpu_flags;
299 }
300
301 const struct
302 {
303     uint32_t value;
304     char name[12];
305 } cap_dirs[] = {
306 #if defined ( __i386__ ) || defined ( __x86_64__ )
307     { CPU_CAPABILITY_MMX,     "mmx" },
308     { CPU_CAPABILITY_MMXEXT,  "mmxext" },
309     { CPU_CAPABILITY_3DNOW,   "3dnow" },
310     { CPU_CAPABILITY_SSE,     "sse" },
311 #endif
312 #if defined (__ppc__) || defined (__ppc64__) || defined (__powerpc__)
313     { CPU_CAPABILITY_ALTIVEC, "altivec" },
314 #endif
315 #if defined (__arm__)
316     { CPU_CAPABILITY_NEON,    "arm_neon" },
317 #endif
318 };
319
320 /**
321  * Check if a directory name contains usable plugins w.r.t. the hardware
322  * capabilities. Loading a plugin when the hardware has insufficient
323  * capabilities may lead to illegal instructions (SIGILL) and must be avoided.
324  *
325  * @param name the name of the directory (<b>not</b> the path)
326  *
327  * @return true if the hardware has sufficient capabilities or the directory
328  * does not require any special capability; false if the running hardware has
329  * insufficient capabilities.
330  */
331 bool vlc_CPU_CheckPluginDir (const char *name)
332 {
333     const unsigned flags = vlc_CPU ();
334     for (size_t i = 0; i < sizeof (cap_dirs) / sizeof (cap_dirs[0]); i++)
335     {
336         if (strcmp (name, cap_dirs[i].name))
337             continue;
338         return (flags & cap_dirs[i].value) != 0;
339     }
340     return true;
341 }
342
343 static vlc_memcpy_t pf_vlc_memcpy = memcpy;
344 static vlc_memset_t pf_vlc_memset = memset;
345
346 void vlc_fastmem_register (vlc_memcpy_t cpy, vlc_memset_t set)
347 {
348     if (cpy)
349         pf_vlc_memcpy = cpy;
350     if (set)
351         pf_vlc_memset = set;
352 }
353
354 /**
355  * vlc_memcpy: fast CPU-dependent memcpy
356  */
357 void *vlc_memcpy (void *tgt, const void *src, size_t n)
358 {
359     return pf_vlc_memcpy (tgt, src, n);
360 }
361
362 /**
363  * vlc_memset: fast CPU-dependent memset
364  */
365 void *vlc_memset (void *tgt, int c, size_t n)
366 {
367     return pf_vlc_memset (tgt, c, n);
368 }