cpu.c 9.61 KB
Newer Older
Laurent Aimar's avatar
Laurent Aimar committed
1 2 3
/*****************************************************************************
 * cpu.c: h264 encoder library
 *****************************************************************************
4
 * Copyright (C) 2003-2008 x264 project
Laurent Aimar's avatar
Laurent Aimar committed
5
 *
6 7
 * Authors: Loren Merritt <lorenm@u.washington.edu>
 *          Laurent Aimar <fenrir@via.ecp.fr>
8
 *          Fiona Glaser <fiona@x264.com>
Laurent Aimar's avatar
Laurent Aimar committed
9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * 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
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
Laurent Aimar's avatar
Laurent Aimar committed
23 24
 *****************************************************************************/

25 26 27 28
#if defined(HAVE_PTHREAD) && defined(SYS_LINUX)
#define _GNU_SOURCE
#include <sched.h>
#endif
29 30 31
#ifdef SYS_BEOS
#include <kernel/OS.h>
#endif
32
#if defined(SYS_MACOSX) || defined(SYS_FREEBSD)
33 34 35
#include <sys/types.h>
#include <sys/sysctl.h>
#endif
36 37 38 39 40
#ifdef SYS_OPENBSD
#include <sys/param.h>
#include <sys/sysctl.h>
#include <machine/cpu.h>
#endif
41

Loren Merritt's avatar
Loren Merritt committed
42
#include "common.h"
43
#include "cpu.h"
Laurent Aimar's avatar
Laurent Aimar committed
44

45 46 47
const x264_cpu_name_t x264_cpu_names[] = {
    {"Altivec", X264_CPU_ALTIVEC},
//  {"MMX",     X264_CPU_MMX}, // we don't support asm on mmx1 cpus anymore
48 49
    {"MMX2",    X264_CPU_MMX|X264_CPU_MMXEXT},
    {"MMXEXT",  X264_CPU_MMX|X264_CPU_MMXEXT},
50 51
//  {"SSE",     X264_CPU_MMX|X264_CPU_MMXEXT|X264_CPU_SSE}, // there are no sse1 functions in x264
    {"SSE2Slow",X264_CPU_MMX|X264_CPU_MMXEXT|X264_CPU_SSE|X264_CPU_SSE2|X264_CPU_SSE2_IS_SLOW},
52
    {"SSE2",    X264_CPU_MMX|X264_CPU_MMXEXT|X264_CPU_SSE|X264_CPU_SSE2},
53
    {"SSE2Fast",X264_CPU_MMX|X264_CPU_MMXEXT|X264_CPU_SSE|X264_CPU_SSE2|X264_CPU_SSE2_IS_FAST},
54 55
    {"SSE3",    X264_CPU_MMX|X264_CPU_MMXEXT|X264_CPU_SSE|X264_CPU_SSE2|X264_CPU_SSE3},
    {"SSSE3",   X264_CPU_MMX|X264_CPU_MMXEXT|X264_CPU_SSE|X264_CPU_SSE2|X264_CPU_SSE3|X264_CPU_SSSE3},
Fiona Glaser's avatar
Fiona Glaser committed
56
    {"FastShuffle",   X264_CPU_MMX|X264_CPU_MMXEXT|X264_CPU_SSE|X264_CPU_SSE2|X264_CPU_SHUFFLE_IS_FAST},
57 58
    {"SSE4.1",  X264_CPU_MMX|X264_CPU_MMXEXT|X264_CPU_SSE|X264_CPU_SSE2|X264_CPU_SSE3|X264_CPU_SSSE3|X264_CPU_SSE4},
    {"SSE4.2",  X264_CPU_MMX|X264_CPU_MMXEXT|X264_CPU_SSE|X264_CPU_SSE2|X264_CPU_SSE3|X264_CPU_SSSE3|X264_CPU_SSE4|X264_CPU_SSE42},
59 60
    {"Cache32", X264_CPU_CACHELINE_32},
    {"Cache64", X264_CPU_CACHELINE_64},
Fiona Glaser's avatar
Fiona Glaser committed
61
    {"SSEMisalign", X264_CPU_SSE_MISALIGN},
62
    {"LZCNT", X264_CPU_LZCNT},
63
    {"Slow_mod4_stack", X264_CPU_STACK_MOD4},
64 65 66
    {"", 0},
};

Fiona Glaser's avatar
Fiona Glaser committed
67

Loren Merritt's avatar
Loren Merritt committed
68
#ifdef HAVE_MMX
Laurent Aimar's avatar
Laurent Aimar committed
69 70 71 72 73 74 75
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 );

