]> git.sesse.net Git - vlc/blob - src/misc/cpu.c
* ALL: x86-64 fixes.
[vlc] / src / misc / cpu.c
1 /*****************************************************************************
2  * cpu.c: CPU detection code
3  *****************************************************************************
4  * Copyright (C) 1998-2004 VideoLAN
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 #   define cpuid( reg )                    \
88         asm volatile ( "push %%ebx\n\t"   \
89                        "cpuid\n\t"         \
90                        "movl %%ebx,%1\n\t" \
91                        "pop %%ebx\n\t"    \
92                      : "=a" ( i_eax ),     \
93                        "=r" ( i_ebx ),     \
94                        "=c" ( i_ecx ),     \
95                        "=d" ( i_edx )      \
96                      : "a"  ( reg )        \
97                      : "cc" );
98
99 #   if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW ) \
100      && defined( HAVE_SIGNAL_H )
101     void (*pf_sigill) (int) = signal( SIGILL, SigHandler );
102 #   endif
103
104     i_capabilities |= CPU_CAPABILITY_FPU;
105
106     asm volatile ( "push %%ebx\n\t"
107                    "pushf\n\t"
108                    "pop %%eax\n\t"
109                    "movl %%eax, %%ebx\n\t"
110                    "xorl $0x200000, %%eax\n\t"
111                    "push %%eax\n\t"
112                    "popf\n\t"
113                    "pushf\n\t"
114                    "pop %%eax\n\t"
115                    "movl %%ebx,%1\n\t"
116                    "pop %%ebx\n\t"
117                  : "=a" ( i_eax ),
118                    "=r" ( i_ebx )
119                  :
120                  : "cc" );
121
122     if( i_eax == i_ebx )
123     {
124 #   if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW ) \
125      && defined( HAVE_SIGNAL_H )
126         signal( SIGILL, pf_sigill );
127 #   endif
128         return i_capabilities;
129     }
130
131     i_capabilities |= CPU_CAPABILITY_486;
132
133     /* the CPU supports the CPUID instruction - get its level */
134     cpuid( 0x00000000 );
135
136     if( !i_eax )
137     {
138 #   if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW ) \
139      && defined( HAVE_SIGNAL_H )
140         signal( SIGILL, pf_sigill );
141 #   endif
142         return i_capabilities;
143     }
144
145     /* FIXME: this isn't correct, since some 486s have cpuid */
146     i_capabilities |= CPU_CAPABILITY_586;
147
148     /* borrowed from mpeg2dec */
149     b_amd = ( i_ebx == 0x68747541 ) && ( i_ecx == 0x444d4163 )
150                     && ( i_edx == 0x69746e65 );
151
152     /* test for the MMX flag */
153     cpuid( 0x00000001 );
154
155     if( ! (i_edx & 0x00800000) )
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     i_capabilities |= CPU_CAPABILITY_MMX;
165
166     if( i_edx & 0x02000000 )
167     {
168         i_capabilities |= CPU_CAPABILITY_MMXEXT;
169
170 #   ifdef CAN_COMPILE_SSE
171         /* We test if OS supports the SSE instructions */
172         psz_capability = "SSE";
173         i_illegal = 0;
174
175         if( setjmp( env ) == 0 )
176         {
177             /* Test a SSE instruction */
178             __asm__ __volatile__ ( "xorps %%xmm0,%%xmm0\n" : : );
179         }
180
181         if( i_illegal == 0 )
182         {
183             i_capabilities |= CPU_CAPABILITY_SSE;
184         }
185 #   endif
186     }
187
188     if( i_edx & 0x04000000 )
189     {
190 #   if defined(CAN_COMPILE_SSE)
191         /* We test if OS supports the SSE instructions */
192         psz_capability = "SSE2";
193         i_illegal = 0;
194
195         if( setjmp( env ) == 0 )
196         {
197             /* Test a SSE2 instruction */
198             __asm__ __volatile__ ( "movupd %%xmm0, %%xmm0\n" : : );
199         }
200
201         if( i_illegal == 0 )
202         {
203             i_capabilities |= CPU_CAPABILITY_SSE2;
204         }
205 #   endif
206     }
207
208     /* test for additional capabilities */
209     cpuid( 0x80000000 );
210
211     if( i_eax < 0x80000001 )
212     {
213 #   if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW ) \
214      && defined( HAVE_SIGNAL_H )
215         signal( SIGILL, pf_sigill );
216 #   endif
217         return i_capabilities;
218     }
219
220     /* list these additional capabilities */
221     cpuid( 0x80000001 );
222
223 #   ifdef CAN_COMPILE_3DNOW
224     if( i_edx & 0x80000000 )
225     {
226         psz_capability = "3D Now!";
227         i_illegal = 0;
228
229         if( setjmp( env ) == 0 )
230         {
231             /* Test a 3D Now! instruction */
232             __asm__ __volatile__ ( "pfadd %%mm0,%%mm0\n" "femms\n" : : );
233         }
234
235         if( i_illegal == 0 )
236         {
237             i_capabilities |= CPU_CAPABILITY_3DNOW;
238         }
239     }
240 #   endif
241
242     if( b_amd && ( i_edx & 0x00400000 ) )
243     {
244         i_capabilities |= CPU_CAPABILITY_MMXEXT;
245     }
246
247 #   if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW ) \
248      && defined( HAVE_SIGNAL_H )
249     signal( SIGILL, pf_sigill );
250 #   endif
251     return i_capabilities;
252
253 #elif defined( __powerpc__ )
254
255 #   ifdef CAN_COMPILE_ALTIVEC && defined( HAVE_SIGNAL_H )
256     void (*pf_sigill) (int) = signal( SIGILL, SigHandler );
257
258     i_capabilities |= CPU_CAPABILITY_FPU;
259
260     i_illegal = 0;
261
262     if( setjmp( env ) == 0 )
263     {
264         asm volatile ("mtspr 256, %0\n\t"
265                       "vand %%v0, %%v0, %%v0"
266                       :
267                       : "r" (-1));
268     }
269
270     if( i_illegal == 0 )
271     {
272         i_capabilities |= CPU_CAPABILITY_ALTIVEC;
273     }
274
275     signal( SIGILL, pf_sigill );
276 #   endif
277
278     return i_capabilities;
279
280 #elif defined( __sparc__ )
281
282     i_capabilities |= CPU_CAPABILITY_FPU;
283     return i_capabilities;
284
285 #elif defined( _MSC_VER ) && !defined( UNDER_CE )
286     i_capabilities |= CPU_CAPABILITY_FPU;
287     return i_capabilities;
288
289 #else
290     /* default behaviour */
291     return i_capabilities;
292
293 #endif
294 }
295
296 /*****************************************************************************
297  * SigHandler: system signal handler
298  *****************************************************************************
299  * This function is called when an illegal instruction signal is received by
300  * the program. We use this function to test OS and CPU capabilities
301  *****************************************************************************/
302 #if defined( HAVE_SIGNAL_H )
303 static void SigHandler( int i_signal )
304 {
305     /* Acknowledge the signal received */
306     i_illegal = 1;
307
308 #ifdef HAVE_SIGRELSE
309     sigrelse( i_signal );
310 #endif
311
312 #if defined( __i386__ )
313     fprintf( stderr, "warning: your CPU has %s instructions, but not your "
314                      "operating system.\n", psz_capability );
315     fprintf( stderr, "         some optimizations will be disabled unless "
316                      "you upgrade your OS\n" );
317 #   if defined( SYS_LINUX )
318     fprintf( stderr, "         (for instance Linux kernel 2.4.x or later)\n" );
319 #   endif
320 #endif
321
322     longjmp( env, 1 );
323 }
324 #endif
325