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

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