uint32_t x264_cpu_detect( void )
{
    uint32_t cpu = 0;
    uint32_t eax, ebx, ecx, edx;
76 77 78
    uint32_t vendor[4] = {0};
    int max_extended_cap;
    int cache;
Laurent Aimar's avatar
Laurent Aimar committed
79

80
#ifndef ARCH_X86_64
Laurent Aimar's avatar
Laurent Aimar committed
81 82
    if( !x264_cpu_cpuid_test() )
        return 0;
83
#endif
Laurent Aimar's avatar
Laurent Aimar committed
84

85
    x264_cpu_cpuid( 0, &eax, vendor+0, vendor+2, vendor+1 );
Laurent Aimar's avatar
Laurent Aimar committed
86 87 88 89
    if( eax == 0 )
        return 0;

    x264_cpu_cpuid( 1, &eax, &ebx, &ecx, &edx );
90 91 92
    if( edx&0x00800000 )
        cpu |= X264_CPU_MMX;
    else
Laurent Aimar's avatar
Laurent Aimar committed
93
        return 0;
94
    if( edx&0x02000000 )
Laurent Aimar's avatar
Laurent Aimar committed
95
        cpu |= X264_CPU_MMXEXT|X264_CPU_SSE;
96
    if( edx&0x04000000 )
Laurent Aimar's avatar
Laurent Aimar committed
97
        cpu |= X264_CPU_SSE2;
98
    if( ecx&0x00000001 )
99
        cpu |= X264_CPU_SSE3;
100
    if( ecx&0x00000200 )
101
        cpu |= X264_CPU_SSSE3;
102 103
    if( ecx&0x00080000 )
        cpu |= X264_CPU_SSE4;
104 105
    if( ecx&0x00100000 )
        cpu |= X264_CPU_SSE42;
Laurent Aimar's avatar
Laurent Aimar committed
106

107 108 109
    if( cpu & X264_CPU_SSSE3 )
        cpu |= X264_CPU_SSE2_IS_FAST;
    if( cpu & X264_CPU_SSE4 )
Fiona Glaser's avatar
Fiona Glaser committed
110
        cpu |= X264_CPU_SHUFFLE_IS_FAST;
111

Laurent Aimar's avatar
Laurent Aimar committed
112
    x264_cpu_cpuid( 0x80000000, &eax, &ebx, &ecx, &edx );
113 114 115
    max_extended_cap = eax;

    if( !strcmp((char*)vendor, "AuthenticAMD") && max_extended_cap >= 0x80000001 )
Laurent Aimar's avatar
Laurent Aimar committed
116
    {
117 118 119
        x264_cpu_cpuid( 0x80000001, &eax, &ebx, &ecx, &edx );
        if( edx&0x00400000 )
            cpu |= X264_CPU_MMXEXT;
120 121 122
        if( cpu & X264_CPU_SSE2 )
        {
            if( ecx&0x00000040 ) /* SSE4a */
Fiona Glaser's avatar
Fiona Glaser committed
123
            {
124
                cpu |= X264_CPU_SSE2_IS_FAST;
Fiona Glaser's avatar
Fiona Glaser committed
125
                cpu |= X264_CPU_SSE_MISALIGN;
126
                cpu |= X264_CPU_LZCNT;
Fiona Glaser's avatar
Fiona Glaser committed
127
                cpu |= X264_CPU_SHUFFLE_IS_FAST;
Fiona Glaser's avatar
Fiona Glaser committed
128 129
                x264_cpu_mask_misalign_sse();
            }
130 131 132
            else
                cpu |= X264_CPU_SSE2_IS_SLOW;
        }
Laurent Aimar's avatar
Laurent Aimar committed
133 134
    }

135
    if( !strcmp((char*)vendor, "GenuineIntel") )
Laurent Aimar's avatar
Laurent Aimar committed
136
    {
137 138 139 140 141 142 143 144 145 146 147 148 149
        int family, model, stepping;
        x264_cpu_cpuid( 1, &eax, &ebx, &ecx, &edx );
        family = ((eax>>8)&0xf) + ((eax>>20)&0xff);
        model  = ((eax>>4)&0xf) + ((eax>>12)&0xf0);
        stepping = eax&0xf;
        /* 6/9 (pentium-m "banias"), 6/13 (pentium-m "dothan"), and 6/14 (core1 "yonah")
         * theoretically support sse2, but it's significantly slower than mmx for
         * almost all of x264's functions, so let's just pretend they don't. */
        if( family==6 && (model==9 || model==13 || model==14) )
        {
            cpu &= ~(X264_CPU_SSE2|X264_CPU_SSE3);
            assert(!(cpu&(X264_CPU_SSSE3|X264_CPU_SSE4)));
        }
Laurent Aimar's avatar
Laurent Aimar committed
150
    }
151

152
    if( (!strcmp((char*)vendor, "GenuineIntel") || !strcmp((char*)vendor, "CyrixInstead")) && !(cpu&X264_CPU_SSE42))
Laurent Aimar's avatar
Laurent Aimar committed
153
    {
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
        /* cacheline size is specified in 3 places, any of which may be missing */
        x264_cpu_cpuid( 1, &eax, &ebx, &ecx, &edx );
        cache = (ebx&0xff00)>>5; // cflush size
        if( !cache && max_extended_cap >= 0x80000006 )
        {
            x264_cpu_cpuid( 0x80000006, &eax, &ebx, &ecx, &edx );
            cache = ecx&0xff; // cacheline size
        }
        if( !cache )
        {
            // Cache and TLB Information
            static const char cache32_ids[] = { 0x0a, 0x0c, 0x41, 0x42, 0x43, 0x44, 0x45, 0x82, 0x83, 0x84, 0x85, 0 };
            static const char cache64_ids[] = { 0x22, 0x23, 0x25, 0x29, 0x2c, 0x46, 0x47, 0x49, 0x60, 0x66, 0x67, 0x68, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7c, 0x7f, 0x86, 0x87, 0 };
            uint32_t buf[4];
            int max, i=0, j;
            do {
                x264_cpu_cpuid( 2, buf+0, buf+1, buf+2, buf+3 );
                max = buf[0]&0xff;
                buf[0] &= ~0xff;
                for(j=0; j<4; j++)
                    if( !(buf[j]>>31) )
                        while( buf[j] )
                        {
                            if( strchr( cache32_ids, buf[j]&0xff ) )
                                cache = 32;
                            if( strchr( cache64_ids, buf[j]&0xff ) )
                                cache = 64;
                            buf[j] >>= 8;
                        }
            } while( ++i < max );
        }
Laurent Aimar's avatar
Laurent Aimar committed
185

186 187 188 189 190 191 192
        if( cache == 32 )
            cpu |= X264_CPU_CACHELINE_32;
        else if( cache == 64 )
            cpu |= X264_CPU_CACHELINE_64;
        else
            fprintf( stderr, "x264 [warning]: unable to determine cacheline size\n" );
    }
193

194 195 196 197
#ifdef BROKEN_STACK_ALIGNMENT
    cpu |= X264_CPU_STACK_MOD4;
#endif

Laurent Aimar's avatar
Laurent Aimar committed
198 199 200
    return cpu;
}

