]> git.sesse.net Git - vlc/blob - src/misc/cpu.c
Remove useless check for (C89) <signal.h>
[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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc_common.h>
34
35 #include <signal.h>                            /* SIGHUP, SIGINT, SIGKILL */
36 #include <setjmp.h>                                    /* longjmp, setjmp */
37
38 #include "libvlc.h"
39
40 #if defined(__APPLE__) && (defined(__ppc__) || defined(__ppc64__))
41 #include <sys/sysctl.h>
42 #endif
43
44 /*****************************************************************************
45  * Local prototypes
46  *****************************************************************************/
47 static void SigHandler   ( int );
48
49 /*****************************************************************************
50  * Global variables - they're needed for signal handling
51  *****************************************************************************/
52 static jmp_buf env;
53 static int     i_illegal;
54 #if defined( __i386__ ) || defined( __x86_64__ )
55 static const char *psz_capability;
56 #endif
57
58 /*****************************************************************************
59  * CPUCapabilities: get the CPU capabilities
60  *****************************************************************************
61  * This function is called to list extensions the CPU may have.
62  *****************************************************************************/
63 uint32_t CPUCapabilities( void )
64 {
65     volatile uint32_t i_capabilities = CPU_CAPABILITY_NONE;
66
67 #if defined(__APPLE__) && (defined(__ppc__) || defined(__ppc64__))
68     int selectors[2] = { CTL_HW, HW_VECTORUNIT };
69     int i_has_altivec = 0;
70     size_t i_length = sizeof( i_has_altivec );
71     int i_error = sysctl( selectors, 2, &i_has_altivec, &i_length, NULL, 0);
72
73     i_capabilities |= CPU_CAPABILITY_FPU;
74
75     if( i_error == 0 && i_has_altivec != 0 )
76         i_capabilities |= CPU_CAPABILITY_ALTIVEC;
77
78     return i_capabilities;
79
80 #elif defined( __i386__ ) || defined( __x86_64__ )
81     volatile unsigned int  i_eax, i_ebx, i_ecx, i_edx;
82     volatile bool    b_amd;
83
84     /* Needed for x86 CPU capabilities detection */
85 #   if defined( __x86_64__ )
86 #       define cpuid( reg )                    \
87             asm volatile ( "cpuid\n\t"         \
88                            "movl %%ebx,%1\n\t" \
89                          : "=a" ( i_eax ),     \
90                            "=b" ( i_ebx ),     \
91                            "=c" ( i_ecx ),     \
92                            "=d" ( i_edx )      \
93                          : "a"  ( reg )        \
94                          : "cc" );
95 #   else
96 #       define cpuid( reg )                    \
97             asm volatile ( "push %%ebx\n\t"    \
98                            "cpuid\n\t"         \
99                            "movl %%ebx,%1\n\t" \
100                            "pop %%ebx\n\t"     \
101                          : "=a" ( i_eax ),     \
102                            "=r" ( i_ebx ),     \
103                            "=c" ( i_ecx ),     \
104                            "=d" ( i_edx )      \
105                          : "a"  ( reg )        \
106                          : "cc" );
107 #   endif
108
109 #   if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
110     void (*pf_sigill) (int) = signal( SIGILL, SigHandler );
111 #   endif
112
113     i_capabilities |= CPU_CAPABILITY_FPU;
114
115 #   if defined( __i386__ )
116     /* check if cpuid instruction is supported */
117     asm volatile ( "push %%ebx\n\t"
118                    "pushf\n\t"
119                    "pop %%eax\n\t"
120                    "movl %%eax, %%ebx\n\t"
121                    "xorl $0x200000, %%eax\n\t"
122                    "push %%eax\n\t"
123                    "popf\n\t"
124                    "pushf\n\t"
125                    "pop %%eax\n\t"
126                    "movl %%ebx,%1\n\t"
127                    "pop %%ebx\n\t"
128                  : "=a" ( i_eax ),
129                    "=r" ( i_ebx )
130                  :
131                  : "cc" );
132
133     if( i_eax == i_ebx )
134     {
135 #       if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
136         signal( SIGILL, pf_sigill );
137 #       endif
138         return i_capabilities;
139     }
140 #   else
141     /* x86_64 supports cpuid instruction, so we dont need to check it */
142 #   endif
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, pf_sigill );
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, pf_sigill );
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         if( setjmp( env ) == 0 )
187         {
188             /* Test a SSE instruction */
189             __asm__ __volatile__ ( "xorps %%xmm0,%%xmm0\n" : : );
190         }
191
192         if( i_illegal == 0 )
193         {
194             i_capabilities |= CPU_CAPABILITY_SSE;
195         }
196 #   endif
197     }
198
199     if( i_edx & 0x04000000 )
200     {
201 #   if defined(CAN_COMPILE_SSE)
202         /* We test if OS supports the SSE instructions */
203         psz_capability = "SSE2";
204         i_illegal = 0;
205
206         if( setjmp( env ) == 0 )
207         {
208             /* Test a SSE2 instruction */
209             __asm__ __volatile__ ( "movupd %%xmm0, %%xmm0\n" : : );
210         }
211
212         if( i_illegal == 0 )
213         {
214             i_capabilities |= CPU_CAPABILITY_SSE2;
215         }
216 #   endif
217     }
218
219     /* test for additional capabilities */
220     cpuid( 0x80000000 );
221
222     if( i_eax < 0x80000001 )
223     {
224 #   if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
225         signal( SIGILL, pf_sigill );
226 #   endif
227         return i_capabilities;
228     }
229
230     /* list these additional capabilities */
231     cpuid( 0x80000001 );
232
233 #   ifdef CAN_COMPILE_3DNOW
234     if( i_edx & 0x80000000 )
235     {
236         psz_capability = "3D Now!";
237         i_illegal = 0;
238
239         if( setjmp( env ) == 0 )
240         {
241             /* Test a 3D Now! instruction */
242             __asm__ __volatile__ ( "pfadd %%mm0,%%mm0\n" "femms\n" : : );
243         }
244
245         if( i_illegal == 0 )
246         {
247             i_capabilities |= CPU_CAPABILITY_3DNOW;
248         }
249     }
250 #   endif
251
252     if( b_amd && ( i_edx & 0x00400000 ) )
253     {
254         i_capabilities |= CPU_CAPABILITY_MMXEXT;
255     }
256
257 #   if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
258     signal( SIGILL, pf_sigill );
259 #   endif
260     return i_capabilities;
261
262 #elif defined( __powerpc__ ) || defined( __ppc__ ) || defined( __ppc64__ )
263
264 #   ifdef CAN_COMPILE_ALTIVEC
265     void (*pf_sigill) (int) = signal( SIGILL, SigHandler );
266
267     i_capabilities |= CPU_CAPABILITY_FPU;
268
269     i_illegal = 0;
270
271     if( setjmp( env ) == 0 )
272     {
273         asm volatile ("mtspr 256, %0\n\t"
274                       "vand %%v0, %%v0, %%v0"
275                       :
276                       : "r" (-1));
277     }
278
279     if( i_illegal == 0 )
280     {
281         i_capabilities |= CPU_CAPABILITY_ALTIVEC;
282     }
283
284     signal( SIGILL, pf_sigill );
285 #   else
286     (void)SigHandler; /* Don't complain about dead code here */
287 #   endif
288
289     return i_capabilities;
290
291 #elif defined( __sparc__ )
292
293     i_capabilities |= CPU_CAPABILITY_FPU;
294     return i_capabilities;
295
296 #elif defined( _MSC_VER ) && !defined( UNDER_CE )
297     i_capabilities |= CPU_CAPABILITY_FPU;
298     return i_capabilities;
299
300 #else
301     /* default behaviour */
302     return i_capabilities;
303
304 #endif
305 }
306
307 /*****************************************************************************
308  * SigHandler: system signal handler
309  *****************************************************************************
310  * This function is called when an illegal instruction signal is received by
311  * the program. We use this function to test OS and CPU capabilities
312  *****************************************************************************/
313 static void SigHandler( int i_signal )
314 {
315     /* Acknowledge the signal received */
316     i_illegal = 1;
317
318 #ifdef HAVE_SIGRELSE
319     sigrelse( i_signal );
320 #else
321     VLC_UNUSED( i_signal );
322 #endif
323
324 #if defined( __i386__ )
325     fprintf( stderr, "warning: your CPU has %s instructions, but not your "
326                      "operating system.\n", psz_capability );
327     fprintf( stderr, "         some optimizations will be disabled unless "
328                      "you upgrade your OS\n" );
329 #   if defined( __linux__ )
330     fprintf( stderr, "         (for instance Linux kernel 2.4.x or later)\n" );
331 #   endif
332 #endif
333
334     longjmp( env, 1 );
335 }
336
337
338 uint32_t cpu_flags = 0;
339
340
341 /*****************************************************************************
342  * vlc_CPU: get pre-computed CPU capability flags
343  ****************************************************************************/
344 unsigned vlc_CPU (void)
345 {
346     return cpu_flags;
347 }
348
349 static vlc_memcpy_t pf_vlc_memcpy = memcpy;
350 static vlc_memset_t pf_vlc_memset = memset;
351
352 void vlc_fastmem_register (vlc_memcpy_t cpy, vlc_memset_t set)
353 {
354     if (cpy)
355         pf_vlc_memcpy = cpy;
356     if (set)
357         pf_vlc_memset = set;
358 }
359
360 /**
361  * vlc_memcpy: fast CPU-dependent memcpy
362  */
363 void *vlc_memcpy (void *tgt, const void *src, size_t n)
364 {
365     return pf_vlc_memcpy (tgt, src, n);
366 }
367
368 /**
369  * vlc_memset: fast CPU-dependent memset
370  */
371 void *vlc_memset (void *tgt, int c, size_t n)
372 {
373     return pf_vlc_memset (tgt, c, n);
374 }