cpu.c 4.51 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
#include <stdio.h>
Laurent Aimar's avatar
Laurent Aimar committed
33 34
#include <string.h>

Loren Merritt's avatar
Loren Merritt committed
35
#include "common.h"
Laurent Aimar's avatar
Laurent Aimar committed
36

Loren Merritt's avatar
Loren Merritt committed
37
#if defined(ARCH_X86) || defined(ARCH_X86_64)
Laurent Aimar's avatar
Laurent Aimar committed
38 39 40 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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
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;
    }

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

110
#elif defined( ARCH_PPC )
Laurent Aimar's avatar
Laurent Aimar committed
111

112 113
#ifdef SYS_MACOSX
#include <sys/sysctl.h>
Laurent Aimar's avatar
Laurent Aimar committed
114 115
uint32_t x264_cpu_detect( void )
{
116
    /* Thank you VLC */
Laurent Aimar's avatar
Laurent Aimar committed
117 118 119 120 121 122 123 124 125 126 127 128 129 130
    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;
}

131 132 133 134 135 136 137 138
#elif defined( SYS_LINUX )
uint32_t x264_cpu_detect( void )
{
    /* FIXME (Linux PPC) */
    return X264_CPU_ALTIVEC;
}
#endif

Laurent Aimar's avatar
Laurent Aimar committed
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
void     x264_cpu_restore( uint32_t cpu )
{
}

#else

uint32_t x264_cpu_detect( void )
{
    return 0;
}

void     x264_cpu_restore( uint32_t cpu )
{
}

#endif
155 156 157 158


int x264_cpu_num_processors( void )
{
159 160 161 162
#if !defined(HAVE_PTHREAD)
    return 1;

#elif defined(SYS_LINUX) || defined(WIN32)
163 164 165 166 167 168 169 170 171 172 173 174
    int np;
#if defined(WIN32)
    uint32_t p_aff, s_aff;
    GetProcessAffinityMask( GetCurrentProcess(), &p_aff, &s_aff );
#else
    uint64_t p_aff;
    sched_getaffinity( 0, sizeof(p_aff), (cpu_set_t*)&p_aff );
#endif
    for( np = 0; p_aff != 0; p_aff >>= 1 )
        np += p_aff&1;
    return np;

175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
#elif defined(SYS_BEOS)
    system_info info;
    get_system_info( &info );
    return info.cpu_count;

#elif defined(SYS_MACOSX)
    FILE * pipe;
    char   buffer[16];
    int    num = 1;
    if( ( pipe = popen( "/usr/sbin/sysctl hw.ncpu", "r" ) ) )
    {   
        memset( buffer, 0, 16 );
        if( fgets( buffer, 16, pipe ) )
        {   
            if( sscanf( buffer, "hw.ncpu: %d", &num ) != 1 )
            {   
                num = 1;
            }
        }
        fclose( pipe );
    }
    return num;
197

198
#else
199 200
    return 1;
#endif
201
}