cpu.c 11 KB
Newer Older
1
2
3
/*****************************************************************************
 * cpu.c: CPU detection code
 *****************************************************************************
4
 * Copyright (C) 1998-2004 the VideoLAN team
5
 * $Id$
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 *
 * Authors: Samuel Hocevar <sam@zoy.org>
 *          Christophe Massiot <massiot@via.ecp.fr>
 *          Eugenio Jarosiewicz <ej0@cise.ufl.eduEujenio>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
dionoea's avatar
dionoea committed
23
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24
25
26
27
28
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
29
30
31
32
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

33
34
#include <vlc/vlc.h>

35
36
37
38
39
#ifdef HAVE_SIGNAL_H
#   include <signal.h>                            /* SIGHUP, SIGINT, SIGKILL */
#   include <setjmp.h>                                    /* longjmp, setjmp */
#endif

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
40
41
#include "libvlc.h"

42
#if defined(__APPLE__) && (defined(__ppc__) || defined(__ppc64__))
43
#include <sys/sysctl.h>
44
45
46
47
48
#endif

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
49
#ifdef HAVE_SIGNAL_H
50
static void SigHandler   ( int );
51
#endif
52
53
54
55

/*****************************************************************************
 * Global variables - they're needed for signal handling
 *****************************************************************************/
56
#ifdef HAVE_SIGNAL_H
57
58
static jmp_buf env;
static int     i_illegal;
Christophe Massiot's avatar
Christophe Massiot committed
59
#if defined( __i386__ ) || defined( __x86_64__ )
60
static const char *psz_capability;
Sam Hocevar's avatar
Sam Hocevar committed
61
#endif
62
#endif
63
64

/*****************************************************************************
65
 * CPUCapabilities: get the CPU capabilities
66
 *****************************************************************************
67
68
 * This function is called to list extensions the CPU may have.
 *****************************************************************************/
