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