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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250
  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:
  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:
  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:
  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:
  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:
  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. return CPUTYPE_NEHALEM;
  1273. }
  1274. break;
  1275. case 6:
  1276. switch (model) {
  1277. case 6: // Cannon Lake
  1278. if(support_avx512())
  1279. return CPUTYPE_SKYLAKEX;
  1280. if(support_avx2())
  1281. return CPUTYPE_HASWELL;
  1282. if(support_avx())
  1283. return CPUTYPE_SANDYBRIDGE;
  1284. else
  1285. return CPUTYPE_NEHALEM;
  1286. }
  1287. break;
  1288. case 9:
  1289. case 8:
  1290. switch (model) {
  1291. case 14: // Kaby Lake
  1292. if(support_avx2())
  1293. return CPUTYPE_HASWELL;
  1294. if(support_avx())
  1295. return CPUTYPE_SANDYBRIDGE;
  1296. else
  1297. return CPUTYPE_NEHALEM;
  1298. }
  1299. break;
  1300. }
  1301. break;
  1302. case 0x7:
  1303. return CPUTYPE_ITANIUM;
  1304. case 0xf:
  1305. switch (exfamily) {
  1306. case 0 :
  1307. return CPUTYPE_PENTIUM4;
  1308. case 1 :
  1309. return CPUTYPE_ITANIUM;
  1310. }
  1311. break;
  1312. }
  1313. return CPUTYPE_INTEL_UNKNOWN;
  1314. }
  1315. if (vendor == VENDOR_AMD){
  1316. switch (family) {
  1317. case 0x4:
  1318. return CPUTYPE_AMD5X86;
  1319. case 0x5:
  1320. return CPUTYPE_AMDK6;
  1321. case 0x6:
  1322. return CPUTYPE_ATHLON;
  1323. case 0xf:
  1324. switch (exfamily) {
  1325. case 0:
  1326. case 2:
  1327. return CPUTYPE_OPTERON;
  1328. case 1:
  1329. case 3:
  1330. case 7:
  1331. case 10:
  1332. return CPUTYPE_BARCELONA;
  1333. case 5:
  1334. return CPUTYPE_BOBCAT;
  1335. case 6:
  1336. switch (model) {
  1337. case 1:
  1338. //AMD Bulldozer Opteron 6200 / Opteron 4200 / AMD FX-Series
  1339. if(support_avx())
  1340. return CPUTYPE_BULLDOZER;
  1341. else
  1342. return CPUTYPE_BARCELONA; //OS don't support AVX.
  1343. case 2: //AMD Piledriver
  1344. case 3: //AMD Richland
  1345. if(support_avx())
  1346. return CPUTYPE_PILEDRIVER;
  1347. else
  1348. return CPUTYPE_BARCELONA; //OS don't support AVX.
  1349. case 5: // New EXCAVATOR CPUS
  1350. if(support_avx())
  1351. return CPUTYPE_EXCAVATOR;
  1352. else
  1353. return CPUTYPE_BARCELONA; //OS don't support AVX.
  1354. case 0:
  1355. case 8:
  1356. switch(exmodel){
  1357. case 1: //AMD Trinity
  1358. if(support_avx())
  1359. return CPUTYPE_PILEDRIVER;
  1360. else
  1361. return CPUTYPE_BARCELONA; //OS don't support AVX.
  1362. case 3:
  1363. if(support_avx())
  1364. return CPUTYPE_STEAMROLLER;
  1365. else
  1366. return CPUTYPE_BARCELONA; //OS don't support AVX.
  1367. case 6:
  1368. if(support_avx())
  1369. return CPUTYPE_EXCAVATOR;
  1370. else
  1371. return CPUTYPE_BARCELONA; //OS don't support AVX.
  1372. }
  1373. break;
  1374. }
  1375. break;
  1376. case 8:
  1377. switch (model) {
  1378. case 1:
  1379. // AMD Ryzen
  1380. case 8:
  1381. // AMD Ryzen2
  1382. if(support_avx())
  1383. #ifndef NO_AVX2
  1384. return CPUTYPE_ZEN;
  1385. #else
  1386. return CPUTYPE_SANDYBRIDGE; // Zen is closer in architecture to Sandy Bridge than to Excavator
  1387. #endif
  1388. else
  1389. return CPUTYPE_BARCELONA;
  1390. }
  1391. }
  1392. break;
  1393. }
  1394. return CPUTYPE_AMD_UNKNOWN;
  1395. }
  1396. if (vendor == VENDOR_HYGON){
  1397. switch (family) {
  1398. case 0xf:
  1399. switch (exfamily) {
  1400. case 9:
  1401. //Hygon Dhyana
  1402. if(support_avx())
  1403. #ifndef NO_AVX2
  1404. return CPUTYPE_ZEN;
  1405. #else
  1406. return CPUTYPE_SANDYBRIDGE; // closer in architecture to Sandy Bridge than to Excavator
  1407. #endif
  1408. else
  1409. return CPUTYPE_BARCELONA;
  1410. }
  1411. break;
  1412. }
  1413. return CPUTYPE_HYGON_UNKNOWN;
  1414. }
  1415. if (vendor == VENDOR_CYRIX){
  1416. switch (family) {
  1417. case 0x4:
  1418. return CPUTYPE_CYRIX5X86;
  1419. case 0x5:
  1420. return CPUTYPE_CYRIXM1;
  1421. case 0x6:
  1422. return CPUTYPE_CYRIXM2;
  1423. }
  1424. return CPUTYPE_CYRIX_UNKNOWN;
  1425. }
  1426. if (vendor == VENDOR_NEXGEN){
  1427. switch (family) {
  1428. case 0x5:
  1429. return CPUTYPE_NEXGENNX586;
  1430. }
  1431. return CPUTYPE_NEXGEN_UNKNOWN;
  1432. }
  1433. if (vendor == VENDOR_CENTAUR){
  1434. switch (family) {
  1435. case 0x5:
  1436. return CPUTYPE_CENTAURC6;
  1437. break;
  1438. case 0x6:
  1439. return CPUTYPE_NANO;
  1440. break;
  1441. }
  1442. return CPUTYPE_VIAC3;
  1443. }
  1444. if (vendor == VENDOR_RISE){
  1445. switch (family) {
  1446. case 0x5:
  1447. return CPUTYPE_RISEMP6;
  1448. }
  1449. return CPUTYPE_RISE_UNKNOWN;
  1450. }
  1451. if (vendor == VENDOR_SIS){
  1452. switch (family) {
  1453. case 0x5:
  1454. return CPUTYPE_SYS55X;
  1455. }
  1456. return CPUTYPE_SIS_UNKNOWN;
  1457. }
  1458. if (vendor == VENDOR_TRANSMETA){
  1459. switch (family) {
  1460. case 0x5:
  1461. return CPUTYPE_CRUSOETM3X;
  1462. }
  1463. return CPUTYPE_TRANSMETA_UNKNOWN;
  1464. }
  1465. if (vendor == VENDOR_NSC){
  1466. switch (family) {
  1467. case 0x5:
  1468. return CPUTYPE_NSGEODE;
  1469. }
  1470. return CPUTYPE_NSC_UNKNOWN;
  1471. }
  1472. return CPUTYPE_UNKNOWN;
  1473. }
  1474. static char *cpuname[] = {
  1475. "UNKNOWN",
  1476. "INTEL_UNKNOWN",
  1477. "UMC_UNKNOWN",
  1478. "AMD_UNKNOWN",
  1479. "CYRIX_UNKNOWN",
  1480. "NEXGEN_UNKNOWN",
  1481. "CENTAUR_UNKNOWN",
  1482. "RISE_UNKNOWN",
  1483. "SIS_UNKNOWN",
  1484. "TRANSMETA_UNKNOWN",
  1485. "NSC_UNKNOWN",
  1486. "80386",
  1487. "80486",
  1488. "PENTIUM",
  1489. "PENTIUM2",
  1490. "PENTIUM3",
  1491. "PENTIUMM",
  1492. "PENTIUM4",
  1493. "CORE2",
  1494. "PENRYN",
  1495. "DUNNINGTON",
  1496. "NEHALEM",
  1497. "ATOM",
  1498. "ITANIUM",
  1499. "ITANIUM2",
  1500. "5X86",
  1501. "K6",
  1502. "ATHLON",
  1503. "DURON",
  1504. "OPTERON",
  1505. "BARCELONA",
  1506. "SHANGHAI",
  1507. "ISTANBUL",
  1508. "CYRIX5X86",
  1509. "CYRIXM1",
  1510. "CYRIXM2",
  1511. "NEXGENNX586",
  1512. "CENTAURC6",
  1513. "RISEMP6",
  1514. "SYS55X",
  1515. "TM3X00",
  1516. "NSGEODE",
  1517. "VIAC3",
  1518. "NANO",
  1519. "SANDYBRIDGE",
  1520. "BOBCAT",
  1521. "BULLDOZER",
  1522. "PILEDRIVER",
  1523. "HASWELL",
  1524. "STEAMROLLER",
  1525. "EXCAVATOR",
  1526. "ZEN",
  1527. "SKYLAKEX",
  1528. "DHYANA"
  1529. };
  1530. static char *lowercpuname[] = {
  1531. "unknown",
  1532. "intel_unknown",
  1533. "umc_unknown",
  1534. "amd_unknown",
  1535. "cyrix_unknown",
  1536. "nexgen_unknown",
  1537. "centaur_unknown",
  1538. "rise_unknown",
  1539. "sis_unknown",
  1540. "transmeta_unknown",
  1541. "nsc_unknown",
  1542. "80386",
  1543. "80486",
  1544. "pentium",
  1545. "pentium2",
  1546. "pentium3",
  1547. "pentiumm",
  1548. "pentium4",
  1549. "core2",
  1550. "penryn",
  1551. "dunnington",
  1552. "nehalem",
  1553. "atom",
  1554. "itanium",
  1555. "itanium2",
  1556. "5x86",
  1557. "k6",
  1558. "athlon",
  1559. "duron",
  1560. "opteron",
  1561. "barcelona",
  1562. "shanghai",
  1563. "istanbul",
  1564. "cyrix5x86",
  1565. "cyrixm1",
  1566. "cyrixm2",
  1567. "nexgennx586",
  1568. "centaurc6",
  1569. "risemp6",
  1570. "sys55x",
  1571. "tms3x00",
  1572. "nsgeode",
  1573. "nano",
  1574. "sandybridge",
  1575. "bobcat",
  1576. "bulldozer",
  1577. "piledriver",
  1578. "haswell",
  1579. "steamroller",
  1580. "excavator",
  1581. "zen",
  1582. "skylakex",
  1583. "dhyana"
  1584. };
  1585. static char *corename[] = {
  1586. "UNKNOWN",
  1587. "80486",
  1588. "P5",
  1589. "P6",
  1590. "KATMAI",
  1591. "COPPERMINE",
  1592. "NORTHWOOD",
  1593. "PRESCOTT",
  1594. "BANIAS",
  1595. "ATHLON",
  1596. "OPTERON",
  1597. "BARCELONA",
  1598. "VIAC3",
  1599. "YONAH",
  1600. "CORE2",
  1601. "PENRYN",
  1602. "DUNNINGTON",
  1603. "NEHALEM",
  1604. "ATOM",
  1605. "NANO",
  1606. "SANDYBRIDGE",
  1607. "BOBCAT",
  1608. "BULLDOZER",
  1609. "PILEDRIVER",
  1610. "HASWELL",
  1611. "STEAMROLLER",
  1612. "EXCAVATOR",
  1613. "ZEN",
  1614. "SKYLAKEX",
  1615. "DHYANA"
  1616. };
  1617. static char *corename_lower[] = {
  1618. "unknown",
  1619. "80486",
  1620. "p5",
  1621. "p6",
  1622. "katmai",
  1623. "coppermine",
  1624. "northwood",
  1625. "prescott",
  1626. "banias",
  1627. "athlon",
  1628. "opteron",
  1629. "barcelona",
  1630. "viac3",
  1631. "yonah",
  1632. "core2",
  1633. "penryn",
  1634. "dunnington",
  1635. "nehalem",
  1636. "atom",
  1637. "nano",
  1638. "sandybridge",
  1639. "bobcat",
  1640. "bulldozer",
  1641. "piledriver",
  1642. "haswell",
  1643. "steamroller",
  1644. "excavator",
  1645. "zen",
  1646. "skylakex",
  1647. "dhyana"
  1648. };
  1649. char *get_cpunamechar(void){
  1650. return cpuname[get_cpuname()];
  1651. }
  1652. char *get_lower_cpunamechar(void){
  1653. return lowercpuname[get_cpuname()];
  1654. }
  1655. int get_coretype(void){
  1656. int family, exfamily, model, exmodel, vendor;
  1657. if (!have_cpuid()) return CORE_80486;
  1658. family = get_cputype(GET_FAMILY);
  1659. exfamily = get_cputype(GET_EXFAMILY);
  1660. model = get_cputype(GET_MODEL);
  1661. exmodel = get_cputype(GET_EXMODEL);
  1662. vendor = get_vendor();
  1663. if (vendor == VENDOR_INTEL){
  1664. switch (family) {
  1665. case 4:
  1666. return CORE_80486;
  1667. case 5:
  1668. return CORE_P5;
  1669. case 6:
  1670. switch (exmodel) {
  1671. case 0:
  1672. switch (model) {
  1673. case 0:
  1674. case 1:
  1675. case 2:
  1676. case 3:
  1677. case 4:
  1678. case 5:
  1679. case 6:
  1680. return CORE_P6;
  1681. case 7:
  1682. return CORE_KATMAI;
  1683. case 8:
  1684. case 10:
  1685. case 11:
  1686. return CORE_COPPERMINE;
  1687. case 9:
  1688. case 13:
  1689. case 14:
  1690. return CORE_BANIAS;
  1691. case 15:
  1692. return CORE_CORE2;
  1693. }
  1694. break;
  1695. case 1:
  1696. switch (model) {
  1697. case 6:
  1698. return CORE_CORE2;
  1699. case 7:
  1700. return CORE_PENRYN;
  1701. case 10:
  1702. case 11:
  1703. case 14:
  1704. case 15:
  1705. return CORE_NEHALEM;
  1706. case 12:
  1707. return CORE_ATOM;
  1708. case 13:
  1709. return CORE_DUNNINGTON;
  1710. }
  1711. break;
  1712. case 2:
  1713. switch (model) {
  1714. case 5:
  1715. //Intel Core (Clarkdale) / Core (Arrandale)
  1716. // Pentium (Clarkdale) / Pentium Mobile (Arrandale)
  1717. // Xeon (Clarkdale), 32nm
  1718. return CORE_NEHALEM;
  1719. case 10:
  1720. //Intel Core i5-2000 /i7-2000 (Sandy Bridge)
  1721. if(support_avx())
  1722. return CORE_SANDYBRIDGE;
  1723. else
  1724. return CORE_NEHALEM; //OS doesn't support AVX
  1725. case 12:
  1726. //Xeon Processor 5600 (Westmere-EP)
  1727. return CORE_NEHALEM;
  1728. case 13:
  1729. //Intel Core i7-3000 / Xeon E5 (Sandy Bridge)
  1730. if(support_avx())
  1731. return CORE_SANDYBRIDGE;
  1732. else
  1733. return CORE_NEHALEM; //OS doesn't support AVX
  1734. case 14:
  1735. //Xeon E7540
  1736. case 15:
  1737. //Xeon Processor E7 (Westmere-EX)
  1738. return CORE_NEHALEM;
  1739. }
  1740. break;
  1741. case 3:
  1742. switch (model) {
  1743. case 7:
  1744. return CORE_ATOM;
  1745. case 10:
  1746. case 14:
  1747. if(support_avx())
  1748. return CORE_SANDYBRIDGE;
  1749. else
  1750. return CORE_NEHALEM; //OS doesn't support AVX
  1751. case 12:
  1752. case 15:
  1753. if(support_avx())
  1754. #ifndef NO_AVX2
  1755. return CORE_HASWELL;
  1756. #else
  1757. return CORE_SANDYBRIDGE;
  1758. #endif
  1759. else
  1760. return CORE_NEHALEM;
  1761. case 13:
  1762. //broadwell
  1763. if(support_avx())
  1764. #ifndef NO_AVX2
  1765. return CORE_HASWELL;
  1766. #else
  1767. return CORE_SANDYBRIDGE;
  1768. #endif
  1769. else
  1770. return CORE_NEHALEM;
  1771. }
  1772. break;
  1773. case 4:
  1774. switch (model) {
  1775. case 5:
  1776. case 6:
  1777. if(support_avx())
  1778. #ifndef NO_AVX2
  1779. return CORE_HASWELL;
  1780. #else
  1781. return CORE_SANDYBRIDGE;
  1782. #endif
  1783. else
  1784. return CORE_NEHALEM;
  1785. case 7:
  1786. case 15:
  1787. //broadwell
  1788. if(support_avx())
  1789. #ifndef NO_AVX2
  1790. return CORE_HASWELL;
  1791. #else
  1792. return CORE_SANDYBRIDGE;
  1793. #endif
  1794. else
  1795. return CORE_NEHALEM;
  1796. case 14:
  1797. //Skylake
  1798. if(support_avx())
  1799. #ifndef NO_AVX2
  1800. return CORE_HASWELL;
  1801. #else
  1802. return CORE_SANDYBRIDGE;
  1803. #endif
  1804. else
  1805. return CORE_NEHALEM;
  1806. case 12:
  1807. // Braswell
  1808. case 13:
  1809. // Avoton
  1810. return CORE_NEHALEM;
  1811. }
  1812. break;
  1813. case 5:
  1814. switch (model) {
  1815. case 6:
  1816. //broadwell
  1817. if(support_avx())
  1818. #ifndef NO_AVX2
  1819. return CORE_HASWELL;
  1820. #else
  1821. return CORE_SANDYBRIDGE;
  1822. #endif
  1823. else
  1824. return CORE_NEHALEM;
  1825. case 5:
  1826. // Skylake X
  1827. #ifndef NO_AVX512
  1828. return CORE_SKYLAKEX;
  1829. #else
  1830. if(support_avx())
  1831. #ifndef NO_AVX2
  1832. return CORE_HASWELL;
  1833. #else
  1834. return CORE_SANDYBRIDGE;
  1835. #endif
  1836. else
  1837. return CORE_NEHALEM;
  1838. #endif
  1839. case 14:
  1840. // Skylake
  1841. if(support_avx())
  1842. #ifndef NO_AVX2
  1843. return CORE_HASWELL;
  1844. #else
  1845. return CORE_SANDYBRIDGE;
  1846. #endif
  1847. else
  1848. return CORE_NEHALEM;
  1849. case 7:
  1850. // Phi Knights Landing
  1851. if(support_avx())
  1852. #ifndef NO_AVX2
  1853. return CORE_HASWELL;
  1854. #else
  1855. return CORE_SANDYBRIDGE;
  1856. #endif
  1857. else
  1858. return CORE_NEHALEM;
  1859. case 12:
  1860. // Apollo Lake
  1861. return CORE_NEHALEM;
  1862. }
  1863. break;
  1864. case 9:
  1865. case 8:
  1866. if (model == 14) { // Kaby Lake
  1867. if(support_avx())
  1868. #ifndef NO_AVX2
  1869. return CORE_HASWELL;
  1870. #else
  1871. return CORE_SANDYBRIDGE;
  1872. #endif
  1873. else
  1874. return CORE_NEHALEM;
  1875. }
  1876. }
  1877. break;
  1878. case 15:
  1879. if (model <= 0x2) return CORE_NORTHWOOD;
  1880. else return CORE_PRESCOTT;
  1881. }
  1882. }
  1883. if (vendor == VENDOR_AMD){
  1884. if (family <= 0x5) return CORE_80486;
  1885. if (family <= 0xe) return CORE_ATHLON;
  1886. if (family == 0xf){
  1887. if ((exfamily == 0) || (exfamily == 2)) return CORE_OPTERON;
  1888. else if (exfamily == 5) return CORE_BOBCAT;
  1889. else if (exfamily == 6) {
  1890. switch (model) {
  1891. case 1:
  1892. //AMD Bulldozer Opteron 6200 / Opteron 4200 / AMD FX-Series
  1893. if(support_avx())
  1894. return CORE_BULLDOZER;
  1895. else
  1896. return CORE_BARCELONA; //OS don't support AVX.
  1897. case 2: //AMD Piledriver
  1898. case 3: //AMD Richland
  1899. if(support_avx())
  1900. return CORE_PILEDRIVER;
  1901. else
  1902. return CORE_BARCELONA; //OS don't support AVX.
  1903. case 5: // New EXCAVATOR
  1904. if(support_avx())
  1905. return CORE_EXCAVATOR;
  1906. else
  1907. return CORE_BARCELONA; //OS don't support AVX.
  1908. case 0:
  1909. case 8:
  1910. switch(exmodel){
  1911. case 1: //AMD Trinity
  1912. if(support_avx())
  1913. return CORE_PILEDRIVER;
  1914. else
  1915. return CORE_BARCELONA; //OS don't support AVX.
  1916. case 3:
  1917. if(support_avx())
  1918. return CORE_STEAMROLLER;
  1919. else
  1920. return CORE_BARCELONA; //OS don't support AVX.
  1921. case 6:
  1922. if(support_avx())
  1923. return CORE_EXCAVATOR;
  1924. else
  1925. return CORE_BARCELONA; //OS don't support AVX.
  1926. }
  1927. break;
  1928. }
  1929. } else if (exfamily == 8) {
  1930. switch (model) {
  1931. case 1:
  1932. // AMD Ryzen
  1933. case 8:
  1934. // Ryzen 2
  1935. if(support_avx())
  1936. #ifndef NO_AVX2
  1937. return CORE_ZEN;
  1938. #else
  1939. return CORE_SANDYBRIDGE; // Zen is closer in architecture to Sandy Bridge than to Excavator
  1940. #endif
  1941. else
  1942. return CORE_BARCELONA;
  1943. }
  1944. } else {
  1945. return CORE_BARCELONA;
  1946. }
  1947. }
  1948. }
  1949. if (vendor == VENDOR_HYGON){
  1950. if (family == 0xf){
  1951. if (exfamily == 9) {
  1952. if(support_avx())
  1953. #ifndef NO_AVX2
  1954. return CORE_ZEN;
  1955. #else
  1956. return CORE_SANDYBRIDGE; // closer in architecture to Sandy Bridge than to Excavator
  1957. #endif
  1958. else
  1959. return CORE_BARCELONA;
  1960. } else {
  1961. return CORE_BARCELONA;
  1962. }
  1963. }
  1964. }
  1965. if (vendor == VENDOR_CENTAUR) {
  1966. switch (family) {
  1967. case 0x6:
  1968. return CORE_NANO;
  1969. break;
  1970. }
  1971. return CORE_VIAC3;
  1972. }
  1973. return CORE_UNKNOWN;
  1974. }
  1975. void get_cpuconfig(void){
  1976. cache_info_t info;
  1977. int features;
  1978. printf("#define %s\n", cpuname[get_cpuname()]);
  1979. if (get_coretype() != CORE_P5) {
  1980. get_cacheinfo(CACHE_INFO_L1_I, &info);
  1981. if (info.size > 0) {
  1982. printf("#define L1_CODE_SIZE %d\n", info.size * 1024);
  1983. printf("#define L1_CODE_ASSOCIATIVE %d\n", info.associative);
  1984. printf("#define L1_CODE_LINESIZE %d\n", info.linesize);
  1985. }
  1986. get_cacheinfo(CACHE_INFO_L1_D, &info);
  1987. if (info.size > 0) {
  1988. printf("#define L1_DATA_SIZE %d\n", info.size * 1024);
  1989. printf("#define L1_DATA_ASSOCIATIVE %d\n", info.associative);
  1990. printf("#define L1_DATA_LINESIZE %d\n", info.linesize);
  1991. }
  1992. get_cacheinfo(CACHE_INFO_L2, &info);
  1993. if (info.size > 0) {
  1994. printf("#define L2_SIZE %d\n", info.size * 1024);
  1995. printf("#define L2_ASSOCIATIVE %d\n", info.associative);
  1996. printf("#define L2_LINESIZE %d\n", info.linesize);
  1997. } else {
  1998. //fall back for some virtual machines.
  1999. printf("#define L2_SIZE 1048576\n");
  2000. printf("#define L2_ASSOCIATIVE 6\n");
  2001. printf("#define L2_LINESIZE 64\n");
  2002. }
  2003. get_cacheinfo(CACHE_INFO_L3, &info);
  2004. if (info.size > 0) {
  2005. printf("#define L3_SIZE %d\n", info.size * 1024);
  2006. printf("#define L3_ASSOCIATIVE %d\n", info.associative);
  2007. printf("#define L3_LINESIZE %d\n", info.linesize);
  2008. }
  2009. get_cacheinfo(CACHE_INFO_L1_ITB, &info);
  2010. if (info.size > 0) {
  2011. printf("#define ITB_SIZE %d\n", info.size * 1024);
  2012. printf("#define ITB_ASSOCIATIVE %d\n", info.associative);
  2013. printf("#define ITB_ENTRIES %d\n", info.linesize);
  2014. }
  2015. get_cacheinfo(CACHE_INFO_L1_DTB, &info);
  2016. if (info.size > 0) {
  2017. printf("#define DTB_SIZE %d\n", info.size * 1024);
  2018. printf("#define DTB_ASSOCIATIVE %d\n", info.associative);
  2019. printf("#define DTB_DEFAULT_ENTRIES %d\n", info.linesize);
  2020. } else {
  2021. //fall back for some virtual machines.
  2022. printf("#define DTB_DEFAULT_ENTRIES 32\n");
  2023. }
  2024. features = get_cputype(GET_FEATURE);
  2025. if (features & HAVE_CMOV ) printf("#define HAVE_CMOV\n");
  2026. if (features & HAVE_MMX ) printf("#define HAVE_MMX\n");
  2027. if (features & HAVE_SSE ) printf("#define HAVE_SSE\n");
  2028. if (features & HAVE_SSE2 ) printf("#define HAVE_SSE2\n");
  2029. if (features & HAVE_SSE3 ) printf("#define HAVE_SSE3\n");
  2030. if (features & HAVE_SSSE3) printf("#define HAVE_SSSE3\n");
  2031. if (features & HAVE_SSE4_1) printf("#define HAVE_SSE4_1\n");
  2032. if (features & HAVE_SSE4_2) printf("#define HAVE_SSE4_2\n");
  2033. if (features & HAVE_SSE4A) printf("#define HAVE_SSE4A\n");
  2034. if (features & HAVE_SSE5 ) printf("#define HAVE_SSSE5\n");
  2035. if (features & HAVE_AVX ) printf("#define HAVE_AVX\n");
  2036. if (features & HAVE_AVX2 ) printf("#define HAVE_AVX2\n");
  2037. if (features & HAVE_AVX512VL ) printf("#define HAVE_AVX512VL\n");
  2038. if (features & HAVE_3DNOWEX) printf("#define HAVE_3DNOWEX\n");
  2039. if (features & HAVE_3DNOW) printf("#define HAVE_3DNOW\n");
  2040. if (features & HAVE_FMA4 ) printf("#define HAVE_FMA4\n");
  2041. if (features & HAVE_FMA3 ) printf("#define HAVE_FMA3\n");
  2042. if (features & HAVE_CFLUSH) printf("#define HAVE_CFLUSH\n");
  2043. if (features & HAVE_HIT) printf("#define HAVE_HIT 1\n");
  2044. if (features & HAVE_MISALIGNSSE) printf("#define HAVE_MISALIGNSSE\n");
  2045. if (features & HAVE_128BITFPU) printf("#define HAVE_128BITFPU\n");
  2046. if (features & HAVE_FASTMOVU) printf("#define HAVE_FASTMOVU\n");
  2047. printf("#define NUM_SHAREDCACHE %d\n", get_cputype(GET_NUMSHARE) + 1);
  2048. printf("#define NUM_CORES %d\n", get_cputype(GET_NUMCORES) + 1);
  2049. features = get_coretype();
  2050. if (features > 0) printf("#define CORE_%s\n", corename[features]);
  2051. } else {
  2052. printf("#define DTB_DEFAULT_ENTRIES 16\n");
  2053. printf("#define L1_CODE_SIZE 8192\n");
  2054. printf("#define L1_DATA_SIZE 8192\n");
  2055. printf("#define L2_SIZE 0\n");
  2056. }
  2057. }
  2058. void get_architecture(void){
  2059. #ifndef __64BIT__
  2060. printf("X86");
  2061. #else
  2062. printf("X86_64");
  2063. #endif
  2064. }
  2065. void get_subarchitecture(void){
  2066. printf("%s", get_cpunamechar());
  2067. }
  2068. void get_subdirname(void){
  2069. #ifndef __64BIT__
  2070. printf("x86");
  2071. #else
  2072. printf("x86_64");
  2073. #endif
  2074. }
  2075. char *get_corename(void){
  2076. return corename[get_coretype()];
  2077. }
  2078. void get_libname(void){
  2079. printf("%s", corename_lower[get_coretype()]);
  2080. }
  2081. /* This if for Makefile */
  2082. void get_sse(void){
  2083. int features;
  2084. features = get_cputype(GET_FEATURE);
  2085. if (features & HAVE_MMX ) printf("HAVE_MMX=1\n");
  2086. if (features & HAVE_SSE ) printf("HAVE_SSE=1\n");
  2087. if (features & HAVE_SSE2 ) printf("HAVE_SSE2=1\n");
  2088. if (features & HAVE_SSE3 ) printf("HAVE_SSE3=1\n");
  2089. if (features & HAVE_SSSE3) printf("HAVE_SSSE3=1\n");
  2090. if (features & HAVE_SSE4_1) printf("HAVE_SSE4_1=1\n");
  2091. if (features & HAVE_SSE4_2) printf("HAVE_SSE4_2=1\n");
  2092. if (features & HAVE_SSE4A) printf("HAVE_SSE4A=1\n");
  2093. if (features & HAVE_SSE5 ) printf("HAVE_SSSE5=1\n");
  2094. if (features & HAVE_AVX ) printf("HAVE_AVX=1\n");
  2095. if (features & HAVE_AVX2 ) printf("HAVE_AVX2=1\n");
  2096. if (features & HAVE_AVX512VL ) printf("HAVE_AVX512VL=1\n");
  2097. if (features & HAVE_3DNOWEX) printf("HAVE_3DNOWEX=1\n");
  2098. if (features & HAVE_3DNOW) printf("HAVE_3DNOW=1\n");
  2099. if (features & HAVE_FMA4 ) printf("HAVE_FMA4=1\n");
  2100. if (features & HAVE_FMA3 ) printf("HAVE_FMA3=1\n");
  2101. }