Christophe Mutricy's avatar
Christophe Mutricy committed
69
uint32_t CPUCapabilities( void )
70
{
71
    volatile uint32_t i_capabilities = CPU_CAPABILITY_NONE;
72

73
#if defined(__APPLE__) && (defined(__ppc__) || defined(__ppc64__))
74
75
76
77
    int selectors[2] = { CTL_HW, HW_VECTORUNIT };
    int i_has_altivec = 0;
    size_t i_length = sizeof( i_has_altivec );
    int i_error = sysctl( selectors, 2, &i_has_altivec, &i_length, NULL, 0);
78
79
80

    i_capabilities |= CPU_CAPABILITY_FPU;

81
    if( i_error == 0 && i_has_altivec != 0 )
82
83
        i_capabilities |= CPU_CAPABILITY_ALTIVEC;

84
    return i_capabilities;
85

Christophe Massiot's avatar
Christophe Massiot committed
86
#elif defined( __i386__ ) || defined( __x86_64__ )
87
    volatile unsigned int  i_eax, i_ebx, i_ecx, i_edx;
88
    volatile bool    b_amd;
89
90

    /* Needed for x86 CPU capabilities detection */
91
92
#   if defined( __x86_64__ )
#       define cpuid( reg )                    \
93
            asm volatile ( "cpuid\n\t"         \
94
95
                           "movl %%ebx,%1\n\t" \
                         : "=a" ( i_eax ),     \
Sam Hocevar's avatar
Sam Hocevar committed
96
                           "=b" ( i_ebx ),     \
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
                           "=c" ( i_ecx ),     \
                           "=d" ( i_edx )      \
                         : "a"  ( reg )        \
                         : "cc" );
#   else
#       define cpuid( reg )                    \
            asm volatile ( "push %%ebx\n\t"    \
                           "cpuid\n\t"         \
                           "movl %%ebx,%1\n\t" \
                           "pop %%ebx\n\t"     \
                         : "=a" ( i_eax ),     \
                           "=r" ( i_ebx ),     \
                           "=c" ( i_ecx ),     \
                           "=d" ( i_edx )      \
                         : "a"  ( reg )        \
                         : "cc" );
#   endif
114

115
116
#   if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW ) \
     && defined( HAVE_SIGNAL_H )
117
    void (*pf_sigill) (int) = signal( SIGILL, SigHandler );
118
119
#   endif

120
121
    i_capabilities |= CPU_CAPABILITY_FPU;

122
123
#   if defined( __i386__ )
    /* check if cpuid instruction is supported */
Christophe Massiot's avatar
Christophe Massiot committed
124
125
126
    asm volatile ( "push %%ebx\n\t"
                   "pushf\n\t"
                   "pop %%eax\n\t"
127
128
                   "movl %%eax, %%ebx\n\t"
                   "xorl $0x200000, %%eax\n\t"
Christophe Massiot's avatar
Christophe Massiot committed
129
130
131
132
                   "push %%eax\n\t"
                   "popf\n\t"
                   "pushf\n\t"
                   "pop %%eax\n\t"
133
                   "movl %%ebx,%1\n\t"
Christophe Massiot's avatar
Christophe Massiot committed
134
                   "pop %%ebx\n\t"
135
136
137
138
139
140
141
                 : "=a" ( i_eax ),
                   "=r" ( i_ebx )
                 :
                 : "cc" );

    if( i_eax == i_ebx )
    {
142
143
#       if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW ) \
            && defined( HAVE_SIGNAL_H )
144
        signal( SIGILL, pf_sigill );
145
#       endif
146
        return i_capabilities;
147
    }
148
149
150
#   else
    /* x86_64 supports cpuid instruction, so we dont need to check it */
#   endif
151
152
153
154
155
156
157
158

    i_capabilities |= CPU_CAPABILITY_486;

    /* the CPU supports the CPUID instruction - get its level */
    cpuid( 0x00000000 );

    if( !i_eax )
    {
159
160
#   if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW ) \
     && defined( HAVE_SIGNAL_H )
161
        signal( SIGILL, pf_sigill );
162
#   endif
163
        return i_capabilities;
164
165
166
167
168
169
170
171
172
173
174
175
176
177
    }

    /* FIXME: this isn't correct, since some 486s have cpuid */
    i_capabilities |= CPU_CAPABILITY_586;

    /* borrowed from mpeg2dec */
    b_amd = ( i_ebx == 0x68747541 ) && ( i_ecx == 0x444d4163 )
                    && ( i_edx == 0x69746e65 );

    /* test for the MMX flag */
    cpuid( 0x00000001 );

    if( ! (i_edx & 0x00800000) )
    {
178
179
#   if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW ) \
     && defined( HAVE_SIGNAL_H )
180
        signal( SIGILL, pf_sigill );
181
#   endif
182
        return i_capabilities;
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
    }

    i_capabilities |= CPU_CAPABILITY_MMX;

    if( i_edx & 0x02000000 )
    {
        i_capabilities |= CPU_CAPABILITY_MMXEXT;

#   ifdef CAN_COMPILE_SSE
        /* We test if OS supports the SSE instructions */
        psz_capability = "SSE";
        i_illegal = 0;

        if( setjmp( env ) == 0 )
        {
            /* Test a SSE instruction */
            __asm__ __volatile__ ( "xorps %%xmm0,%%xmm0\n" : : );
        }

        if( i_illegal == 0 )
        {
            i_capabilities |= CPU_CAPABILITY_SSE;
        }
#   endif
    }
208

sigmunau's avatar
sigmunau committed
209
210
    if( i_edx & 0x04000000 )
    {
Eric Petit's avatar
Eric Petit committed
211
#   if defined(CAN_COMPILE_SSE)
sigmunau's avatar
sigmunau committed
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
        /* We test if OS supports the SSE instructions */
        psz_capability = "SSE2";
        i_illegal = 0;

        if( setjmp( env ) == 0 )
        {
            /* Test a SSE2 instruction */
            __asm__ __volatile__ ( "movupd %%xmm0, %%xmm0\n" : : );
        }

        if( i_illegal == 0 )
        {
            i_capabilities |= CPU_CAPABILITY_SSE2;
        }
#   endif
    }
228
229
230
231
232
233

    /* test for additional capabilities */
    cpuid( 0x80000000 );

    if( i_eax < 0x80000001 )
    {
234
235
#   if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW ) \
     && defined( HAVE_SIGNAL_H )
236
        signal( SIGILL, pf_sigill );
237
#   endif
238
        return i_capabilities;
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
    }

    /* list these additional capabilities */
    cpuid( 0x80000001 );

#   ifdef CAN_COMPILE_3DNOW
    if( i_edx & 0x80000000 )
    {
        psz_capability = "3D Now!";
        i_illegal = 0;

        if( setjmp( env ) == 0 )
        {
            /* Test a 3D Now! instruction */
            __asm__ __volatile__ ( "pfadd %%mm0,%%mm0\n" "femms\n" : : );
        }

        if( i_illegal == 0 )
        {
            i_capabilities |= CPU_CAPABILITY_3DNOW;
        }
    }
#   endif

    if( b_amd && ( i_edx & 0x00400000 ) )
    {
        i_capabilities |= CPU_CAPABILITY_MMXEXT;
    }

268
269
#   if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW ) \
     && defined( HAVE_SIGNAL_H )
270
    signal( SIGILL, pf_sigill );
271
#   endif
272
    return i_capabilities;
273

274
#elif defined( __powerpc__ ) || defined( __ppc__ ) || defined( __ppc64__ )
275

276
#   ifdef CAN_COMPILE_ALTIVEC && defined( HAVE_SIGNAL_H )
277
    void (*pf_sigill) (int) = signal( SIGILL, SigHandler );
278
279

    i_capabilities |= CPU_CAPABILITY_FPU;
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295

    i_illegal = 0;

    if( setjmp( env ) == 0 )
    {
        asm volatile ("mtspr 256, %0\n\t"
                      "vand %%v0, %%v0, %%v0"
                      :
                      : "r" (-1));
    }

    if( i_illegal == 0 )
    {
        i_capabilities |= CPU_CAPABILITY_ALTIVEC;
    }

296
    signal( SIGILL, pf_sigill );
297
298
#   endif

299
    return i_capabilities;
300
301
302
303

#elif defined( __sparc__ )

    i_capabilities |= CPU_CAPABILITY_FPU;
304
    return i_capabilities;
305

306
#elif defined( _MSC_VER ) && !defined( UNDER_CE )
gbazin's avatar
   
gbazin committed
307
308
309
    i_capabilities |= CPU_CAPABILITY_FPU;
    return i_capabilities;

310
311
#else
    /* default behaviour */
312
    return i_capabilities;
313
314
315
316
317

#endif
}

