You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

cpuid_x86.c 49 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135
  1. /*********************************************************************/
  2. /* Copyright 2009, 2010 The University of Texas at Austin. */
  3. /* All rights reserved. */
  4. /* */
  5. /* Redistribution and use in source and binary forms, with or */
  6. /* without modification, are permitted provided that the following */
  7. /* conditions are met: */
  8. /* */
  9. /* 1. Redistributions of source code must retain the above */
  10. /* copyright notice, this list of conditions and the following */
  11. /* disclaimer. */
  12. /* */
  13. /* 2. Redistributions in binary form must reproduce the above */
  14. /* copyright notice, this list of conditions and the following */
  15. /* disclaimer in the documentation and/or other materials */
  16. /* provided with the distribution. */
  17. /* */
  18. /* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY OF TEXAS AT */
  19. /* AUSTIN ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, */
  20. /* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */
  21. /* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
  22. /* DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OF TEXAS AT */
  23. /* AUSTIN OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, */
  24. /* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
  25. /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE */
  26. /* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR */
  27. /* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF */
  28. /* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
  29. /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT */
  30. /* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE */
  31. /* POSSIBILITY OF SUCH DAMAGE. */
  32. /* */
  33. /* The views and conclusions contained in the software and */
  34. /* documentation are those of the authors and should not be */
  35. /* interpreted as representing official policies, either expressed */
  36. /* or implied, of The University of Texas at Austin. */
  37. /*********************************************************************/
  38. #include <stdio.h>
  39. #include <string.h>
  40. #include "cpuid.h"
  41. #if defined(_MSC_VER) && !defined(__clang__)
  42. #define C_INLINE __inline
  43. #else
  44. #define C_INLINE inline
  45. #endif
  46. /*
  47. #ifdef NO_AVX
  48. #define CPUTYPE_HASWELL CPUTYPE_NEHALEM
  49. #define CORE_HASWELL CORE_NEHALEM
  50. #define CPUTYPE_SANDYBRIDGE CPUTYPE_NEHALEM
  51. #define CORE_SANDYBRIDGE CORE_NEHALEM
  52. #define CPUTYPE_BULLDOZER CPUTYPE_BARCELONA
  53. #define CORE_BULLDOZER CORE_BARCELONA
  54. #define CPUTYPE_PILEDRIVER CPUTYPE_BARCELONA
  55. #define CORE_PILEDRIVER CORE_BARCELONA
  56. #endif
  57. */
  58. #if defined(_MSC_VER) && !defined(__clang__)
  59. void cpuid(int op, int *eax, int *ebx, int *ecx, int *edx)
  60. {
  61. int cpuInfo[4] = {-1};
  62. __cpuid(cpuInfo, op);
  63. *eax = cpuInfo[0];
  64. *ebx = cpuInfo[1];
  65. *ecx = cpuInfo[2];
  66. *edx = cpuInfo[3];
  67. }
  68. void cpuid_count(int op, int count, int *eax, int *ebx, int *ecx, int *edx)
  69. {
  70. int cpuInfo[4] = {-1};
  71. __cpuidex(cpuInfo, op, count);
  72. *eax = cpuInfo[0];
  73. *ebx = cpuInfo[1];
  74. *ecx = cpuInfo[2];
  75. *edx = cpuInfo[3];
  76. }
  77. #else
  78. #ifndef CPUIDEMU
  79. #if defined(__APPLE__) && defined(__i386__)
  80. void cpuid(int op, int *eax, int *ebx, int *ecx, int *edx);
  81. void cpuid_count(int op, int count, int *eax, int *ebx, int *ecx, int *edx);
  82. #else
  83. static C_INLINE void cpuid(int op, int *eax, int *ebx, int *ecx, int *edx){
  84. #if defined(__i386__) && defined(__PIC__)
  85. __asm__ __volatile__
  86. ("mov %%ebx, %%edi;"
  87. "cpuid;"
  88. "xchgl %%ebx, %%edi;"
  89. : "=a" (*eax), "=D" (*ebx), "=c" (*ecx), "=d" (*edx) : "a" (op) : "cc");
  90. #else
  91. __asm__ __volatile__
  92. ("cpuid": "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx) : "a" (op) : "cc");
  93. #endif
  94. }
  95. static C_INLINE void cpuid_count(int op, int count ,int *eax, int *ebx, int *ecx, int *edx){
  96. #if defined(__i386__) && defined(__PIC__)
  97. __asm__ __volatile__
  98. ("mov %%ebx, %%edi;"
  99. "cpuid;"
  100. "xchgl %%ebx, %%edi;"
  101. : "=a" (*eax), "=D" (*ebx), "=c" (*ecx), "=d" (*edx) : "0" (op), "2" (count) : "cc");
  102. #else
  103. __asm__ __volatile__
  104. ("cpuid": "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx) : "0" (op), "2" (count) : "cc");
  105. #endif
  106. }
  107. #endif
  108. #else
  109. typedef struct {
  110. unsigned int id, a, b, c, d;
  111. } idlist_t;
  112. typedef struct {
  113. char *vendor;
  114. char *name;
  115. int start, stop;
  116. } vendor_t;
  117. extern idlist_t idlist[];
  118. extern vendor_t vendor[];
  119. static int cv = VENDOR;
  120. void cpuid(unsigned int op, unsigned int *eax, unsigned int *ebx, unsigned int *ecx, unsigned int *edx){
  121. static int current = 0;
  122. int start = vendor[cv].start;
  123. int stop = vendor[cv].stop;
  124. int count = stop - start;
  125. if ((current < start) || (current > stop)) current = start;
  126. while ((count > 0) && (idlist[current].id != op)) {
  127. current ++;
  128. if (current > stop) current = start;
  129. count --;
  130. }
  131. *eax = idlist[current].a;
  132. *ebx = idlist[current].b;
  133. *ecx = idlist[current].c;
  134. *edx = idlist[current].d;
  135. }
  136. void cpuid_count (unsigned int op, unsigned int count, unsigned int *eax, unsigned int *ebx, unsigned int *ecx, unsigned int *edx) {
  137. return cpuid (op, eax, ebx, ecx, edx);
  138. }
  139. #endif
  140. #endif // _MSC_VER
  141. static C_INLINE int have_cpuid(void){
  142. int eax, ebx, ecx, edx;
  143. cpuid(0, &eax, &ebx, &ecx, &edx);
  144. return eax;
  145. }
  146. static C_INLINE int have_excpuid(void){
  147. int eax, ebx, ecx, edx;
  148. cpuid(0x80000000, &eax, &ebx, &ecx, &edx);
  149. return eax & 0xffff;
  150. }
  151. #ifndef NO_AVX
  152. static C_INLINE void xgetbv(int op, int * eax, int * edx){
  153. //Use binary code for xgetbv
  154. #if defined(_MSC_VER) && !defined(__clang__)
  155. *eax = __xgetbv(op);
  156. #else
  157. __asm__ __volatile__
  158. (".byte 0x0f, 0x01, 0xd0": "=a" (*eax), "=d" (*edx) : "c" (op) : "cc");
  159. #endif
  160. }
  161. #endif
  162. int support_avx(){
  163. #ifndef NO_AVX
  164. int eax, ebx, ecx, edx;
  165. int ret=0;
  166. cpuid(1, &eax, &ebx, &ecx, &edx);
  167. if ((ecx & (1 << 28)) != 0 && (ecx & (1 << 27)) != 0 && (ecx & (1 << 26)) != 0){
  168. xgetbv(0, &eax, &edx);
  169. if((eax & 6) == 6){
  170. ret=1; //OS support AVX
  171. }
  172. }
  173. return ret;
  174. #else
  175. return 0;
  176. #endif
  177. }
  178. int get_vendor(void){
  179. int eax, ebx, ecx, edx;
  180. char vendor[13];
  181. cpuid(0, &eax, &ebx, &ecx, &edx);
  182. *(int *)(&vendor[0]) = ebx;
  183. *(int *)(&vendor[4]) = edx;
  184. *(int *)(&vendor[8]) = ecx;
  185. vendor[12] = (char)0;
  186. if (!strcmp(vendor, "GenuineIntel")) return VENDOR_INTEL;
  187. if (!strcmp(vendor, " UMC UMC UMC")) return VENDOR_UMC;
  188. if (!strcmp(vendor, "AuthenticAMD")) return VENDOR_AMD;
  189. if (!strcmp(vendor, "CyrixInstead")) return VENDOR_CYRIX;
  190. if (!strcmp(vendor, "NexGenDriven")) return VENDOR_NEXGEN;
  191. if (!strcmp(vendor, "CentaurHauls")) return VENDOR_CENTAUR;
  192. if (!strcmp(vendor, "RiseRiseRise")) return VENDOR_RISE;
  193. if (!strcmp(vendor, " SiS SiS SiS")) return VENDOR_SIS;
  194. if (!strcmp(vendor, "GenuineTMx86")) return VENDOR_TRANSMETA;
  195. if (!strcmp(vendor, "Geode by NSC")) return VENDOR_NSC;
  196. if ((eax == 0) || ((eax & 0x500) != 0)) return VENDOR_INTEL;
  197. return VENDOR_UNKNOWN;
  198. }
  199. int get_cputype(int gettype){
  200. int eax, ebx, ecx, edx;
  201. int extend_family, family;
  202. int extend_model, model;
  203. int type, stepping;
  204. int feature = 0;
  205. cpuid(1, &eax, &ebx, &ecx, &edx);
  206. switch (gettype) {
  207. case GET_EXFAMILY :
  208. return BITMASK(eax, 20, 0xff);
  209. case GET_EXMODEL :
  210. return BITMASK(eax, 16, 0x0f);
  211. case GET_TYPE :
  212. return BITMASK(eax, 12, 0x03);
  213. case GET_FAMILY :
  214. return BITMASK(eax, 8, 0x0f);
  215. case GET_MODEL :
  216. return BITMASK(eax, 4, 0x0f);
  217. case GET_APICID :
  218. return BITMASK(ebx, 24, 0x0f);
  219. case GET_LCOUNT :
  220. return BITMASK(ebx, 16, 0x0f);
  221. case GET_CHUNKS :
  222. return BITMASK(ebx, 8, 0x0f);
  223. case GET_STEPPING :
  224. return BITMASK(eax, 0, 0x0f);
  225. case GET_BLANDID :
  226. return BITMASK(ebx, 0, 0xff);
  227. case GET_NUMSHARE :
  228. if (have_cpuid() < 4) return 0;
  229. cpuid(4, &eax, &ebx, &ecx, &edx);
  230. return BITMASK(eax, 14, 0xfff);
  231. case GET_NUMCORES :
  232. if (have_cpuid() < 4) return 0;
  233. cpuid(4, &eax, &ebx, &ecx, &edx);
  234. return BITMASK(eax, 26, 0x3f);
  235. case GET_FEATURE :
  236. if ((edx & (1 << 3)) != 0) feature |= HAVE_PSE;
  237. if ((edx & (1 << 15)) != 0) feature |= HAVE_CMOV;
  238. if ((edx & (1 << 19)) != 0) feature |= HAVE_CFLUSH;
  239. if ((edx & (1 << 23)) != 0) feature |= HAVE_MMX;
  240. if ((edx & (1 << 25)) != 0) feature |= HAVE_SSE;
  241. if ((edx & (1 << 26)) != 0) feature |= HAVE_SSE2;
  242. if ((edx & (1 << 27)) != 0) {
  243. if (BITMASK(ebx, 16, 0x0f) > 0) feature |= HAVE_HIT;
  244. }
  245. if ((ecx & (1 << 0)) != 0) feature |= HAVE_SSE3;
  246. if ((ecx & (1 << 9)) != 0) feature |= HAVE_SSSE3;
  247. if ((ecx & (1 << 19)) != 0) feature |= HAVE_SSE4_1;
  248. if ((ecx & (1 << 20)) != 0) feature |= HAVE_SSE4_2;
  249. #ifndef NO_AVX
  250. if (support_avx()) feature |= HAVE_AVX;
  251. if ((ecx & (1 << 12)) != 0) feature |= HAVE_FMA3;
  252. #endif
  253. if (have_excpuid() >= 0x01) {
  254. cpuid(0x80000001, &eax, &ebx, &ecx, &edx);
  255. if ((ecx & (1 << 6)) != 0) feature |= HAVE_SSE4A;
  256. if ((ecx & (1 << 7)) != 0) feature |= HAVE_MISALIGNSSE;
  257. #ifndef NO_AVX
  258. if ((ecx & (1 << 16)) != 0) feature |= HAVE_FMA4;
  259. #endif
  260. if ((edx & (1 << 30)) != 0) feature |= HAVE_3DNOWEX;
  261. if ((edx & (1 << 31)) != 0) feature |= HAVE_3DNOW;
  262. }
  263. if (have_excpuid() >= 0x1a) {
  264. cpuid(0x8000001a, &eax, &ebx, &ecx, &edx);
  265. if ((eax & (1 << 0)) != 0) feature |= HAVE_128BITFPU;
  266. if ((eax & (1 << 1)) != 0) feature |= HAVE_FASTMOVU;
  267. }
  268. }
  269. return feature;
  270. }
  271. int get_cacheinfo(int type, cache_info_t *cacheinfo){
  272. int eax, ebx, ecx, edx, cpuid_level;
  273. int info[15];
  274. int i;
  275. cache_info_t LC1, LD1, L2, L3,
  276. ITB, DTB, LITB, LDTB,
  277. L2ITB, L2DTB, L2LITB, L2LDTB;
  278. LC1.size = 0; LC1.associative = 0; LC1.linesize = 0; LC1.shared = 0;
  279. LD1.size = 0; LD1.associative = 0; LD1.linesize = 0; LD1.shared = 0;
  280. L2.size = 0; L2.associative = 0; L2.linesize = 0; L2.shared = 0;
  281. L3.size = 0; L3.associative = 0; L3.linesize = 0; L3.shared = 0;
  282. ITB.size = 0; ITB.associative = 0; ITB.linesize = 0; ITB.shared = 0;
  283. DTB.size = 0; DTB.associative = 0; DTB.linesize = 0; DTB.shared = 0;
  284. LITB.size = 0; LITB.associative = 0; LITB.linesize = 0; LITB.shared = 0;
  285. LDTB.size = 0; LDTB.associative = 0; LDTB.linesize = 0; LDTB.shared = 0;
  286. L2ITB.size = 0; L2ITB.associative = 0; L2ITB.linesize = 0; L2ITB.shared = 0;
  287. L2DTB.size = 0; L2DTB.associative = 0; L2DTB.linesize = 0; L2DTB.shared = 0;
  288. L2LITB.size = 0; L2LITB.associative = 0; L2LITB.linesize = 0; L2LITB.shared = 0;
  289. L2LDTB.size = 0; L2LDTB.associative = 0; L2LDTB.linesize = 0; L2LDTB.shared = 0;
  290. cpuid(0, &cpuid_level, &ebx, &ecx, &edx);
  291. if (cpuid_level > 1) {
  292. int numcalls =0 ;
  293. cpuid(2, &eax, &ebx, &ecx, &edx);
  294. numcalls = BITMASK(eax, 0, 0xff); //FIXME some systems may require repeated calls to read all entries
  295. info[ 0] = BITMASK(eax, 8, 0xff);
  296. info[ 1] = BITMASK(eax, 16, 0xff);
  297. info[ 2] = BITMASK(eax, 24, 0xff);
  298. info[ 3] = BITMASK(ebx, 0, 0xff);
  299. info[ 4] = BITMASK(ebx, 8, 0xff);
  300. info[ 5] = BITMASK(ebx, 16, 0xff);
  301. info[ 6] = BITMASK(ebx, 24, 0xff);
  302. info[ 7] = BITMASK(ecx, 0, 0xff);
  303. info[ 8] = BITMASK(ecx, 8, 0xff);
  304. info[ 9] = BITMASK(ecx, 16, 0xff);
  305. info[10] = BITMASK(ecx, 24, 0xff);
  306. info[11] = BITMASK(edx, 0, 0xff);
  307. info[12] = BITMASK(edx, 8, 0xff);
  308. info[13] = BITMASK(edx, 16, 0xff);
  309. info[14] = BITMASK(edx, 24, 0xff);
  310. for (i = 0; i < 15; i++){
  311. switch (info[i]){
  312. /* This table is from http://www.sandpile.org/ia32/cpuid.htm */
  313. case 0x01 :
  314. ITB.size = 4;
  315. ITB.associative = 4;
  316. ITB.linesize = 32;
  317. break;
  318. case 0x02 :
  319. LITB.size = 4096;
  320. LITB.associative = 0;
  321. LITB.linesize = 2;
  322. break;
  323. case 0x03 :
  324. DTB.size = 4;
  325. DTB.associative = 4;
  326. DTB.linesize = 64;
  327. break;
  328. case 0x04 :
  329. LDTB.size = 4096;
  330. LDTB.associative = 4;
  331. LDTB.linesize = 8;
  332. break;
  333. case 0x05 :
  334. LDTB.size = 4096;
  335. LDTB.associative = 4;
  336. LDTB.linesize = 32;
  337. break;
  338. case 0x06 :
  339. LC1.size = 8;
  340. LC1.associative = 4;
  341. LC1.linesize = 32;
  342. break;
  343. case 0x08 :
  344. LC1.size = 16;
  345. LC1.associative = 4;
  346. LC1.linesize = 32;
  347. break;
  348. case 0x09 :
  349. LC1.size = 32;
  350. LC1.associative = 4;
  351. LC1.linesize = 64;
  352. break;
  353. case 0x0a :
  354. LD1.size = 8;
  355. LD1.associative = 2;
  356. LD1.linesize = 32;
  357. break;
  358. case 0x0c :
  359. LD1.size = 16;
  360. LD1.associative = 4;
  361. LD1.linesize = 32;
  362. break;
  363. case 0x0d :
  364. LD1.size = 16;
  365. LD1.associative = 4;
  366. LD1.linesize = 64;
  367. break;
  368. case 0x0e :
  369. LD1.size = 24;
  370. LD1.associative = 6;
  371. LD1.linesize = 64;
  372. break;
  373. case 0x10 :
  374. LD1.size = 16;
  375. LD1.associative = 4;
  376. LD1.linesize = 32;
  377. break;
  378. case 0x15 :
  379. LC1.size = 16;
  380. LC1.associative = 4;
  381. LC1.linesize = 32;
  382. break;
  383. case 0x1a :
  384. L2.size = 96;
  385. L2.associative = 6;
  386. L2.linesize = 64;
  387. break;
  388. case 0x21 :
  389. L2.size = 256;
  390. L2.associative = 8;
  391. L2.linesize = 64;
  392. break;
  393. case 0x22 :
  394. L3.size = 512;
  395. L3.associative = 4;
  396. L3.linesize = 64;
  397. break;
  398. case 0x23 :
  399. L3.size = 1024;
  400. L3.associative = 8;
  401. L3.linesize = 64;
  402. break;
  403. case 0x25 :
  404. L3.size = 2048;
  405. L3.associative = 8;
  406. L3.linesize = 64;
  407. break;
  408. case 0x29 :
  409. L3.size = 4096;
  410. L3.associative = 8;
  411. L3.linesize = 64;
  412. break;
  413. case 0x2c :
  414. LD1.size = 32;
  415. LD1.associative = 8;
  416. LD1.linesize = 64;
  417. break;
  418. case 0x30 :
  419. LC1.size = 32;
  420. LC1.associative = 8;
  421. LC1.linesize = 64;
  422. break;
  423. case 0x39 :
  424. L2.size = 128;
  425. L2.associative = 4;
  426. L2.linesize = 64;
  427. break;
  428. case 0x3a :
  429. L2.size = 192;
  430. L2.associative = 6;
  431. L2.linesize = 64;
  432. break;
  433. case 0x3b :
  434. L2.size = 128;
  435. L2.associative = 2;
  436. L2.linesize = 64;
  437. break;
  438. case 0x3c :
  439. L2.size = 256;
  440. L2.associative = 4;
  441. L2.linesize = 64;
  442. break;
  443. case 0x3d :
  444. L2.size = 384;
  445. L2.associative = 6;
  446. L2.linesize = 64;
  447. break;
  448. case 0x3e :
  449. L2.size = 512;
  450. L2.associative = 4;
  451. L2.linesize = 64;
  452. break;
  453. case 0x41 :
  454. L2.size = 128;
  455. L2.associative = 4;
  456. L2.linesize = 32;
  457. break;
  458. case 0x42 :
  459. L2.size = 256;
  460. L2.associative = 4;
  461. L2.linesize = 32;
  462. break;
  463. case 0x43 :
  464. L2.size = 512;
  465. L2.associative = 4;
  466. L2.linesize = 32;
  467. break;
  468. case 0x44 :
  469. L2.size = 1024;
  470. L2.associative = 4;
  471. L2.linesize = 32;
  472. break;
  473. case 0x45 :
  474. L2.size = 2048;
  475. L2.associative = 4;
  476. L2.linesize = 32;
  477. break;
  478. case 0x46 :
  479. L3.size = 4096;
  480. L3.associative = 4;
  481. L3.linesize = 64;
  482. break;
  483. case 0x47 :
  484. L3.size = 8192;
  485. L3.associative = 8;
  486. L3.linesize = 64;
  487. break;
  488. case 0x48 :
  489. L2.size = 3184;
  490. L2.associative = 12;
  491. L2.linesize = 64;
  492. break;
  493. case 0x49 :
  494. if ((get_cputype(GET_FAMILY) == 0x0f) && (get_cputype(GET_MODEL) == 0x06)) {
  495. L3.size = 4096;
  496. L3.associative = 16;
  497. L3.linesize = 64;
  498. } else {
  499. L2.size = 4096;
  500. L2.associative = 16;
  501. L2.linesize = 64;
  502. }
  503. break;
  504. case 0x4a :
  505. L3.size = 6144;
  506. L3.associative = 12;
  507. L3.linesize = 64;
  508. break;
  509. case 0x4b :
  510. L3.size = 8192;
  511. L3.associative = 16;
  512. L3.linesize = 64;
  513. break;
  514. case 0x4c :
  515. L3.size = 12280;
  516. L3.associative = 12;
  517. L3.linesize = 64;
  518. break;
  519. case 0x4d :
  520. L3.size = 16384;
  521. L3.associative = 16;
  522. L3.linesize = 64;
  523. break;
  524. case 0x4e :
  525. L2.size = 6144;
  526. L2.associative = 24;
  527. L2.linesize = 64;
  528. break;
  529. case 0x4f :
  530. ITB.size = 4;
  531. ITB.associative = 0;
  532. ITB.linesize = 32;
  533. break;
  534. case 0x50 :
  535. ITB.size = 4;
  536. ITB.associative = 0;
  537. ITB.linesize = 64;
  538. LITB.size = 4096;
  539. LITB.associative = 0;
  540. LITB.linesize = 64;
  541. LITB.shared = 1;
  542. break;
  543. case 0x51 :
  544. ITB.size = 4;
  545. ITB.associative = 0;
  546. ITB.linesize = 128;
  547. LITB.size = 4096;
  548. LITB.associative = 0;
  549. LITB.linesize = 128;
  550. LITB.shared = 1;
  551. break;
  552. case 0x52 :
  553. ITB.size = 4;
  554. ITB.associative = 0;
  555. ITB.linesize = 256;
  556. LITB.size = 4096;
  557. LITB.associative = 0;
  558. LITB.linesize = 256;
  559. LITB.shared = 1;
  560. break;
  561. case 0x55 :
  562. LITB.size = 4096;
  563. LITB.associative = 0;
  564. LITB.linesize = 7;
  565. LITB.shared = 1;
  566. break;
  567. case 0x56 :
  568. LDTB.size = 4096;
  569. LDTB.associative = 4;
  570. LDTB.linesize = 16;
  571. break;
  572. case 0x57 :
  573. LDTB.size = 4096;
  574. LDTB.associative = 4;
  575. LDTB.linesize = 16;
  576. break;
  577. case 0x5b :
  578. DTB.size = 4;
  579. DTB.associative = 0;
  580. DTB.linesize = 64;
  581. LDTB.size = 4096;
  582. LDTB.associative = 0;
  583. LDTB.linesize = 64;
  584. LDTB.shared = 1;
  585. break;
  586. case 0x5c :
  587. DTB.size = 4;
  588. DTB.associative = 0;
  589. DTB.linesize = 128;
  590. LDTB.size = 4096;
  591. LDTB.associative = 0;
  592. LDTB.linesize = 128;
  593. LDTB.shared = 1;
  594. break;
  595. case 0x5d :
  596. DTB.size = 4;
  597. DTB.associative = 0;
  598. DTB.linesize = 256;
  599. LDTB.size = 4096;
  600. LDTB.associative = 0;
  601. LDTB.linesize = 256;
  602. LDTB.shared = 1;
  603. break;
  604. case 0x60 :
  605. LD1.size = 16;
  606. LD1.associative = 8;
  607. LD1.linesize = 64;
  608. break;
  609. case 0x63 :
  610. DTB.size = 2048;
  611. DTB.associative = 4;
  612. DTB.linesize = 32;
  613. LDTB.size = 4096;
  614. LDTB.associative= 4;
  615. LDTB.linesize = 32;
  616. break;
  617. case 0x66 :
  618. LD1.size = 8;
  619. LD1.associative = 4;
  620. LD1.linesize = 64;
  621. break;
  622. case 0x67 :
  623. LD1.size = 16;
  624. LD1.associative = 4;
  625. LD1.linesize = 64;
  626. break;
  627. case 0x68 :
  628. LD1.size = 32;
  629. LD1.associative = 4;
  630. LD1.linesize = 64;
  631. break;
  632. case 0x70 :
  633. LC1.size = 12;
  634. LC1.associative = 8;
  635. break;
  636. case 0x71 :
  637. LC1.size = 16;
  638. LC1.associative = 8;
  639. break;
  640. case 0x72 :
  641. LC1.size = 32;
  642. LC1.associative = 8;
  643. break;
  644. case 0x73 :
  645. LC1.size = 64;
  646. LC1.associative = 8;
  647. break;
  648. case 0x76 :
  649. ITB.size = 2048;
  650. ITB.associative = 0;
  651. ITB.linesize = 8;
  652. LITB.size = 4096;
  653. LITB.associative= 0;
  654. LITB.linesize = 8;
  655. break;
  656. case 0x77 :
  657. LC1.size = 16;
  658. LC1.associative = 4;
  659. LC1.linesize = 64;
  660. break;
  661. case 0x78 :
  662. L2.size = 1024;
  663. L2.associative = 4;
  664. L2.linesize = 64;
  665. break;
  666. case 0x79 :
  667. L2.size = 128;
  668. L2.associative = 8;
  669. L2.linesize = 64;
  670. break;
  671. case 0x7a :
  672. L2.size = 256;
  673. L2.associative = 8;
  674. L2.linesize = 64;
  675. break;
  676. case 0x7b :
  677. L2.size = 512;
  678. L2.associative = 8;
  679. L2.linesize = 64;
  680. break;
  681. case 0x7c :
  682. L2.size = 1024;
  683. L2.associative = 8;
  684. L2.linesize = 64;
  685. break;
  686. case 0x7d :
  687. L2.size = 2048;
  688. L2.associative = 8;
  689. L2.linesize = 64;
  690. break;
  691. case 0x7e :
  692. L2.size = 256;
  693. L2.associative = 8;
  694. L2.linesize = 128;
  695. break;
  696. case 0x7f :
  697. L2.size = 512;
  698. L2.associative = 2;
  699. L2.linesize = 64;
  700. break;
  701. case 0x81 :
  702. L2.size = 128;
  703. L2.associative = 8;
  704. L2.linesize = 32;
  705. break;
  706. case 0x82 :
  707. L2.size = 256;
  708. L2.associative = 8;
  709. L2.linesize = 32;
  710. break;
  711. case 0x83 :
  712. L2.size = 512;
  713. L2.associative = 8;
  714. L2.linesize = 32;
  715. break;
  716. case 0x84 :
  717. L2.size = 1024;
  718. L2.associative = 8;
  719. L2.linesize = 32;
  720. break;
  721. case 0x85 :
  722. L2.size = 2048;
  723. L2.associative = 8;
  724. L2.linesize = 32;
  725. break;
  726. case 0x86 :
  727. L2.size = 512;
  728. L2.associative = 4;
  729. L2.linesize = 64;
  730. break;
  731. case 0x87 :
  732. L2.size = 1024;
  733. L2.associative = 8;
  734. L2.linesize = 64;
  735. break;
  736. case 0x88 :
  737. L3.size = 2048;
  738. L3.associative = 4;
  739. L3.linesize = 64;
  740. break;
  741. case 0x89 :
  742. L3.size = 4096;
  743. L3.associative = 4;
  744. L3.linesize = 64;
  745. break;
  746. case 0x8a :
  747. L3.size = 8192;
  748. L3.associative = 4;
  749. L3.linesize = 64;
  750. break;
  751. case 0x8d :
  752. L3.size = 3096;
  753. L3.associative = 12;
  754. L3.linesize = 128;
  755. break;
  756. case 0x90 :
  757. ITB.size = 4;
  758. ITB.associative = 0;
  759. ITB.linesize = 64;
  760. break;
  761. case 0x96 :
  762. DTB.size = 4;
  763. DTB.associative = 0;
  764. DTB.linesize = 32;
  765. break;
  766. case 0x9b :
  767. L2DTB.size = 4;
  768. L2DTB.associative = 0;
  769. L2DTB.linesize = 96;
  770. break;
  771. case 0xb0 :
  772. ITB.size = 4;
  773. ITB.associative = 4;
  774. ITB.linesize = 128;
  775. break;
  776. case 0xb1 :
  777. LITB.size = 4096;
  778. LITB.associative = 4;
  779. LITB.linesize = 4;
  780. break;
  781. case 0xb2 :
  782. ITB.size = 4;
  783. ITB.associative = 4;
  784. ITB.linesize = 64;
  785. break;
  786. case 0xb3 :
  787. DTB.size = 4;
  788. DTB.associative = 4;
  789. DTB.linesize = 128;
  790. break;
  791. case 0xb4 :
  792. DTB.size = 4;
  793. DTB.associative = 4;
  794. DTB.linesize = 256;
  795. break;
  796. case 0xba :
  797. DTB.size = 4;
  798. DTB.associative = 4;
  799. DTB.linesize = 64;
  800. break;
  801. case 0xd0 :
  802. L3.size = 512;
  803. L3.associative = 4;
  804. L3.linesize = 64;
  805. break;
  806. case 0xd1 :
  807. L3.size = 1024;
  808. L3.associative = 4;
  809. L3.linesize = 64;
  810. break;
  811. case 0xd2 :
  812. L3.size = 2048;
  813. L3.associative = 4;
  814. L3.linesize = 64;
  815. break;
  816. case 0xd6 :
  817. L3.size = 1024;
  818. L3.associative = 8;
  819. L3.linesize = 64;
  820. break;
  821. case 0xd7 :
  822. L3.size = 2048;
  823. L3.associative = 8;
  824. L3.linesize = 64;
  825. break;
  826. case 0xd8 :
  827. L3.size = 4096;
  828. L3.associative = 8;
  829. L3.linesize = 64;
  830. break;
  831. case 0xdc :
  832. L3.size = 2048;
  833. L3.associative = 12;
  834. L3.linesize = 64;
  835. break;
  836. case 0xdd :
  837. L3.size = 4096;
  838. L3.associative = 12;
  839. L3.linesize = 64;
  840. break;
  841. case 0xde :
  842. L3.size = 8192;
  843. L3.associative = 12;
  844. L3.linesize = 64;
  845. break;
  846. case 0xe2 :
  847. L3.size = 2048;
  848. L3.associative = 16;
  849. L3.linesize = 64;
  850. break;
  851. case 0xe3 :
  852. L3.size = 4096;
  853. L3.associative = 16;
  854. L3.linesize = 64;
  855. break;
  856. case 0xe4 :
  857. L3.size = 8192;
  858. L3.associative = 16;
  859. L3.linesize = 64;
  860. break;
  861. }
  862. }
  863. }
  864. if (get_vendor() == VENDOR_INTEL) {
  865. if(LD1.size<=0 || LC1.size<=0){
  866. //If we didn't detect L1 correctly before,
  867. int count;
  868. for (count=0;count <4;count++) {
  869. cpuid_count(4, count, &eax, &ebx, &ecx, &edx);
  870. switch (eax &0x1f) {
  871. case 0:
  872. continue;
  873. case 1:
  874. case 3:
  875. {
  876. switch ((eax >>5) &0x07)
  877. {
  878. case 1:
  879. {
  880. // fprintf(stderr,"L1 data cache...\n");
  881. int sets = ecx+1;
  882. int lines = (ebx & 0x0fff) +1;
  883. ebx>>=12;
  884. int part = (ebx&0x03ff)+1;
  885. ebx >>=10;
  886. int assoc = (ebx&0x03ff)+1;
  887. LD1.size = (assoc*part*lines*sets)/1024;
  888. LD1.associative = assoc;
  889. LD1.linesize= lines;
  890. break;
  891. }
  892. default:
  893. break;
  894. }
  895. break;
  896. }
  897. case 2:
  898. {
  899. switch ((eax >>5) &0x07)
  900. {
  901. case 1:
  902. {
  903. // fprintf(stderr,"L1 instruction cache...\n");
  904. int sets = ecx+1;
  905. int lines = (ebx & 0x0fff) +1;
  906. ebx>>=12;
  907. int part = (ebx&0x03ff)+1;
  908. ebx >>=10;
  909. int assoc = (ebx&0x03ff)+1;
  910. LC1.size = (assoc*part*lines*sets)/1024;
  911. LC1.associative = assoc;
  912. LC1.linesize= lines;
  913. break;
  914. }
  915. default:
  916. break;
  917. }
  918. break;
  919. }
  920. default:
  921. break;
  922. }
  923. }
  924. }
  925. cpuid(0x80000000, &cpuid_level, &ebx, &ecx, &edx);
  926. if (cpuid_level >= 0x80000006) {
  927. if(L2.size<=0){
  928. //If we didn't detect L2 correctly before,
  929. cpuid(0x80000006, &eax, &ebx, &ecx, &edx);
  930. L2.size = BITMASK(ecx, 16, 0xffff);
  931. L2.associative = BITMASK(ecx, 12, 0x0f);
  932. switch (L2.associative){
  933. case 0x06:
  934. L2.associative = 8;
  935. break;
  936. case 0x08:
  937. L2.associative = 16;
  938. break;
  939. }
  940. L2.linesize = BITMASK(ecx, 0, 0xff);
  941. }
  942. }
  943. }
  944. if ((get_vendor() == VENDOR_AMD) || (get_vendor() == VENDOR_CENTAUR)) {
  945. cpuid(0x80000005, &eax, &ebx, &ecx, &edx);
  946. LDTB.size = 4096;
  947. LDTB.associative = BITMASK(eax, 24, 0xff);
  948. if (LDTB.associative == 0xff) LDTB.associative = 0;
  949. LDTB.linesize = BITMASK(eax, 16, 0xff);
  950. LITB.size = 4096;
  951. LITB.associative = BITMASK(eax, 8, 0xff);
  952. if (LITB.associative == 0xff) LITB.associative = 0;
  953. LITB.linesize = BITMASK(eax, 0, 0xff);
  954. DTB.size = 4;
  955. DTB.associative = BITMASK(ebx, 24, 0xff);
  956. if (DTB.associative == 0xff) DTB.associative = 0;
  957. DTB.linesize = BITMASK(ebx, 16, 0xff);
  958. ITB.size = 4;
  959. ITB.associative = BITMASK(ebx, 8, 0xff);
  960. if (ITB.associative == 0xff) ITB.associative = 0;
  961. ITB.linesize = BITMASK(ebx, 0, 0xff);
  962. LD1.size = BITMASK(ecx, 24, 0xff);
  963. LD1.associative = BITMASK(ecx, 16, 0xff);
  964. if (LD1.associative == 0xff) LD1.associative = 0;
  965. LD1.linesize = BITMASK(ecx, 0, 0xff);
  966. LC1.size = BITMASK(ecx, 24, 0xff);
  967. LC1.associative = BITMASK(ecx, 16, 0xff);
  968. if (LC1.associative == 0xff) LC1.associative = 0;
  969. LC1.linesize = BITMASK(ecx, 0, 0xff);
  970. cpuid(0x80000006, &eax, &ebx, &ecx, &edx);
  971. L2LDTB.size = 4096;
  972. L2LDTB.associative = BITMASK(eax, 24, 0xff);
  973. if (L2LDTB.associative == 0xff) L2LDTB.associative = 0;
  974. L2LDTB.linesize = BITMASK(eax, 16, 0xff);
  975. L2LITB.size = 4096;
  976. L2LITB.associative = BITMASK(eax, 8, 0xff);
  977. if (L2LITB.associative == 0xff) L2LITB.associative = 0;
  978. L2LITB.linesize = BITMASK(eax, 0, 0xff);
  979. L2DTB.size = 4;
  980. L2DTB.associative = BITMASK(ebx, 24, 0xff);
  981. if (L2DTB.associative == 0xff) L2DTB.associative = 0;
  982. L2DTB.linesize = BITMASK(ebx, 16, 0xff);
  983. L2ITB.size = 4;
  984. L2ITB.associative = BITMASK(ebx, 8, 0xff);
  985. if (L2ITB.associative == 0xff) L2ITB.associative = 0;
  986. L2ITB.linesize = BITMASK(ebx, 0, 0xff);
  987. if(L2.size <= 0){
  988. //If we didn't detect L2 correctly before,
  989. L2.size = BITMASK(ecx, 16, 0xffff);
  990. L2.associative = BITMASK(ecx, 12, 0xf);
  991. switch (L2.associative){
  992. case 0x06:
  993. L2.associative = 8;
  994. break;
  995. case 0x08:
  996. L2.associative = 16;
  997. break;
  998. }
  999. if (L2.associative == 0xff) L2.associative = 0;
  1000. L2.linesize = BITMASK(ecx, 0, 0xff);
  1001. }
  1002. L3.size = BITMASK(edx, 18, 0x3fff) * 512;
  1003. L3.associative = BITMASK(edx, 12, 0xf);
  1004. if (L3.associative == 0xff) L2.associative = 0;
  1005. L3.linesize = BITMASK(edx, 0, 0xff);
  1006. }
  1007. switch (type) {
  1008. case CACHE_INFO_L1_I :
  1009. *cacheinfo = LC1;
  1010. break;
  1011. case CACHE_INFO_L1_D :
  1012. *cacheinfo = LD1;
  1013. break;
  1014. case CACHE_INFO_L2 :
  1015. *cacheinfo = L2;
  1016. break;
  1017. case CACHE_INFO_L3 :
  1018. *cacheinfo = L3;
  1019. break;
  1020. case CACHE_INFO_L1_DTB :
  1021. *cacheinfo = DTB;
  1022. break;
  1023. case CACHE_INFO_L1_ITB :
  1024. *cacheinfo = ITB;
  1025. break;
  1026. case CACHE_INFO_L1_LDTB :
  1027. *cacheinfo = LDTB;
  1028. break;
  1029. case CACHE_INFO_L1_LITB :
  1030. *cacheinfo = LITB;
  1031. break;
  1032. case CACHE_INFO_L2_DTB :
  1033. *cacheinfo = L2DTB;
  1034. break;
  1035. case CACHE_INFO_L2_ITB :
  1036. *cacheinfo = L2ITB;
  1037. break;
  1038. case CACHE_INFO_L2_LDTB :
  1039. *cacheinfo = L2LDTB;
  1040. break;
  1041. case CACHE_INFO_L2_LITB :
  1042. *cacheinfo = L2LITB;
  1043. break;
  1044. }
  1045. return 0;
  1046. }
  1047. int get_cpuname(void){
  1048. int family, exfamily, model, vendor, exmodel;
  1049. if (!have_cpuid()) return CPUTYPE_80386;
  1050. family = get_cputype(GET_FAMILY);
  1051. exfamily = get_cputype(GET_EXFAMILY);
  1052. model = get_cputype(GET_MODEL);
  1053. exmodel = get_cputype(GET_EXMODEL);
  1054. vendor = get_vendor();
  1055. if (vendor == VENDOR_INTEL){
  1056. switch (family) {
  1057. case 0x4:
  1058. return CPUTYPE_80486;
  1059. case 0x5:
  1060. return CPUTYPE_PENTIUM;
  1061. case 0x6:
  1062. switch (exmodel) {
  1063. case 0:
  1064. switch (model) {
  1065. case 1:
  1066. case 3:
  1067. case 5:
  1068. case 6:
  1069. return CPUTYPE_PENTIUM2;
  1070. case 7:
  1071. case 8:
  1072. case 10:
  1073. case 11:
  1074. return CPUTYPE_PENTIUM3;
  1075. case 9:
  1076. case 13:
  1077. case 14:
  1078. return CPUTYPE_PENTIUMM;
  1079. case 15:
  1080. return CPUTYPE_CORE2;
  1081. }
  1082. break;
  1083. case 1:
  1084. switch (model) {
  1085. case 6:
  1086. return CPUTYPE_CORE2;
  1087. case 7:
  1088. return CPUTYPE_PENRYN;
  1089. case 10:
  1090. case 11:
  1091. case 14:
  1092. case 15:
  1093. return CPUTYPE_NEHALEM;
  1094. case 12:
  1095. return CPUTYPE_ATOM;
  1096. case 13:
  1097. return CPUTYPE_DUNNINGTON;
  1098. }
  1099. break;
  1100. case 2:
  1101. switch (model) {
  1102. case 5:
  1103. //Intel Core (Clarkdale) / Core (Arrandale)
  1104. // Pentium (Clarkdale) / Pentium Mobile (Arrandale)
  1105. // Xeon (Clarkdale), 32nm
  1106. return CPUTYPE_NEHALEM;
  1107. case 10:
  1108. //Intel Core i5-2000 /i7-2000 (Sandy Bridge)
  1109. if(support_avx())
  1110. return CPUTYPE_SANDYBRIDGE;
  1111. else
  1112. return CPUTYPE_NEHALEM; //OS doesn't support AVX
  1113. case 12:
  1114. //Xeon Processor 5600 (Westmere-EP)
  1115. return CPUTYPE_NEHALEM;
  1116. case 13:
  1117. //Intel Core i7-3000 / Xeon E5 (Sandy Bridge)
  1118. if(support_avx())
  1119. return CPUTYPE_SANDYBRIDGE;
  1120. else
  1121. return CPUTYPE_NEHALEM;
  1122. case 14:
  1123. // Xeon E7540
  1124. case 15:
  1125. //Xeon Processor E7 (Westmere-EX)
  1126. return CPUTYPE_NEHALEM;
  1127. }
  1128. break;
  1129. case 3:
  1130. switch (model) {
  1131. case 7:
  1132. // Bay Trail
  1133. return CPUTYPE_ATOM;
  1134. case 10:
  1135. case 14:
  1136. // Ivy Bridge
  1137. if(support_avx())
  1138. return CPUTYPE_SANDYBRIDGE;
  1139. else
  1140. return CPUTYPE_NEHALEM;
  1141. case 12:
  1142. case 15:
  1143. if(support_avx())
  1144. #ifndef NO_AVX2
  1145. return CPUTYPE_HASWELL;
  1146. #else
  1147. return CPUTYPE_SANDYBRIDGE;
  1148. #endif
  1149. else
  1150. return CPUTYPE_NEHALEM;
  1151. case 13:
  1152. //Broadwell
  1153. if(support_avx())
  1154. #ifndef NO_AVX2
  1155. return CPUTYPE_HASWELL;
  1156. #else
  1157. return CPUTYPE_SANDYBRIDGE;
  1158. #endif
  1159. else
  1160. return CPUTYPE_NEHALEM;
  1161. }
  1162. break;
  1163. case 4:
  1164. switch (model) {
  1165. case 5:
  1166. case 6:
  1167. if(support_avx())
  1168. #ifndef NO_AVX2
  1169. return CPUTYPE_HASWELL;
  1170. #else
  1171. return CPUTYPE_SANDYBRIDGE;
  1172. #endif
  1173. else
  1174. return CPUTYPE_NEHALEM;
  1175. case 7:
  1176. case 15:
  1177. //Broadwell
  1178. if(support_avx())
  1179. #ifndef NO_AVX2
  1180. return CPUTYPE_HASWELL;
  1181. #else
  1182. return CPUTYPE_SANDYBRIDGE;
  1183. #endif
  1184. else
  1185. return CPUTYPE_NEHALEM;
  1186. case 14:
  1187. //Skylake
  1188. if(support_avx())
  1189. #ifndef NO_AVX2
  1190. return CPUTYPE_HASWELL;
  1191. #else
  1192. return CPUTYPE_SANDYBRIDGE;
  1193. #endif
  1194. else
  1195. return CPUTYPE_NEHALEM;
  1196. case 12:
  1197. // Braswell
  1198. case 13:
  1199. // Avoton
  1200. return CPUTYPE_NEHALEM;
  1201. }
  1202. break;
  1203. case 5:
  1204. switch (model) {
  1205. case 6:
  1206. //Broadwell
  1207. if(support_avx())
  1208. #ifndef NO_AVX2
  1209. return CPUTYPE_HASWELL;
  1210. #else
  1211. return CPUTYPE_SANDYBRIDGE;
  1212. #endif
  1213. else
  1214. return CPUTYPE_NEHALEM;
  1215. case 5:
  1216. case 14:
  1217. // Skylake
  1218. if(support_avx())
  1219. #ifndef NO_AVX2
  1220. return CPUTYPE_HASWELL;
  1221. #else
  1222. return CPUTYPE_SANDYBRIDGE;
  1223. #endif
  1224. else
  1225. return CPUTYPE_NEHALEM;
  1226. case 7:
  1227. // Xeon Phi Knights Landing
  1228. if(support_avx())
  1229. #ifndef NO_AVX2
  1230. return CPUTYPE_HASWELL;
  1231. #else
  1232. return CPUTYPE_SANDYBRIDGE;
  1233. #endif
  1234. else
  1235. return CPUTYPE_NEHALEM;
  1236. case 12:
  1237. // Apollo Lake
  1238. return CPUTYPE_NEHALEM;
  1239. }
  1240. break;
  1241. case 9:
  1242. case 8:
  1243. switch (model) {
  1244. case 14: // Kaby Lake
  1245. if(support_avx())
  1246. #ifndef NO_AVX2
  1247. return CPUTYPE_HASWELL;
  1248. #else
  1249. return CPUTYPE_SANDYBRIDGE;
  1250. #endif
  1251. else
  1252. return CPUTYPE_NEHALEM;
  1253. }
  1254. break;
  1255. }
  1256. break;
  1257. case 0x7:
  1258. return CPUTYPE_ITANIUM;
  1259. case 0xf:
  1260. switch (exfamily) {
  1261. case 0 :
  1262. return CPUTYPE_PENTIUM4;
  1263. case 1 :
  1264. return CPUTYPE_ITANIUM;
  1265. }
  1266. break;
  1267. }
  1268. return CPUTYPE_INTEL_UNKNOWN;
  1269. }
  1270. if (vendor == VENDOR_AMD){
  1271. switch (family) {
  1272. case 0x4:
  1273. return CPUTYPE_AMD5X86;
  1274. case 0x5:
  1275. return CPUTYPE_AMDK6;
  1276. case 0x6:
  1277. return CPUTYPE_ATHLON;
  1278. case 0xf:
  1279. switch (exfamily) {
  1280. case 0:
  1281. case 2:
  1282. return CPUTYPE_OPTERON;
  1283. case 1:
  1284. case 3:
  1285. case 7:
  1286. case 10:
  1287. return CPUTYPE_BARCELONA;
  1288. case 5:
  1289. return CPUTYPE_BOBCAT;
  1290. case 6:
  1291. switch (model) {
  1292. case 1:
  1293. //AMD Bulldozer Opteron 6200 / Opteron 4200 / AMD FX-Series
  1294. if(support_avx())
  1295. return CPUTYPE_BULLDOZER;
  1296. else
  1297. return CPUTYPE_BARCELONA; //OS don't support AVX.
  1298. case 2: //AMD Piledriver
  1299. case 3: //AMD Richland
  1300. if(support_avx())
  1301. return CPUTYPE_PILEDRIVER;
  1302. else
  1303. return CPUTYPE_BARCELONA; //OS don't support AVX.
  1304. case 5: // New EXCAVATOR CPUS
  1305. if(support_avx())
  1306. return CPUTYPE_EXCAVATOR;
  1307. else
  1308. return CPUTYPE_BARCELONA; //OS don't support AVX.
  1309. case 0:
  1310. case 8:
  1311. switch(exmodel){
  1312. case 1: //AMD Trinity
  1313. if(support_avx())
  1314. return CPUTYPE_PILEDRIVER;
  1315. else
  1316. return CPUTYPE_BARCELONA; //OS don't support AVX.
  1317. case 3:
  1318. if(support_avx())
  1319. return CPUTYPE_STEAMROLLER;
  1320. else
  1321. return CPUTYPE_BARCELONA; //OS don't support AVX.
  1322. case 6:
  1323. if(support_avx())
  1324. return CPUTYPE_EXCAVATOR;
  1325. else
  1326. return CPUTYPE_BARCELONA; //OS don't support AVX.
  1327. }
  1328. break;
  1329. }
  1330. break;
  1331. case 8:
  1332. switch (model) {
  1333. case 1:
  1334. // AMD Ryzen
  1335. if(support_avx())
  1336. #ifndef NO_AVX2
  1337. return CPUTYPE_ZEN;
  1338. #else
  1339. return CPUTYPE_SANDYBRIDGE; // Zen is closer in architecture to Sandy Bridge than to Excavator
  1340. #endif
  1341. else
  1342. return CPUTYPE_BARCELONA;
  1343. }
  1344. }
  1345. break;
  1346. }
  1347. return CPUTYPE_AMD_UNKNOWN;
  1348. }
  1349. if (vendor == VENDOR_CYRIX){
  1350. switch (family) {
  1351. case 0x4:
  1352. return CPUTYPE_CYRIX5X86;
  1353. case 0x5:
  1354. return CPUTYPE_CYRIXM1;
  1355. case 0x6:
  1356. return CPUTYPE_CYRIXM2;
  1357. }
  1358. return CPUTYPE_CYRIX_UNKNOWN;
  1359. }
  1360. if (vendor == VENDOR_NEXGEN){
  1361. switch (family) {
  1362. case 0x5:
  1363. return CPUTYPE_NEXGENNX586;
  1364. }
  1365. return CPUTYPE_NEXGEN_UNKNOWN;
  1366. }
  1367. if (vendor == VENDOR_CENTAUR){
  1368. switch (family) {
  1369. case 0x5:
  1370. return CPUTYPE_CENTAURC6;
  1371. break;
  1372. case 0x6:
  1373. return CPUTYPE_NANO;
  1374. break;
  1375. }
  1376. return CPUTYPE_VIAC3;
  1377. }
  1378. if (vendor == VENDOR_RISE){
  1379. switch (family) {
  1380. case 0x5:
  1381. return CPUTYPE_RISEMP6;
  1382. }
  1383. return CPUTYPE_RISE_UNKNOWN;
  1384. }
  1385. if (vendor == VENDOR_SIS){
  1386. switch (family) {
  1387. case 0x5:
  1388. return CPUTYPE_SYS55X;
  1389. }
  1390. return CPUTYPE_SIS_UNKNOWN;
  1391. }
  1392. if (vendor == VENDOR_TRANSMETA){
  1393. switch (family) {
  1394. case 0x5:
  1395. return CPUTYPE_CRUSOETM3X;
  1396. }
  1397. return CPUTYPE_TRANSMETA_UNKNOWN;
  1398. }
  1399. if (vendor == VENDOR_NSC){
  1400. switch (family) {
  1401. case 0x5:
  1402. return CPUTYPE_NSGEODE;
  1403. }
  1404. return CPUTYPE_NSC_UNKNOWN;
  1405. }
  1406. return CPUTYPE_UNKNOWN;
  1407. }
  1408. static char *cpuname[] = {
  1409. "UNKNOWN",
  1410. "INTEL_UNKNOWN",
  1411. "UMC_UNKNOWN",
  1412. "AMD_UNKNOWN",
  1413. "CYRIX_UNKNOWN",
  1414. "NEXGEN_UNKNOWN",
  1415. "CENTAUR_UNKNOWN",
  1416. "RISE_UNKNOWN",
  1417. "SIS_UNKNOWN",
  1418. "TRANSMETA_UNKNOWN",
  1419. "NSC_UNKNOWN",
  1420. "80386",
  1421. "80486",
  1422. "PENTIUM",
  1423. "PENTIUM2",
  1424. "PENTIUM3",
  1425. "PENTIUMM",
  1426. "PENTIUM4",
  1427. "CORE2",
  1428. "PENRYN",
  1429. "DUNNINGTON",
  1430. "NEHALEM",
  1431. "ATOM",
  1432. "ITANIUM",
  1433. "ITANIUM2",
  1434. "5X86",
  1435. "K6",
  1436. "ATHLON",
  1437. "DURON",
  1438. "OPTERON",
  1439. "BARCELONA",
  1440. "SHANGHAI",
  1441. "ISTANBUL",
  1442. "CYRIX5X86",
  1443. "CYRIXM1",
  1444. "CYRIXM2",
  1445. "NEXGENNX586",
  1446. "CENTAURC6",
  1447. "RISEMP6",
  1448. "SYS55X",
  1449. "TM3X00",
  1450. "NSGEODE",
  1451. "VIAC3",
  1452. "NANO",
  1453. "SANDYBRIDGE",
  1454. "BOBCAT",
  1455. "BULLDOZER",
  1456. "PILEDRIVER",
  1457. "HASWELL",
  1458. "STEAMROLLER",
  1459. "EXCAVATOR",
  1460. "ZEN",
  1461. };
  1462. static char *lowercpuname[] = {
  1463. "unknown",
  1464. "intel_unknown",
  1465. "umc_unknown",
  1466. "amd_unknown",
  1467. "cyrix_unknown",
  1468. "nexgen_unknown",
  1469. "centaur_unknown",
  1470. "rise_unknown",
  1471. "sis_unknown",
  1472. "transmeta_unknown",
  1473. "nsc_unknown",
  1474. "80386",
  1475. "80486",
  1476. "pentium",
  1477. "pentium2",
  1478. "pentium3",
  1479. "pentiumm",
  1480. "pentium4",
  1481. "core2",
  1482. "penryn",
  1483. "dunnington",
  1484. "nehalem",
  1485. "atom",
  1486. "itanium",
  1487. "itanium2",
  1488. "5x86",
  1489. "k6",
  1490. "athlon",
  1491. "duron",
  1492. "opteron",
  1493. "barcelona",
  1494. "shanghai",
  1495. "istanbul",
  1496. "cyrix5x86",
  1497. "cyrixm1",
  1498. "cyrixm2",
  1499. "nexgennx586",
  1500. "centaurc6",
  1501. "risemp6",
  1502. "sys55x",
  1503. "tms3x00",
  1504. "nsgeode",
  1505. "nano",
  1506. "sandybridge",
  1507. "bobcat",
  1508. "bulldozer",
  1509. "piledriver",
  1510. "haswell",
  1511. "steamroller",
  1512. "excavator",
  1513. "zen",
  1514. };
  1515. static char *corename[] = {
  1516. "UNKOWN",
  1517. "80486",
  1518. "P5",
  1519. "P6",
  1520. "KATMAI",
  1521. "COPPERMINE",
  1522. "NORTHWOOD",
  1523. "PRESCOTT",
  1524. "BANIAS",
  1525. "ATHLON",
  1526. "OPTERON",
  1527. "BARCELONA",
  1528. "VIAC3",
  1529. "YONAH",
  1530. "CORE2",
  1531. "PENRYN",
  1532. "DUNNINGTON",
  1533. "NEHALEM",
  1534. "ATOM",
  1535. "NANO",
  1536. "SANDYBRIDGE",
  1537. "BOBCAT",
  1538. "BULLDOZER",
  1539. "PILEDRIVER",
  1540. "HASWELL",
  1541. "STEAMROLLER",
  1542. "EXCAVATOR",
  1543. "ZEN",
  1544. };
  1545. static char *corename_lower[] = {
  1546. "unknown",
  1547. "80486",
  1548. "p5",
  1549. "p6",
  1550. "katmai",
  1551. "coppermine",
  1552. "northwood",
  1553. "prescott",
  1554. "banias",
  1555. "athlon",
  1556. "opteron",
  1557. "barcelona",
  1558. "viac3",
  1559. "yonah",
  1560. "core2",
  1561. "penryn",
  1562. "dunnington",
  1563. "nehalem",
  1564. "atom",
  1565. "nano",
  1566. "sandybridge",
  1567. "bobcat",
  1568. "bulldozer",
  1569. "piledriver",
  1570. "haswell",
  1571. "steamroller",
  1572. "excavator",
  1573. "zen",
  1574. };
  1575. char *get_cpunamechar(void){
  1576. return cpuname[get_cpuname()];
  1577. }
  1578. char *get_lower_cpunamechar(void){
  1579. return lowercpuname[get_cpuname()];
  1580. }
  1581. int get_coretype(void){
  1582. int family, exfamily, model, exmodel, vendor;
  1583. if (!have_cpuid()) return CORE_80486;
  1584. family = get_cputype(GET_FAMILY);
  1585. exfamily = get_cputype(GET_EXFAMILY);
  1586. model = get_cputype(GET_MODEL);
  1587. exmodel = get_cputype(GET_EXMODEL);
  1588. vendor = get_vendor();
  1589. if (vendor == VENDOR_INTEL){
  1590. switch (family) {
  1591. case 4:
  1592. return CORE_80486;
  1593. case 5:
  1594. return CORE_P5;
  1595. case 6:
  1596. switch (exmodel) {
  1597. case 0:
  1598. switch (model) {
  1599. case 0:
  1600. case 1:
  1601. case 2:
  1602. case 3:
  1603. case 4:
  1604. case 5:
  1605. case 6:
  1606. return CORE_P6;
  1607. case 7:
  1608. return CORE_KATMAI;
  1609. case 8:
  1610. case 10:
  1611. case 11:
  1612. return CORE_COPPERMINE;
  1613. case 9:
  1614. case 13:
  1615. case 14:
  1616. return CORE_BANIAS;
  1617. case 15:
  1618. return CORE_CORE2;
  1619. }
  1620. break;
  1621. case 1:
  1622. switch (model) {
  1623. case 6:
  1624. return CORE_CORE2;
  1625. case 7:
  1626. return CORE_PENRYN;
  1627. case 10:
  1628. case 11:
  1629. case 14:
  1630. case 15:
  1631. return CORE_NEHALEM;
  1632. case 12:
  1633. return CORE_ATOM;
  1634. case 13:
  1635. return CORE_DUNNINGTON;
  1636. }
  1637. break;
  1638. case 2:
  1639. switch (model) {
  1640. case 5:
  1641. //Intel Core (Clarkdale) / Core (Arrandale)
  1642. // Pentium (Clarkdale) / Pentium Mobile (Arrandale)
  1643. // Xeon (Clarkdale), 32nm
  1644. return CORE_NEHALEM;
  1645. case 10:
  1646. //Intel Core i5-2000 /i7-2000 (Sandy Bridge)
  1647. if(support_avx())
  1648. return CORE_SANDYBRIDGE;
  1649. else
  1650. return CORE_NEHALEM; //OS doesn't support AVX
  1651. case 12:
  1652. //Xeon Processor 5600 (Westmere-EP)
  1653. return CORE_NEHALEM;
  1654. case 13:
  1655. //Intel Core i7-3000 / Xeon E5 (Sandy Bridge)
  1656. if(support_avx())
  1657. return CORE_SANDYBRIDGE;
  1658. else
  1659. return CORE_NEHALEM; //OS doesn't support AVX
  1660. case 14:
  1661. //Xeon E7540
  1662. case 15:
  1663. //Xeon Processor E7 (Westmere-EX)
  1664. return CORE_NEHALEM;
  1665. }
  1666. break;
  1667. case 3:
  1668. switch (model) {
  1669. case 7:
  1670. return CORE_ATOM;
  1671. case 10:
  1672. case 14:
  1673. if(support_avx())
  1674. return CORE_SANDYBRIDGE;
  1675. else
  1676. return CORE_NEHALEM; //OS doesn't support AVX
  1677. case 12:
  1678. case 15:
  1679. if(support_avx())
  1680. #ifndef NO_AVX2
  1681. return CORE_HASWELL;
  1682. #else
  1683. return CORE_SANDYBRIDGE;
  1684. #endif
  1685. else
  1686. return CORE_NEHALEM;
  1687. case 13:
  1688. //broadwell
  1689. if(support_avx())
  1690. #ifndef NO_AVX2
  1691. return CORE_HASWELL;
  1692. #else
  1693. return CORE_SANDYBRIDGE;
  1694. #endif
  1695. else
  1696. return CORE_NEHALEM;
  1697. }
  1698. break;
  1699. case 4:
  1700. switch (model) {
  1701. case 5:
  1702. case 6:
  1703. if(support_avx())
  1704. #ifndef NO_AVX2
  1705. return CORE_HASWELL;
  1706. #else
  1707. return CORE_SANDYBRIDGE;
  1708. #endif
  1709. else
  1710. return CORE_NEHALEM;
  1711. case 7:
  1712. case 15:
  1713. //broadwell
  1714. if(support_avx())
  1715. #ifndef NO_AVX2
  1716. return CORE_HASWELL;
  1717. #else
  1718. return CORE_SANDYBRIDGE;
  1719. #endif
  1720. else
  1721. return CORE_NEHALEM;
  1722. case 14:
  1723. //Skylake
  1724. if(support_avx())
  1725. #ifndef NO_AVX2
  1726. return CORE_HASWELL;
  1727. #else
  1728. return CORE_SANDYBRIDGE;
  1729. #endif
  1730. else
  1731. return CORE_NEHALEM;
  1732. case 12:
  1733. // Braswell
  1734. case 13:
  1735. // Avoton
  1736. return CORE_NEHALEM;
  1737. }
  1738. break;
  1739. case 5:
  1740. switch (model) {
  1741. case 6:
  1742. //broadwell
  1743. if(support_avx())
  1744. #ifndef NO_AVX2
  1745. return CORE_HASWELL;
  1746. #else
  1747. return CORE_SANDYBRIDGE;
  1748. #endif
  1749. else
  1750. return CORE_NEHALEM;
  1751. case 5:
  1752. case 14:
  1753. // Skylake
  1754. if(support_avx())
  1755. #ifndef NO_AVX2
  1756. return CORE_HASWELL;
  1757. #else
  1758. return CORE_SANDYBRIDGE;
  1759. #endif
  1760. else
  1761. return CORE_NEHALEM;
  1762. case 7:
  1763. // Phi Knights Landing
  1764. if(support_avx())
  1765. #ifndef NO_AVX2
  1766. return CORE_HASWELL;
  1767. #else
  1768. return CORE_SANDYBRIDGE;
  1769. #endif
  1770. else
  1771. return CORE_NEHALEM;
  1772. case 12:
  1773. // Apollo Lake
  1774. return CORE_NEHALEM;
  1775. }
  1776. break;
  1777. case 9:
  1778. case 8:
  1779. if (model == 14) { // Kaby Lake
  1780. if(support_avx())
  1781. #ifndef NO_AVX2
  1782. return CORE_HASWELL;
  1783. #else
  1784. return CORE_SANDYBRIDGE;
  1785. #endif
  1786. else
  1787. return CORE_NEHALEM;
  1788. }
  1789. }
  1790. break;
  1791. case 15:
  1792. if (model <= 0x2) return CORE_NORTHWOOD;
  1793. else return CORE_PRESCOTT;
  1794. }
  1795. }
  1796. if (vendor == VENDOR_AMD){
  1797. if (family <= 0x5) return CORE_80486;
  1798. if (family <= 0xe) return CORE_ATHLON;
  1799. if (family == 0xf){
  1800. if ((exfamily == 0) || (exfamily == 2)) return CORE_OPTERON;
  1801. else if (exfamily == 5) return CORE_BOBCAT;
  1802. else if (exfamily == 6) {
  1803. switch (model) {
  1804. case 1:
  1805. //AMD Bulldozer Opteron 6200 / Opteron 4200 / AMD FX-Series
  1806. if(support_avx())
  1807. return CORE_BULLDOZER;
  1808. else
  1809. return CORE_BARCELONA; //OS don't support AVX.
  1810. case 2: //AMD Piledriver
  1811. case 3: //AMD Richland
  1812. if(support_avx())
  1813. return CORE_PILEDRIVER;
  1814. else
  1815. return CORE_BARCELONA; //OS don't support AVX.
  1816. case 5: // New EXCAVATOR
  1817. if(support_avx())
  1818. return CORE_EXCAVATOR;
  1819. else
  1820. return CORE_BARCELONA; //OS don't support AVX.
  1821. case 0:
  1822. case 8:
  1823. switch(exmodel){
  1824. case 1: //AMD Trinity
  1825. if(support_avx())
  1826. return CORE_PILEDRIVER;
  1827. else
  1828. return CORE_BARCELONA; //OS don't support AVX.
  1829. case 3:
  1830. if(support_avx())
  1831. return CORE_STEAMROLLER;
  1832. else
  1833. return CORE_BARCELONA; //OS don't support AVX.
  1834. case 6:
  1835. if(support_avx())
  1836. return CORE_EXCAVATOR;
  1837. else
  1838. return CORE_BARCELONA; //OS don't support AVX.
  1839. }
  1840. break;
  1841. }
  1842. } else if (exfamily == 8) {
  1843. switch (model) {
  1844. case 1:
  1845. // AMD Ryzen
  1846. if(support_avx())
  1847. #ifndef NO_AVX2
  1848. return CORE_ZEN;
  1849. #else
  1850. return CORE_SANDYBRIDGE; // Zen is closer in architecture to Sandy Bridge than to Excavator
  1851. #endif
  1852. else
  1853. return CORE_BARCELONA;
  1854. }
  1855. } else {
  1856. return CORE_BARCELONA;
  1857. }
  1858. }
  1859. }
  1860. if (vendor == VENDOR_CENTAUR) {
  1861. switch (family) {
  1862. case 0x6:
  1863. return CORE_NANO;
  1864. break;
  1865. }
  1866. return CORE_VIAC3;
  1867. }
  1868. return CORE_UNKNOWN;
  1869. }
  1870. void get_cpuconfig(void){
  1871. cache_info_t info;
  1872. int features;
  1873. printf("#define %s\n", cpuname[get_cpuname()]);
  1874. if (get_coretype() != CORE_P5) {
  1875. get_cacheinfo(CACHE_INFO_L1_I, &info);
  1876. if (info.size > 0) {
  1877. printf("#define L1_CODE_SIZE %d\n", info.size * 1024);
  1878. printf("#define L1_CODE_ASSOCIATIVE %d\n", info.associative);
  1879. printf("#define L1_CODE_LINESIZE %d\n", info.linesize);
  1880. }
  1881. get_cacheinfo(CACHE_INFO_L1_D, &info);
  1882. if (info.size > 0) {
  1883. printf("#define L1_DATA_SIZE %d\n", info.size * 1024);
  1884. printf("#define L1_DATA_ASSOCIATIVE %d\n", info.associative);
  1885. printf("#define L1_DATA_LINESIZE %d\n", info.linesize);
  1886. }
  1887. get_cacheinfo(CACHE_INFO_L2, &info);
  1888. if (info.size > 0) {
  1889. printf("#define L2_SIZE %d\n", info.size * 1024);
  1890. printf("#define L2_ASSOCIATIVE %d\n", info.associative);
  1891. printf("#define L2_LINESIZE %d\n", info.linesize);
  1892. } else {
  1893. //fall back for some virtual machines.
  1894. printf("#define L2_SIZE 1048576\n");
  1895. printf("#define L2_ASSOCIATIVE 6\n");
  1896. printf("#define L2_LINESIZE 64\n");
  1897. }
  1898. get_cacheinfo(CACHE_INFO_L3, &info);
  1899. if (info.size > 0) {
  1900. printf("#define L3_SIZE %d\n", info.size * 1024);
  1901. printf("#define L3_ASSOCIATIVE %d\n", info.associative);
  1902. printf("#define L3_LINESIZE %d\n", info.linesize);
  1903. }
  1904. get_cacheinfo(CACHE_INFO_L1_ITB, &info);
  1905. if (info.size > 0) {
  1906. printf("#define ITB_SIZE %d\n", info.size * 1024);
  1907. printf("#define ITB_ASSOCIATIVE %d\n", info.associative);
  1908. printf("#define ITB_ENTRIES %d\n", info.linesize);
  1909. }
  1910. get_cacheinfo(CACHE_INFO_L1_DTB, &info);
  1911. if (info.size > 0) {
  1912. printf("#define DTB_SIZE %d\n", info.size * 1024);
  1913. printf("#define DTB_ASSOCIATIVE %d\n", info.associative);
  1914. printf("#define DTB_DEFAULT_ENTRIES %d\n", info.linesize);
  1915. } else {
  1916. //fall back for some virtual machines.
  1917. printf("#define DTB_DEFAULT_ENTRIES 32\n");
  1918. }
  1919. features = get_cputype(GET_FEATURE);
  1920. if (features & HAVE_CMOV ) printf("#define HAVE_CMOV\n");
  1921. if (features & HAVE_MMX ) printf("#define HAVE_MMX\n");
  1922. if (features & HAVE_SSE ) printf("#define HAVE_SSE\n");
  1923. if (features & HAVE_SSE2 ) printf("#define HAVE_SSE2\n");
  1924. if (features & HAVE_SSE3 ) printf("#define HAVE_SSE3\n");
  1925. if (features & HAVE_SSSE3) printf("#define HAVE_SSSE3\n");
  1926. if (features & HAVE_SSE4_1) printf("#define HAVE_SSE4_1\n");
  1927. if (features & HAVE_SSE4_2) printf("#define HAVE_SSE4_2\n");
  1928. if (features & HAVE_SSE4A) printf("#define HAVE_SSE4A\n");
  1929. if (features & HAVE_SSE5 ) printf("#define HAVE_SSSE5\n");
  1930. if (features & HAVE_AVX ) printf("#define HAVE_AVX\n");
  1931. if (features & HAVE_3DNOWEX) printf("#define HAVE_3DNOWEX\n");
  1932. if (features & HAVE_3DNOW) printf("#define HAVE_3DNOW\n");
  1933. if (features & HAVE_FMA4 ) printf("#define HAVE_FMA4\n");
  1934. if (features & HAVE_FMA3 ) printf("#define HAVE_FMA3\n");
  1935. if (features & HAVE_CFLUSH) printf("#define HAVE_CFLUSH\n");
  1936. if (features & HAVE_HIT) printf("#define HAVE_HIT 1\n");
  1937. if (features & HAVE_MISALIGNSSE) printf("#define HAVE_MISALIGNSSE\n");
  1938. if (features & HAVE_128BITFPU) printf("#define HAVE_128BITFPU\n");
  1939. if (features & HAVE_FASTMOVU) printf("#define HAVE_FASTMOVU\n");
  1940. printf("#define NUM_SHAREDCACHE %d\n", get_cputype(GET_NUMSHARE) + 1);
  1941. printf("#define NUM_CORES %d\n", get_cputype(GET_NUMCORES) + 1);
  1942. features = get_coretype();
  1943. if (features > 0) printf("#define CORE_%s\n", corename[features]);
  1944. } else {
  1945. printf("#define DTB_DEFAULT_ENTRIES 16\n");
  1946. printf("#define L1_CODE_SIZE 8192\n");
  1947. printf("#define L1_DATA_SIZE 8192\n");
  1948. printf("#define L2_SIZE 0\n");
  1949. }
  1950. }
  1951. void get_architecture(void){
  1952. #ifndef __64BIT__
  1953. printf("X86");
  1954. #else
  1955. printf("X86_64");
  1956. #endif
  1957. }
  1958. void get_subarchitecture(void){
  1959. printf("%s", get_cpunamechar());
  1960. }
  1961. void get_subdirname(void){
  1962. #ifndef __64BIT__
  1963. printf("x86");
  1964. #else
  1965. printf("x86_64");
  1966. #endif
  1967. }
  1968. char *get_corename(void){
  1969. return corename[get_coretype()];
  1970. }
  1971. void get_libname(void){
  1972. printf("%s", corename_lower[get_coretype()]);
  1973. }
  1974. /* This if for Makefile */
  1975. void get_sse(void){
  1976. int features;
  1977. features = get_cputype(GET_FEATURE);
  1978. if (features & HAVE_MMX ) printf("HAVE_MMX=1\n");
  1979. if (features & HAVE_SSE ) printf("HAVE_SSE=1\n");
  1980. if (features & HAVE_SSE2 ) printf("HAVE_SSE2=1\n");
  1981. if (features & HAVE_SSE3 ) printf("HAVE_SSE3=1\n");
  1982. if (features & HAVE_SSSE3) printf("HAVE_SSSE3=1\n");
  1983. if (features & HAVE_SSE4_1) printf("HAVE_SSE4_1=1\n");
  1984. if (features & HAVE_SSE4_2) printf("HAVE_SSE4_2=1\n");
  1985. if (features & HAVE_SSE4A) printf("HAVE_SSE4A=1\n");
  1986. if (features & HAVE_SSE5 ) printf("HAVE_SSSE5=1\n");
  1987. if (features & HAVE_AVX ) printf("HAVE_AVX=1\n");
  1988. if (features & HAVE_3DNOWEX) printf("HAVE_3DNOWEX=1\n");
  1989. if (features & HAVE_3DNOW) printf("HAVE_3DNOW=1\n");
  1990. if (features & HAVE_FMA4 ) printf("HAVE_FMA4=1\n");
  1991. if (features & HAVE_FMA3 ) printf("HAVE_FMA3=1\n");
  1992. }