]> git.sesse.net Git - vlc/blob - src/misc/cpu.c
* ./src/playlist/playlist.c: lots of playlist behaviour enhancements.
[vlc] / src / misc / cpu.c
1 /*****************************************************************************
2  * cpu.c: CPU detection code
3  *****************************************************************************
4  * Copyright (C) 1998-2002 VideoLAN
5  * $Id: cpu.c,v 1.4 2002/06/07 14:30:41 sam Exp $
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 <signal.h>                               /* SIGHUP, SIGINT, SIGKILL */
30 #include <setjmp.h>                                       /* longjmp, setjmp */
31
32 #include <vlc/vlc.h>
33
34 #ifdef SYS_DARWIN
35 #   include <mach/mach.h>                               /* AltiVec detection */
36 #   include <mach/mach_error.h>       /* some day the header files||compiler *
37                                                        will define it for us */
38 #   include <mach/bootstrap.h>
39 #endif
40
41 #include "vlc_cpu.h"
42
43 /*****************************************************************************
44  * Local prototypes
45  *****************************************************************************/
46 static void IllegalSignalHandler( int i_signal );
47
48 /*****************************************************************************
49  * Global variables - they're needed for signal handling
50  *****************************************************************************/
51 static jmp_buf env;
52 static int     i_illegal;
53 #if defined( __i386__ )
54 static char   *psz_capability;
55 #endif
56
57 /*****************************************************************************
58  * CPUCapabilities: list the processors MMX support and other capabilities
59  *****************************************************************************
60  * This function is called to list extensions the CPU may have.
61  *****************************************************************************/
62 u32 __CPUCapabilities( vlc_object_t *p_this )
63 {
64     volatile u32 i_capabilities = CPU_CAPABILITY_NONE;
65
66 #if defined( SYS_DARWIN )
67     struct host_basic_info hi;
68     kern_return_t          ret;
69     host_name_port_t       host;
70
71     int i_size;
72     char *psz_name, *psz_subname;
73
74     i_capabilities |= CPU_CAPABILITY_FPU;
75
76     /* Should 'never' fail? */
77     host = mach_host_self();
78
79     i_size = sizeof( hi ) / sizeof( int );
80     ret = host_info( host, HOST_BASIC_INFO, ( host_info_t )&hi, &i_size );
81
82     if( ret != KERN_SUCCESS )
83     {
84         fprintf( stderr, "error: couldn't get CPU information\n" );
85         return i_capabilities;
86     }
87
88     slot_name( hi.cpu_type, hi.cpu_subtype, &psz_name, &psz_subname );
89     /* FIXME: need better way to detect newer proccessors.
90      * could do strncmp(a,b,5), but that's real ugly */
91     if( !strcmp(psz_name, "ppc7400") || !strcmp(psz_name, "ppc7450") )
92     {
93         i_capabilities |= CPU_CAPABILITY_ALTIVEC;
94     }
95
96     return i_capabilities;
97
98 #elif defined( __i386__ )
99     volatile unsigned int  i_eax, i_ebx, i_ecx, i_edx;
100     volatile vlc_bool_t    b_amd;
101
102     /* Needed for x86 CPU capabilities detection */
103 #   define cpuid( a )                      \
104         asm volatile ( "pushl %%ebx\n\t"   \
105                        "cpuid\n\t"         \
106                        "movl %%ebx,%1\n\t" \
107                        "popl %%ebx\n\t"    \
108                      : "=a" ( i_eax ),     \
109                        "=r" ( i_ebx ),     \
110                        "=c" ( i_ecx ),     \
111                        "=d" ( i_edx )      \
112                      : "a"  ( a )          \
113                      : "cc" );
114
115     i_capabilities |= CPU_CAPABILITY_FPU;
116
117 #   if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
118     signal( SIGILL, IllegalSignalHandler );
119 #   endif
120
121     /* test for a 486 CPU */
122     asm volatile ( "pushl %%ebx\n\t"
123                    "pushfl\n\t"
124                    "popl %%eax\n\t"
125                    "movl %%eax, %%ebx\n\t"
126                    "xorl $0x200000, %%eax\n\t"
127                    "pushl %%eax\n\t"
128                    "popfl\n\t"
129                    "pushfl\n\t"
130                    "popl %%eax\n\t"
131                    "movl %%ebx,%1\n\t"
132                    "popl %%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         signal( SIGILL, NULL );
142 #   endif
143         return i_capabilities;
144     }
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         signal( SIGILL, NULL );
155 #   endif
156         return i_capabilities;
157     }
158
159     /* FIXME: this isn't correct, since some 486s have cpuid */
160     i_capabilities |= CPU_CAPABILITY_586;
161
162     /* borrowed from mpeg2dec */
163     b_amd = ( i_ebx == 0x68747541 ) && ( i_ecx == 0x444d4163 )
164                     && ( i_edx == 0x69746e65 );
165
166     /* test for the MMX flag */
167     cpuid( 0x00000001 );
168
169     if( ! (i_edx & 0x00800000) )
170     {
171 #   if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
172         signal( SIGILL, NULL );
173 #   endif
174         return i_capabilities;
175     }
176
177     i_capabilities |= CPU_CAPABILITY_MMX;
178
179     if( i_edx & 0x02000000 )
180     {
181         i_capabilities |= CPU_CAPABILITY_MMXEXT;
182
183 #   ifdef CAN_COMPILE_SSE
184         /* We test if OS supports the SSE instructions */
185         psz_capability = "SSE";
186         i_illegal = 0;
187
188         vlc_mutex_lock( p_this->p_vlc->p_global_lock );
189         if( setjmp( env ) == 0 )
190         {
191             /* Test a SSE instruction */
192             __asm__ __volatile__ ( "xorps %%xmm0,%%xmm0\n" : : );
193         }
194         vlc_mutex_unlock( p_this->p_vlc->p_global_lock );
195
196         if( i_illegal == 0 )
197         {
198             i_capabilities |= CPU_CAPABILITY_SSE;
199         }
200 #   endif
201     }
202
203     /* test for additional capabilities */
204     cpuid( 0x80000000 );
205
206     if( i_eax < 0x80000001 )
207     {
208 #   if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
209         signal( SIGILL, NULL );
210 #   endif
211         return i_capabilities;
212     }
213
214     /* list these additional capabilities */
215     cpuid( 0x80000001 );
216
217 #   ifdef CAN_COMPILE_3DNOW
218     if( i_edx & 0x80000000 )
219     {
220         psz_capability = "3D Now!";
221         i_illegal = 0;
222
223         vlc_mutex_lock( p_this->p_vlc->p_global_lock );
224         if( setjmp( env ) == 0 )
225         {
226             /* Test a 3D Now! instruction */
227             __asm__ __volatile__ ( "pfadd %%mm0,%%mm0\n" "femms\n" : : );
228         }
229         vlc_mutex_unlock( p_this->p_vlc->p_global_lock );
230
231         if( i_illegal == 0 )
232         {
233             i_capabilities |= CPU_CAPABILITY_3DNOW;
234         }
235     }
236 #   endif
237
238     if( b_amd && ( i_edx & 0x00400000 ) )
239     {
240         i_capabilities |= CPU_CAPABILITY_MMXEXT;
241     }
242
243 #   if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
244     signal( SIGILL, NULL );
245 #   endif
246     return i_capabilities;
247
248 #elif defined( __powerpc__ )
249
250     i_capabilities |= CPU_CAPABILITY_FPU;
251
252 #   ifdef CAN_COMPILE_ALTIVEC
253     signal( SIGILL, IllegalSignalHandler );
254
255     i_illegal = 0;
256
257     vlc_mutex_lock( p_this->p_vlc->p_global_lock );
258     if( setjmp( env ) == 0 )
259     {
260         asm volatile ("mtspr 256, %0\n\t"
261                       "vand %%v0, %%v0, %%v0"
262                       :
263                       : "r" (-1));
264     }
265     vlc_mutex_unlock( p_this->p_vlc->p_global_lock );
266
267     if( i_illegal == 0 )
268     {
269         i_capabilities |= CPU_CAPABILITY_ALTIVEC;
270     }
271
272     signal( SIGILL, NULL );
273 #   endif
274
275     return i_capabilities;
276
277 #elif defined( __sparc__ )
278
279     i_capabilities |= CPU_CAPABILITY_FPU;
280     return i_capabilities;
281
282 #else
283     /* default behaviour */
284     return i_capabilities;
285
286 #endif
287 }
288
289 /*****************************************************************************
290  * IllegalSignalHandler: system signal handler
291  *****************************************************************************
292  * This function is called when an illegal instruction signal is received by
293  * the program. We use this function to test OS and CPU capabilities
294  *****************************************************************************/
295 static void IllegalSignalHandler( int i_signal )
296 {
297     /* Acknowledge the signal received */
298     i_illegal = 1;
299
300 #ifdef HAVE_SIGRELSE
301     sigrelse( i_signal );
302 #endif
303
304 #if defined( __i386__ )
305     fprintf( stderr, "warning: your CPU has %s instructions, but not your "
306                      "operating system.\n", psz_capability );
307     fprintf( stderr, "         some optimizations will be disabled unless "
308                      "you upgrade your OS\n" );
309 #   if defined( SYS_LINUX )
310     fprintf( stderr, "         (for instance Linux kernel 2.4.x or later)\n" );
311 #   endif
312 #endif
313
314     longjmp( env, 1 );
315 }
316