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

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