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

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