cpu.c
上传用户:hjq518
上传日期:2021-12-09
资源大小:5084k
文件大小:9k
源码类别:

Audio

开发平台:

Visual C++

  1. /*****************************************************************************
  2.  * cpu.c: h264 encoder library
  3.  *****************************************************************************
  4.  * Copyright (C) 2003-2008 x264 project
  5.  *
  6.  * Authors: Loren Merritt <lorenm@u.washington.edu>
  7.  *          Laurent Aimar <fenrir@via.ecp.fr>
  8.  *
  9.  * This program is free software; you can redistribute it and/or modify
  10.  * it under the terms of the GNU General Public License as published by
  11.  * the Free Software Foundation; either version 2 of the License, or
  12.  * (at your option) any later version.
  13.  *
  14.  * This program is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  * GNU General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with this program; if not, write to the Free Software
  21.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
  22.  *****************************************************************************/
  23. #if defined(HAVE_PTHREAD) && defined(SYS_LINUX)
  24. #define _GNU_SOURCE
  25. #include <sched.h>
  26. #endif
  27. #ifdef SYS_BEOS
  28. #include <kernel/OS.h>
  29. #endif
  30. #if defined(SYS_MACOSX) || defined(SYS_FREEBSD)
  31. #include <sys/types.h>
  32. #include <sys/sysctl.h>
  33. #endif
  34. #include "common.h"
  35. #include "cpu.h"
  36. const x264_cpu_name_t x264_cpu_names[] = {
  37.     {"Altivec", X264_CPU_ALTIVEC},
  38. //  {"MMX",     X264_CPU_MMX}, // we don't support asm on mmx1 cpus anymore
  39.     {"MMX2",    X264_CPU_MMX|X264_CPU_MMXEXT},
  40.     {"MMXEXT",  X264_CPU_MMX|X264_CPU_MMXEXT},
  41. //  {"SSE",     X264_CPU_MMX|X264_CPU_MMXEXT|X264_CPU_SSE}, // there are no sse1 functions in x264
  42.     {"SSE2Slow",X264_CPU_MMX|X264_CPU_MMXEXT|X264_CPU_SSE|X264_CPU_SSE2|X264_CPU_SSE2_IS_SLOW},
  43.     {"SSE2",    X264_CPU_MMX|X264_CPU_MMXEXT|X264_CPU_SSE|X264_CPU_SSE2},
  44.     {"SSE2Fast",X264_CPU_MMX|X264_CPU_MMXEXT|X264_CPU_SSE|X264_CPU_SSE2|X264_CPU_SSE2_IS_FAST},
  45.     {"SSE3",    X264_CPU_MMX|X264_CPU_MMXEXT|X264_CPU_SSE|X264_CPU_SSE2|X264_CPU_SSE3},
  46.     {"SSSE3",   X264_CPU_MMX|X264_CPU_MMXEXT|X264_CPU_SSE|X264_CPU_SSE2|X264_CPU_SSE3|X264_CPU_SSSE3},
  47.     {"PHADD",   X264_CPU_MMX|X264_CPU_MMXEXT|X264_CPU_SSE|X264_CPU_SSE2|X264_CPU_SSE3|X264_CPU_SSSE3|X264_CPU_PHADD_IS_FAST},
  48.     {"SSE4",    X264_CPU_MMX|X264_CPU_MMXEXT|X264_CPU_SSE|X264_CPU_SSE2|X264_CPU_SSE3|X264_CPU_SSSE3|X264_CPU_SSE4},
  49.     {"Cache32", X264_CPU_CACHELINE_32},
  50.     {"Cache64", X264_CPU_CACHELINE_64},
  51.     {"Slow_mod4_stack", X264_CPU_STACK_MOD4},
  52.     {"", 0},
  53. };
  54. #ifdef HAVE_MMX
  55. extern int  x264_cpu_cpuid_test( void );
  56. extern uint32_t  x264_cpu_cpuid( uint32_t op, uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx );
  57. uint32_t x264_cpu_detect( void )
  58. {
  59.     uint32_t cpu = 0;
  60.     uint32_t eax, ebx, ecx, edx;
  61.     uint32_t vendor[4] = {0};
  62.     int max_extended_cap;
  63.     int cache;
  64. #ifndef ARCH_X86_64
  65.     if( !x264_cpu_cpuid_test() )
  66.         return 0;
  67. #endif
  68.     x264_cpu_cpuid( 0, &eax, vendor+0, vendor+2, vendor+1 );
  69.     if( eax == 0 )
  70.         return 0;
  71.     x264_cpu_cpuid( 1, &eax, &ebx, &ecx, &edx );
  72.     if( edx&0x00800000 )
  73.         cpu |= X264_CPU_MMX;
  74.     else
  75.         return 0;
  76.     if( edx&0x02000000 )
  77.         cpu |= X264_CPU_MMXEXT|X264_CPU_SSE;
  78.     if( edx&0x04000000 )
  79.         cpu |= X264_CPU_SSE2;
  80.     if( ecx&0x00000001 )
  81.         cpu |= X264_CPU_SSE3;
  82.     if( ecx&0x00000200 )
  83.         cpu |= X264_CPU_SSSE3;
  84.     if( ecx&0x00080000 )
  85.         cpu |= X264_CPU_SSE4;
  86.     if( cpu & X264_CPU_SSSE3 )
  87.         cpu |= X264_CPU_SSE2_IS_FAST;
  88.     if( cpu & X264_CPU_SSE4 )
  89.         cpu |= X264_CPU_PHADD_IS_FAST;
  90.     x264_cpu_cpuid( 0x80000000, &eax, &ebx, &ecx, &edx );
  91.     max_extended_cap = eax;
  92.     if( !strcmp((char*)vendor, "AuthenticAMD") && max_extended_cap >= 0x80000001 )
  93.     {
  94.         x264_cpu_cpuid( 0x80000001, &eax, &ebx, &ecx, &edx );
  95.         if( edx&0x00400000 )
  96.             cpu |= X264_CPU_MMXEXT;
  97.         if( cpu & X264_CPU_SSE2 )
  98.         {
  99.             if( ecx&0x00000040 ) /* SSE4a */
  100.                 cpu |= X264_CPU_SSE2_IS_FAST;
  101.             else
  102.                 cpu |= X264_CPU_SSE2_IS_SLOW;
  103.         }
  104.     }
  105.     if( !strcmp((char*)vendor, "GenuineIntel") )
  106.     {
  107.         int family, model, stepping;
  108.         x264_cpu_cpuid( 1, &eax, &ebx, &ecx, &edx );
  109.         family = ((eax>>8)&0xf) + ((eax>>20)&0xff);
  110.         model  = ((eax>>4)&0xf) + ((eax>>12)&0xf0);
  111.         stepping = eax&0xf;
  112.         /* 6/9 (pentium-m "banias"), 6/13 (pentium-m "dothan"), and 6/14 (core1 "yonah")
  113.          * theoretically support sse2, but it's significantly slower than mmx for
  114.          * almost all of x264's functions, so let's just pretend they don't. */
  115.         if( family==6 && (model==9 || model==13 || model==14) )
  116.         {
  117.             cpu &= ~(X264_CPU_SSE2|X264_CPU_SSE3);
  118.             assert(!(cpu&(X264_CPU_SSSE3|X264_CPU_SSE4)));
  119.         }
  120.     }
  121.     if( !strcmp((char*)vendor, "GenuineIntel") || !strcmp((char*)vendor, "CyrixInstead") )
  122.     {
  123.         /* cacheline size is specified in 3 places, any of which may be missing */
  124.         x264_cpu_cpuid( 1, &eax, &ebx, &ecx, &edx );
  125.         cache = (ebx&0xff00)>>5; // cflush size
  126.         if( !cache && max_extended_cap >= 0x80000006 )
  127.         {
  128.             x264_cpu_cpuid( 0x80000006, &eax, &ebx, &ecx, &edx );
  129.             cache = ecx&0xff; // cacheline size
  130.         }
  131.         if( !cache )
  132.         {
  133.             // Cache and TLB Information
  134.             static const char cache32_ids[] = { 0x0a, 0x0c, 0x41, 0x42, 0x43, 0x44, 0x45, 0x82, 0x83, 0x84, 0x85, 0 };
  135.             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 };
  136.             uint32_t buf[4];
  137.             int max, i=0, j;
  138.             do {
  139.                 x264_cpu_cpuid( 2, buf+0, buf+1, buf+2, buf+3 );
  140.                 max = buf[0]&0xff;
  141.                 buf[0] &= ~0xff;
  142.                 for(j=0; j<4; j++)
  143.                     if( !(buf[j]>>31) )
  144.                         while( buf[j] )
  145.                         {
  146.                             if( strchr( cache32_ids, buf[j]&0xff ) )
  147.                                 cache = 32;
  148.                             if( strchr( cache64_ids, buf[j]&0xff ) )
  149.                                 cache = 64;
  150.                             buf[j] >>= 8;
  151.                         }
  152.             } while( ++i < max );
  153.         }
  154.         if( cache == 32 )
  155.             cpu |= X264_CPU_CACHELINE_32;
  156.         else if( cache == 64 )
  157.             cpu |= X264_CPU_CACHELINE_64;
  158.         else
  159.             fprintf( stderr, "x264 [warning]: unable to determine cacheline sizen" );
  160.     }
  161. #ifdef BROKEN_STACK_ALIGNMENT
  162.     cpu |= X264_CPU_STACK_MOD4;
  163. #endif
  164.     return cpu;
  165. }
  166. #elif defined( ARCH_PPC )
  167. #ifdef SYS_MACOSX
  168. #include <sys/sysctl.h>
  169. uint32_t x264_cpu_detect( void )
  170. {
  171.     /* Thank you VLC */
  172.     uint32_t cpu = 0;
  173.     int      selectors[2] = { CTL_HW, HW_VECTORUNIT };
  174.     int      has_altivec = 0;
  175.     size_t   length = sizeof( has_altivec );
  176.     int      error = sysctl( selectors, 2, &has_altivec, &length, NULL, 0 );
  177.     if( error == 0 && has_altivec != 0 )
  178.     {
  179.         cpu |= X264_CPU_ALTIVEC;
  180.     }
  181.     return cpu;
  182. }
  183. #elif defined( SYS_LINUX )
  184. #include <signal.h>
  185. #include <setjmp.h>
  186. static sigjmp_buf jmpbuf;
  187. static volatile sig_atomic_t canjump = 0;
  188. static void sigill_handler( int sig )
  189. {
  190.     if( !canjump )
  191.     {
  192.         signal( sig, SIG_DFL );
  193.         raise( sig );
  194.     }
  195.     canjump = 0;
  196.     siglongjmp( jmpbuf, 1 );
  197. }
  198. uint32_t x264_cpu_detect( void )
  199. {
  200.     static void (* oldsig)( int );
  201.     oldsig = signal( SIGILL, sigill_handler );
  202.     if( sigsetjmp( jmpbuf, 1 ) )
  203.     {
  204.         signal( SIGILL, oldsig );
  205.         return 0;
  206.     }
  207.     canjump = 1;
  208.     asm volatile( "mtspr 256, %0nt"
  209.                   "vand 0, 0, 0nt"
  210.                   :
  211.                   : "r"(-1) );
  212.     canjump = 0;
  213.     signal( SIGILL, oldsig );
  214.     return X264_CPU_ALTIVEC;
  215. }
  216. #endif
  217. #else
  218. uint32_t x264_cpu_detect( void )
  219. {
  220.     return 0;
  221. }
  222. #endif
  223. #ifndef HAVE_MMX
  224. void x264_emms( void )
  225. {
  226. }
  227. #endif
  228. int x264_cpu_num_processors( void )
  229. {
  230. #if !defined(HAVE_PTHREAD)
  231.     return 1;
  232. #elif defined(_WIN32)
  233.     return pthread_num_processors_np();
  234. #elif defined(SYS_LINUX)
  235.     unsigned int bit;
  236.     int np;
  237.     cpu_set_t p_aff;
  238.     memset( &p_aff, 0, sizeof(p_aff) );
  239.     sched_getaffinity( 0, sizeof(p_aff), &p_aff );
  240.     for( np = 0, bit = 0; bit < sizeof(p_aff); bit++ )
  241.         np += (((uint8_t *)&p_aff)[bit / 8] >> (bit % 8)) & 1;
  242.     return np;
  243. #elif defined(SYS_BEOS)
  244.     system_info info;
  245.     get_system_info( &info );
  246.     return info.cpu_count;
  247. #elif defined(SYS_MACOSX) || defined(SYS_FREEBSD)
  248.     int numberOfCPUs;
  249.     size_t length = sizeof( numberOfCPUs );
  250.     if( sysctlbyname("hw.ncpu", &numberOfCPUs, &length, NULL, 0) )
  251.     {
  252.         numberOfCPUs = 1;
  253.     }
  254.     return numberOfCPUs;
  255. #else
  256.     return 1;
  257. #endif
  258. }