]> git.sesse.net Git - vlc/blob - src/misc/cpu.c
Use a vlc_CPU() wrapper instead of (ab)using libvlc_global
[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 #include <vlc/vlc.h>
30
31 #ifdef HAVE_SIGNAL_H
32 #   include <signal.h>                            /* SIGHUP, SIGINT, SIGKILL */
33 #   include <setjmp.h>                                    /* longjmp, setjmp */
34 #endif
35
36 #if defined(__APPLE__) && (defined(__ppc__) || defined(__ppc64__))
37 #include <sys/sysctl.h>
38 #endif
39
40 /*****************************************************************************
41  * Local prototypes
42  *****************************************************************************/
43 #ifdef HAVE_SIGNAL_H
44 static void SigHandler   ( int );
45 #endif
46
47 /*****************************************************************************
48  * Global variables - they're needed for signal handling
49  *****************************************************************************/
50 #ifdef HAVE_SIGNAL_H
51 static jmp_buf env;
52 static int     i_illegal;
53 #if defined( __i386__ ) || defined( __x86_64__ )
54 static const char *psz_capability;
55 #endif
56 #endif
57
58 /*****************************************************************************
59  * CPUCapabilities: get the CPU capabilities
60  *****************************************************************************
61  * This function is called to list extensions the CPU may have.
62  *****************************************************************************/
63 static uint32_t CPUCapabilities( void )
64 {
65     volatile uint32_t i_capabilities = CPU_CAPABILITY_NONE;
66
67 #if defined(__APPLE__) && (defined(__ppc__) || defined(__ppc64__))
68     int selectors[2] = { CTL_HW, HW_VECTORUNIT };
69     int i_has_altivec = 0;
70     size_t i_length = sizeof( i_has_altivec );
71     int i_error = sysctl( selectors, 2, &i_has_altivec, &i_length, NULL, 0);
72
73     i_capabilities |= CPU_CAPABILITY_FPU;
74
75     if( i_error == 0 && i_has_altivec != 0 )
76         i_capabilities |= CPU_CAPABILITY_ALTIVEC;
77
78     return i_capabilities;
79
80 #elif defined( __i386__ ) || defined( __x86_64__ )
81     volatile unsigned int  i_eax, i_ebx, i_ecx, i_edx;
82     volatile vlc_bool_t    b_amd;
83
84     /* Needed for x86 CPU capabilities detection */
85 #   if defined( __x86_64__ )
86 #       define cpuid( reg )                    \
87             asm volatile ( "cpuid\n\t"         \
88                            "movl %%ebx,%1\n\t" \
89                          : "=a" ( i_eax ),     \
90                            "=b" ( i_ebx ),     \
91                            "=c" ( i_ecx ),     \
92                            "=d" ( i_edx )      \
93                          : "a"  ( reg )        \
94                          : "cc" );
95 #   else
96 #       define cpuid( reg )                    \
97             asm volatile ( "push %%ebx\n\t"    \
98                            "cpuid\n\t"         \
99                            "movl %%ebx,%1\n\t" \
100                            "pop %%ebx\n\t"     \
101                          : "=a" ( i_eax ),     \
102                            "=r" ( i_ebx ),     \
103                            "=c" ( i_ecx ),     \
104                            "=d" ( i_edx )      \
105                          : "a"  ( reg )        \
106                          : "cc" );
107 #   endif
108
109 #   if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW ) \
110      && defined( HAVE_SIGNAL_H )
111     void (*pf_sigill) (int) = signal( SIGILL, SigHandler );
112 #   endif
113
114     i_capabilities |= CPU_CAPABILITY_FPU;
115
116 #   if defined( __i386__ )
117     /* check if cpuid instruction is supported */
118     asm volatile ( "push %%ebx\n\t"
119                    "pushf\n\t"
120                    "pop %%eax\n\t"
121                    "movl %%eax, %%ebx\n\t"
122                    "xorl $0x200000, %%eax\n\t"
123                    "push %%eax\n\t"
124                    "popf\n\t"
125                    "pushf\n\t"
126                    "pop %%eax\n\t"
127                    "movl %%ebx,%1\n\t"
128                    "pop %%ebx\n\t"
129                  : "=a" ( i_eax ),
130                    "=r" ( i_ebx )
131                  :
132                  : "cc" );
133
134     if( i_eax == i_ebx )
135     {
136 #       if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW ) \
137             && defined( HAVE_SIGNAL_H )
138         signal( SIGILL, pf_sigill );
139 #       endif
140         return i_capabilities;
141     }
142 #   else
143     /* x86_64 supports cpuid instruction, so we dont need to check it */
144 #   endif
145
146     i_capabilities |= CPU_CAPABILITY_486;
147
148     /* the CPU supports the CPUID instruction - get its level */
149     cpuid( 0x00000000 );
150
151     if( !i_eax )
152     {
153 #   if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW ) \
154      && defined( HAVE_SIGNAL_H )
155         signal( SIGILL, pf_sigill );
156 #   endif
157         return i_capabilities;
158     }
159
160     /* FIXME: this isn't correct, since some 486s have cpuid */
161     i_capabilities |= CPU_CAPABILITY_586;
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
170     if( ! (i_edx & 0x00800000) )
171     {
172 #   if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW ) \
173      && defined( HAVE_SIGNAL_H )
174         signal( SIGILL, pf_sigill );
175 #   endif
176         return i_capabilities;
177     }
178
179     i_capabilities |= CPU_CAPABILITY_MMX;
180
181     if( i_edx & 0x02000000 )
182     {
183         i_capabilities |= CPU_CAPABILITY_MMXEXT;
184
185 #   ifdef CAN_COMPILE_SSE
186         /* We test if OS supports the SSE instructions */
187         psz_capability = "SSE";
188         i_illegal = 0;
189
190         if( setjmp( env ) == 0 )
191         {
192             /* Test a SSE instruction */
193             __asm__ __volatile__ ( "xorps %%xmm0,%%xmm0\n" : : );
194         }
195
196         if( i_illegal == 0 )
197         {
198             i_capabilities |= CPU_CAPABILITY_SSE;
199         }
200 #   endif
201     }
202
203     if( i_edx & 0x04000000 )
204     {
205 #   if defined(CAN_COMPILE_SSE)
206         /* We test if OS supports the SSE instructions */
207         psz_capability = "SSE2";
208         i_illegal = 0;
209
210         if( setjmp( env ) == 0 )
211         {
212             /* Test a SSE2 instruction */
213             __asm__ __volatile__ ( "movupd %%xmm0, %%xmm0\n" : : );
214         }
215
216         if( i_illegal == 0 )
217         {
218             i_capabilities |= CPU_CAPABILITY_SSE2;
219         }
220 #   endif
221     }
222
223     /* test for additional capabilities */
224     cpuid( 0x80000000 );
225
226     if( i_eax < 0x80000001 )
227     {
228 #   if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW ) \
229      && defined( HAVE_SIGNAL_H )
230         signal( SIGILL, pf_sigill );
231 #   endif
232         return i_capabilities;
233     }
234
235     /* list these additional capabilities */
236     cpuid( 0x80000001 );
237
238 #   ifdef CAN_COMPILE_3DNOW
239     if( i_edx & 0x80000000 )
240     {
241         psz_capability = "3D Now!";
242         i_illegal = 0;
243
244         if( setjmp( env ) == 0 )
245         {
246             /* Test a 3D Now! instruction */
247             __asm__ __volatile__ ( "pfadd %%mm0,%%mm0\n" "femms\n" : : );
248         }
249
250         if( i_illegal == 0 )
251         {
252             i_capabilities |= CPU_CAPABILITY_3DNOW;
253         }
254     }
255 #   endif
256
257     if( b_amd && ( i_edx & 0x00400000 ) )
258     {
259         i_capabilities |= CPU_CAPABILITY_MMXEXT;
260     }
261
262 #   if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW ) \
263      && defined( HAVE_SIGNAL_H )
264     signal( SIGILL, pf_sigill );
265 #   endif
266     return i_capabilities;
267
268 #elif defined( __powerpc__ ) || defined( __ppc__ ) || defined( __ppc64__ )
269
270 #   ifdef CAN_COMPILE_ALTIVEC && defined( HAVE_SIGNAL_H )
271     void (*pf_sigill) (int) = signal( SIGILL, SigHandler );
272
273     i_capabilities |= CPU_CAPABILITY_FPU;
274
275     i_illegal = 0;
276
277     if( setjmp( env ) == 0 )
278     {
279         asm volatile ("mtspr 256, %0\n\t"
280                       "vand %%v0, %%v0, %%v0"
281                       :
282                       : "r" (-1));
283     }
284
285     if( i_illegal == 0 )
286     {
287         i_capabilities |= CPU_CAPABILITY_ALTIVEC;
288     }
289
290     signal( SIGILL, pf_sigill );
291 #   endif
292
293     return i_capabilities;
294
295 #elif defined( __sparc__ )
296
297     i_capabilities |= CPU_CAPABILITY_FPU;
298     return i_capabilities;
299
300 #elif defined( _MSC_VER ) && !defined( UNDER_CE )
301     i_capabilities |= CPU_CAPABILITY_FPU;
302     return i_capabilities;
303
304 #else
305     /* default behaviour */
306     return i_capabilities;
307
308 #endif
309 }
310
311 /*****************************************************************************
312  * SigHandler: system signal handler
313  *****************************************************************************
314  * This function is called when an illegal instruction signal is received by
315  * the program. We use this function to test OS and CPU capabilities
316  *****************************************************************************/
317 #if defined( HAVE_SIGNAL_H )
318 static void SigHandler( int i_signal )
319 {
320     /* Acknowledge the signal received */
321     i_illegal = 1;
322
323 #ifdef HAVE_SIGRELSE
324     sigrelse( i_signal );
325 #endif
326
327 #if defined( __i386__ )
328     fprintf( stderr, "warning: your CPU has %s instructions, but not your "
329                      "operating system.\n", psz_capability );
330     fprintf( stderr, "         some optimizations will be disabled unless "
331                      "you upgrade your OS\n" );
332 #   if defined( SYS_LINUX )
333     fprintf( stderr, "         (for instance Linux kernel 2.4.x or later)\n" );
334 #   endif
335 #endif
336
337     longjmp( env, 1 );
338 }
339 #endif
340
341
342 extern uint32_t cpu_flags = 0;
343
344
345 /*****************************************************************************
346  * vlc_CPU: get pre-computed CPU capability flags
347  ****************************************************************************/
348 unsigned vlc_CPU (void)
349 {
350     return cpu_flags;
351 }
352