cpu.c 14.5 KB
Newer Older
1
/*****************************************************************************
2
 * cpu.c: cpu detection
3
 *****************************************************************************
Anton Mitrofanov's avatar
Anton Mitrofanov committed
4
 * Copyright (C) 2003-2015 x264 project
5
 *
6 7
 * Authors: Loren Merritt <lorenm@u.washington.edu>
 *          Laurent Aimar <fenrir@via.ecp.fr>
8
 *          Fiona Glaser <fiona@x264.com>
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.
23 24 25
 *
 * This program is also available under a commercial proprietary license.
 * For more information, contact us at licensing@x264.com.
26 27
 *****************************************************************************/

28 29 30 31
#define _GNU_SOURCE // for sched_getaffinity
#include "common.h"
#include "cpu.h"

32
#if HAVE_POSIXTHREAD && SYS_LINUX
33 34
#include <sched.h>
#endif
Steven Walters's avatar
Steven Walters committed
35
#if SYS_BEOS
36 37
#include <kernel/OS.h>
#endif
Steven Walters's avatar
Steven Walters committed
38
#if SYS_MACOSX || SYS_FREEBSD
39 40 41
#include <sys/types.h>
#include <sys/sysctl.h>
#endif
Steven Walters's avatar
Steven Walters committed
42
#if SYS_OPENBSD
43 44 45 46
#include <sys/param.h>
#include <sys/sysctl.h>
#include <machine/cpu.h>
#endif
47

48 49
const x264_cpu_name_t x264_cpu_names[] =
{
50 51 52 53 54 55 56 57
#if HAVE_MMX
//  {"MMX",         X264_CPU_MMX},  // we don't support asm on mmx1 cpus anymore
//  {"CMOV",        X264_CPU_CMOV}, // we require this unconditionally, so don't print it
#define MMX2 X264_CPU_MMX|X264_CPU_MMX2|X264_CPU_CMOV
    {"MMX2",        MMX2},
    {"MMXEXT",      MMX2},
    {"SSE",         MMX2|X264_CPU_SSE},
#define SSE2 MMX2|X264_CPU_SSE|X264_CPU_SSE2
58 59 60 61 62 63 64 65
    {"SSE2Slow",    SSE2|X264_CPU_SSE2_IS_SLOW},
    {"SSE2",        SSE2},
    {"SSE2Fast",    SSE2|X264_CPU_SSE2_IS_FAST},
    {"SSE3",        SSE2|X264_CPU_SSE3},
    {"SSSE3",       SSE2|X264_CPU_SSE3|X264_CPU_SSSE3},
    {"SSE4.1",      SSE2|X264_CPU_SSE3|X264_CPU_SSSE3|X264_CPU_SSE4},
    {"SSE4",        SSE2|X264_CPU_SSE3|X264_CPU_SSSE3|X264_CPU_SSE4},
    {"SSE4.2",      SSE2|X264_CPU_SSE3|X264_CPU_SSSE3|X264_CPU_SSE4|X264_CPU_SSE42},
66 67 68 69 70
#define AVX SSE2|X264_CPU_SSE3|X264_CPU_SSSE3|X264_CPU_SSE4|X264_CPU_SSE42|X264_CPU_AVX
    {"AVX",         AVX},
    {"XOP",         AVX|X264_CPU_XOP},
    {"FMA4",        AVX|X264_CPU_FMA4},
    {"FMA3",        AVX|X264_CPU_FMA3},
71
    {"AVX2",        AVX|X264_CPU_FMA3|X264_CPU_AVX2},
72
#undef AVX
73
#undef SSE2
74
#undef MMX2
75 76 77
    {"Cache32",         X264_CPU_CACHELINE_32},
    {"Cache64",         X264_CPU_CACHELINE_64},
    {"LZCNT",           X264_CPU_LZCNT},
78 79
    {"BMI1",            X264_CPU_BMI1},
    {"BMI2",            X264_CPU_BMI1|X264_CPU_BMI2},
80 81
    {"SlowCTZ",         X264_CPU_SLOW_CTZ},
    {"SlowAtom",        X264_CPU_SLOW_ATOM},
82 83 84 85 86 87 88 89 90 91
    {"SlowPshufb",      X264_CPU_SLOW_PSHUFB},
    {"SlowPalignr",     X264_CPU_SLOW_PALIGNR},
    {"SlowShuffle",     X264_CPU_SLOW_SHUFFLE},
    {"UnalignedStack",  X264_CPU_STACK_MOD4},
#elif ARCH_PPC
    {"Altivec",         X264_CPU_ALTIVEC},
#elif ARCH_ARM
    {"ARMv6",           X264_CPU_ARMV6},
    {"NEON",            X264_CPU_NEON},
    {"FastNeonMRC",     X264_CPU_FAST_NEON_MRC},
92 93 94
#elif ARCH_AARCH64
    {"ARMv8",           X264_CPU_ARMV8},
    {"NEON",            X264_CPU_NEON},
95
#endif
96 97 98
    {"", 0},
};

