cpu.c 10.1 KB
Newer Older
1 2 3
/*****************************************************************************
 * cpu.c: CPU detection code
 *****************************************************************************
Clément Stenac's avatar
Clément Stenac committed
4
 * Copyright (C) 1998-2004 VideoLAN
5
 * $Id$
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
 *
 * 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
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <vlc/vlc.h>

31 32 33 34 35
#ifdef HAVE_SIGNAL_H
#   include <signal.h>                            /* SIGHUP, SIGINT, SIGKILL */
#   include <setjmp.h>                                    /* longjmp, setjmp */
#endif

36
#ifdef SYS_DARWIN
37
#include <sys/sysctl.h>
38 39 40 41 42 43 44
#endif

#include "vlc_cpu.h"

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
45
#ifdef HAVE_SIGNAL_H
46
static void SigHandler   ( int );
47
#endif
48 49 50 51

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

/*****************************************************************************
61
 * CPUCapabilities: get the CPU capabilities
62
 *****************************************************************************
63 64
 * This function is called to list extensions the CPU may have.
 *****************************************************************************/
65
uint32_t CPUCapabilities( void )
66
{
67
    volatile uint32_t i_capabilities = CPU_CAPABILITY_NONE;
68 69

#if defined( SYS_DARWIN )
70 71 72 73
    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);
74 75 76

    i_capabilities |= CPU_CAPABILITY_FPU;

77
    if( i_error == 0 && i_has_altivec != 0 )
78 79
        i_capabilities |= CPU_CAPABILITY_ALTIVEC;

80
    return i_capabilities;
81

Christophe Massiot's avatar
Christophe Massiot committed
82
#elif defined( __i386__ ) || defined( __x86_64__ )
83 84 85 86
    volatile unsigned int  i_eax, i_ebx, i_ecx, i_edx;
    volatile vlc_bool_t    b_amd;

    /* Needed for x86 CPU capabilities detection */
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
#   if defined( __x86_64__ )
#       define cpuid( reg )                    \
            asm volatile ( "push %%rbx\n\t"    \
                           "cpuid\n\t"         \
                           "movl %%ebx,%1\n\t" \
                           "pop %%rbx\n\t"     \
                         : "=a" ( i_eax ),     \
                           "=r" ( i_ebx ),     \
                           "=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
112

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

118 119
    i_capabilities |= CPU_CAPABILITY_FPU;

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

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

    i_capabilities |= CPU_CAPABILITY_486;

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

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

    /* 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) )
    {
176 177
#   if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW ) \
     && defined( HAVE_SIGNAL_H )
178
        signal( SIGILL, pf_sigill );
179
#   endif
180
        return i_capabilities;
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
    }

    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
    }
206

Sigmund Augdal Helberg's avatar
Sigmund Augdal Helberg committed
207 208
    if( i_edx & 0x04000000 )
    {
Eric Petit's avatar
Eric Petit committed
209
#   if defined(CAN_COMPILE_SSE)
Sigmund Augdal Helberg's avatar
Sigmund Augdal Helberg committed
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
        /* 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
    }
226 227 228 229 230 231

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

    if( i_eax < 0x80000001 )
    {
232 233
#   if defined( CAN_COMPILE_SSE ) || defined ( CAN_COMPILE_3DNOW ) \
     && defined( HAVE_SIGNAL_H )
234
        signal( SIGILL, pf_sigill );
235
#   endif
236
        return i_capabilities;
237 238 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
    }

    /* 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;
    }

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

#elif defined( __powerpc__ )

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

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

    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;
    }

294
    signal( SIGILL, pf_sigill );
295 296
#   endif

297
    return i_capabilities;
298 299 300 301

#elif defined( __sparc__ )

    i_capabilities |= CPU_CAPABILITY_FPU;
302
    return i_capabilities;
303

304
#elif defined( _MSC_VER ) && !defined( UNDER_CE )
Gildas Bazin's avatar
 
Gildas Bazin committed
305 306 307
    i_capabilities |= CPU_CAPABILITY_FPU;
    return i_capabilities;

308 309
#else
    /* default behaviour */
310
    return i_capabilities;
311 312 313 314 315

#endif
}

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