00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00042
00043 #include <stdexcept>
00044 #include <string>
00045 #include "cpu_detect.h"
00046
00047 #ifndef __GNUC__
00048 #error wrong platform - this source code file is for the GNU C compiler.
00049 #endif
00050
00051 #include "config.h"
00052
00053 using namespace std;
00054
00055 #include <stdio.h>
00057
00058
00059
00061
00062
00063
00064 static uint _dwDisabledISA = 0x00;
00065
00066
00067 void disableExtensions(uint dwDisableMask)
00068 {
00069 _dwDisabledISA = dwDisableMask;
00070 }
00071
00072 #if ARCH_X86
00073
00074 #if ARCH_X86_64
00075 # define REG_b "rbx"
00076 # define REG_S "rsi"
00077 #else
00078 # define REG_b "ebx"
00079 # define REG_S "esi"
00080 #endif
00081
00082
00083 #define cpuid(index,eax,ebx,ecx,edx)\
00084 __asm __volatile\
00085 ("mov %%"REG_b", %%"REG_S"\n\t"\
00086 "cpuid\n\t"\
00087 "xchg %%"REG_b", %%"REG_S\
00088 : "=a" (eax), "=S" (ebx),\
00089 "=c" (ecx), "=d" (edx)\
00090 : "0" (index));
00091
00092
00093 static int mm_support(void)
00094 {
00095 int rval = 0;
00096 int eax, ebx, ecx, edx;
00097 int max_std_level, max_ext_level, std_caps=0, ext_caps=0;
00098 long a, c;
00099
00100 __asm__ __volatile__ (
00101
00102
00103 "pushf\n\t"
00104 "pop %0\n\t"
00105 "mov %0, %1\n\t"
00106
00107
00108
00109 "xor $0x200000, %0\n\t"
00110 "push %0\n\t"
00111 "popf\n\t"
00112
00113
00114 "pushf\n\t"
00115 "pop %0\n\t"
00116 : "=a" (a), "=c" (c)
00117 :
00118 : "cc"
00119 );
00120
00121 if (a == c)
00122 return 0;
00123
00124 cpuid(0, max_std_level, ebx, ecx, edx);
00125
00126 if(max_std_level >= 1){
00127 cpuid(1, eax, ebx, ecx, std_caps);
00128 if (std_caps & (1<<23))
00129 rval |= MM_MMX;
00130 if (std_caps & (1<<25))
00131 rval |= MM_MMXEXT | MM_SSE;
00132 if (std_caps & (1<<26))
00133 rval |= MM_SSE2;
00134 if (ecx & 1)
00135 rval |= MM_SSE3;
00136 if (ecx & 0x00000200 )
00137 rval |= MM_SSSE3;
00138 if (ecx & 0x00080000 )
00139 rval |= MM_SSE4;
00140 if (ecx & 0x00100000 )
00141 rval |= MM_SSE42;
00142 }
00143
00144 cpuid(0x80000000, max_ext_level, ebx, ecx, edx);
00145
00146 if(max_ext_level >= 0x80000001){
00147 cpuid(0x80000001, eax, ebx, ecx, ext_caps);
00148 if (ext_caps & (1<<31))
00149 rval |= MM_3DNOW;
00150 if (ext_caps & (1<<30))
00151 rval |= MM_3DNOWEXT;
00152 if (ext_caps & (1<<23))
00153 rval |= MM_MMX;
00154 }
00155
00156 cpuid(0, eax, ebx, ecx, edx);
00157 if ( ebx == 0x68747541 &&
00158 edx == 0x69746e65 &&
00159 ecx == 0x444d4163) {
00160
00161 if(ext_caps & (1<<22))
00162 rval |= MM_MMXEXT;
00163 } else if (ebx == 0x746e6543 &&
00164 edx == 0x48727561 &&
00165 ecx == 0x736c7561) {
00166
00167 if(ext_caps & (1<<24))
00168 rval |= MM_MMXEXT;
00169 } else if (ebx == 0x69727943 &&
00170 edx == 0x736e4978 &&
00171 ecx == 0x64616574) {
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181 if (eax < 2)
00182 return rval;
00183 if (ext_caps & (1<<24))
00184 rval |= MM_MMXEXT;
00185 }
00186 #if 0
00187 av_log(NULL, AV_LOG_DEBUG, "%s%s%s%s%s%s\n",
00188 (rval&MM_MMX) ? "MMX ":"",
00189 (rval&MM_MMXEXT) ? "MMX2 ":"",
00190 (rval&MM_SSE) ? "SSE ":"",
00191 (rval&MM_SSE2) ? "SSE2 ":"",
00192 (rval&MM_3DNOW) ? "3DNow ":"",
00193 (rval&MM_3DNOWEXT) ? "3DNowExt ":"");
00194 #endif
00195 return rval;
00196 }
00197 #endif
00198
00200 uint detectCPUextensions(void)
00201 {
00202 #if !ARCH_X86
00203 return 0;
00204 #else
00205 return mm_support() & ~_dwDisabledISA;
00206 #endif
00207 }