Steven Walters's avatar
Steven Walters committed
99
#if (ARCH_PPC && SYS_LINUX) || (ARCH_ARM && !HAVE_NEON)
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
#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 );
}
#endif
Fiona Glaser's avatar
Fiona Glaser committed
117

Steven Walters's avatar
Steven Walters committed
118
#if HAVE_MMX
Anton Mitrofanov's avatar
Anton Mitrofanov committed
119
int x264_cpu_cpuid_test( void );
120
void x264_cpu_cpuid( uint32_t op, uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx );
Fiona Glaser's avatar
Fiona Glaser committed
121
void x264_cpu_xgetbv( uint32_t op, uint32_t *eax, uint32_t *edx );
122 123 124 125 126

uint32_t x264_cpu_detect( void )
{
    uint32_t cpu = 0;
    uint32_t eax, ebx, ecx, edx;
127
    uint32_t vendor[4] = {0};
128
    uint32_t max_extended_cap, max_basic_cap;
129
    int cache;
130

Steven Walters's avatar
Steven Walters committed
131
#if !ARCH_X86_64
132 133
    if( !x264_cpu_cpuid_test() )
        return 0;
134
#endif
135

136
    x264_cpu_cpuid( 0, &eax, vendor+0, vendor+2, vendor+1 );
137 138
    max_basic_cap = eax;
    if( max_basic_cap == 0 )
139 140 141
        return 0;

    x264_cpu_cpuid( 1, &eax, &ebx, &ecx, &edx );
142 143 144
    if( edx&0x00800000 )
        cpu |= X264_CPU_MMX;
    else
145
        return cpu;
146
    if( edx&0x02000000 )
147
        cpu |= X264_CPU_MMX2|X264_CPU_SSE;
148 149 150 151
    if( edx&0x00008000 )
        cpu |= X264_CPU_CMOV;
    else
        return cpu;
152
    if( edx&0x04000000 )
153
        cpu |= X264_CPU_SSE2;
154
    if( ecx&0x00000001 )
155
        cpu |= X264_CPU_SSE3;
156
    if( ecx&0x00000200 )
157
        cpu |= X264_CPU_SSSE3;
158 159
    if( ecx&0x00080000 )
        cpu |= X264_CPU_SSE4;
160 161
    if( ecx&0x00100000 )
        cpu |= X264_CPU_SSE42;
162 163 164 165 166 167
    /* Check OXSAVE and AVX bits */
    if( (ecx&0x18000000) == 0x18000000 )
    {
        /* Check for OS support */
        x264_cpu_xgetbv( 0, &eax, &edx );
        if( (eax&0x6) == 0x6 )
168
        {
169
            cpu |= X264_CPU_AVX;
170 171 172 173 174
            if( ecx&0x00001000 )
                cpu |= X264_CPU_FMA3;
        }
    }

175
    if( max_basic_cap >= 7 )
176
    {
177 178 179 180 181 182 183 184 185 186
        x264_cpu_cpuid( 7, &eax, &ebx, &ecx, &edx );
        /* AVX2 requires OS support, but BMI1/2 don't. */
        if( (cpu&X264_CPU_AVX) && (ebx&0x00000020) )
            cpu |= X264_CPU_AVX2;
        if( ebx&0x00000008 )
        {
            cpu |= X264_CPU_BMI1;
            if( ebx&0x00000100 )
                cpu |= X264_CPU_BMI2;
        }
187
    }
188

189 190 191
    if( cpu & X264_CPU_SSSE3 )
        cpu |= X264_CPU_SSE2_IS_FAST;

192
    x264_cpu_cpuid( 0x80000000, &eax, &ebx, &ecx, &edx );
193 194
    max_extended_cap = eax;

195
    if( max_extended_cap >= 0x80000001 )
196
    {
197
        x264_cpu_cpuid( 0x80000001, &eax, &ebx, &ecx, &edx );
198 199 200 201

        if( ecx&0x00000020 )
            cpu |= X264_CPU_LZCNT;             /* Supported by Intel chips starting with Haswell */
        if( ecx&0x00000040 ) /* SSE4a, AMD only */
202
        {
203 204 205
            int family = ((eax>>8)&0xf) + ((eax>>20)&0xff);
            cpu |= X264_CPU_SSE2_IS_FAST;      /* Phenom and later CPUs have fast SSE units */
            if( family == 0x14 )
Fiona Glaser's avatar
Fiona Glaser committed
206
            {
207 208 209
                cpu &= ~X264_CPU_SSE2_IS_FAST; /* SSSE3 doesn't imply fast SSE anymore... */
                cpu |= X264_CPU_SSE2_IS_SLOW;  /* Bobcat has 64-bit SIMD units */
                cpu |= X264_CPU_SLOW_PALIGNR;  /* palignr is insanely slow on Bobcat */
Fiona Glaser's avatar
Fiona Glaser committed
210
            }
211 212 213 214 215 216
            if( family == 0x16 )
            {
                cpu |= X264_CPU_SLOW_PSHUFB;   /* Jaguar's pshufb isn't that slow, but it's slow enough
                                                * compared to alternate instruction sequences that this
                                                * is equal or faster on almost all such functions. */
            }
217
        }
218

219 220 221 222 223 224 225
        if( cpu & X264_CPU_AVX )
        {
            if( ecx&0x00000800 ) /* XOP */
                cpu |= X264_CPU_XOP;
            if( ecx&0x00010000 ) /* FMA4 */
                cpu |= X264_CPU_FMA4;
        }
226

227 228 229 230 231 232 233 234
        if( !strcmp((char*)vendor, "AuthenticAMD") )
        {
            if( edx&0x00400000 )
                cpu |= X264_CPU_MMX2;
            if( !(cpu&X264_CPU_LZCNT) )
                cpu |= X264_CPU_SLOW_CTZ;
            if( (cpu&X264_CPU_SSE2) && !(cpu&X264_CPU_SSE2_IS_FAST) )
                cpu |= X264_CPU_SSE2_IS_SLOW; /* AMD CPUs come in two types: terrible at SSE and great at it */
235
        }
236 237
    }

238
    if( !strcmp((char*)vendor, "GenuineIntel") )
239
    {
240
        x264_cpu_cpuid( 1, &eax, &ebx, &ecx, &edx );
241 242
        int family = ((eax>>8)&0xf) + ((eax>>20)&0xff);
        int model  = ((eax>>4)&0xf) + ((eax>>12)&0xf0);
243
        if( family == 6 )
244
        {
245 246 247 248 249 250 251 252 253 254 255 256 257
            /* 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( model == 9 || model == 13 || model == 14 )
            {
                cpu &= ~(X264_CPU_SSE2|X264_CPU_SSE3);
                assert(!(cpu&(X264_CPU_SSSE3|X264_CPU_SSE4)));
            }
            /* Detect Atom CPU */
            else if( model == 28 )
            {
                cpu |= X264_CPU_SLOW_ATOM;
                cpu |= X264_CPU_SLOW_CTZ;
258
                cpu |= X264_CPU_SLOW_PSHUFB;
259
            }
260 261 262 263
            /* Conroe has a slow shuffle unit. Check the model number to make sure not
             * to include crippled low-end Penryns and Nehalems that don't have SSE4. */
            else if( (cpu&X264_CPU_SSSE3) && !(cpu&X264_CPU_SSE4) && model < 23 )
                cpu |= X264_CPU_SLOW_SHUFFLE;
264
        }
265
    }
266

267
    if( (!strcmp((char*)vendor, "GenuineIntel") || !strcmp((char*)vendor, "CyrixInstead")) && !(cpu&X264_CPU_SSE42))
268
    {
269 270 271 272 273 274 275 276
        /* 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
        }
277
        if( !cache && max_basic_cap >= 2 )
278 279 280
        {
            // Cache and TLB Information
            static const char cache32_ids[] = { 0x0a, 0x0c, 0x41, 0x42, 0x43, 0x44, 0x45, 0x82, 0x83, 0x84, 0x85, 0 };
281 282
            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 };
283
            uint32_t buf[4];
284
            int max, i = 0;
285 286 287 288
            do {
                x264_cpu_cpuid( 2, buf+0, buf+1, buf+2, buf+3 );
                max = buf[0]&0xff;
                buf[0] &= ~0xff;
289
                for( int j = 0; j < 4; j++ )
290 291 292 293 294 295 296 297 298 299 300
                    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 );
        }
301

302 303 304 305 306
        if( cache == 32 )
            cpu |= X264_CPU_CACHELINE_32;
        else if( cache == 64 )
            cpu |= X264_CPU_CACHELINE_64;
        else
307
            x264_log( NULL, X264_LOG_WARNING, "unable to determine cacheline size\n" );
308
    }
309

310
#if STACK_ALIGNMENT < 16
311 312 313
    cpu |= X264_CPU_STACK_MOD4;
#endif

314 315 316
    return cpu;
}

Steven Walters's avatar
Steven Walters committed
317
#elif ARCH_PPC
318

Steven Walters's avatar
Steven Walters committed
319
#if SYS_MACOSX || SYS_OPENBSD
320
#include <sys/sysctl.h>
321 322
uint32_t x264_cpu_detect( void )
{
323
    /* Thank you VLC */
324
    uint32_t cpu = 0;
Steven Walters's avatar
Steven Walters committed
325
#if SYS_OPENBSD
326 327
    int      selectors[2] = { CTL_MACHDEP, CPU_ALTIVEC };
#else
328
    int      selectors[2] = { CTL_HW, HW_VECTORUNIT };
329
#endif
330 331 332 333 334 335 336 337 338 339
    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;
}

Steven Walters's avatar
Steven Walters committed
340
#elif SYS_LINUX
341

342 343
uint32_t x264_cpu_detect( void )
{
344 345 346
#ifdef __NO_FPRS__
    return 0;
#else
347
    static void (*oldsig)( int );
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364

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

365
    return X264_CPU_ALTIVEC;
366
#endif
367 368 369
}
#endif

Steven Walters's avatar
Steven Walters committed
370
#elif ARCH_ARM
371

372 373
void x264_cpu_neon_test( void );
int x264_cpu_fast_neon_mrc_test( void );
374 375 376 377

uint32_t x264_cpu_detect( void )
{
    int flags = 0;
Steven Walters's avatar
Steven Walters committed
378
#if HAVE_ARMV6
379 380 381
    flags |= X264_CPU_ARMV6;

    // don't do this hack if compiled with -mfpu=neon
Steven Walters's avatar
Steven Walters committed
382
#if !HAVE_NEON
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
    static void (* oldsig)( int );
    oldsig = signal( SIGILL, sigill_handler );
    if( sigsetjmp( jmpbuf, 1 ) )
    {
        signal( SIGILL, oldsig );
        return flags;
    }

    canjump = 1;
    x264_cpu_neon_test();
    canjump = 0;
    signal( SIGILL, oldsig );
#endif

    flags |= X264_CPU_NEON;

    // fast neon -> arm (Cortex-A9) detection relies on user access to the
    // cycle counter; this assumes ARMv7 performance counters.
    // NEON requires at least ARMv7, ARMv8 may require changes here, but
    // hopefully this hacky detection method will have been replaced by then.
    // Note that there is potential for a race condition if another program or
    // x264 instance disables or reinits the counters while x264 is using them,
    // which may result in incorrect detection and the counters stuck enabled.
406 407
    // right now Apple does not seem to support performance counters for this test
#ifndef __MACH__
408
    flags |= x264_cpu_fast_neon_mrc_test() ? X264_CPU_FAST_NEON_MRC : 0;
409
#endif
410 411 412 413 414
    // TODO: write dual issue test? currently it's A8 (dual issue) vs. A9 (fast mrc)
#endif
    return flags;
}

415 416 417 418 419 420 421
#elif ARCH_AARCH64

uint32_t x264_cpu_detect( void )
{
    return X264_CPU_ARMV8 | X264_CPU_NEON;
}

422 423 424 425 426 427 428
#else

uint32_t x264_cpu_detect( void )
{
    return 0;
}

429 430
#endif

431 432
int x264_cpu_num_processors( void )
{
433
#if !HAVE_THREAD
434 435
    return 1;

436
#elif SYS_WINDOWS
437
    return x264_pthread_num_processors_np();
438

439
#elif SYS_CYGWIN || SYS_SunOS
440 441
    return sysconf( _SC_NPROCESSORS_ONLN );

Steven Walters's avatar
Steven Walters committed
442
#elif SYS_LINUX
Xiaolei Yu's avatar
Xiaolei Yu committed
443 444 445 446
#ifdef __ANDROID__
    // Android NDK does not expose sched_getaffinity
    return sysconf( _SC_NPROCESSORS_CONF );
#else
447
    cpu_set_t p_aff;
448 449 450 451 452 453 454 455 456 457 458
    memset( &p_aff, 0, sizeof(p_aff) );
    if( sched_getaffinity( 0, sizeof(p_aff), &p_aff ) )
        return 1;
#if HAVE_CPU_COUNT
    return CPU_COUNT(&p_aff);
#else
    int np = 0;
    for( unsigned int bit = 0; bit < 8 * sizeof(p_aff); bit++ )
        np += (((uint8_t *)&p_aff)[bit / 8] >> (bit % 8)) & 1;
    return np;
#endif
Xiaolei Yu's avatar
Xiaolei Yu committed
459
#endif
460

Steven Walters's avatar
Steven Walters committed
461
#elif SYS_BEOS
462 463 464 465
    system_info info;
    get_system_info( &info );
    return info.cpu_count;

Steven Walters's avatar
Steven Walters committed
466
#elif SYS_MACOSX || SYS_FREEBSD || SYS_OPENBSD
Anton Mitrofanov's avatar
Anton Mitrofanov committed
467 468
    int ncpu;
    size_t length = sizeof( ncpu );
Steven Walters's avatar
Steven Walters committed
469
#if SYS_OPENBSD
470
    int mib[2] = { CTL_HW, HW_NCPU };
Anton Mitrofanov's avatar
Anton Mitrofanov committed
471
    if( sysctl(mib, 2, &ncpu, &length, NULL, 0) )
472
#else
Anton Mitrofanov's avatar
Anton Mitrofanov committed
473
    if( sysctlbyname("hw.ncpu", &ncpu, &length, NULL, 0) )
474
#endif
475
    {
Anton Mitrofanov's avatar
Anton Mitrofanov committed
476
        ncpu = 1;
477
    }
Anton Mitrofanov's avatar
Anton Mitrofanov committed
478
    return ncpu;
479

480
#else
481 482
    return 1;
#endif
483
}