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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381
  1. /*********************************************************************/
  2. /* Copyright 2009, 2010 The University of Texas at Austin. */
  3. /* All rights reserved. */
  4. /* */
  5. /* Redistribution and use in source and binary forms, with or */
  6. /* without modification, are permitted provided that the following */
  7. /* conditions are met: */
  8. /* */
  9. /* 1. Redistributions of source code must retain the above */
  10. /* copyright notice, this list of conditions and the following */
  11. /* disclaimer. */
  12. /* */
  13. /* 2. Redistributions in binary form must reproduce the above */
  14. /* copyright notice, this list of conditions and the following */
  15. /* disclaimer in the documentation and/or other materials */
  16. /* provided with the distribution. */
  17. /* */
  18. /* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY OF TEXAS AT */
  19. /* AUSTIN ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, */
  20. /* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */
  21. /* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
  22. /* DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OF TEXAS AT */
  23. /* AUSTIN OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, */
  24. /* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
  25. /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE */
  26. /* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR */
  27. /* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF */
  28. /* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
  29. /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT */
  30. /* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE */
  31. /* POSSIBILITY OF SUCH DAMAGE. */
  32. /* */
  33. /* The views and conclusions contained in the software and */
  34. /* documentation are those of the authors and should not be */
  35. /* interpreted as representing official policies, either expressed */
  36. /* or implied, of The University of Texas at Austin. */
  37. /*********************************************************************/
  38. #include <stdio.h>
  39. #include <string.h>
  40. #include "cpuid.h"
  41. #if defined(_MSC_VER) && !defined(__clang__)
  42. #define C_INLINE __inline
  43. #else
  44. #define C_INLINE inline
  45. #endif
  46. /*
  47. #ifdef NO_AVX
  48. #define CPUTYPE_HASWELL CPUTYPE_NEHALEM
  49. #define CORE_HASWELL CORE_NEHALEM
  50. #define CPUTYPE_SKYLAKEX CPUTYPE_NEHALEM
  51. #define CORE_SKYLAKEX CORE_NEHALEM
  52. #define CPUTYPE_SANDYBRIDGE CPUTYPE_NEHALEM
  53. #define CORE_SANDYBRIDGE CORE_NEHALEM
  54. #define CPUTYPE_BULLDOZER CPUTYPE_BARCELONA
  55. #define CORE_BULLDOZER CORE_BARCELONA
  56. #define CPUTYPE_PILEDRIVER CPUTYPE_BARCELONA
  57. #define CORE_PILEDRIVER CORE_BARCELONA
  58. #endif
  59. */
  60. #if defined(_MSC_VER) && !defined(__clang__)
  61. void cpuid(int op, int *eax, int *ebx, int *ecx, int *edx)
  62. {
  63. int cpuInfo[4] = {-1};
  64. __cpuid(cpuInfo, op);
  65. *eax = cpuInfo[0];
  66. *ebx = cpuInfo[1];
  67. *ecx = cpuInfo[2];
  68. *edx = cpuInfo[3];
  69. }
  70. void cpuid_count(int op, int count, int *eax, int *ebx, int *ecx, int *edx)
  71. {
  72. int cpuInfo[4] = {-1};
  73. __cpuidex(cpuInfo, op, count);
  74. *eax = cpuInfo[0];
  75. *ebx = cpuInfo[1];
  76. *ecx = cpuInfo[2];
  77. *edx = cpuInfo[3];
  78. }
  79. #else
  80. #ifndef CPUIDEMU
  81. #if defined(__APPLE__) && defined(__i386__)
  82. void cpuid(int op, int *eax, int *ebx, int *ecx, int *edx);
  83. void cpuid_count(int op, int count, int *eax, int *ebx, int *ecx, int *edx);
  84. #else
  85. static C_INLINE void cpuid(int op, int *eax, int *ebx, int *ecx, int *edx){
  86. #if defined(__i386__) && defined(__PIC__)
  87. __asm__ __volatile__
  88. ("mov %%ebx, %%edi;"
  89. "cpuid;"
  90. "xchgl %%ebx, %%edi;"
  91. : "=a" (*eax), "=D" (*ebx), "=c" (*ecx), "=d" (*edx) : "a" (op), "c" (0) : "cc");
  92. #else
  93. __asm__ __volatile__
  94. ("cpuid": "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx) : "a" (op) , "c" (0) : "cc");
  95. #endif
  96. }
  97. static C_INLINE void cpuid_count(int op, int count ,int *eax, int *ebx, int *ecx, int *edx){
  98. #if defined(__i386__) && defined(__PIC__)
  99. __asm__ __volatile__
  100. ("mov %%ebx, %%edi;"
  101. "cpuid;"
  102. "xchgl %%ebx, %%edi;"
  103. : "=a" (*eax), "=D" (*ebx), "=c" (*ecx), "=d" (*edx) : "0" (op), "2" (count) : "cc");
  104. #else
  105. __asm__ __volatile__
  106. ("cpuid": "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx) : "0" (op), "2" (count) : "cc");
  107. #endif
  108. }
  109. #endif
  110. #else
  111. typedef struct {
  112. unsigned int id, a, b, c, d;
  113. } idlist_t;
  114. typedef struct {
  115. char *vendor;
  116. char *name;
  117. int start, stop;
  118. } vendor_t;
  119. extern idlist_t idlist[];
  120. extern vendor_t vendor[];
  121. static int cv = VENDOR;
  122. void cpuid(unsigned int op, unsigned int *eax, unsigned int *ebx, unsigned int *ecx, unsigned int *edx){
  123. static int current = 0;
  124. int start = vendor[cv].start;
  125. int stop = vendor[cv].stop;
  126. int count = stop - start;
  127. if ((current < start) || (current > stop)) current = start;
  128. while ((count > 0) && (idlist[current].id != op)) {
  129. current ++;
  130. if (current > stop) current = start;
  131. count --;
  132. }
  133. *eax = idlist[current].a;
  134. *ebx = idlist[current].b;
  135. *ecx = idlist[current].c;
  136. *edx = idlist[current].d;
  137. }
  138. void cpuid_count (unsigned int op, unsigned int count, unsigned int *eax, unsigned int *ebx, unsigned int *ecx, unsigned int *edx) {
  139. return cpuid (op, eax, ebx, ecx, edx);
  140. }
  141. #endif
  142. #endif // _MSC_VER
  143. static C_INLINE int have_cpuid(void){
  144. int eax, ebx, ecx, edx;
  145. cpuid(0, &eax, &ebx, &ecx, &edx);
  146. return eax;
  147. }
  148. static C_INLINE int have_excpuid(void){
  149. int eax, ebx, ecx, edx;
  150. cpuid(0x80000000, &eax, &ebx, &ecx, &edx);
  151. return eax & 0xffff;
  152. }
  153. #ifndef NO_AVX
  154. static C_INLINE void xgetbv(int op, int * eax, int * edx){
  155. //Use binary code for xgetbv
  156. #if defined(_MSC_VER) && !defined(__clang__)
  157. *eax = __xgetbv(op);
  158. #else
  159. __asm__ __volatile__
  160. (".byte 0x0f, 0x01, 0xd0": "=a" (*eax), "=d" (*edx) : "c" (op) : "cc");
  161. #endif
  162. }
  163. #endif
  164. int support_avx(){
  165. #ifndef NO_AVX
  166. int eax, ebx, ecx, edx;
  167. int ret=0;
  168. cpuid(1, &eax, &ebx, &ecx, &edx);
  169. if ((ecx & (1 << 28)) != 0 && (ecx & (1 << 27)) != 0 && (ecx & (1 << 26)) != 0){
  170. xgetbv(0, &eax, &edx);
  171. if((eax & 6) == 6){
  172. ret=1; //OS support AVX
  173. }
  174. }
  175. return ret;
  176. #else
  177. return 0;
  178. #endif
  179. }
  180. int support_avx2(){
  181. #ifndef NO_AVX2
  182. int eax, ebx, ecx=0, edx;
  183. int ret=0;
  184. if (!support_avx())
  185. return 0;
  186. cpuid(7, &eax, &ebx, &ecx, &edx);
  187. if((ebx & (1<<7)) != 0)
  188. ret=1; //OS supports AVX2
  189. return ret;
  190. #else
  191. return 0;
  192. #endif
  193. }
  194. int support_avx512(){
  195. #if !defined(NO_AVX) && !defined(NO_AVX512)
  196. int eax, ebx, ecx, edx;
  197. int ret=0;
  198. if (!support_avx())
  199. return 0;
  200. cpuid(7, &eax, &ebx, &ecx, &edx);
  201. if((ebx & 32) != 32){
  202. ret=0; //OS does not even support AVX2
  203. }
  204. if((ebx & (1<<31)) != 0){
  205. xgetbv(0, &eax, &edx);
  206. if((eax & 0xe0) == 0xe0)
  207. ret=1; //OS supports AVX512VL
  208. }
  209. return ret;
  210. #else
  211. return 0;
  212. #endif
  213. }
  214. int 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. }
  1349. break;
  1350. }
  1351. break;
  1352. case 0x7:
  1353. return CPUTYPE_ITANIUM;
  1354. case 0xf:
  1355. switch (exfamily) {
  1356. case 0 :
  1357. return CPUTYPE_PENTIUM4;
  1358. case 1 :
  1359. return CPUTYPE_ITANIUM;
  1360. }
  1361. break;
  1362. }
  1363. return CPUTYPE_INTEL_UNKNOWN;
  1364. }
  1365. if (vendor == VENDOR_AMD){
  1366. switch (family) {
  1367. case 0x4:
  1368. return CPUTYPE_AMD5X86;
  1369. case 0x5:
  1370. return CPUTYPE_AMDK6;
  1371. case 0x6:
  1372. #if defined(__x86_64__) || defined(__amd64__)
  1373. return CPUTYPE_BARCELONA;
  1374. #else
  1375. return CPUTYPE_ATHLON;
  1376. #endif
  1377. case 0xf:
  1378. switch (exfamily) {
  1379. case 0:
  1380. case 2:
  1381. return CPUTYPE_OPTERON;
  1382. case 1:
  1383. case 3:
  1384. // case 7:
  1385. // case 10:
  1386. return CPUTYPE_BARCELONA;
  1387. case 5:
  1388. case 7:
  1389. return CPUTYPE_BOBCAT;
  1390. case 6:
  1391. switch (model) {
  1392. case 1:
  1393. //AMD Bulldozer Opteron 6200 / Opteron 4200 / AMD FX-Series
  1394. if(support_avx())
  1395. return CPUTYPE_BULLDOZER;
  1396. else
  1397. return CPUTYPE_BARCELONA; //OS don't support AVX.
  1398. case 2: //AMD Piledriver
  1399. case 3: //AMD Richland
  1400. if(support_avx())
  1401. return CPUTYPE_PILEDRIVER;
  1402. else
  1403. return CPUTYPE_BARCELONA; //OS don't support AVX.
  1404. case 5: // New EXCAVATOR CPUS
  1405. if(support_avx())
  1406. return CPUTYPE_EXCAVATOR;
  1407. else
  1408. return CPUTYPE_BARCELONA; //OS don't support AVX.
  1409. case 0:
  1410. case 8:
  1411. switch(exmodel){
  1412. case 1: //AMD Trinity
  1413. if(support_avx())
  1414. return CPUTYPE_PILEDRIVER;
  1415. else
  1416. return CPUTYPE_BARCELONA; //OS don't support AVX.
  1417. case 3:
  1418. if(support_avx())
  1419. return CPUTYPE_STEAMROLLER;
  1420. else
  1421. return CPUTYPE_BARCELONA; //OS don't support AVX.
  1422. case 6:
  1423. if(support_avx())
  1424. return CPUTYPE_EXCAVATOR;
  1425. else
  1426. return CPUTYPE_BARCELONA; //OS don't support AVX.
  1427. }
  1428. break;
  1429. }
  1430. break;
  1431. case 8:
  1432. switch (model) {
  1433. case 1:
  1434. // AMD Ryzen
  1435. case 8:
  1436. // AMD Ryzen2
  1437. default:
  1438. // Matisse/Renoir and other recent Ryzen2
  1439. if(support_avx())
  1440. #ifndef NO_AVX2
  1441. return CPUTYPE_ZEN;
  1442. #else
  1443. return CPUTYPE_SANDYBRIDGE; // Zen is closer in architecture to Sandy Bridge than to Excavator
  1444. #endif
  1445. else
  1446. return CPUTYPE_BARCELONA;
  1447. }
  1448. break;
  1449. case 10: // Zen3
  1450. if(support_avx())
  1451. #ifndef NO_AVX2
  1452. return CPUTYPE_ZEN;
  1453. #else
  1454. return CPUTYPE_SANDYBRIDGE; // Zen is closer in architecture to Sandy Bridge than to Excavator
  1455. #endif
  1456. else
  1457. return CPUTYPE_BARCELONA;
  1458. }
  1459. break;
  1460. }
  1461. return CPUTYPE_AMD_UNKNOWN;
  1462. }
  1463. if (vendor == VENDOR_HYGON){
  1464. switch (family) {
  1465. case 0xf:
  1466. switch (exfamily) {
  1467. case 9:
  1468. //Hygon Dhyana
  1469. if(support_avx())
  1470. #ifndef NO_AVX2
  1471. return CPUTYPE_ZEN;
  1472. #else
  1473. return CPUTYPE_SANDYBRIDGE; // closer in architecture to Sandy Bridge than to Excavator
  1474. #endif
  1475. else
  1476. return CPUTYPE_BARCELONA;
  1477. }
  1478. break;
  1479. }
  1480. return CPUTYPE_HYGON_UNKNOWN;
  1481. }
  1482. if (vendor == VENDOR_CYRIX){
  1483. switch (family) {
  1484. case 0x4:
  1485. return CPUTYPE_CYRIX5X86;
  1486. case 0x5:
  1487. return CPUTYPE_CYRIXM1;
  1488. case 0x6:
  1489. return CPUTYPE_CYRIXM2;
  1490. }
  1491. return CPUTYPE_CYRIX_UNKNOWN;
  1492. }
  1493. if (vendor == VENDOR_NEXGEN){
  1494. switch (family) {
  1495. case 0x5:
  1496. return CPUTYPE_NEXGENNX586;
  1497. }
  1498. return CPUTYPE_NEXGEN_UNKNOWN;
  1499. }
  1500. if (vendor == VENDOR_CENTAUR){
  1501. switch (family) {
  1502. case 0x5:
  1503. return CPUTYPE_CENTAURC6;
  1504. break;
  1505. case 0x6:
  1506. return CPUTYPE_NANO;
  1507. break;
  1508. }
  1509. return CPUTYPE_VIAC3;
  1510. }
  1511. if (vendor == VENDOR_RISE){
  1512. switch (family) {
  1513. case 0x5:
  1514. return CPUTYPE_RISEMP6;
  1515. }
  1516. return CPUTYPE_RISE_UNKNOWN;
  1517. }
  1518. if (vendor == VENDOR_SIS){
  1519. switch (family) {
  1520. case 0x5:
  1521. return CPUTYPE_SYS55X;
  1522. }
  1523. return CPUTYPE_SIS_UNKNOWN;
  1524. }
  1525. if (vendor == VENDOR_TRANSMETA){
  1526. switch (family) {
  1527. case 0x5:
  1528. return CPUTYPE_CRUSOETM3X;
  1529. }
  1530. return CPUTYPE_TRANSMETA_UNKNOWN;
  1531. }
  1532. if (vendor == VENDOR_NSC){
  1533. switch (family) {
  1534. case 0x5:
  1535. return CPUTYPE_NSGEODE;
  1536. }
  1537. return CPUTYPE_NSC_UNKNOWN;
  1538. }
  1539. return CPUTYPE_UNKNOWN;
  1540. }
  1541. static char *cpuname[] = {
  1542. "UNKNOWN",
  1543. "INTEL_UNKNOWN",
  1544. "UMC_UNKNOWN",
  1545. "AMD_UNKNOWN",
  1546. "CYRIX_UNKNOWN",
  1547. "NEXGEN_UNKNOWN",
  1548. "CENTAUR_UNKNOWN",
  1549. "RISE_UNKNOWN",
  1550. "SIS_UNKNOWN",
  1551. "TRANSMETA_UNKNOWN",
  1552. "NSC_UNKNOWN",
  1553. "80386",
  1554. "80486",
  1555. "PENTIUM",
  1556. "PENTIUM2",
  1557. "PENTIUM3",
  1558. "PENTIUMM",
  1559. "PENTIUM4",
  1560. "CORE2",
  1561. "PENRYN",
  1562. "DUNNINGTON",
  1563. "NEHALEM",
  1564. "ATOM",
  1565. "ITANIUM",
  1566. "ITANIUM2",
  1567. "5X86",
  1568. "K6",
  1569. "ATHLON",
  1570. "DURON",
  1571. "OPTERON",
  1572. "BARCELONA",
  1573. "SHANGHAI",
  1574. "ISTANBUL",
  1575. "CYRIX5X86",
  1576. "CYRIXM1",
  1577. "CYRIXM2",
  1578. "NEXGENNX586",
  1579. "CENTAURC6",
  1580. "RISEMP6",
  1581. "SYS55X",
  1582. "TM3X00",
  1583. "NSGEODE",
  1584. "VIAC3",
  1585. "NANO",
  1586. "SANDYBRIDGE",
  1587. "BOBCAT",
  1588. "BULLDOZER",
  1589. "PILEDRIVER",
  1590. "HASWELL",
  1591. "STEAMROLLER",
  1592. "EXCAVATOR",
  1593. "ZEN",
  1594. "SKYLAKEX",
  1595. "DHYANA",
  1596. "COOPERLAKE"
  1597. };
  1598. static char *lowercpuname[] = {
  1599. "unknown",
  1600. "intel_unknown",
  1601. "umc_unknown",
  1602. "amd_unknown",
  1603. "cyrix_unknown",
  1604. "nexgen_unknown",
  1605. "centaur_unknown",
  1606. "rise_unknown",
  1607. "sis_unknown",
  1608. "transmeta_unknown",
  1609. "nsc_unknown",
  1610. "80386",
  1611. "80486",
  1612. "pentium",
  1613. "pentium2",
  1614. "pentium3",
  1615. "pentiumm",
  1616. "pentium4",
  1617. "core2",
  1618. "penryn",
  1619. "dunnington",
  1620. "nehalem",
  1621. "atom",
  1622. "itanium",
  1623. "itanium2",
  1624. "5x86",
  1625. "k6",
  1626. "athlon",
  1627. "duron",
  1628. "opteron",
  1629. "barcelona",
  1630. "shanghai",
  1631. "istanbul",
  1632. "cyrix5x86",
  1633. "cyrixm1",
  1634. "cyrixm2",
  1635. "nexgennx586",
  1636. "centaurc6",
  1637. "risemp6",
  1638. "sys55x",
  1639. "tms3x00",
  1640. "nsgeode",
  1641. "nano",
  1642. "sandybridge",
  1643. "bobcat",
  1644. "bulldozer",
  1645. "piledriver",
  1646. "haswell",
  1647. "steamroller",
  1648. "excavator",
  1649. "zen",
  1650. "skylakex",
  1651. "dhyana",
  1652. "cooperlake"
  1653. };
  1654. static char *corename[] = {
  1655. "UNKNOWN",
  1656. "80486",
  1657. "P5",
  1658. "P6",
  1659. "KATMAI",
  1660. "COPPERMINE",
  1661. "NORTHWOOD",
  1662. "PRESCOTT",
  1663. "BANIAS",
  1664. "ATHLON",
  1665. "OPTERON",
  1666. "BARCELONA",
  1667. "VIAC3",
  1668. "YONAH",
  1669. "CORE2",
  1670. "PENRYN",
  1671. "DUNNINGTON",
  1672. "NEHALEM",
  1673. "ATOM",
  1674. "NANO",
  1675. "SANDYBRIDGE",
  1676. "BOBCAT",
  1677. "BULLDOZER",
  1678. "PILEDRIVER",
  1679. "HASWELL",
  1680. "STEAMROLLER",
  1681. "EXCAVATOR",
  1682. "ZEN",
  1683. "SKYLAKEX",
  1684. "DHYANA",
  1685. "COOPERLAKE"
  1686. };
  1687. static char *corename_lower[] = {
  1688. "unknown",
  1689. "80486",
  1690. "p5",
  1691. "p6",
  1692. "katmai",
  1693. "coppermine",
  1694. "northwood",
  1695. "prescott",
  1696. "banias",
  1697. "athlon",
  1698. "opteron",
  1699. "barcelona",
  1700. "viac3",
  1701. "yonah",
  1702. "core2",
  1703. "penryn",
  1704. "dunnington",
  1705. "nehalem",
  1706. "atom",
  1707. "nano",
  1708. "sandybridge",
  1709. "bobcat",
  1710. "bulldozer",
  1711. "piledriver",
  1712. "haswell",
  1713. "steamroller",
  1714. "excavator",
  1715. "zen",
  1716. "skylakex",
  1717. "dhyana",
  1718. "cooperlake"
  1719. };
  1720. char *get_cpunamechar(void){
  1721. return cpuname[get_cpuname()];
  1722. }
  1723. char *get_lower_cpunamechar(void){
  1724. return lowercpuname[get_cpuname()];
  1725. }
  1726. int get_coretype(void){
  1727. int family, exfamily, model, exmodel, vendor;
  1728. if (!have_cpuid()) return CORE_80486;
  1729. family = get_cputype(GET_FAMILY);
  1730. exfamily = get_cputype(GET_EXFAMILY);
  1731. model = get_cputype(GET_MODEL);
  1732. exmodel = get_cputype(GET_EXMODEL);
  1733. vendor = get_vendor();
  1734. if (vendor == VENDOR_INTEL){
  1735. switch (family) {
  1736. case 4:
  1737. return CORE_80486;
  1738. case 5:
  1739. return CORE_P5;
  1740. case 6:
  1741. switch (exmodel) {
  1742. case 0:
  1743. switch (model) {
  1744. case 0:
  1745. case 1:
  1746. case 2:
  1747. case 3:
  1748. case 4:
  1749. case 5:
  1750. case 6:
  1751. #if defined(__x86_64__) || defined(__amd64__)
  1752. return CORE_CORE2;
  1753. #else
  1754. return CORE_P6;
  1755. #endif
  1756. case 7:
  1757. return CORE_KATMAI;
  1758. case 8:
  1759. case 10:
  1760. case 11:
  1761. return CORE_COPPERMINE;
  1762. case 9:
  1763. case 13:
  1764. case 14:
  1765. return CORE_BANIAS;
  1766. case 15:
  1767. return CORE_CORE2;
  1768. }
  1769. break;
  1770. case 1:
  1771. switch (model) {
  1772. case 6:
  1773. return CORE_CORE2;
  1774. case 7:
  1775. return CORE_PENRYN;
  1776. case 10:
  1777. case 11:
  1778. case 14:
  1779. case 15:
  1780. return CORE_NEHALEM;
  1781. case 12:
  1782. return CORE_ATOM;
  1783. case 13:
  1784. return CORE_DUNNINGTON;
  1785. }
  1786. break;
  1787. case 2:
  1788. switch (model) {
  1789. case 5:
  1790. //Intel Core (Clarkdale) / Core (Arrandale)
  1791. // Pentium (Clarkdale) / Pentium Mobile (Arrandale)
  1792. // Xeon (Clarkdale), 32nm
  1793. return CORE_NEHALEM;
  1794. case 10:
  1795. //Intel Core i5-2000 /i7-2000 (Sandy Bridge)
  1796. if(support_avx())
  1797. return CORE_SANDYBRIDGE;
  1798. else
  1799. return CORE_NEHALEM; //OS doesn't support AVX
  1800. case 12:
  1801. //Xeon Processor 5600 (Westmere-EP)
  1802. return CORE_NEHALEM;
  1803. case 13:
  1804. //Intel Core i7-3000 / Xeon E5 (Sandy Bridge)
  1805. if(support_avx())
  1806. return CORE_SANDYBRIDGE;
  1807. else
  1808. return CORE_NEHALEM; //OS doesn't support AVX
  1809. case 14:
  1810. //Xeon E7540
  1811. case 15:
  1812. //Xeon Processor E7 (Westmere-EX)
  1813. return CORE_NEHALEM;
  1814. }
  1815. break;
  1816. case 3:
  1817. switch (model) {
  1818. case 7:
  1819. return CORE_ATOM;
  1820. case 10:
  1821. case 14:
  1822. if(support_avx())
  1823. return CORE_SANDYBRIDGE;
  1824. else
  1825. return CORE_NEHALEM; //OS doesn't support AVX
  1826. case 12:
  1827. case 15:
  1828. if(support_avx())
  1829. #ifndef NO_AVX2
  1830. return CORE_HASWELL;
  1831. #else
  1832. return CORE_SANDYBRIDGE;
  1833. #endif
  1834. else
  1835. return CORE_NEHALEM;
  1836. case 13:
  1837. //broadwell
  1838. if(support_avx())
  1839. #ifndef NO_AVX2
  1840. return CORE_HASWELL;
  1841. #else
  1842. return CORE_SANDYBRIDGE;
  1843. #endif
  1844. else
  1845. return CORE_NEHALEM;
  1846. }
  1847. break;
  1848. case 4:
  1849. switch (model) {
  1850. case 5:
  1851. case 6:
  1852. if(support_avx())
  1853. #ifndef NO_AVX2
  1854. return CORE_HASWELL;
  1855. #else
  1856. return CORE_SANDYBRIDGE;
  1857. #endif
  1858. else
  1859. return CORE_NEHALEM;
  1860. case 7:
  1861. case 15:
  1862. //broadwell
  1863. if(support_avx())
  1864. #ifndef NO_AVX2
  1865. return CORE_HASWELL;
  1866. #else
  1867. return CORE_SANDYBRIDGE;
  1868. #endif
  1869. else
  1870. return CORE_NEHALEM;
  1871. case 14:
  1872. //Skylake
  1873. if(support_avx())
  1874. #ifndef NO_AVX2
  1875. return CORE_HASWELL;
  1876. #else
  1877. return CORE_SANDYBRIDGE;
  1878. #endif
  1879. else
  1880. return CORE_NEHALEM;
  1881. case 12:
  1882. // Braswell
  1883. case 13:
  1884. // Avoton
  1885. return CORE_NEHALEM;
  1886. }
  1887. break;
  1888. case 10:
  1889. switch (model) {
  1890. case 5: // Comet Lake H and S
  1891. case 6: // Comet Lake U
  1892. if(support_avx())
  1893. #ifndef NO_AVX2
  1894. return CORE_HASWELL;
  1895. #else
  1896. return CORE_SANDYBRIDGE;
  1897. #endif
  1898. else
  1899. return CORE_NEHALEM;
  1900. }
  1901. case 5:
  1902. switch (model) {
  1903. case 6:
  1904. //broadwell
  1905. if(support_avx())
  1906. #ifndef NO_AVX2
  1907. return CORE_HASWELL;
  1908. #else
  1909. return CORE_SANDYBRIDGE;
  1910. #endif
  1911. else
  1912. return CORE_NEHALEM;
  1913. case 5:
  1914. // Skylake X
  1915. #ifndef NO_AVX512
  1916. if(support_avx512_bf16())
  1917. return CORE_COOPERLAKE;
  1918. return CORE_SKYLAKEX;
  1919. #else
  1920. if(support_avx())
  1921. #ifndef NO_AVX2
  1922. return CORE_HASWELL;
  1923. #else
  1924. return CORE_SANDYBRIDGE;
  1925. #endif
  1926. else
  1927. return CORE_NEHALEM;
  1928. #endif
  1929. case 14:
  1930. // Skylake
  1931. if(support_avx())
  1932. #ifndef NO_AVX2
  1933. return CORE_HASWELL;
  1934. #else
  1935. return CORE_SANDYBRIDGE;
  1936. #endif
  1937. else
  1938. return CORE_NEHALEM;
  1939. case 7:
  1940. // Phi Knights Landing
  1941. if(support_avx())
  1942. #ifndef NO_AVX2
  1943. return CORE_HASWELL;
  1944. #else
  1945. return CORE_SANDYBRIDGE;
  1946. #endif
  1947. else
  1948. return CORE_NEHALEM;
  1949. case 12:
  1950. // Apollo Lake
  1951. return CORE_NEHALEM;
  1952. }
  1953. break;
  1954. case 6:
  1955. if (model == 6)
  1956. #ifndef NO_AVX512
  1957. return CORE_SKYLAKEX;
  1958. #else
  1959. if(support_avx())
  1960. #ifndef NO_AVX2
  1961. return CORE_HASWELL;
  1962. #else
  1963. return CORE_SANDYBRIDGE;
  1964. #endif
  1965. else
  1966. return CORE_NEHALEM;
  1967. #endif
  1968. break;
  1969. case 7:
  1970. if (model == 10)
  1971. return CORE_NEHALEM;
  1972. if (model == 14)
  1973. #ifndef NO_AVX512
  1974. return CORE_SKYLAKEX;
  1975. #else
  1976. if(support_avx())
  1977. #ifndef NO_AVX2
  1978. return CORE_HASWELL;
  1979. #else
  1980. return CORE_SANDYBRIDGE;
  1981. #endif
  1982. else
  1983. return CORE_NEHALEM;
  1984. #endif
  1985. break;
  1986. case 9:
  1987. case 8:
  1988. if (model == 14) { // Kaby Lake
  1989. if(support_avx())
  1990. #ifndef NO_AVX2
  1991. return CORE_HASWELL;
  1992. #else
  1993. return CORE_SANDYBRIDGE;
  1994. #endif
  1995. else
  1996. return CORE_NEHALEM;
  1997. }
  1998. }
  1999. break;
  2000. case 15:
  2001. if (model <= 0x2) return CORE_NORTHWOOD;
  2002. else return CORE_PRESCOTT;
  2003. }
  2004. }
  2005. if (vendor == VENDOR_AMD){
  2006. if (family <= 0x5) return CORE_80486;
  2007. #if defined(__x86_64__) || defined(__amd64__)
  2008. if (family <= 0xe) return CORE_BARCELONA;
  2009. #else
  2010. if (family <= 0xe) return CORE_ATHLON;
  2011. #endif
  2012. if (family == 0xf){
  2013. if ((exfamily == 0) || (exfamily == 2)) return CORE_OPTERON;
  2014. else if (exfamily == 5) return CORE_BOBCAT;
  2015. else if (exfamily == 6) {
  2016. switch (model) {
  2017. case 1:
  2018. //AMD Bulldozer Opteron 6200 / Opteron 4200 / AMD FX-Series
  2019. if(support_avx())
  2020. return CORE_BULLDOZER;
  2021. else
  2022. return CORE_BARCELONA; //OS don't support AVX.
  2023. case 2: //AMD Piledriver
  2024. case 3: //AMD Richland
  2025. if(support_avx())
  2026. return CORE_PILEDRIVER;
  2027. else
  2028. return CORE_BARCELONA; //OS don't support AVX.
  2029. case 5: // New EXCAVATOR
  2030. if(support_avx())
  2031. return CORE_EXCAVATOR;
  2032. else
  2033. return CORE_BARCELONA; //OS don't support AVX.
  2034. case 0:
  2035. case 8:
  2036. switch(exmodel){
  2037. case 1: //AMD Trinity
  2038. if(support_avx())
  2039. return CORE_PILEDRIVER;
  2040. else
  2041. return CORE_BARCELONA; //OS don't support AVX.
  2042. case 3:
  2043. if(support_avx())
  2044. return CORE_STEAMROLLER;
  2045. else
  2046. return CORE_BARCELONA; //OS don't support AVX.
  2047. case 6:
  2048. if(support_avx())
  2049. return CORE_EXCAVATOR;
  2050. else
  2051. return CORE_BARCELONA; //OS don't support AVX.
  2052. }
  2053. break;
  2054. }
  2055. } else if (exfamily == 8 || exfamily == 10) {
  2056. switch (model) {
  2057. case 1:
  2058. // AMD Ryzen
  2059. case 8:
  2060. // Ryzen 2
  2061. default:
  2062. // Matisse,Renoir Ryzen2 models
  2063. if(support_avx())
  2064. #ifndef NO_AVX2
  2065. return CORE_ZEN;
  2066. #else
  2067. return CORE_SANDYBRIDGE; // Zen is closer in architecture to Sandy Bridge than to Excavator
  2068. #endif
  2069. else
  2070. return CORE_BARCELONA;
  2071. }
  2072. } else {
  2073. return CORE_BARCELONA;
  2074. }
  2075. }
  2076. }
  2077. if (vendor == VENDOR_HYGON){
  2078. if (family == 0xf){
  2079. if (exfamily == 9) {
  2080. if(support_avx())
  2081. #ifndef NO_AVX2
  2082. return CORE_ZEN;
  2083. #else
  2084. return CORE_SANDYBRIDGE; // closer in architecture to Sandy Bridge than to Excavator
  2085. #endif
  2086. else
  2087. return CORE_BARCELONA;
  2088. } else {
  2089. return CORE_BARCELONA;
  2090. }
  2091. }
  2092. }
  2093. if (vendor == VENDOR_CENTAUR) {
  2094. switch (family) {
  2095. case 0x6:
  2096. return CORE_NANO;
  2097. break;
  2098. }
  2099. return CORE_VIAC3;
  2100. }
  2101. return CORE_UNKNOWN;
  2102. }
  2103. void get_cpuconfig(void){
  2104. cache_info_t info;
  2105. int features;
  2106. printf("#define %s\n", cpuname[get_cpuname()]);
  2107. if (get_coretype() != CORE_P5) {
  2108. get_cacheinfo(CACHE_INFO_L1_I, &info);
  2109. if (info.size > 0) {
  2110. printf("#define L1_CODE_SIZE %d\n", info.size * 1024);
  2111. printf("#define L1_CODE_ASSOCIATIVE %d\n", info.associative);
  2112. printf("#define L1_CODE_LINESIZE %d\n", info.linesize);
  2113. }
  2114. get_cacheinfo(CACHE_INFO_L1_D, &info);
  2115. if (info.size > 0) {
  2116. printf("#define L1_DATA_SIZE %d\n", info.size * 1024);
  2117. printf("#define L1_DATA_ASSOCIATIVE %d\n", info.associative);
  2118. printf("#define L1_DATA_LINESIZE %d\n", info.linesize);
  2119. }
  2120. get_cacheinfo(CACHE_INFO_L2, &info);
  2121. if (info.size > 0) {
  2122. printf("#define L2_SIZE %d\n", info.size * 1024);
  2123. printf("#define L2_ASSOCIATIVE %d\n", info.associative);
  2124. printf("#define L2_LINESIZE %d\n", info.linesize);
  2125. } else {
  2126. //fall back for some virtual machines.
  2127. printf("#define L2_SIZE 1048576\n");
  2128. printf("#define L2_ASSOCIATIVE 6\n");
  2129. printf("#define L2_LINESIZE 64\n");
  2130. }
  2131. get_cacheinfo(CACHE_INFO_L3, &info);
  2132. if (info.size > 0) {
  2133. printf("#define L3_SIZE %d\n", info.size * 1024);
  2134. printf("#define L3_ASSOCIATIVE %d\n", info.associative);
  2135. printf("#define L3_LINESIZE %d\n", info.linesize);
  2136. }
  2137. get_cacheinfo(CACHE_INFO_L1_ITB, &info);
  2138. if (info.size > 0) {
  2139. printf("#define ITB_SIZE %d\n", info.size * 1024);
  2140. printf("#define ITB_ASSOCIATIVE %d\n", info.associative);
  2141. printf("#define ITB_ENTRIES %d\n", info.linesize);
  2142. }
  2143. get_cacheinfo(CACHE_INFO_L1_DTB, &info);
  2144. if (info.size > 0) {
  2145. printf("#define DTB_SIZE %d\n", info.size * 1024);
  2146. printf("#define DTB_ASSOCIATIVE %d\n", info.associative);
  2147. printf("#define DTB_DEFAULT_ENTRIES %d\n", info.linesize);
  2148. } else {
  2149. //fall back for some virtual machines.
  2150. printf("#define DTB_DEFAULT_ENTRIES 32\n");
  2151. }
  2152. features = get_cputype(GET_FEATURE);
  2153. if (features & HAVE_CMOV ) printf("#define HAVE_CMOV\n");
  2154. if (features & HAVE_MMX ) printf("#define HAVE_MMX\n");
  2155. if (features & HAVE_SSE ) printf("#define HAVE_SSE\n");
  2156. if (features & HAVE_SSE2 ) printf("#define HAVE_SSE2\n");
  2157. if (features & HAVE_SSE3 ) printf("#define HAVE_SSE3\n");
  2158. if (features & HAVE_SSSE3) printf("#define HAVE_SSSE3\n");
  2159. if (features & HAVE_SSE4_1) printf("#define HAVE_SSE4_1\n");
  2160. if (features & HAVE_SSE4_2) printf("#define HAVE_SSE4_2\n");
  2161. if (features & HAVE_SSE4A) printf("#define HAVE_SSE4A\n");
  2162. if (features & HAVE_SSE5 ) printf("#define HAVE_SSSE5\n");
  2163. if (features & HAVE_AVX ) printf("#define HAVE_AVX\n");
  2164. if (features & HAVE_AVX2 ) printf("#define HAVE_AVX2\n");
  2165. if (features & HAVE_AVX512VL ) printf("#define HAVE_AVX512VL\n");
  2166. if (features & HAVE_AVX512BF16 ) printf("#define HAVE_AVX512BF16\n");
  2167. if (features & HAVE_3DNOWEX) printf("#define HAVE_3DNOWEX\n");
  2168. if (features & HAVE_3DNOW) printf("#define HAVE_3DNOW\n");
  2169. if (features & HAVE_FMA4 ) printf("#define HAVE_FMA4\n");
  2170. if (features & HAVE_FMA3 ) printf("#define HAVE_FMA3\n");
  2171. if (features & HAVE_CFLUSH) printf("#define HAVE_CFLUSH\n");
  2172. if (features & HAVE_HIT) printf("#define HAVE_HIT 1\n");
  2173. if (features & HAVE_MISALIGNSSE) printf("#define HAVE_MISALIGNSSE\n");
  2174. if (features & HAVE_128BITFPU) printf("#define HAVE_128BITFPU\n");
  2175. if (features & HAVE_FASTMOVU) printf("#define HAVE_FASTMOVU\n");
  2176. printf("#define NUM_SHAREDCACHE %d\n", get_cputype(GET_NUMSHARE) + 1);
  2177. printf("#define NUM_CORES %d\n", get_cputype(GET_NUMCORES) + 1);
  2178. features = get_coretype();
  2179. if (features > 0) printf("#define CORE_%s\n", corename[features]);
  2180. } else {
  2181. printf("#define DTB_DEFAULT_ENTRIES 16\n");
  2182. printf("#define L1_CODE_SIZE 8192\n");
  2183. printf("#define L1_DATA_SIZE 8192\n");
  2184. printf("#define L2_SIZE 0\n");
  2185. }
  2186. }
  2187. void get_architecture(void){
  2188. #ifndef __64BIT__
  2189. printf("X86");
  2190. #else
  2191. printf("X86_64");
  2192. #endif
  2193. }
  2194. void get_subarchitecture(void){
  2195. printf("%s", get_cpunamechar());
  2196. }
  2197. void get_subdirname(void){
  2198. #ifndef __64BIT__
  2199. printf("x86");
  2200. #else
  2201. printf("x86_64");
  2202. #endif
  2203. }
  2204. char *get_corename(void){
  2205. return corename[get_coretype()];
  2206. }
  2207. void get_libname(void){
  2208. printf("%s", corename_lower[get_coretype()]);
  2209. }
  2210. /* This if for Makefile */
  2211. void get_sse(void){
  2212. int features;
  2213. features = get_cputype(GET_FEATURE);
  2214. if (features & HAVE_MMX ) printf("HAVE_MMX=1\n");
  2215. if (features & HAVE_SSE ) printf("HAVE_SSE=1\n");
  2216. if (features & HAVE_SSE2 ) printf("HAVE_SSE2=1\n");
  2217. if (features & HAVE_SSE3 ) printf("HAVE_SSE3=1\n");
  2218. if (features & HAVE_SSSE3) printf("HAVE_SSSE3=1\n");
  2219. if (features & HAVE_SSE4_1) printf("HAVE_SSE4_1=1\n");
  2220. if (features & HAVE_SSE4_2) printf("HAVE_SSE4_2=1\n");
  2221. if (features & HAVE_SSE4A) printf("HAVE_SSE4A=1\n");
  2222. if (features & HAVE_SSE5 ) printf("HAVE_SSSE5=1\n");
  2223. if (features & HAVE_AVX ) printf("HAVE_AVX=1\n");
  2224. if (features & HAVE_AVX2 ) printf("HAVE_AVX2=1\n");
  2225. if (features & HAVE_AVX512VL ) printf("HAVE_AVX512VL=1\n");
  2226. if (features & HAVE_AVX512BF16 ) printf("HAVE_AVX512BF16=1\n");
  2227. if (features & HAVE_3DNOWEX) printf("HAVE_3DNOWEX=1\n");
  2228. if (features & HAVE_3DNOW) printf("HAVE_3DNOW=1\n");
  2229. if (features & HAVE_FMA4 ) printf("HAVE_FMA4=1\n");
  2230. if (features & HAVE_FMA3 ) printf("HAVE_FMA3=1\n");
  2231. }