]> git.sesse.net Git - vlc/blob - src/misc/cpu.c
Call vlc_CPU_init() when loading a DLL on OS/2 as Win32.
[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 #include <assert.h>
45
46 #ifdef __APPLE__
47 #include <sys/sysctl.h>
48 #endif
49
50 #include "libvlc.h"
51
52 static uint32_t cpu_flags;
53
54 #if defined( __i386__ ) || defined( __x86_64__ ) || defined( __powerpc__ ) \
55  || defined( __ppc__ ) || defined( __ppc64__ ) || defined( __powerpc64__ )
56 # ifndef WIN32
57 static bool check_OS_capability( const char *psz_capability, pid_t pid )
58 {
59     int status;
60
61     if( pid == -1 )
62         return false; /* fail safe :-/ */
63
64     while( waitpid( pid, &status, 0 ) == -1 );
65
66     if( WIFEXITED( status ) && WEXITSTATUS( status ) == 0 )
67         return true;
68
69     fprintf( stderr, "warning: your CPU has %s instructions, but not your "
70                      "operating system.\n", psz_capability );
71     fprintf( stderr, "         some optimizations will be disabled unless "
72                      "you upgrade your OS\n" );
73     return false;
74 }
75
76 #  define check_capability(name, flag, code, input)     \
77      do {                                               \
78         pid_t pid = fork();                             \
79         if( pid == 0 )                                  \
80         {                                               \
81             signal(SIGILL, SIG_DFL);                    \
82             __asm__ __volatile__ ( code : : input );    \
83             _exit(0);                                   \
84         }                                               \
85         if( check_OS_capability((name), pid ))          \
86             i_capabilities |= (flag);                   \
87      } while(0)
88
89 # else /* WIN32 */
90 #  define check_capability(name, flag, code, input)   \
91         i_capabilities |= (flag);
92 # endif
93 #endif
94
95 /**
96  * Determines the CPU capabilities and stores them in cpu_flags.
97  * The result can be retrieved with vlc_CPU().
98  */
99 void vlc_CPU_init (void)
100 {
101     uint32_t i_capabilities = 0;
102
103 #if defined( __i386__ ) || defined( __x86_64__ )
104      unsigned int i_eax, i_ebx, i_ecx, i_edx;
105      bool b_amd;
106
107     /* Needed for x86 CPU capabilities detection */
108 #   if defined( __x86_64__ )
109 #       define cpuid( reg )                    \
110             asm volatile ( "cpuid\n\t"         \
111                            "movl %%ebx,%1\n\t" \
112                          : "=a" ( i_eax ),     \
113                            "=b" ( i_ebx ),     \
114                            "=c" ( i_ecx ),     \
115                            "=d" ( i_edx )      \
116                          : "a"  ( reg )        \
117                          : "cc" );
118 #   else
119 #       define cpuid( reg )                    \
120             asm volatile ( "push %%ebx\n\t"    \
121                            "cpuid\n\t"         \
122                            "movl %%ebx,%1\n\t" \
123                            "pop %%ebx\n\t"     \
124                          : "=a" ( i_eax ),     \
125                            "=r" ( i_ebx ),     \
126                            "=c" ( i_ecx ),     \
127                            "=d" ( i_edx )      \
128                          : "a"  ( reg )        \
129                          : "cc" );
130 #   endif
131      /* Check if the OS really supports the requested instructions */
132 # if defined (__i386__) && !defined (__i486__) && !defined (__i586__) \
133   && !defined (__i686__) && !defined (__pentium4__) \
134   && !defined (__k6__) && !defined (__athlon__) && !defined (__k8__)
135     /* check if cpuid instruction is supported */
136     asm volatile ( "push %%ebx\n\t"
137                    "pushf\n\t"
138                    "pop %%eax\n\t"
139                    "movl %%eax, %%ebx\n\t"
140                    "xorl $0x200000, %%eax\n\t"
141                    "push %%eax\n\t"
142                    "popf\n\t"
143                    "pushf\n\t"
144                    "pop %%eax\n\t"
145                    "movl %%ebx,%1\n\t"
146                    "pop %%ebx\n\t"
147                  : "=a" ( i_eax ),
148                    "=r" ( i_ebx )
149                  :
150                  : "cc" );
151
152     if( i_eax == i_ebx )
153         goto out;
154 # endif
155
156     /* the CPU supports the CPUID instruction - get its level */
157     cpuid( 0x00000000 );
158
159 # if defined (__i386__) && !defined (__i586__) \
160   && !defined (__i686__) && !defined (__pentium4__) \
161   && !defined (__k6__) && !defined (__athlon__) && !defined (__k8__)
162     if( !i_eax )
163         goto out;
164 #endif
165
166     /* borrowed from mpeg2dec */
167     b_amd = ( i_ebx == 0x68747541 ) && ( i_ecx == 0x444d4163 )
168                     && ( i_edx == 0x69746e65 );
169
170     /* test for the MMX flag */
171     cpuid( 0x00000001 );
172 # if !defined (__MMX__)
173     if( ! (i_edx & 0x00800000) )
174         goto out;
175 # endif
176     i_capabilities |= CPU_CAPABILITY_MMX;
177
178 # if defined (__SSE__)
179     i_capabilities |= CPU_CAPABILITY_MMXEXT | CPU_CAPABILITY_SSE;
180 # else
181     if( i_edx & 0x02000000 )
182     {
183         i_capabilities |= CPU_CAPABILITY_MMXEXT;
184
185 #   ifdef CAN_COMPILE_SSE
186         check_capability( "SSE", CPU_CAPABILITY_SSE,
187                           "xorps %%xmm0,%%xmm0\n", );
188 #   endif
189     }
190 # endif
191
192 # if defined (__SSE2__)
193     i_capabilities |= CPU_CAPABILITY_SSE2;
194 # elif defined (CAN_COMPILE_SSE2)
195     if( i_edx & 0x04000000 )
196         check_capability( "SSE2", CPU_CAPABILITY_SSE2,
197                           "movupd %%xmm0, %%xmm0\n", );
198 # endif
199
200 # if defined (__SSE3__)
201     i_capabilities |= CPU_CAPABILITY_SSE3;
202 # elif defined (CAN_COMPILE_SSE3)
203     if( i_ecx & 0x00000001 )
204         check_capability( "SSE3", CPU_CAPABILITY_SSE3,
205                           "movsldup %%xmm1, %%xmm0\n", );
206 # endif
207
208 # if defined (__SSSE3__)
209     i_capabilities |= CPU_CAPABILITY_SSSE3;
210 # elif defined (CAN_COMPILE_SSSE3)
211     if( i_ecx & 0x00000200 )
212         check_capability( "SSSE3", CPU_CAPABILITY_SSSE3,
213                           "pabsw %%xmm1, %%xmm0\n", );
214 # endif
215
216 # if defined (__SSE4_1__)
217     i_capabilities |= CPU_CAPABILITY_SSE4_1;
218 # elif defined (CAN_COMPILE_SSE4_1)
219     if( i_ecx & 0x00080000 )
220         check_capability( "SSE4.1", CPU_CAPABILITY_SSE4_1,
221                           "pmaxsb %%xmm1, %%xmm0\n", );
222 # endif
223
224 # if defined (__SSE4_2__)
225     i_capabilities |= CPU_CAPABILITY_SSE4_2;
226 # elif defined (CAN_COMPILE_SSE4_2)
227     if( i_ecx & 0x00100000 )
228         check_capability( "SSE4.2", CPU_CAPABILITY_SSE4_2,
229                           "pcmpgtq %%xmm1, %%xmm0\n", );
230 # endif
231
232     /* test for additional capabilities */
233     cpuid( 0x80000000 );
234
235     if( i_eax < 0x80000001 )
236         goto out;
237
238     /* list these additional capabilities */
239     cpuid( 0x80000001 );
240
241 # if defined (__3dNOW__)
242     i_capabilities |= CPU_CAPABILITY_3DNOW;
243 # elif defined (CAN_COMPILE_3DNOW)
244     if( i_edx & 0x80000000 )
245         check_capability( "3D Now!", CPU_CAPABILITY_3DNOW,
246                           "pfadd %%mm0,%%mm0\n" "femms\n", );
247 # endif
248
249     if( b_amd && ( i_edx & 0x00400000 ) )
250     {
251         i_capabilities |= CPU_CAPABILITY_MMXEXT;
252     }
253 out:
254
255 #elif defined (__arm__)
256
257 # if defined (__ARM_NEON__)
258     i_capabilities |= CPU_CAPABILITY_NEON;
259 # elif defined (CAN_COMPILE_NEON)
260 #  define NEED_RUNTIME_CPU_CHECK 1
261 # endif
262
263 # ifdef NEED_RUNTIME_CPU_CHECK
264 #  if defined (__linux__)
265     FILE *info = fopen ("/proc/cpuinfo", "rt");
266     if (info != NULL)
267     {
268         char *line = NULL;
269         size_t linelen = 0;
270
271         while (getline (&line, &linelen, info) != -1)
272         {
273             const char *cap;
274
275             if (strncmp (line, "Features\t:", 10))
276                 continue;
277
278             /* TODO: detect other CPU features when we use them */
279 #   if defined (CAN_COMPILE_NEON) && !defined (__ARM_NEON__)
280                 cap = strstr (line + 10, " neon");
281             if (cap != NULL && (cap[5] == '\0' || cap[5] == ' '))
282                 i_capabilities |= CPU_CAPABILITY_NEON;
283 #   endif
284             break;
285         }
286         fclose (info);
287         free (line);
288     }
289 #  else
290 #   warning Run-time CPU detection missing: optimizations disabled!
291 #  endif
292 # endif
293
294 #elif defined( __powerpc__ ) || defined( __ppc__ ) || defined( __powerpc64__ ) \
295     || defined( __ppc64__ )
296
297 #   if defined(__APPLE__) || defined(__OpenBSD__)
298 #   if defined(__OpenBSD__)
299     int selectors[2] = { CTL_MACHDEP, CPU_ALTIVEC };
300 #   else
301     int selectors[2] = { CTL_HW, HW_VECTORUNIT };
302 #   endif
303     int i_has_altivec = 0;
304     size_t i_length = sizeof( i_has_altivec );
305     int i_error = sysctl( selectors, 2, &i_has_altivec, &i_length, NULL, 0);
306
307     if( i_error == 0 && i_has_altivec != 0 )
308         i_capabilities |= CPU_CAPABILITY_ALTIVEC;
309
310 #   elif defined( CAN_COMPILE_ALTIVEC )
311     check_capability( "Altivec", CPU_CAPABILITY_ALTIVEC,
312         "mtspr 256, %0\n\t"
313         "vand %%v0, %%v0, %%v0",
314                       "r" (-1));
315
316 #   endif
317
318 #endif
319
320     cpu_flags = i_capabilities;
321 }
322
323 /**
324  * Retrieves pre-computed CPU capability flags
325  */
326 unsigned vlc_CPU (void)
327 {
328 /* On Windows and OS/2,
329  * initialized from DllMain() and _DLL_InitTerm() respectively, instead */
330 #if !defined(WIN32) && !defined(__OS2__)
331     static pthread_once_t once = PTHREAD_ONCE_INIT;
332     pthread_once (&once, vlc_CPU_init);
333 #endif
334     return cpu_flags;
335 }
336
337 void vlc_CPU_dump (vlc_object_t *obj)
338 {
339     const unsigned flags = vlc_CPU();
340     char buf[200], *p = buf;
341
342 #define PRINT_CAPABILITY( capability, string ) \
343     if (flags & (capability)) \
344         p += sprintf (p, "%s ", (string) )
345
346 #if defined (__i386__) || defined (__x86_64__)
347     PRINT_CAPABILITY(CPU_CAPABILITY_MMX, "MMX");
348     PRINT_CAPABILITY(CPU_CAPABILITY_3DNOW, "3DNow!");
349     PRINT_CAPABILITY(CPU_CAPABILITY_MMXEXT, "MMXEXT");
350     PRINT_CAPABILITY(CPU_CAPABILITY_SSE, "SSE");
351     PRINT_CAPABILITY(CPU_CAPABILITY_SSE2, "SSE2");
352     PRINT_CAPABILITY(CPU_CAPABILITY_SSE3, "SSE3");
353     PRINT_CAPABILITY(CPU_CAPABILITY_SSSE3, "SSSE3");
354     PRINT_CAPABILITY(CPU_CAPABILITY_SSE4_1, "SSE4.1");
355     PRINT_CAPABILITY(CPU_CAPABILITY_SSE4_2, "SSE4.2");
356     PRINT_CAPABILITY(CPU_CAPABILITY_SSE4A,  "SSE4A");
357
358 #elif defined (__powerpc__) || defined (__ppc__) || defined (__ppc64__)
359     PRINT_CAPABILITY(CPU_CAPABILITY_ALTIVEC, "AltiVec");
360
361 #elif defined (__arm__)
362     PRINT_CAPABILITY(CPU_CAPABILITY_NEON, "NEONv1");
363
364 #endif
365
366 #if HAVE_FPU
367     p += sprintf (p, "FPU ");
368 #endif
369
370     if (p > buf)
371         msg_Dbg (obj, "CPU has capabilities %s", buf);
372 }
373
374
375 static vlc_memcpy_t pf_vlc_memcpy = memcpy;
376
377 void vlc_fastmem_register (vlc_memcpy_t cpy)
378 {
379     assert (cpy != NULL);
380     pf_vlc_memcpy = cpy;
381 }
382
383 /**
384  * vlc_memcpy: fast CPU-dependent memcpy
385  */
386 void *vlc_memcpy (void *tgt, const void *src, size_t n)
387 {
388     return pf_vlc_memcpy (tgt, src, n);
389 }