]> git.sesse.net Git - vlc/blob - src/misc/cpu.c
Remove unused 486 and 586 capabilities
[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 <sys/types.h>
36 #ifndef WIN32
37 #include <unistd.h>
38 #include <sys/wait.h>
39 #endif
40
41 #include "libvlc.h"
42
43 #if defined(__APPLE__) && (defined(__ppc__) || defined(__ppc64__))
44 #include <sys/sysctl.h>
45 #endif
46
47 #if defined( __i386__ ) || defined( __x86_64__ ) || defined( __powerpc__ ) \
48  || defined( __ppc__ ) || defined( __ppc64__ ) || defined( __powerpc64__ )
49 static bool check_OS_capability( const char *psz_capability, pid_t pid )
50 {
51 #ifndef WIN32
52     int status;
53
54     if( pid == -1 )
55         return false; /* fail safe :-/ */
56
57     while( waitpid( pid, &status, 0 ) == -1 );
58
59     if( WIFEXITED( status ) && WEXITSTATUS( status ) == 0 )
60         return true;
61
62     fprintf( stderr, "warning: your CPU has %s instructions, but not your "
63                      "operating system.\n", psz_capability );
64     fprintf( stderr, "         some optimizations will be disabled unless "
65                      "you upgrade your OS\n" );
66     return false;
67 #else
68 # warning FIXME!
69 # define fork() (errno = ENOSYS, -1)
70     (void)pid;
71     (void)psz_capability;
72     return true;
73 #endif
74 }
75 #endif
76
77 /*****************************************************************************
78  * CPUCapabilities: get the CPU capabilities
79  *****************************************************************************
80  * This function is called to list extensions the CPU may have.
81  *****************************************************************************/
82 uint32_t CPUCapabilities( void )
83 {
84     uint32_t i_capabilities = CPU_CAPABILITY_NONE;
85
86 #if defined( __i386__ ) || defined( __x86_64__ )
87      unsigned int i_eax, i_ebx, i_ecx, i_edx;
88      bool b_amd;
89
90     /* Needed for x86 CPU capabilities detection */
91 #   if defined( __x86_64__ )
92 #       define cpuid( reg )                    \
93             asm volatile ( "cpuid\n\t"         \
94                            "movl %%ebx,%1\n\t" \
95                          : "=a" ( i_eax ),     \
96                            "=b" ( i_ebx ),     \
97                            "=c" ( i_ecx ),     \
98                            "=d" ( i_edx )      \
99                          : "a"  ( reg )        \
100                          : "cc" );
101 #   else
102 #       define cpuid( reg )                    \
103             asm volatile ( "push %%ebx\n\t"    \
104                            "cpuid\n\t"         \
105                            "movl %%ebx,%1\n\t" \
106                            "pop %%ebx\n\t"     \
107                          : "=a" ( i_eax ),     \
108                            "=r" ( i_ebx ),     \
109                            "=c" ( i_ecx ),     \
110                            "=d" ( i_edx )      \
111                          : "a"  ( reg )        \
112                          : "cc" );
113 #   endif
114
115     i_capabilities |= CPU_CAPABILITY_FPU;
116
117 # if defined (__i386__) && !defined (__i486__) && !defined (__i586__) \
118   && !defined (__i686__) && !defined (__pentium4__) \
119   && !defined (__k6__) && !defined (__athlon__) && !defined (__k8__)
120     /* check if cpuid instruction is supported */
121     asm volatile ( "push %%ebx\n\t"
122                    "pushf\n\t"
123                    "pop %%eax\n\t"
124                    "movl %%eax, %%ebx\n\t"
125                    "xorl $0x200000, %%eax\n\t"
126                    "push %%eax\n\t"
127                    "popf\n\t"
128                    "pushf\n\t"
129                    "pop %%eax\n\t"
130                    "movl %%ebx,%1\n\t"
131                    "pop %%ebx\n\t"
132                  : "=a" ( i_eax ),
133                    "=r" ( i_ebx )
134                  :
135                  : "cc" );
136
137     if( i_eax == i_ebx )
138         goto out;
139 # endif
140
141     /* the CPU supports the CPUID instruction - get its level */
142     cpuid( 0x00000000 );
143
144 # if defined (__i386__) && !defined (__i586__) \
145   && !defined (__i686__) && !defined (__pentium4__) \
146   && !defined (__k6__) && !defined (__athlon__) && !defined (__k8__)
147     if( !i_eax )
148         goto out;
149 #endif
150
151     /* borrowed from mpeg2dec */
152     b_amd = ( i_ebx == 0x68747541 ) && ( i_ecx == 0x444d4163 )
153                     && ( i_edx == 0x69746e65 );
154
155     /* test for the MMX flag */
156     cpuid( 0x00000001 );
157 # if !defined (__MMX__)
158     if( ! (i_edx & 0x00800000) )
159         goto out;
160 # endif
161     i_capabilities |= CPU_CAPABILITY_MMX;
162
163 # if defined (__SSE__)
164     i_capabilities |= CPU_CAPABILITY_MMXEXT | CPU_CAPABILITY_SSE;
165 # else
166     if( i_edx & 0x02000000 )
167     {
168         i_capabilities |= CPU_CAPABILITY_MMXEXT;
169
170 #   ifdef CAN_COMPILE_SSE
171         /* We test if OS supports the SSE instructions */
172         pid_t pid = fork();
173         if( pid == 0 )
174         {
175             /* Test a SSE instruction */
176             __asm__ __volatile__ ( "xorps %%xmm0,%%xmm0\n" : : );
177             exit(0);
178         }
179         if( check_OS_capability( "SSE", pid ) )
180             i_capabilities |= CPU_CAPABILITY_SSE;
181 #   endif
182     }
183 # endif
184
185 # if defined (__SSE2__)
186     i_capabilities |= CPU_CAPABILITY_SSE2;
187 # elif defined (CAN_COMPILE_SSE)
188     if( i_edx & 0x04000000 )
189     {
190         /* We test if OS supports the SSE2 instructions */
191         pid_t pid = fork();
192         if( pid == 0 )
193         {
194             /* Test a SSE2 instruction */
195             __asm__ __volatile__ ( "movupd %%xmm0, %%xmm0\n" : : );
196             exit(0);
197         }
198         if( check_OS_capability( "SSE2", pid ) )
199             i_capabilities |= CPU_CAPABILITY_SSE2;
200     }
201 # endif
202
203     /* test for additional capabilities */
204     cpuid( 0x80000000 );
205
206     if( i_eax < 0x80000001 )
207         goto out;
208
209     /* list these additional capabilities */
210     cpuid( 0x80000001 );
211
212 # if defined (__3dNOW__)
213     i_capabilities |= CPU_CAPABILITY_3DNOW;
214 # elif defined (CAN_COMPILE_3DNOW)
215     if( i_edx & 0x80000000 )
216     {
217         pid_t pid = fork();
218         if( pid == 0 )
219         {
220             /* Test a 3D Now! instruction */
221             __asm__ __volatile__ ( "pfadd %%mm0,%%mm0\n" "femms\n" : : );
222             exit(0);
223         }
224         if( check_OS_capability( "3D Now!", pid ) )
225             i_capabilities |= CPU_CAPABILITY_3DNOW;
226     }
227 # endif
228
229     if( b_amd && ( i_edx & 0x00400000 ) )
230     {
231         i_capabilities |= CPU_CAPABILITY_MMXEXT;
232     }
233 out:
234
235 #elif defined( __arm__ )
236 #   if defined( __ARM_EABI__ ) && !defined( __SOFTFP__ )
237 //    i_capabilities |= CPU_CAPABILITY_FPU;
238 #   endif
239 #   if defined( __ARM_NEON__ )
240     i_capabilities |= CPU_CAPABILITY_NEON;
241 #   endif
242
243 #elif defined( __powerpc__ ) || defined( __ppc__ ) || defined( __powerpc64__ ) \
244     || defined( __ppc64__ )
245
246     i_capabilities |= CPU_CAPABILITY_FPU;
247
248 #   if defined(__APPLE__)
249     int selectors[2] = { CTL_HW, HW_VECTORUNIT };
250     int i_has_altivec = 0;
251     size_t i_length = sizeof( i_has_altivec );
252     int i_error = sysctl( selectors, 2, &i_has_altivec, &i_length, NULL, 0);
253
254     if( i_error == 0 && i_has_altivec != 0 )
255         i_capabilities |= CPU_CAPABILITY_ALTIVEC;
256
257 #   elif defined( CAN_COMPILE_ALTIVEC )
258     pid_t pid = fork();
259     if( pid == 0 )
260     {
261         asm volatile ("mtspr 256, %0\n\t"
262                       "vand %%v0, %%v0, %%v0"
263                       :
264                       : "r" (-1));
265         exit(0);
266     }
267
268     if( check_OS_capability( "Altivec", pid ) )
269         i_capabilities |= CPU_CAPABILITY_ALTIVEC;
270
271 #   endif
272
273 #elif defined( __sparc__ )
274     i_capabilities |= CPU_CAPABILITY_FPU;
275
276 #elif defined( _MSC_VER ) && !defined( UNDER_CE )
277     i_capabilities |= CPU_CAPABILITY_FPU;
278
279 #endif
280     return i_capabilities;
281 }
282
283 uint32_t cpu_flags = 0;
284
285
286 /*****************************************************************************
287  * vlc_CPU: get pre-computed CPU capability flags
288  ****************************************************************************/
289 unsigned vlc_CPU (void)
290 {
291     return cpu_flags;
292 }
293
294 static vlc_memcpy_t pf_vlc_memcpy = memcpy;
295 static vlc_memset_t pf_vlc_memset = memset;
296
297 void vlc_fastmem_register (vlc_memcpy_t cpy, vlc_memset_t set)
298 {
299     if (cpy)
300         pf_vlc_memcpy = cpy;
301     if (set)
302         pf_vlc_memset = set;
303 }
304
305 /**
306  * vlc_memcpy: fast CPU-dependent memcpy
307  */
308 void *vlc_memcpy (void *tgt, const void *src, size_t n)
309 {
310     return pf_vlc_memcpy (tgt, src, n);
311 }
312
313 /**
314  * vlc_memset: fast CPU-dependent memset
315  */
316 void *vlc_memset (void *tgt, int c, size_t n)
317 {
318     return pf_vlc_memset (tgt, c, n);
319 }