201
#elif defined( ARCH_PPC )
Laurent Aimar's avatar
Laurent Aimar committed
202

203
#if defined(SYS_MACOSX) || defined(SYS_OPENBSD)
204
#include <sys/sysctl.h>
Laurent Aimar's avatar
Laurent Aimar committed
205 206
uint32_t x264_cpu_detect( void )
{
207
    /* Thank you VLC */
Laurent Aimar's avatar
Laurent Aimar committed
208
    uint32_t cpu = 0;
209 210 211
#ifdef SYS_OPENBSD
    int      selectors[2] = { CTL_MACHDEP, CPU_ALTIVEC };
#else
Laurent Aimar's avatar
Laurent Aimar committed
212
    int      selectors[2] = { CTL_HW, HW_VECTORUNIT };
213
#endif
Laurent Aimar's avatar
Laurent Aimar committed
214 215 216 217 218 219 220 221 222 223 224 225
    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;
}

226
#elif defined( SYS_LINUX )
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
#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 );
}

244 245
uint32_t x264_cpu_detect( void )
{
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
    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 );

264 265 266 267
    return X264_CPU_ALTIVEC;
}
#endif

Laurent Aimar's avatar
Laurent Aimar committed
268 269 270 271 272 273 274
#else

uint32_t x264_cpu_detect( void )
{
    return 0;
}

Loren Merritt's avatar
Loren Merritt committed
275 276 277 278
#endif

#ifndef HAVE_MMX
void x264_emms( void )
Laurent Aimar's avatar
Laurent Aimar committed
279 280 281
{
}
#endif
282 283 284 285


int x264_cpu_num_processors( void )
{
286 287 288
#if !defined(HAVE_PTHREAD)
    return 1;

289
#elif defined(_WIN32)
Loren Merritt's avatar
Loren Merritt committed
290 291 292
    return pthread_num_processors_np();

#elif defined(SYS_LINUX)
293
    unsigned int bit;
294
    int np;
295 296 297 298 299
    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;
300 301
    return np;

302 303 304 305 306
#elif defined(SYS_BEOS)
    system_info info;
    get_system_info( &info );
    return info.cpu_count;

307
#elif defined(SYS_MACOSX) || defined(SYS_FREEBSD) || defined(SYS_OPENBSD)
308 309
    int numberOfCPUs;
    size_t length = sizeof( numberOfCPUs );
310 311 312 313
#ifdef SYS_OPENBSD
    int mib[2] = { CTL_HW, HW_NCPU };
    if( sysctl(mib, 2, &numberOfCPUs, &length, NULL, 0) )
#else
314
    if( sysctlbyname("hw.ncpu", &numberOfCPUs, &length, NULL, 0) )
315
#endif
316 317
    {
        numberOfCPUs = 1;
318
    }
319
    return numberOfCPUs;
320

321
#else
322 323
    return 1;
#endif
324
}