cpu.c 5.19 KB
Newer Older
Laurent Aimar's avatar
Laurent Aimar committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*****************************************************************************
 * cpu.c: h264 encoder library
 *****************************************************************************
 * Copyright (C) 2003 Laurent Aimar
 * $Id: cpu.c,v 1.1 2004/06/03 19:27:06 fenrir Exp $
 *
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
 *
 * 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.
 *****************************************************************************/

24 25 26 27
#if defined(HAVE_PTHREAD) && defined(SYS_LINUX)
#define _GNU_SOURCE
#include <sched.h>
#endif
28 29 30
#ifdef SYS_BEOS
#include <kernel/OS.h>
#endif
31 32 33 34
#ifdef SYS_MACOSX
#include <sys/types.h>
#include <sys/sysctl.h>
#endif
35

Laurent Aimar's avatar
Laurent Aimar committed
36 37
#include <string.h>

Loren Merritt's avatar
Loren Merritt committed
38
#include "common.h"
Laurent Aimar's avatar
Laurent Aimar committed
39

40
#if ( defined(ARCH_X86) || defined(ARCH_X86_64) ) && defined(HAVE_MMX)
Laurent Aimar's avatar
Laurent Aimar committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
extern int  x264_cpu_cpuid_test( void );
extern uint32_t  x264_cpu_cpuid( uint32_t op, uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx );
extern void x264_emms( void );

uint32_t x264_cpu_detect( void )
{
    uint32_t cpu = 0;

    uint32_t eax, ebx, ecx, edx;
    int      b_amd;

    if( !x264_cpu_cpuid_test() )
    {
        /* No cpuid */
        return 0;
    }

    x264_cpu_cpuid( 0, &eax, &ebx, &ecx, &edx);
    if( eax == 0 )
    {
        return 0;
    }
    b_amd   = (ebx == 0x68747541) && (ecx == 0x444d4163) && (edx == 0x69746e65);

    x264_cpu_cpuid( 1, &eax, &ebx, &ecx, &edx );
    if( (edx&0x00800000) == 0 )
    {
        /* No MMX */
        return 0;
    }
    cpu = X264_CPU_MMX;
    if( (edx&0x02000000) )
    {
        /* SSE - identical to AMD MMX extensions */
        cpu |= X264_CPU_MMXEXT|X264_CPU_SSE;
    }
    if( (edx&0x04000000) )
    {
        /* Is it OK ? */
        cpu |= X264_CPU_SSE2;
    }
82 83 84 85 86 87 88 89 90 91
#ifdef HAVE_SSE3
    if( (ecx&0x00000001) )
    {
        cpu |= X264_CPU_SSE3;
    }
    if( (ecx&0x00000200) )
    {
        cpu |= X264_CPU_SSSE3;
    }
#endif
Laurent Aimar's avatar
Laurent Aimar committed
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121

    x264_cpu_cpuid( 0x80000000, &eax, &ebx, &ecx, &edx );
    if( eax < 0x80000001 )
    {
        /* no extended capabilities */
        return cpu;
    }

    x264_cpu_cpuid( 0x80000001, &eax, &ebx, &ecx, &edx );
    if( edx&0x80000000 )
    {
        cpu |= X264_CPU_3DNOW;
    }
    if( b_amd && (edx&0x00400000) )
    {
        /* AMD MMX extensions */
        cpu |= X264_CPU_MMXEXT;
    }

    return cpu;
}

void     x264_cpu_restore( uint32_t cpu )
{
    if( cpu&(X264_CPU_MMX|X264_CPU_MMXEXT|X264_CPU_3DNOW|X264_CPU_3DNOWEXT) )
    {
        x264_emms();
    }
}

122
#elif defined( ARCH_PPC )
Laurent Aimar's avatar
Laurent Aimar committed
123

124 125
#ifdef SYS_MACOSX
#include <sys/sysctl.h>
Laurent Aimar's avatar
Laurent Aimar committed
126 127
uint32_t x264_cpu_detect( void )
{
128
    /* Thank you VLC */
Laurent Aimar's avatar
Laurent Aimar committed
129 130 131 132 133 134 135 136 137 138 139 140 141 142
    uint32_t cpu = 0;
    int      selectors[2] = { CTL_HW, HW_VECTORUNIT };
    int      has_altivec = 0;
    size_t   length = sizeof( has_altivec );
    int      error = sysctl( selectors, 2, &has_altivec, &length, NULL, 0 );

    if( error == 0 && has_altivec != 0 )
    {
        cpu |= X264_CPU_ALTIVEC;
    }

    return cpu;
}

143
#elif defined( SYS_LINUX )
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
#include <signal.h>
#include <setjmp.h>
static sigjmp_buf jmpbuf;
static volatile sig_atomic_t canjump = 0;

static void sigill_handler( int sig )
{
    if( !canjump )
    {
        signal( sig, SIG_DFL );
        raise( sig );
    }

    canjump = 0;
    siglongjmp( jmpbuf, 1 );
}

161 162
uint32_t x264_cpu_detect( void )
{
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
    static void (* oldsig)( int );

    oldsig = signal( SIGILL, sigill_handler );
    if( sigsetjmp( jmpbuf, 1 ) )
    {
        signal( SIGILL, oldsig );
        return 0;
    }

    canjump = 1;
    asm volatile( "mtspr 256, %0\n\t"
                  "vand 0, 0, 0\n\t"
                  :
                  : "r"(-1) );
    canjump = 0;

    signal( SIGILL, oldsig );

181 182 183 184
    return X264_CPU_ALTIVEC;
}
#endif

Laurent Aimar's avatar
Laurent Aimar committed
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
void     x264_cpu_restore( uint32_t cpu )
{
}

#else

uint32_t x264_cpu_detect( void )
{
    return 0;
}

void     x264_cpu_restore( uint32_t cpu )
{
}

#endif
201 202 203 204


int x264_cpu_num_processors( void )
{
205 206 207
#if !defined(HAVE_PTHREAD)
    return 1;

Loren Merritt's avatar
Loren Merritt committed
208 209 210 211
#elif defined(WIN32)
    return pthread_num_processors_np();

#elif defined(SYS_LINUX)
212
    unsigned int bit;
213
    int np;
214 215 216 217 218
    cpu_set_t p_aff;
    memset( &p_aff, 0, sizeof(p_aff) );
    sched_getaffinity( 0, sizeof(p_aff), &p_aff );
    for( np = 0, bit = 0; bit < sizeof(p_aff); bit++ )
        np += (((uint8_t *)&p_aff)[bit / 8] >> (bit % 8)) & 1;
219 220
    return np;

221 222 223 224 225 226
#elif defined(SYS_BEOS)
    system_info info;
    get_system_info( &info );
    return info.cpu_count;

#elif defined(SYS_MACOSX)
227 228 229 230 231
    int numberOfCPUs;
    size_t length = sizeof( numberOfCPUs );
    if( sysctlbyname("hw.ncpu", &numberOfCPUs, &length, NULL, 0) )
    {
        numberOfCPUs = 1;
232
    }
233
    return numberOfCPUs;
234

235
#else
236 237
    return 1;
#endif
238
}