]> git.sesse.net Git - vlc/blob - src/misc/cpu.c
Compile-time detection for FPU on ARM EABI
[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( __i386__ ) || defined( __x86_64__ )
68     volatile unsigned int  i_eax, i_ebx, i_ecx, i_edx;
69     volatile bool    b_amd;
70
71     /* Needed for x86 CPU capabilities detection */
72 #   if defined( __x86_64__ )
73 #       define cpuid( reg )                    \
74             asm volatile ( "cpuid\n\t"         \
75                            "movl %%ebx,%1\n\t" \
76                          : "=a" ( i_eax ),     \
77                            "=b" ( i_ebx ),     \
78                            "=c" ( i_ecx ),     \
79                            "=d" ( i_edx )      \
80                          : "a"  ( reg )        \
81                          : "cc" );
82 #   else
83 #       define cpuid( reg )                    \
84             asm volatile ( "push %%ebx\n\t"    \
85                            "cpuid\n\t"         \
86                            "movl %%ebx,%1\n\t" \
87                            "pop %%ebx\n\t"     \
88                          : "=a" ( i_eax ),     \
89                            "=r" ( i_ebx ),     \
90                            "=c" ( i_ecx ),     \
91                            "=d" ( i_edx )      \
92                          : "a"  ( reg )        \
93                          : "cc" );
94 #   endif
95
96 #   if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
97     void (*pf_sigill) (int) = signal( SIGILL, SigHandler );
98 #   endif
99
100     i_capabilities |= CPU_CAPABILITY_FPU;
101
102 #   if defined( __i386__ )
103     /* check if cpuid instruction is supported */
104     asm volatile ( "push %%ebx\n\t"
105                    "pushf\n\t"
106                    "pop %%eax\n\t"
107                    "movl %%eax, %%ebx\n\t"
108                    "xorl $0x200000, %%eax\n\t"
109                    "push %%eax\n\t"
110                    "popf\n\t"
111                    "pushf\n\t"
112                    "pop %%eax\n\t"
113                    "movl %%ebx,%1\n\t"
114                    "pop %%ebx\n\t"
115                  : "=a" ( i_eax ),
116                    "=r" ( i_ebx )
117                  :
118                  : "cc" );
119
120     if( i_eax == i_ebx )
121         goto out;
122 #   else
123     /* x86_64 supports cpuid instruction, so we dont need to check it */
124 #   endif
125
126     i_capabilities |= CPU_CAPABILITY_486;
127
128     /* the CPU supports the CPUID instruction - get its level */
129     cpuid( 0x00000000 );
130
131     if( !i_eax )
132         goto out;
133
134     /* FIXME: this isn't correct, since some 486s have cpuid */
135     i_capabilities |= CPU_CAPABILITY_586;
136
137     /* borrowed from mpeg2dec */
138     b_amd = ( i_ebx == 0x68747541 ) && ( i_ecx == 0x444d4163 )
139                     && ( i_edx == 0x69746e65 );
140
141     /* test for the MMX flag */
142     cpuid( 0x00000001 );
143
144     if( ! (i_edx & 0x00800000) )
145         goto out;
146
147     i_capabilities |= CPU_CAPABILITY_MMX;
148
149     if( i_edx & 0x02000000 )
150     {
151         i_capabilities |= CPU_CAPABILITY_MMXEXT;
152
153 #   ifdef CAN_COMPILE_SSE
154         /* We test if OS supports the SSE instructions */
155         psz_capability = "SSE";
156         i_illegal = 0;
157
158         if( setjmp( env ) == 0 )
159         {
160             /* Test a SSE instruction */
161             __asm__ __volatile__ ( "xorps %%xmm0,%%xmm0\n" : : );
162         }
163
164         if( i_illegal == 0 )
165         {
166             i_capabilities |= CPU_CAPABILITY_SSE;
167         }
168 #   endif
169     }
170
171     if( i_edx & 0x04000000 )
172     {
173 #   if defined(CAN_COMPILE_SSE)
174         /* We test if OS supports the SSE instructions */
175         psz_capability = "SSE2";
176         i_illegal = 0;
177
178         if( setjmp( env ) == 0 )
179         {
180             /* Test a SSE2 instruction */
181             __asm__ __volatile__ ( "movupd %%xmm0, %%xmm0\n" : : );
182         }
183
184         if( i_illegal == 0 )
185         {
186             i_capabilities |= CPU_CAPABILITY_SSE2;
187         }
188 #   endif
189     }
190
191     /* test for additional capabilities */
192     cpuid( 0x80000000 );
193
194     if( i_eax < 0x80000001 )
195         goto out;
196
197     /* list these additional capabilities */
198     cpuid( 0x80000001 );
199
200 #   ifdef CAN_COMPILE_3DNOW
201     if( i_edx & 0x80000000 )
202     {
203         psz_capability = "3D Now!";
204         i_illegal = 0;
205
206         if( setjmp( env ) == 0 )
207         {
208             /* Test a 3D Now! instruction */
209             __asm__ __volatile__ ( "pfadd %%mm0,%%mm0\n" "femms\n" : : );
210         }
211
212         if( i_illegal == 0 )
213         {
214             i_capabilities |= CPU_CAPABILITY_3DNOW;
215         }
216     }
217 #   endif
218
219     if( b_amd && ( i_edx & 0x00400000 ) )
220     {
221         i_capabilities |= CPU_CAPABILITY_MMXEXT;
222     }
223
224 out:
225 #   if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW )
226     signal( SIGILL, pf_sigill );
227 #   endif
228
229 #elif defined( __arm__ )
230 #   if defined( __ARM_EABI__ ) && !defined( __SOFTFP__ )
231     i_capabilities |= CPU_CAPABILITY_FPU;
232 #   endif
233     return i_capabilities;
234
235 #elif defined( __powerpc__ ) || defined( __ppc__ ) || defined( __ppc64__ )
236
237     i_capabilities |= CPU_CAPABILITY_FPU;
238
239 #   if defined(__APPLE__)
240     int selectors[2] = { CTL_HW, HW_VECTORUNIT };
241     int i_has_altivec = 0;
242     size_t i_length = sizeof( i_has_altivec );
243     int i_error = sysctl( selectors, 2, &i_has_altivec, &i_length, NULL, 0);
244
245     if( i_error == 0 && i_has_altivec != 0 )
246         i_capabilities |= CPU_CAPABILITY_ALTIVEC;
247
248 #   elif defined( CAN_COMPILE_ALTIVEC )
249     void (*pf_sigill) (int) = signal( SIGILL, SigHandler );
250
251     i_illegal = 0;
252
253     if( setjmp( env ) == 0 )
254     {
255         asm volatile ("mtspr 256, %0\n\t"
256                       "vand %%v0, %%v0, %%v0"
257                       :
258                       : "r" (-1));
259     }
260
261     if( i_illegal == 0 )
262         i_capabilities |= CPU_CAPABILITY_ALTIVEC;
263
264     signal( SIGILL, pf_sigill );
265 #   else
266     (void)SigHandler; /* Don't complain about dead code here */
267 #   endif
268
269 #elif defined( __sparc__ )
270     i_capabilities |= CPU_CAPABILITY_FPU;
271
272 #elif defined( _MSC_VER ) && !defined( UNDER_CE )
273     i_capabilities |= CPU_CAPABILITY_FPU;
274
275 #endif
276     return i_capabilities;
277 }
278
279 /*****************************************************************************
280  * SigHandler: system signal handler
281  *****************************************************************************
282  * This function is called when an illegal instruction signal is received by
283  * the program. We use this function to test OS and CPU capabilities
284  *****************************************************************************/
285 static void SigHandler( int i_signal )
286 {
287     /* Acknowledge the signal received */
288     i_illegal = 1;
289
290 #ifdef HAVE_SIGRELSE
291     sigrelse( i_signal );
292 #else
293     VLC_UNUSED( i_signal );
294 #endif
295
296 #if defined( __i386__ )
297     fprintf( stderr, "warning: your CPU has %s instructions, but not your "
298                      "operating system.\n", psz_capability );
299     fprintf( stderr, "         some optimizations will be disabled unless "
300                      "you upgrade your OS\n" );
301 #   if defined( __linux__ )
302     fprintf( stderr, "         (for instance Linux kernel 2.4.x or later)\n" );
303 #   endif
304 #endif
305
306     longjmp( env, 1 );
307 }
308
309
310 uint32_t cpu_flags = 0;
311
312
313 /*****************************************************************************
314  * vlc_CPU: get pre-computed CPU capability flags
315  ****************************************************************************/
316 unsigned vlc_CPU (void)
317 {
318     return cpu_flags;
319 }
320
321 static vlc_memcpy_t pf_vlc_memcpy = memcpy;
322 static vlc_memset_t pf_vlc_memset = memset;
323
324 void vlc_fastmem_register (vlc_memcpy_t cpy, vlc_memset_t set)
325 {
326     if (cpy)
327         pf_vlc_memcpy = cpy;
328     if (set)
329         pf_vlc_memset = set;
330 }
331
332 /**
333  * vlc_memcpy: fast CPU-dependent memcpy
334  */
335 void *vlc_memcpy (void *tgt, const void *src, size_t n)
336 {
337     return pf_vlc_memcpy (tgt, src, n);
338 }
339
340 /**
341  * vlc_memset: fast CPU-dependent memset
342  */
343 void *vlc_memset (void *tgt, int c, size_t n)
344 {
345     return pf_vlc_memset (tgt, c, n);
346 }