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

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