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