]> git.sesse.net Git - vlc/blob - src/misc/cpu.c
CPU: get rid of signal(SIGILL) - not thread-safe (in LibVLC)
[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
35 #include <sys/types.h>
36 #include <signal.h>                            /* SIGHUP, SIGINT, SIGKILL */
37 #ifndef WIN32
38 #include <unistd.h>
39 #include <sys/wait.h>
40 #endif
41
42 #include "libvlc.h"
43
44 #if defined(__APPLE__) && (defined(__ppc__) || defined(__ppc64__))
45 #include <sys/sysctl.h>
46 #endif
47
48 #if defined( __i386__ ) || defined( __x86_64__ ) \
49  || defined( __ppc__ ) || defined( __ppc64__ )
50 static bool check_OS_capability( const char *psz_capability, pid_t pid )
51 {
52 #ifndef WIN32
53     int status;
54
55     if( pid == -1 )
56         return false; /* fail safe :-/ */
57
58     while( waitpid( pid, &status, 0 ) == -1 );
59
60     if( WIFEXITED( status ) && WEXITSTATUS( status ) == 0 )
61         return true;
62
63     fprintf( stderr, "warning: your CPU has %s instructions, but not your "
64                      "operating system.\n", psz_capability );
65     fprintf( stderr, "         some optimizations will be disabled unless "
66                      "you upgrade your OS\n" );
67     return false;
68 #else
69 # warning FIXME!
70 # define fork() (errno = ENOSYS, -1)
71     (void)pid;
72     (void)psz_capability;
73     return true;
74 #endif
75 }
76 #endif
77
78 /*****************************************************************************
79  * CPUCapabilities: get the CPU capabilities
80  *****************************************************************************
81  * This function is called to list extensions the CPU may have.
82  *****************************************************************************/
83 uint32_t CPUCapabilities( void )
84 {
85     uint32_t i_capabilities = CPU_CAPABILITY_NONE;
86
87 #if defined( __i386__ ) || defined( __x86_64__ )
88      unsigned int i_eax, i_ebx, i_ecx, i_edx;
89      bool b_amd;
90
91     /* Needed for x86 CPU capabilities detection */
92 #   if defined( __x86_64__ )
93 #       define cpuid( reg )                    \
94             asm volatile ( "cpuid\n\t"         \
95                            "movl %%ebx,%1\n\t" \
96                          : "=a" ( i_eax ),     \
97                            "=b" ( i_ebx ),     \
98                            "=c" ( i_ecx ),     \
99                            "=d" ( i_edx )      \
100                          : "a"  ( reg )        \
101                          : "cc" );
102 #   else
103 #       define cpuid( reg )                    \
104             asm volatile ( "push %%ebx\n\t"    \
105                            "cpuid\n\t"         \
106                            "movl %%ebx,%1\n\t" \
107                            "pop %%ebx\n\t"     \
108                          : "=a" ( i_eax ),     \
109                            "=r" ( i_ebx ),     \
110                            "=c" ( i_ecx ),     \
111                            "=d" ( i_edx )      \
112                          : "a"  ( reg )        \
113                          : "cc" );
114 #   endif
115
116     i_capabilities |= CPU_CAPABILITY_FPU;
117
118 #   if defined( __i386__ )
119     /* check if cpuid instruction is supported */
120     asm volatile ( "push %%ebx\n\t"
121                    "pushf\n\t"
122                    "pop %%eax\n\t"
123                    "movl %%eax, %%ebx\n\t"
124                    "xorl $0x200000, %%eax\n\t"
125                    "push %%eax\n\t"
126                    "popf\n\t"
127                    "pushf\n\t"
128                    "pop %%eax\n\t"
129                    "movl %%ebx,%1\n\t"
130                    "pop %%ebx\n\t"
131                  : "=a" ( i_eax ),
132                    "=r" ( i_ebx )
133                  :
134                  : "cc" );
135
136     if( i_eax == i_ebx )
137         goto out;
138 #   else
139     /* x86_64 supports cpuid instruction, so we dont need to check it */
140 #   endif
141
142     i_capabilities |= CPU_CAPABILITY_486;
143
144     /* the CPU supports the CPUID instruction - get its level */
145     cpuid( 0x00000000 );
146
147     if( !i_eax )
148         goto out;
149
150     /* FIXME: this isn't correct, since some 486s have cpuid */
151     i_capabilities |= CPU_CAPABILITY_586;
152
153     /* borrowed from mpeg2dec */
154     b_amd = ( i_ebx == 0x68747541 ) && ( i_ecx == 0x444d4163 )
155                     && ( i_edx == 0x69746e65 );
156
157     /* test for the MMX flag */
158     cpuid( 0x00000001 );
159
160     if( ! (i_edx & 0x00800000) )
161         goto out;
162
163     i_capabilities |= CPU_CAPABILITY_MMX;
164
165     if( i_edx & 0x02000000 )
166     {
167         i_capabilities |= CPU_CAPABILITY_MMXEXT;
168
169 #   ifdef CAN_COMPILE_SSE
170         /* We test if OS supports the SSE instructions */
171         pid_t pid = fork();
172         if( pid == 0 )
173         {
174             /* Test a SSE instruction */
175             __asm__ __volatile__ ( "xorps %%xmm0,%%xmm0\n" : : );
176             exit(0);
177         }
178         if( check_OS_capability( "SSE", pid ) )
179             i_capabilities |= CPU_CAPABILITY_SSE;
180 #   endif
181     }
182
183     if( i_edx & 0x04000000 )
184     {
185 #   if defined(CAN_COMPILE_SSE)
186         /* We test if OS supports the SSE2 instructions */
187         pid_t pid = fork();
188         if( pid == 0 )
189         {
190             /* Test a SSE2 instruction */
191             __asm__ __volatile__ ( "movupd %%xmm0, %%xmm0\n" : : );
192             exit(0);
193         }
194         if( check_OS_capability( "SSE2", pid ) )
195             i_capabilities |= CPU_CAPABILITY_SSE2;
196 #   endif
197     }
198
199     /* test for additional capabilities */
200     cpuid( 0x80000000 );
201
202     if( i_eax < 0x80000001 )
203         goto out;
204
205     /* list these additional capabilities */
206     cpuid( 0x80000001 );
207
208 #   ifdef CAN_COMPILE_3DNOW
209     if( i_edx & 0x80000000 )
210     {
211         pid_t pid = fork();
212         if( pid == 0 )
213         {
214             /* Test a 3D Now! instruction */
215             __asm__ __volatile__ ( "pfadd %%mm0,%%mm0\n" "femms\n" : : );
216             exit(0);
217         }
218         if( check_OS_capability( "3D Now!", pid ) )
219             i_capabilities |= CPU_CAPABILITY_3DNOW;
220     }
221 #   endif
222
223     if( b_amd && ( i_edx & 0x00400000 ) )
224     {
225         i_capabilities |= CPU_CAPABILITY_MMXEXT;
226     }
227 out:
228
229 #elif defined( __arm__ )
230 #   if defined( __ARM_EABI__ ) && !defined( __SOFTFP__ )
231     i_capabilities |= CPU_CAPABILITY_FPU;
232 #   endif
233
234 #elif defined( __powerpc__ ) || defined( __ppc__ ) || defined( __ppc64__ )
235
236     i_capabilities |= CPU_CAPABILITY_FPU;
237
238 #   if defined(__APPLE__)
239     int selectors[2] = { CTL_HW, HW_VECTORUNIT };
240     int i_has_altivec = 0;
241     size_t i_length = sizeof( i_has_altivec );
242     int i_error = sysctl( selectors, 2, &i_has_altivec, &i_length, NULL, 0);
243
244     if( i_error == 0 && i_has_altivec != 0 )
245         i_capabilities |= CPU_CAPABILITY_ALTIVEC;
246
247 #   elif defined( CAN_COMPILE_ALTIVEC )
248     pid_t pid = fork();
249     if( pid == 0 )
250     {
251         asm volatile ("mtspr 256, %0\n\t"
252                       "vand %%v0, %%v0, %%v0"
253                       :
254                       : "r" (-1));
255         exit(0);
256     }
257
258     if( check_OS_capability( "Altivec", pid ) )
259         i_capabilities |= CPU_CAPABILITY_ALTIVEC;
260
261 #   endif
262
263 #elif defined( __sparc__ )
264     i_capabilities |= CPU_CAPABILITY_FPU;
265
266 #elif defined( _MSC_VER ) && !defined( UNDER_CE )
267     i_capabilities |= CPU_CAPABILITY_FPU;
268
269 #endif
270     return i_capabilities;
271 }
272
273 uint32_t cpu_flags = 0;
274
275
276 /*****************************************************************************
277  * vlc_CPU: get pre-computed CPU capability flags
278  ****************************************************************************/
279 unsigned vlc_CPU (void)
280 {
281     return cpu_flags;
282 }
283
284 static vlc_memcpy_t pf_vlc_memcpy = memcpy;
285 static vlc_memset_t pf_vlc_memset = memset;
286
287 void vlc_fastmem_register (vlc_memcpy_t cpy, vlc_memset_t set)
288 {
289     if (cpy)
290         pf_vlc_memcpy = cpy;
291     if (set)
292         pf_vlc_memset = set;
293 }
294
295 /**
296  * vlc_memcpy: fast CPU-dependent memcpy
297  */
298 void *vlc_memcpy (void *tgt, const void *src, size_t n)
299 {
300     return pf_vlc_memcpy (tgt, src, n);
301 }
302
303 /**
304  * vlc_memset: fast CPU-dependent memset
305  */
306 void *vlc_memset (void *tgt, int c, size_t n)
307 {
308     return pf_vlc_memset (tgt, c, n);
309 }