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

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