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