/*****************************************************************************
318
 * SigHandler: system signal handler
319
320
321
322
 *****************************************************************************
 * This function is called when an illegal instruction signal is received by
 * the program. We use this function to test OS and CPU capabilities
 *****************************************************************************/
323
#if defined( HAVE_SIGNAL_H )
324
static void SigHandler( int i_signal )
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
{
    /* Acknowledge the signal received */
    i_illegal = 1;

#ifdef HAVE_SIGRELSE
    sigrelse( i_signal );
#endif

#if defined( __i386__ )
    fprintf( stderr, "warning: your CPU has %s instructions, but not your "
                     "operating system.\n", psz_capability );
    fprintf( stderr, "         some optimizations will be disabled unless "
                     "you upgrade your OS\n" );
#   if defined( SYS_LINUX )
    fprintf( stderr, "         (for instance Linux kernel 2.4.x or later)\n" );
#   endif
#endif

    longjmp( env, 1 );
}
345
#endif
346

347

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
348
uint32_t cpu_flags = 0;
349
350
351
352
353
354
355
356
357
358


/*****************************************************************************
 * vlc_CPU: get pre-computed CPU capability flags
 ****************************************************************************/
unsigned vlc_CPU (void)
{
    return cpu_flags;
}

359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
static vlc_memcpy_t pf_vlc_memcpy = memcpy;
static vlc_memcpy_t pf_vlc_memset = memset;

void vlc_fastmem_register (vlc_memcpy_t cpy, vlc_memset_t set)
{
    if (cpy)
        pf_vlc_memcpy = cpy;
    if (set)
        pf_vlc_memset = set;
}

/**
 * vlc_memcpy: fast CPU-dependent memcpy
 */
void *vlc_memcpy (void *tgt, const void *src, size_t n)
{
    return pf_vlc_memcpy (tgt, src, n);
}

/**
 * vlc_memset: fast CPU-dependent memset
 */
void *vlc_memset (void *tgt, int c, size_t n)
{
    return pf_vlc_memset (tgt, c, n);
}