]> git.sesse.net Git - vlc/blob - src/misc/cpu.c
Win32: compile fix
[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) (void)0
88 # endif
89 #endif
90
91 /*****************************************************************************
92  * CPUCapabilities: get the CPU capabilities
93  *****************************************************************************
94  * This function is called to list extensions the CPU may have.
95  *****************************************************************************/
96 uint32_t CPUCapabilities( void )
97 {
98     uint32_t i_capabilities = 0;
99
100 #if defined( __i386__ ) || defined( __x86_64__ )
101      unsigned int i_eax, i_ebx, i_ecx, i_edx;
102      bool b_amd;
103
104     /* Needed for x86 CPU capabilities detection */
105 #   if defined( __x86_64__ )
106 #       define cpuid( reg )                    \
107             asm volatile ( "cpuid\n\t"         \
108                            "movl %%ebx,%1\n\t" \
109                          : "=a" ( i_eax ),     \
110                            "=b" ( i_ebx ),     \
111                            "=c" ( i_ecx ),     \
112                            "=d" ( i_edx )      \
113                          : "a"  ( reg )        \
114                          : "cc" );
115 #   else
116 #       define cpuid( reg )                    \
117             asm volatile ( "push %%ebx\n\t"    \
118                            "cpuid\n\t"         \
119                            "movl %%ebx,%1\n\t" \
120                            "pop %%ebx\n\t"     \
121                          : "=a" ( i_eax ),     \
122                            "=r" ( i_ebx ),     \
123                            "=c" ( i_ecx ),     \
124                            "=d" ( i_edx )      \
125                          : "a"  ( reg )        \
126                          : "cc" );
127 #   endif
128      /* Check if the OS really supports the requested instructions */
129 # if defined (__i386__) && !defined (__i486__) && !defined (__i586__) \
130   && !defined (__i686__) && !defined (__pentium4__) \
131   && !defined (__k6__) && !defined (__athlon__) && !defined (__k8__)
132     /* check if cpuid instruction is supported */
133     asm volatile ( "push %%ebx\n\t"
134                    "pushf\n\t"
135                    "pop %%eax\n\t"
136                    "movl %%eax, %%ebx\n\t"
137                    "xorl $0x200000, %%eax\n\t"
138                    "push %%eax\n\t"
139                    "popf\n\t"
140                    "pushf\n\t"
141                    "pop %%eax\n\t"
142                    "movl %%ebx,%1\n\t"
143                    "pop %%ebx\n\t"
144                  : "=a" ( i_eax ),
145                    "=r" ( i_ebx )
146                  :
147                  : "cc" );
148
149     if( i_eax == i_ebx )
150         goto out;
151 # endif
152
153     /* the CPU supports the CPUID instruction - get its level */
154     cpuid( 0x00000000 );
155
156 # if defined (__i386__) && !defined (__i586__) \
157   && !defined (__i686__) && !defined (__pentium4__) \
158   && !defined (__k6__) && !defined (__athlon__) && !defined (__k8__)
159     if( !i_eax )
160         goto out;
161 #endif
162
163     /* borrowed from mpeg2dec */
164     b_amd = ( i_ebx == 0x68747541 ) && ( i_ecx == 0x444d4163 )
165                     && ( i_edx == 0x69746e65 );
166
167     /* test for the MMX flag */
168     cpuid( 0x00000001 );
169 # if !defined (__MMX__)
170     if( ! (i_edx & 0x00800000) )
171         goto out;
172 # endif
173     i_capabilities |= CPU_CAPABILITY_MMX;
174
175 # if defined (__SSE__)
176     i_capabilities |= CPU_CAPABILITY_MMXEXT | CPU_CAPABILITY_SSE;
177 # else
178     if( i_edx & 0x02000000 )
179     {
180         i_capabilities |= CPU_CAPABILITY_MMXEXT;
181
182 #   ifdef CAN_COMPILE_SSE
183         check_capability( "SSE", CPU_CAPABILITY_SSE,
184                           "xorps %%xmm0,%%xmm0\n" );
185 #   endif
186     }
187 # endif
188
189 # if defined (__SSE2__)
190     i_capabilities |= CPU_CAPABILITY_SSE2;
191 # elif defined (CAN_COMPILE_SSE2)
192     if( i_edx & 0x04000000 )
193         check_capability( "SSE2", CPU_CAPABILITY_SSE2,
194                           "movupd %%xmm0, %%xmm0\n" );
195 # endif
196
197 # if defined (__SSE3__)
198     i_capabilities |= CPU_CAPABILITY_SSE3;
199 # elif defined (CAN_COMPILE_SSE3)
200     if( i_ecx & 0x00000001 )
201         check_capability( "SSE3", CPU_CAPABILITY_SSE3,
202                           "movsldup %%xmm1, %%xmm0\n" );
203 # endif
204
205 # if defined (__SSSE3__)
206     i_capabilities |= CPU_CAPABILITY_SSSE3;
207 # elif defined (CAN_COMPILE_SSSE3)
208     if( i_ecx & 0x00000200 )
209         check_capability( "SSSE3", CPU_CAPABILITY_SSSE3,
210                           "pabsw %%xmm1, %%xmm0\n" );
211 # endif
212
213 # if defined (__SSE4_1__)
214     i_capabilities |= CPU_CAPABILITY_SSE4_1;
215 # elif defined (CAN_COMPILE_SSE4_1)
216     if( i_ecx & 0x00080000 )
217         check_capability( "SSE4.1", CPU_CAPABILITY_SSE4_1,
218                           "pmaxsb %%xmm1, %%xmm0\n" );
219 # endif
220
221 # if defined (__SSE4_2__)
222     i_capabilities |= CPU_CAPABILITY_SSE4_2;
223 # elif defined (CAN_COMPILE_SSE4_2)
224     if( i_ecx & 0x00100000 )
225         check_capability( "SSE4.2", CPU_CAPABILITY_SSE4_2,
226                           "pcmpgtq %%xmm1, %%xmm0\n" );
227 # endif
228
229     /* test for additional capabilities */
230     cpuid( 0x80000000 );
231
232     if( i_eax < 0x80000001 )
233         goto out;
234
235     /* list these additional capabilities */
236     cpuid( 0x80000001 );
237
238 # if defined (__3dNOW__)
239     i_capabilities |= CPU_CAPABILITY_3DNOW;
240 # elif defined (CAN_COMPILE_3DNOW)
241     if( i_edx & 0x80000000 )
242         check_capability( "3D Now!", CPU_CAPABILITY_3DNOW,
243                           "pfadd %%mm0,%%mm0\n" "femms\n" );
244 # endif
245
246     if( b_amd && ( i_edx & 0x00400000 ) )
247     {
248         i_capabilities |= CPU_CAPABILITY_MMXEXT;
249     }
250 out:
251
252 #elif defined( __arm__ )
253 #   if defined( __ARM_NEON__ )
254     i_capabilities |= CPU_CAPABILITY_NEON;
255 #   endif
256
257 #elif defined( __powerpc__ ) || defined( __ppc__ ) || defined( __powerpc64__ ) \
258     || defined( __ppc64__ )
259
260 #   if defined(__APPLE__)
261     int selectors[2] = { CTL_HW, HW_VECTORUNIT };
262     int i_has_altivec = 0;
263     size_t i_length = sizeof( i_has_altivec );
264     int i_error = sysctl( selectors, 2, &i_has_altivec, &i_length, NULL, 0);
265
266     if( i_error == 0 && i_has_altivec != 0 )
267         i_capabilities |= CPU_CAPABILITY_ALTIVEC;
268
269 #   elif defined( CAN_COMPILE_ALTIVEC )
270     pid_t pid = fork();
271     if( pid == 0 )
272     {
273         asm volatile ("mtspr 256, %0\n\t"
274                       "vand %%v0, %%v0, %%v0"
275                       :
276                       : "r" (-1));
277         _exit(0);
278     }
279
280     if( check_OS_capability( "Altivec", pid ) )
281         i_capabilities |= CPU_CAPABILITY_ALTIVEC;
282
283 #   endif
284
285 #endif
286     return i_capabilities;
287 }
288
289 uint32_t cpu_flags = 0;
290
291
292 /*****************************************************************************
293  * vlc_CPU: get pre-computed CPU capability flags
294  ****************************************************************************/
295 unsigned vlc_CPU (void)
296 {
297     return cpu_flags;
298 }
299
300 const struct
301 {
302     uint32_t value;
303     char name[12];
304 } cap_dirs[] = {
305 #if defined ( __i386__ ) || defined ( __x86_64__ )
306     { CPU_CAPABILITY_MMX,     "mmx" },
307     { CPU_CAPABILITY_MMXEXT,  "mmxext" },
308     { CPU_CAPABILITY_3DNOW,   "3dnow" },
309     { CPU_CAPABILITY_SSE,     "sse" },
310 #endif
311 #if defined (__ppc__) || defined (__ppc64__) || defined (__powerpc__)
312     { CPU_CAPABILITY_ALTIVEC, "altivec" },
313 #endif
314 #if defined (__arm__)
315     { CPU_CAPABILITY_NEON,    "arm_neon" },
316 #endif
317 };
318
319 /**
320  * Check if a directory name contains usable plugins w.r.t. the hardware
321  * capabilities. Loading a plugin when the hardware has insufficient
322  * capabilities may lead to illegal instructions (SIGILL) and must be avoided.
323  *
324  * @param name the name of the directory (<b>not</b> the path)
325  *
326  * @return true if the hardware has sufficient capabilities or the directory
327  * does not require any special capability; false if the running hardware has
328  * insufficient capabilities.
329  */
330 bool vlc_CPU_CheckPluginDir (const char *name)
331 {
332     const unsigned flags = vlc_CPU ();
333     for (size_t i = 0; i < sizeof (cap_dirs) / sizeof (cap_dirs[0]); i++)
334     {
335         if (strcmp (name, cap_dirs[i].name))
336             continue;
337         return (flags & cap_dirs[i].value) != 0;
338     }
339     return true;
340 }
341
342 static vlc_memcpy_t pf_vlc_memcpy = memcpy;
343 static vlc_memset_t pf_vlc_memset = memset;
344
345 void vlc_fastmem_register (vlc_memcpy_t cpy, vlc_memset_t set)
346 {
347     if (cpy)
348         pf_vlc_memcpy = cpy;
349     if (set)
350         pf_vlc_memset = set;
351 }
352
353 /**
354  * vlc_memcpy: fast CPU-dependent memcpy
355  */
356 void *vlc_memcpy (void *tgt, const void *src, size_t n)
357 {
358     return pf_vlc_memcpy (tgt, src, n);
359 }
360
361 /**
362  * vlc_memset: fast CPU-dependent memset
363  */
364 void *vlc_memset (void *tgt, int c, size_t n)
365 {
366     return pf_vlc_memset (tgt, c, n);
367 }