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

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