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 52 kB

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