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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747
  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. case 6: // Arrow Lake
  1487. if(support_avx512())
  1488. return CPUTYPE_SAPPHIRERAPIDS;
  1489. if(support_avx2())
  1490. return CPUTYPE_HASWELL;
  1491. if(support_avx())
  1492. return CPUTYPE_SANDYBRIDGE;
  1493. else
  1494. return CPUTYPE_NEHALEM;
  1495. }
  1496. break;
  1497. }
  1498. break;
  1499. case 0x7:
  1500. return CPUTYPE_ITANIUM;
  1501. case 0xf:
  1502. switch (exfamily) {
  1503. case 0 :
  1504. return CPUTYPE_PENTIUM4;
  1505. case 1 :
  1506. return CPUTYPE_ITANIUM;
  1507. }
  1508. break;
  1509. }
  1510. return CPUTYPE_INTEL_UNKNOWN;
  1511. }
  1512. if (vendor == VENDOR_AMD){
  1513. switch (family) {
  1514. case 0x4:
  1515. return CPUTYPE_AMD5X86;
  1516. case 0x5:
  1517. return CPUTYPE_AMDK6;
  1518. case 0x6:
  1519. #if defined(__x86_64__) || defined(__amd64__)
  1520. return CPUTYPE_BARCELONA;
  1521. #else
  1522. return CPUTYPE_ATHLON;
  1523. #endif
  1524. case 0xf:
  1525. switch (exfamily) {
  1526. case 0:
  1527. case 2:
  1528. return CPUTYPE_OPTERON;
  1529. case 1:
  1530. case 3:
  1531. // case 7:
  1532. // case 10:
  1533. return CPUTYPE_BARCELONA;
  1534. case 5:
  1535. case 7:
  1536. return CPUTYPE_BOBCAT;
  1537. case 6:
  1538. switch (model) {
  1539. case 1:
  1540. //AMD Bulldozer Opteron 6200 / Opteron 4200 / AMD FX-Series
  1541. if(support_avx())
  1542. return CPUTYPE_BULLDOZER;
  1543. else
  1544. return CPUTYPE_BARCELONA; //OS don't support AVX.
  1545. case 2: //AMD Piledriver
  1546. case 3: //AMD Richland
  1547. if(support_avx())
  1548. return CPUTYPE_PILEDRIVER;
  1549. else
  1550. return CPUTYPE_BARCELONA; //OS don't support AVX.
  1551. case 5: // New EXCAVATOR CPUS
  1552. if(support_avx())
  1553. return CPUTYPE_EXCAVATOR;
  1554. else
  1555. return CPUTYPE_BARCELONA; //OS don't support AVX.
  1556. case 0:
  1557. case 8:
  1558. switch(exmodel){
  1559. case 1: //AMD Trinity
  1560. if(support_avx())
  1561. return CPUTYPE_PILEDRIVER;
  1562. else
  1563. return CPUTYPE_BARCELONA; //OS don't support AVX.
  1564. case 3:
  1565. if(support_avx())
  1566. return CPUTYPE_STEAMROLLER;
  1567. else
  1568. return CPUTYPE_BARCELONA; //OS don't support AVX.
  1569. case 6:
  1570. if(support_avx())
  1571. return CPUTYPE_EXCAVATOR;
  1572. else
  1573. return CPUTYPE_BARCELONA; //OS don't support AVX.
  1574. }
  1575. break;
  1576. }
  1577. break;
  1578. case 8:
  1579. switch (model) {
  1580. case 1:
  1581. // AMD Ryzen
  1582. case 8:
  1583. // AMD Ryzen2
  1584. default:
  1585. // Matisse/Renoir and other recent Ryzen2
  1586. if(support_avx())
  1587. #ifndef NO_AVX2
  1588. return CPUTYPE_ZEN;
  1589. #else
  1590. return CPUTYPE_SANDYBRIDGE; // Zen is closer in architecture to Sandy Bridge than to Excavator
  1591. #endif
  1592. else
  1593. return CPUTYPE_BARCELONA;
  1594. }
  1595. case 10: // Zen3/4
  1596. case 11: // Zen5
  1597. #ifndef NO_AVX512
  1598. if(support_avx512_bf16())
  1599. return CPUTYPE_COOPERLAKE;
  1600. if(support_avx512())
  1601. return CPUTYPE_SKYLAKEX;
  1602. #endif
  1603. if(support_avx())
  1604. #ifndef NO_AVX2
  1605. return CPUTYPE_ZEN;
  1606. #else
  1607. return CPUTYPE_SANDYBRIDGE; // Zen is closer in architecture to Sandy Bridge than to Excavator
  1608. #endif
  1609. else
  1610. return CPUTYPE_BARCELONA;
  1611. }
  1612. break;
  1613. }
  1614. return CPUTYPE_AMD_UNKNOWN;
  1615. }
  1616. if (vendor == VENDOR_HYGON){
  1617. switch (family) {
  1618. case 0xf:
  1619. switch (exfamily) {
  1620. case 9:
  1621. //Hygon Dhyana
  1622. if(support_avx())
  1623. #ifndef NO_AVX2
  1624. return CPUTYPE_ZEN;
  1625. #else
  1626. return CPUTYPE_SANDYBRIDGE; // closer in architecture to Sandy Bridge than to Excavator
  1627. #endif
  1628. else
  1629. return CPUTYPE_BARCELONA;
  1630. }
  1631. break;
  1632. }
  1633. return CPUTYPE_HYGON_UNKNOWN;
  1634. }
  1635. if (vendor == VENDOR_CYRIX){
  1636. switch (family) {
  1637. case 0x4:
  1638. return CPUTYPE_CYRIX5X86;
  1639. case 0x5:
  1640. return CPUTYPE_CYRIXM1;
  1641. case 0x6:
  1642. return CPUTYPE_CYRIXM2;
  1643. }
  1644. return CPUTYPE_CYRIX_UNKNOWN;
  1645. }
  1646. if (vendor == VENDOR_NEXGEN){
  1647. switch (family) {
  1648. case 0x5:
  1649. return CPUTYPE_NEXGENNX586;
  1650. }
  1651. return CPUTYPE_NEXGEN_UNKNOWN;
  1652. }
  1653. if (vendor == VENDOR_CENTAUR){
  1654. switch (family) {
  1655. case 0x5:
  1656. return CPUTYPE_CENTAURC6;
  1657. case 0x6:
  1658. if (model == 0xf && stepping < 0xe)
  1659. return CPUTYPE_NANO;
  1660. return CPUTYPE_NEHALEM;
  1661. case 0x7:
  1662. switch (exmodel) {
  1663. case 5:
  1664. if (support_avx2())
  1665. return CPUTYPE_ZEN;
  1666. else
  1667. return CPUTYPE_DUNNINGTON;
  1668. default:
  1669. return CPUTYPE_NEHALEM;
  1670. }
  1671. default:
  1672. if (family >= 0x8)
  1673. return CPUTYPE_NEHALEM;
  1674. else
  1675. return CPUTYPE_VIAC3;
  1676. }
  1677. }
  1678. if (vendor == VENDOR_ZHAOXIN){
  1679. switch (family) {
  1680. case 0x7:
  1681. switch (exmodel) {
  1682. case 5:
  1683. if (support_avx2())
  1684. return CPUTYPE_ZEN;
  1685. else
  1686. return CPUTYPE_DUNNINGTON;
  1687. default:
  1688. return CPUTYPE_NEHALEM;
  1689. }
  1690. default:
  1691. return CPUTYPE_NEHALEM;
  1692. }
  1693. }
  1694. if (vendor == VENDOR_RISE){
  1695. switch (family) {
  1696. case 0x5:
  1697. return CPUTYPE_RISEMP6;
  1698. }
  1699. return CPUTYPE_RISE_UNKNOWN;
  1700. }
  1701. if (vendor == VENDOR_SIS){
  1702. switch (family) {
  1703. case 0x5:
  1704. return CPUTYPE_SYS55X;
  1705. }
  1706. return CPUTYPE_SIS_UNKNOWN;
  1707. }
  1708. if (vendor == VENDOR_TRANSMETA){
  1709. switch (family) {
  1710. case 0x5:
  1711. return CPUTYPE_CRUSOETM3X;
  1712. }
  1713. return CPUTYPE_TRANSMETA_UNKNOWN;
  1714. }
  1715. if (vendor == VENDOR_NSC){
  1716. switch (family) {
  1717. case 0x5:
  1718. return CPUTYPE_NSGEODE;
  1719. }
  1720. return CPUTYPE_NSC_UNKNOWN;
  1721. }
  1722. return CPUTYPE_UNKNOWN;
  1723. }
  1724. static char *cpuname[] = {
  1725. "UNKNOWN",
  1726. "INTEL_UNKNOWN",
  1727. "UMC_UNKNOWN",
  1728. "AMD_UNKNOWN",
  1729. "CYRIX_UNKNOWN",
  1730. "NEXGEN_UNKNOWN",
  1731. "CENTAUR_UNKNOWN",
  1732. "RISE_UNKNOWN",
  1733. "SIS_UNKNOWN",
  1734. "TRANSMETA_UNKNOWN",
  1735. "NSC_UNKNOWN",
  1736. "80386",
  1737. "80486",
  1738. "PENTIUM",
  1739. "PENTIUM2",
  1740. "PENTIUM3",
  1741. "PENTIUMM",
  1742. "PENTIUM4",
  1743. "CORE2",
  1744. "PENRYN",
  1745. "DUNNINGTON",
  1746. "NEHALEM",
  1747. "ATOM",
  1748. "ITANIUM",
  1749. "ITANIUM2",
  1750. "5X86",
  1751. "K6",
  1752. "ATHLON",
  1753. "DURON",
  1754. "OPTERON",
  1755. "BARCELONA",
  1756. "SHANGHAI",
  1757. "ISTANBUL",
  1758. "CYRIX5X86",
  1759. "CYRIXM1",
  1760. "CYRIXM2",
  1761. "NEXGENNX586",
  1762. "CENTAURC6",
  1763. "RISEMP6",
  1764. "SYS55X",
  1765. "TM3X00",
  1766. "NSGEODE",
  1767. "VIAC3",
  1768. "NANO",
  1769. "SANDYBRIDGE",
  1770. "BOBCAT",
  1771. "BULLDOZER",
  1772. "PILEDRIVER",
  1773. "HASWELL",
  1774. "STEAMROLLER",
  1775. "EXCAVATOR",
  1776. "ZEN",
  1777. "SKYLAKEX",
  1778. "DHYANA",
  1779. "COOPERLAKE",
  1780. "SAPPHIRERAPIDS",
  1781. };
  1782. static char *lowercpuname[] = {
  1783. "unknown",
  1784. "intel_unknown",
  1785. "umc_unknown",
  1786. "amd_unknown",
  1787. "cyrix_unknown",
  1788. "nexgen_unknown",
  1789. "centaur_unknown",
  1790. "rise_unknown",
  1791. "sis_unknown",
  1792. "transmeta_unknown",
  1793. "nsc_unknown",
  1794. "80386",
  1795. "80486",
  1796. "pentium",
  1797. "pentium2",
  1798. "pentium3",
  1799. "pentiumm",
  1800. "pentium4",
  1801. "core2",
  1802. "penryn",
  1803. "dunnington",
  1804. "nehalem",
  1805. "atom",
  1806. "itanium",
  1807. "itanium2",
  1808. "5x86",
  1809. "k6",
  1810. "athlon",
  1811. "duron",
  1812. "opteron",
  1813. "barcelona",
  1814. "shanghai",
  1815. "istanbul",
  1816. "cyrix5x86",
  1817. "cyrixm1",
  1818. "cyrixm2",
  1819. "nexgennx586",
  1820. "centaurc6",
  1821. "risemp6",
  1822. "sys55x",
  1823. "tms3x00",
  1824. "nsgeode",
  1825. "nano",
  1826. "sandybridge",
  1827. "bobcat",
  1828. "bulldozer",
  1829. "piledriver",
  1830. "haswell",
  1831. "steamroller",
  1832. "excavator",
  1833. "zen",
  1834. "skylakex",
  1835. "dhyana",
  1836. "cooperlake",
  1837. "sapphirerapids",
  1838. };
  1839. static char *corename[] = {
  1840. "UNKNOWN",
  1841. "80486",
  1842. "P5",
  1843. "P6",
  1844. "KATMAI",
  1845. "COPPERMINE",
  1846. "NORTHWOOD",
  1847. "PRESCOTT",
  1848. "BANIAS",
  1849. "ATHLON",
  1850. "OPTERON",
  1851. "BARCELONA",
  1852. "VIAC3",
  1853. "YONAH",
  1854. "CORE2",
  1855. "PENRYN",
  1856. "DUNNINGTON",
  1857. "NEHALEM",
  1858. "ATOM",
  1859. "NANO",
  1860. "SANDYBRIDGE",
  1861. "BOBCAT",
  1862. "BULLDOZER",
  1863. "PILEDRIVER",
  1864. "HASWELL",
  1865. "STEAMROLLER",
  1866. "EXCAVATOR",
  1867. "ZEN",
  1868. "SKYLAKEX",
  1869. "DHYANA",
  1870. "COOPERLAKE",
  1871. "SAPPHIRERAPIDS",
  1872. };
  1873. static char *corename_lower[] = {
  1874. "unknown",
  1875. "80486",
  1876. "p5",
  1877. "p6",
  1878. "katmai",
  1879. "coppermine",
  1880. "northwood",
  1881. "prescott",
  1882. "banias",
  1883. "athlon",
  1884. "opteron",
  1885. "barcelona",
  1886. "viac3",
  1887. "yonah",
  1888. "core2",
  1889. "penryn",
  1890. "dunnington",
  1891. "nehalem",
  1892. "atom",
  1893. "nano",
  1894. "sandybridge",
  1895. "bobcat",
  1896. "bulldozer",
  1897. "piledriver",
  1898. "haswell",
  1899. "steamroller",
  1900. "excavator",
  1901. "zen",
  1902. "skylakex",
  1903. "dhyana",
  1904. "cooperlake",
  1905. "sapphirerapids",
  1906. };
  1907. char *get_cpunamechar(void){
  1908. return cpuname[get_cpuname()];
  1909. }
  1910. char *get_lower_cpunamechar(void){
  1911. return lowercpuname[get_cpuname()];
  1912. }
  1913. int get_coretype(void){
  1914. int family, exfamily, model, exmodel, vendor, stepping;
  1915. if (!have_cpuid()) return CORE_80486;
  1916. family = get_cputype(GET_FAMILY);
  1917. exfamily = get_cputype(GET_EXFAMILY);
  1918. model = get_cputype(GET_MODEL);
  1919. exmodel = get_cputype(GET_EXMODEL);
  1920. stepping = get_cputype(GET_STEPPING);
  1921. vendor = get_vendor();
  1922. if (vendor == VENDOR_INTEL){
  1923. switch (family) {
  1924. case 4:
  1925. return CORE_80486;
  1926. case 5:
  1927. return CORE_P5;
  1928. case 6:
  1929. switch (exmodel) {
  1930. case 0:
  1931. switch (model) {
  1932. case 0:
  1933. case 1:
  1934. case 2:
  1935. case 3:
  1936. case 4:
  1937. case 5:
  1938. case 6:
  1939. #if defined(__x86_64__) || defined(__amd64__)
  1940. return CORE_CORE2;
  1941. #else
  1942. return CORE_P6;
  1943. #endif
  1944. case 7:
  1945. return CORE_KATMAI;
  1946. case 8:
  1947. case 10:
  1948. case 11:
  1949. return CORE_COPPERMINE;
  1950. case 9:
  1951. case 13:
  1952. case 14:
  1953. return CORE_BANIAS;
  1954. case 15:
  1955. return CORE_CORE2;
  1956. }
  1957. break;
  1958. case 1:
  1959. switch (model) {
  1960. case 6:
  1961. return CORE_CORE2;
  1962. case 7:
  1963. return CORE_PENRYN;
  1964. case 10:
  1965. case 11:
  1966. case 14:
  1967. case 15:
  1968. return CORE_NEHALEM;
  1969. case 12:
  1970. return CORE_ATOM;
  1971. case 13:
  1972. return CORE_DUNNINGTON;
  1973. }
  1974. break;
  1975. case 2:
  1976. switch (model) {
  1977. case 5:
  1978. //Intel Core (Clarkdale) / Core (Arrandale)
  1979. // Pentium (Clarkdale) / Pentium Mobile (Arrandale)
  1980. // Xeon (Clarkdale), 32nm
  1981. return CORE_NEHALEM;
  1982. case 10:
  1983. //Intel Core i5-2000 /i7-2000 (Sandy Bridge)
  1984. if(support_avx())
  1985. return CORE_SANDYBRIDGE;
  1986. else
  1987. return CORE_NEHALEM; //OS doesn't support AVX
  1988. case 12:
  1989. //Xeon Processor 5600 (Westmere-EP)
  1990. return CORE_NEHALEM;
  1991. case 13:
  1992. //Intel Core i7-3000 / Xeon E5 (Sandy Bridge)
  1993. if(support_avx())
  1994. return CORE_SANDYBRIDGE;
  1995. else
  1996. return CORE_NEHALEM; //OS doesn't support AVX
  1997. case 14:
  1998. //Xeon E7540
  1999. case 15:
  2000. //Xeon Processor E7 (Westmere-EX)
  2001. return CORE_NEHALEM;
  2002. }
  2003. break;
  2004. case 3:
  2005. switch (model) {
  2006. case 7:
  2007. return CORE_ATOM;
  2008. case 10:
  2009. case 14:
  2010. if(support_avx())
  2011. return CORE_SANDYBRIDGE;
  2012. else
  2013. return CORE_NEHALEM; //OS doesn't support AVX
  2014. case 12:
  2015. case 15:
  2016. if(support_avx())
  2017. #ifndef NO_AVX2
  2018. return CORE_HASWELL;
  2019. #else
  2020. return CORE_SANDYBRIDGE;
  2021. #endif
  2022. else
  2023. return CORE_NEHALEM;
  2024. case 13:
  2025. //broadwell
  2026. if(support_avx())
  2027. #ifndef NO_AVX2
  2028. return CORE_HASWELL;
  2029. #else
  2030. return CORE_SANDYBRIDGE;
  2031. #endif
  2032. else
  2033. return CORE_NEHALEM;
  2034. }
  2035. break;
  2036. case 4:
  2037. switch (model) {
  2038. case 5:
  2039. case 6:
  2040. if(support_avx())
  2041. #ifndef NO_AVX2
  2042. return CORE_HASWELL;
  2043. #else
  2044. return CORE_SANDYBRIDGE;
  2045. #endif
  2046. else
  2047. return CORE_NEHALEM;
  2048. case 7:
  2049. case 15:
  2050. //broadwell
  2051. if(support_avx())
  2052. #ifndef NO_AVX2
  2053. return CORE_HASWELL;
  2054. #else
  2055. return CORE_SANDYBRIDGE;
  2056. #endif
  2057. else
  2058. return CORE_NEHALEM;
  2059. case 14:
  2060. //Skylake
  2061. if(support_avx())
  2062. #ifndef NO_AVX2
  2063. return CORE_HASWELL;
  2064. #else
  2065. return CORE_SANDYBRIDGE;
  2066. #endif
  2067. else
  2068. return CORE_NEHALEM;
  2069. case 12:
  2070. // Braswell
  2071. case 13:
  2072. // Avoton
  2073. return CORE_NEHALEM;
  2074. }
  2075. break;
  2076. case 5:
  2077. switch (model) {
  2078. case 6:
  2079. //broadwell
  2080. if(support_avx())
  2081. #ifndef NO_AVX2
  2082. return CORE_HASWELL;
  2083. #else
  2084. return CORE_SANDYBRIDGE;
  2085. #endif
  2086. else
  2087. return CORE_NEHALEM;
  2088. case 5:
  2089. // Skylake X
  2090. #ifndef NO_AVX512
  2091. if(support_avx512_bf16())
  2092. return CORE_COOPERLAKE;
  2093. return CORE_SKYLAKEX;
  2094. #else
  2095. if(support_avx())
  2096. #ifndef NO_AVX2
  2097. return CORE_HASWELL;
  2098. #else
  2099. return CORE_SANDYBRIDGE;
  2100. #endif
  2101. else
  2102. return CORE_NEHALEM;
  2103. #endif
  2104. case 14:
  2105. // Skylake
  2106. if(support_avx())
  2107. #ifndef NO_AVX2
  2108. return CORE_HASWELL;
  2109. #else
  2110. return CORE_SANDYBRIDGE;
  2111. #endif
  2112. else
  2113. return CORE_NEHALEM;
  2114. case 7:
  2115. // Phi Knights Landing
  2116. if(support_avx())
  2117. #ifndef NO_AVX2
  2118. return CORE_HASWELL;
  2119. #else
  2120. return CORE_SANDYBRIDGE;
  2121. #endif
  2122. else
  2123. return CORE_NEHALEM;
  2124. case 12:
  2125. // Apollo Lake
  2126. return CORE_NEHALEM;
  2127. }
  2128. break;
  2129. case 6:
  2130. if (model == 6)
  2131. #ifndef NO_AVX512
  2132. return CORE_SKYLAKEX;
  2133. #else
  2134. if(support_avx())
  2135. #ifndef NO_AVX2
  2136. return CORE_HASWELL;
  2137. #else
  2138. return CORE_SANDYBRIDGE;
  2139. #endif
  2140. else
  2141. return CORE_NEHALEM;
  2142. #endif
  2143. if (model == 10 || model == 12)
  2144. #ifndef NO_AVX512
  2145. if(support_avx512_bf16())
  2146. return CORE_COOPERLAKE;
  2147. return CORE_SKYLAKEX;
  2148. #else
  2149. if(support_avx())
  2150. #ifndef NO_AVX2
  2151. return CORE_HASWELL;
  2152. #else
  2153. return CORE_SANDYBRIDGE;
  2154. #endif
  2155. else
  2156. return CORE_NEHALEM;
  2157. #endif
  2158. case 7:
  2159. if (model == 10)
  2160. return CORE_NEHALEM;
  2161. if (model == 13 || model == 14) // Ice Lake
  2162. #ifndef NO_AVX512
  2163. return CORE_SKYLAKEX;
  2164. #else
  2165. if(support_avx())
  2166. #ifndef NO_AVX2
  2167. return CORE_HASWELL;
  2168. #else
  2169. return CORE_SANDYBRIDGE;
  2170. #endif
  2171. else
  2172. return CORE_NEHALEM;
  2173. #endif
  2174. break;
  2175. case 8:
  2176. if (model == 12 || model == 13) { // Tiger Lake
  2177. if(support_avx512())
  2178. return CORE_SKYLAKEX;
  2179. if(support_avx2())
  2180. return CORE_HASWELL;
  2181. if(support_avx())
  2182. return CORE_SANDYBRIDGE;
  2183. else
  2184. return CORE_NEHALEM;
  2185. }
  2186. if (model == 14) { // Kaby Lake mobile
  2187. if(support_avx())
  2188. #ifndef NO_AVX2
  2189. return CORE_HASWELL;
  2190. #else
  2191. return CORE_SANDYBRIDGE;
  2192. #endif
  2193. else
  2194. return CORE_NEHALEM;
  2195. }
  2196. if (model == 15) { // Sapphire Rapids
  2197. if(support_amx_bf16())
  2198. return CORE_SAPPHIRERAPIDS;
  2199. if(support_avx512_bf16())
  2200. return CORE_COOPERLAKE;
  2201. if(support_avx512())
  2202. return CORE_SKYLAKEX;
  2203. if(support_avx2())
  2204. return CORE_HASWELL;
  2205. if(support_avx())
  2206. return CORE_SANDYBRIDGE;
  2207. else
  2208. return CORE_NEHALEM;
  2209. }
  2210. break;
  2211. case 9:
  2212. if (model == 7 || model == 10) { // Alder Lake
  2213. if(support_avx2())
  2214. return CORE_HASWELL;
  2215. if(support_avx())
  2216. return CORE_SANDYBRIDGE;
  2217. else
  2218. return CORE_NEHALEM;
  2219. }
  2220. if (model == 13) { // Ice Lake NNPI
  2221. if(support_avx512())
  2222. return CORE_SKYLAKEX;
  2223. if(support_avx2())
  2224. return CORE_HASWELL;
  2225. if(support_avx())
  2226. return CORE_SANDYBRIDGE;
  2227. else
  2228. return CORE_NEHALEM;
  2229. }
  2230. if (model == 14) { // Kaby Lake desktop
  2231. if(support_avx())
  2232. #ifndef NO_AVX2
  2233. return CORE_HASWELL;
  2234. #else
  2235. return CORE_SANDYBRIDGE;
  2236. #endif
  2237. else
  2238. return CORE_NEHALEM;
  2239. }
  2240. break;
  2241. case 10:
  2242. switch (model) {
  2243. case 13: // Granite Rapids
  2244. if(support_amx_bf16())
  2245. return CORE_SAPPHIRERAPIDS;
  2246. if(support_avx512_bf16())
  2247. return CORE_COOPERLAKE;
  2248. if(support_avx512())
  2249. return CORE_SKYLAKEX;
  2250. if(support_avx2())
  2251. return CORE_HASWELL;
  2252. if(support_avx())
  2253. return CORE_SANDYBRIDGE;
  2254. else
  2255. return CORE_NEHALEM;
  2256. case 5: // Comet Lake H and S
  2257. case 6: // Comet Lake U
  2258. case 10: // Meteor Lake
  2259. if(support_avx())
  2260. #ifndef NO_AVX2
  2261. return CORE_HASWELL;
  2262. #else
  2263. return CORE_SANDYBRIDGE;
  2264. #endif
  2265. else
  2266. return CORE_NEHALEM;
  2267. case 0: // Meteor Lake
  2268. case 7:// Rocket Lake
  2269. #ifndef NO_AVX512
  2270. if(support_avx512())
  2271. return CORE_SKYLAKEX;
  2272. #endif
  2273. #ifndef NO_AVX2
  2274. if(support_avx2())
  2275. return CORE_HASWELL;
  2276. #endif
  2277. if(support_avx())
  2278. return CORE_SANDYBRIDGE;
  2279. else
  2280. return CORE_NEHALEM;
  2281. }
  2282. case 11:
  2283. switch (model) {
  2284. case 7: // Raptor Lake
  2285. case 10:
  2286. case 15:
  2287. case 14: // Alder Lake N
  2288. #ifndef NO_AVX2
  2289. if(support_avx2())
  2290. return CORE_HASWELL;
  2291. #endif
  2292. if(support_avx())
  2293. return CORE_SANDYBRIDGE;
  2294. else
  2295. return CORE_NEHALEM;
  2296. }
  2297. case 12:
  2298. switch (model) {
  2299. case 6: // Arrow Lake
  2300. if(support_amx_bf16())
  2301. return CORE_SAPPHIRERAPIDS;
  2302. if(support_avx512_bf16())
  2303. return CORE_COOPERLAKE;
  2304. if(support_avx512())
  2305. return CORE_SKYLAKEX;
  2306. if(support_avx2())
  2307. return CORE_HASWELL;
  2308. if(support_avx())
  2309. return CORE_SANDYBRIDGE;
  2310. else
  2311. return CORE_NEHALEM;
  2312. }
  2313. }
  2314. case 15:
  2315. if (model <= 0x2) return CORE_NORTHWOOD;
  2316. else return CORE_PRESCOTT;
  2317. }
  2318. }
  2319. if (vendor == VENDOR_AMD){
  2320. if (family <= 0x5) return CORE_80486;
  2321. #if defined(__x86_64__) || defined(__amd64__)
  2322. if (family <= 0xe) return CORE_BARCELONA;
  2323. #else
  2324. if (family <= 0xe) return CORE_ATHLON;
  2325. #endif
  2326. if (family == 0xf){
  2327. if ((exfamily == 0) || (exfamily == 2)) return CORE_OPTERON;
  2328. else if (exfamily == 5) return CORE_BOBCAT;
  2329. else if (exfamily == 6) {
  2330. switch (model) {
  2331. case 1:
  2332. //AMD Bulldozer Opteron 6200 / Opteron 4200 / AMD FX-Series
  2333. if(support_avx())
  2334. return CORE_BULLDOZER;
  2335. else
  2336. return CORE_BARCELONA; //OS don't support AVX.
  2337. case 2: //AMD Piledriver
  2338. case 3: //AMD Richland
  2339. if(support_avx())
  2340. return CORE_PILEDRIVER;
  2341. else
  2342. return CORE_BARCELONA; //OS don't support AVX.
  2343. case 5: // New EXCAVATOR
  2344. if(support_avx())
  2345. return CORE_EXCAVATOR;
  2346. else
  2347. return CORE_BARCELONA; //OS don't support AVX.
  2348. case 0:
  2349. case 8:
  2350. switch(exmodel){
  2351. case 1: //AMD Trinity
  2352. if(support_avx())
  2353. return CORE_PILEDRIVER;
  2354. else
  2355. return CORE_BARCELONA; //OS don't support AVX.
  2356. case 3:
  2357. if(support_avx())
  2358. return CORE_STEAMROLLER;
  2359. else
  2360. return CORE_BARCELONA; //OS don't support AVX.
  2361. case 6:
  2362. if(support_avx())
  2363. return CORE_EXCAVATOR;
  2364. else
  2365. return CORE_BARCELONA; //OS don't support AVX.
  2366. }
  2367. break;
  2368. }
  2369. } else if (exfamily == 8 || exfamily == 10 || exfamily == 11) {
  2370. switch (model) {
  2371. case 1:
  2372. // AMD Ryzen
  2373. case 8:
  2374. // Ryzen 2
  2375. default:
  2376. // Matisse,Renoir Ryzen2 models
  2377. #ifndef NO_AVX512
  2378. if(support_avx512_bf16())
  2379. return CORE_COOPERLAKE;
  2380. if(support_avx512())
  2381. return CORE_SKYLAKEX;
  2382. #endif
  2383. if(support_avx())
  2384. #ifndef NO_AVX2
  2385. return CORE_ZEN;
  2386. #else
  2387. return CORE_SANDYBRIDGE; // Zen is closer in architecture to Sandy Bridge than to Excavator
  2388. #endif
  2389. else
  2390. return CORE_BARCELONA;
  2391. }
  2392. } else {
  2393. return CORE_BARCELONA;
  2394. }
  2395. }
  2396. }
  2397. if (vendor == VENDOR_HYGON){
  2398. if (family == 0xf){
  2399. if (exfamily == 9) {
  2400. if(support_avx())
  2401. #ifndef NO_AVX2
  2402. return CORE_ZEN;
  2403. #else
  2404. return CORE_SANDYBRIDGE; // closer in architecture to Sandy Bridge than to Excavator
  2405. #endif
  2406. else
  2407. return CORE_BARCELONA;
  2408. } else {
  2409. return CORE_BARCELONA;
  2410. }
  2411. }
  2412. }
  2413. if (vendor == VENDOR_CENTAUR) {
  2414. switch (family) {
  2415. case 0x6:
  2416. if (model == 0xf && stepping < 0xe)
  2417. return CORE_NANO;
  2418. return CORE_NEHALEM;
  2419. case 0x7:
  2420. switch (exmodel) {
  2421. case 5:
  2422. if (support_avx2())
  2423. return CORE_ZEN;
  2424. else
  2425. return CORE_DUNNINGTON;
  2426. default:
  2427. return CORE_NEHALEM;
  2428. }
  2429. default:
  2430. if (family >= 0x8)
  2431. return CORE_NEHALEM;
  2432. else
  2433. return CORE_VIAC3;
  2434. }
  2435. }
  2436. if (vendor == VENDOR_ZHAOXIN) {
  2437. switch (family) {
  2438. case 0x7:
  2439. switch (exmodel) {
  2440. case 5:
  2441. case 6:
  2442. if (support_avx2())
  2443. return CORE_ZEN;
  2444. else
  2445. return CORE_DUNNINGTON;
  2446. default:
  2447. return CORE_NEHALEM;
  2448. }
  2449. default:
  2450. return CORE_NEHALEM;
  2451. }
  2452. }
  2453. return CORE_UNKNOWN;
  2454. }
  2455. void get_cpuconfig(void){
  2456. cache_info_t info;
  2457. int features;
  2458. printf("#define %s\n", cpuname[get_cpuname()]);
  2459. if (get_coretype() != CORE_P5) {
  2460. get_cacheinfo(CACHE_INFO_L1_I, &info);
  2461. if (info.size > 0) {
  2462. printf("#define L1_CODE_SIZE %d\n", info.size * 1024);
  2463. printf("#define L1_CODE_ASSOCIATIVE %d\n", info.associative);
  2464. printf("#define L1_CODE_LINESIZE %d\n", info.linesize);
  2465. }
  2466. get_cacheinfo(CACHE_INFO_L1_D, &info);
  2467. if (info.size > 0) {
  2468. printf("#define L1_DATA_SIZE %d\n", info.size * 1024);
  2469. printf("#define L1_DATA_ASSOCIATIVE %d\n", info.associative);
  2470. printf("#define L1_DATA_LINESIZE %d\n", info.linesize);
  2471. }
  2472. get_cacheinfo(CACHE_INFO_L2, &info);
  2473. if (info.size > 0) {
  2474. printf("#define L2_SIZE %d\n", info.size * 1024);
  2475. printf("#define L2_ASSOCIATIVE %d\n", info.associative);
  2476. printf("#define L2_LINESIZE %d\n", info.linesize);
  2477. } else {
  2478. //fall back for some virtual machines.
  2479. printf("#define L2_SIZE 1048576\n");
  2480. printf("#define L2_ASSOCIATIVE 6\n");
  2481. printf("#define L2_LINESIZE 64\n");
  2482. }
  2483. get_cacheinfo(CACHE_INFO_L3, &info);
  2484. if (info.size > 0) {
  2485. printf("#define L3_SIZE %d\n", info.size * 1024);
  2486. printf("#define L3_ASSOCIATIVE %d\n", info.associative);
  2487. printf("#define L3_LINESIZE %d\n", info.linesize);
  2488. }
  2489. get_cacheinfo(CACHE_INFO_L1_ITB, &info);
  2490. if (info.size > 0) {
  2491. printf("#define ITB_SIZE %d\n", info.size * 1024);
  2492. printf("#define ITB_ASSOCIATIVE %d\n", info.associative);
  2493. printf("#define ITB_ENTRIES %d\n", info.linesize);
  2494. }
  2495. get_cacheinfo(CACHE_INFO_L1_DTB, &info);
  2496. if (info.size > 0) {
  2497. printf("#define DTB_SIZE %d\n", info.size * 1024);
  2498. printf("#define DTB_ASSOCIATIVE %d\n", info.associative);
  2499. printf("#define DTB_DEFAULT_ENTRIES %d\n", info.linesize);
  2500. } else {
  2501. //fall back for some virtual machines.
  2502. printf("#define DTB_DEFAULT_ENTRIES 32\n");
  2503. }
  2504. features = get_cputype(GET_FEATURE);
  2505. if (features & HAVE_CMOV ) printf("#define HAVE_CMOV\n");
  2506. if (features & HAVE_MMX ) printf("#define HAVE_MMX\n");
  2507. if (features & HAVE_SSE ) printf("#define HAVE_SSE\n");
  2508. if (features & HAVE_SSE2 ) printf("#define HAVE_SSE2\n");
  2509. if (features & HAVE_SSE3 ) printf("#define HAVE_SSE3\n");
  2510. if (features & HAVE_SSSE3) printf("#define HAVE_SSSE3\n");
  2511. if (features & HAVE_SSE4_1) printf("#define HAVE_SSE4_1\n");
  2512. if (features & HAVE_SSE4_2) printf("#define HAVE_SSE4_2\n");
  2513. if (features & HAVE_SSE4A) printf("#define HAVE_SSE4A\n");
  2514. if (features & HAVE_SSE5 ) printf("#define HAVE_SSSE5\n");
  2515. if (features & HAVE_AVX ) printf("#define HAVE_AVX\n");
  2516. if (features & HAVE_AVX2 ) printf("#define HAVE_AVX2\n");
  2517. if (features & HAVE_AVX512VL ) printf("#define HAVE_AVX512VL\n");
  2518. if (features & HAVE_AVX512BF16 ) printf("#define HAVE_AVX512BF16\n");
  2519. if (features & HAVE_AMXBF16 ) printf("#define HAVE_AMXBF16\n");
  2520. if (features & HAVE_3DNOWEX) printf("#define HAVE_3DNOWEX\n");
  2521. if (features & HAVE_3DNOW) printf("#define HAVE_3DNOW\n");
  2522. if (features & HAVE_FMA4 ) printf("#define HAVE_FMA4\n");
  2523. if (features & HAVE_FMA3 ) printf("#define HAVE_FMA3\n");
  2524. if (features & HAVE_CFLUSH) printf("#define HAVE_CFLUSH\n");
  2525. if (features & HAVE_HIT) printf("#define HAVE_HIT 1\n");
  2526. if (features & HAVE_MISALIGNSSE) printf("#define HAVE_MISALIGNSSE\n");
  2527. if (features & HAVE_128BITFPU) printf("#define HAVE_128BITFPU\n");
  2528. if (features & HAVE_FASTMOVU) printf("#define HAVE_FASTMOVU\n");
  2529. printf("#define NUM_SHAREDCACHE %d\n", get_cputype(GET_NUMSHARE) + 1);
  2530. printf("#define NUM_CORES %d\n", get_cputype(GET_NUMCORES) + 1);
  2531. features = get_coretype();
  2532. if (features > 0) printf("#define CORE_%s\n", corename[features]);
  2533. } else {
  2534. printf("#define DTB_DEFAULT_ENTRIES 16\n");
  2535. printf("#define L1_CODE_SIZE 8192\n");
  2536. printf("#define L1_DATA_SIZE 8192\n");
  2537. printf("#define L2_SIZE 0\n");
  2538. }
  2539. }
  2540. void get_architecture(void){
  2541. #ifndef __64BIT__
  2542. printf("X86");
  2543. #else
  2544. printf("X86_64");
  2545. #endif
  2546. }
  2547. void get_subarchitecture(void){
  2548. printf("%s", get_cpunamechar());
  2549. }
  2550. void get_subdirname(void){
  2551. #ifndef __64BIT__
  2552. printf("x86");
  2553. #else
  2554. printf("x86_64");
  2555. #endif
  2556. }
  2557. char *get_corename(void){
  2558. return corename[get_coretype()];
  2559. }
  2560. void get_libname(void){
  2561. printf("%s", corename_lower[get_coretype()]);
  2562. }
  2563. /* This if for Makefile */
  2564. void get_sse(void){
  2565. int features;
  2566. features = get_cputype(GET_FEATURE);
  2567. if (features & HAVE_MMX ) printf("HAVE_MMX=1\n");
  2568. if (features & HAVE_SSE ) printf("HAVE_SSE=1\n");
  2569. if (features & HAVE_SSE2 ) printf("HAVE_SSE2=1\n");
  2570. if (features & HAVE_SSE3 ) printf("HAVE_SSE3=1\n");
  2571. if (features & HAVE_SSSE3) printf("HAVE_SSSE3=1\n");
  2572. if (features & HAVE_SSE4_1) printf("HAVE_SSE4_1=1\n");
  2573. if (features & HAVE_SSE4_2) printf("HAVE_SSE4_2=1\n");
  2574. if (features & HAVE_SSE4A) printf("HAVE_SSE4A=1\n");
  2575. if (features & HAVE_SSE5 ) printf("HAVE_SSSE5=1\n");
  2576. if (features & HAVE_AVX ) printf("HAVE_AVX=1\n");
  2577. if (features & HAVE_AVX2 ) printf("HAVE_AVX2=1\n");
  2578. if (features & HAVE_AVX512VL ) printf("HAVE_AVX512VL=1\n");
  2579. if (features & HAVE_AVX512BF16 ) printf("HAVE_AVX512BF16=1\n");
  2580. if (features & HAVE_AMXBF16 ) printf("HAVE_AMXBF16=1\n");
  2581. if (features & HAVE_3DNOWEX) printf("HAVE_3DNOWEX=1\n");
  2582. if (features & HAVE_3DNOW) printf("HAVE_3DNOW=1\n");
  2583. if (features & HAVE_FMA4 ) printf("HAVE_FMA4=1\n");
  2584. if (features & HAVE_FMA3 ) printf("HAVE_FMA3=1\n");
  2585. }
  2586. //}