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 #ifdef ARCH_X86
00073
00074 #ifdef 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 }
00135
00136 cpuid(0x80000000, max_ext_level, ebx, ecx, edx);
00137
00138 if(max_ext_level >= 0x80000001){
00139 cpuid(0x80000001, eax, ebx, ecx, ext_caps);
00140 if (ext_caps & (1<<31))
00141 rval |= MM_3DNOW;
00142 if (ext_caps & (1<<30))
00143 rval |= MM_3DNOWEXT;
00144 if (ext_caps & (1<<23))
00145 rval |= MM_MMX;
00146 }
00147
00148 cpuid(0, eax, ebx, ecx, edx);
00149 if ( ebx == 0x68747541 &&
00150 edx == 0x69746e65 &&
00151 ecx == 0x444d4163) {
00152
00153 if(ext_caps & (1<<22))
00154 rval |= MM_MMXEXT;
00155 } else if (ebx == 0x746e6543 &&
00156 edx == 0x48727561 &&
00157 ecx == 0x736c7561) {
00158
00159 if(ext_caps & (1<<24))
00160 rval |= MM_MMXEXT;
00161 } else if (ebx == 0x69727943 &&
00162 edx == 0x736e4978 &&
00163 ecx == 0x64616574) {
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173 if (eax < 2)
00174 return rval;
00175 if (ext_caps & (1<<24))
00176 rval |= MM_MMXEXT;
00177 }
00178 #if 0
00179 av_log(NULL, AV_LOG_DEBUG, "%s%s%s%s%s%s\n",
00180 (rval&MM_MMX) ? "MMX ":"",
00181 (rval&MM_MMXEXT) ? "MMX2 ":"",
00182 (rval&MM_SSE) ? "SSE ":"",
00183 (rval&MM_SSE2) ? "SSE2 ":"",
00184 (rval&MM_3DNOW) ? "3DNow ":"",
00185 (rval&MM_3DNOWEXT) ? "3DNowExt ":"");
00186 #endif
00187 return rval;
00188 }
00189 #endif
00190
00192 uint detectCPUextensions(void)
00193 {
00194 #ifdef ARCH_X86_64
00195 return 0;
00196 #endif
00197 #ifndef ARCH_X86
00198 return 0;
00199 #else
00200 return mm_support() & ~_dwDisabledISA;
00201 #endif
00202 }