PageRenderTime 28ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/libphobos/libdruntime/core/cpuid.d

https://gitlab.com/4144/gcc
D | 1132 lines | 767 code | 61 blank | 304 comment | 137 complexity | 2d44e8e83ae1812a9a285566dffb9efa MD5 | raw file
  1. /**
  2. * Identify the characteristics of the host CPU, providing information
  3. * about cache sizes and assembly optimisation hints. This module is
  4. * provided primarily for assembly language programmers.
  5. *
  6. * References:
  7. * Some of this information was extremely difficult to track down. Some of the
  8. * documents below were found only in cached versions stored by search engines!
  9. * This code relies on information found in:
  10. *
  11. * $(UL
  12. * $(LI "Intel(R) 64 and IA-32 Architectures Software Developers Manual,
  13. * Volume 2A: Instruction Set Reference, A-M" (2007).
  14. * )
  15. * $(LI "AMD CPUID Specification", Advanced Micro Devices, Rev 2.28 (2008).
  16. * )
  17. * $(LI "AMD Processor Recognition Application Note For Processors Prior to AMD
  18. * Family 0Fh Processors", Advanced Micro Devices, Rev 3.13 (2005).
  19. * )
  20. * $(LI "AMD Geode(TM) GX Processors Data Book",
  21. * Advanced Micro Devices, Publication ID 31505E, (2005).
  22. * )
  23. * $(LI "AMD K6 Processor Code Optimisation", Advanced Micro Devices, Rev D (2000).
  24. * )
  25. * $(LI "Application note 106: Software Customization for the 6x86 Family",
  26. * Cyrix Corporation, Rev 1.5 (1998)
  27. * )
  28. * $(LI $(LINK http://www.datasheetcatalog.org/datasheet/nationalsemiconductor/GX1.pdf))
  29. * $(LI "Geode(TM) GX1 Processor Series Low Power Integrated X86 Solution",
  30. * National Semiconductor, (2002)
  31. * )
  32. * $(LI "The VIA Isaiah Architecture", G. Glenn Henry, Centaur Technology, Inc (2008).
  33. * )
  34. * $(LI $(LINK http://www.sandpile.org/ia32/cpuid.htm))
  35. * $(LI $(LINK http://www.akkadia.org/drepper/cpumemory.pdf))
  36. * $(LI "What every programmer should know about memory",
  37. * Ulrich Depper, Red Hat, Inc., (2007).
  38. * )
  39. * $(LI "CPU Identification by the Windows Kernel", G. Chappell (2009).
  40. * $(LINK http://www.geoffchappell.com/viewer.htm?doc=studies/windows/km/cpu/cx8.htm)
  41. * )
  42. * $(LI "Intel(R) Processor Identification and the CPUID Instruction, Application
  43. * Note 485" (2009).
  44. * )
  45. * )
  46. *
  47. * Bugs: Currently only works on x86 and Itanium CPUs.
  48. * Many processors have bugs in their microcode for the CPUID instruction,
  49. * so sometimes the cache information may be incorrect.
  50. *
  51. * Copyright: Copyright Don Clugston 2007 - 2009.
  52. * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
  53. * Authors: Don Clugston, Tomas Lindquist Olsen <tomas@famolsen.dk>
  54. * Source: $(DRUNTIMESRC core/_cpuid.d)
  55. */
  56. module core.cpuid;
  57. @trusted:
  58. nothrow:
  59. @nogc:
  60. // If optimizing for a particular processor, it is generally better
  61. // to identify based on features rather than model. NOTE: Normally
  62. // it's only worthwhile to optimise for the latest Intel and AMD CPU,
  63. // with a backup for other CPUs.
  64. // Pentium -- preferPentium1()
  65. // PMMX -- + mmx()
  66. // PPro -- default
  67. // PII -- + mmx()
  68. // PIII -- + mmx() + sse()
  69. // PentiumM -- + mmx() + sse() + sse2()
  70. // Pentium4 -- preferPentium4()
  71. // PentiumD -- + isX86_64()
  72. // Core2 -- default + isX86_64()
  73. // AMD K5 -- preferPentium1()
  74. // AMD K6 -- + mmx()
  75. // AMD K6-II -- + mmx() + 3dnow()
  76. // AMD K7 -- preferAthlon()
  77. // AMD K8 -- + sse2()
  78. // AMD K10 -- + isX86_64()
  79. // Cyrix 6x86 -- preferPentium1()
  80. // 6x86MX -- + mmx()
  81. // GDC support uses extended inline assembly:
  82. // https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html (general information and hints)
  83. // https://gcc.gnu.org/onlinedocs/gcc/Simple-Constraints.html (binding variables to registers)
  84. // https://gcc.gnu.org/onlinedocs/gcc/Machine-Constraints.html (x86 specific register short names)
  85. public:
  86. /// Cache size and behaviour
  87. struct CacheInfo
  88. {
  89. /// Size of the cache, in kilobytes, per CPU.
  90. /// For L1 unified (data + code) caches, this size is half the physical size.
  91. /// (we don't halve it for larger sizes, since normally
  92. /// data size is much greater than code size for critical loops).
  93. size_t size;
  94. /// Number of ways of associativity, eg:
  95. /// $(UL
  96. /// $(LI 1 = direct mapped)
  97. /// $(LI 2 = 2-way set associative)
  98. /// $(LI 3 = 3-way set associative)
  99. /// $(LI ubyte.max = fully associative)
  100. /// )
  101. ubyte associativity;
  102. /// Number of bytes read into the cache when a cache miss occurs.
  103. uint lineSize;
  104. }
  105. public:
  106. /// $(RED Scheduled for deprecation. Please use $(D dataCaches) instead.)
  107. // Note: When we deprecate it, we simply make it private.
  108. __gshared CacheInfo[5] datacache;
  109. @property pure
  110. {
  111. /// The data caches. If there are fewer than 5 physical caches levels,
  112. /// the remaining levels are set to size_t.max (== entire memory space)
  113. const(CacheInfo)[5] dataCaches() { return _dataCaches; }
  114. /// Returns vendor string, for display purposes only.
  115. /// Do NOT use this to determine features!
  116. /// Note that some CPUs have programmable vendorIDs.
  117. string vendor() {return _vendor;}
  118. /// Returns processor string, for display purposes only
  119. string processor() {return _processor;}
  120. /// Does it have an x87 FPU on-chip?
  121. bool x87onChip() {return _x87onChip;}
  122. /// Is MMX supported?
  123. bool mmx() {return _mmx;}
  124. /// Is SSE supported?
  125. bool sse() {return _sse;}
  126. /// Is SSE2 supported?
  127. bool sse2() {return _sse2;}
  128. /// Is SSE3 supported?
  129. bool sse3() {return _sse3;}
  130. /// Is SSSE3 supported?
  131. bool ssse3() {return _ssse3;}
  132. /// Is SSE4.1 supported?
  133. bool sse41() {return _sse41;}
  134. /// Is SSE4.2 supported?
  135. bool sse42() {return _sse42;}
  136. /// Is SSE4a supported?
  137. bool sse4a() {return _sse4a;}
  138. /// Is AES supported
  139. bool aes() {return _aes;}
  140. /// Is pclmulqdq supported
  141. bool hasPclmulqdq() {return _hasPclmulqdq;}
  142. /// Is rdrand supported
  143. bool hasRdrand() {return _hasRdrand;}
  144. /// Is AVX supported
  145. bool avx() {return _avx;}
  146. /// Is VEX-Encoded AES supported
  147. bool vaes() {return _vaes;}
  148. /// Is vpclmulqdq supported
  149. bool hasVpclmulqdq(){return _hasVpclmulqdq; }
  150. /// Is FMA supported
  151. bool fma() {return _fma;}
  152. /// Is FP16C supported
  153. bool fp16c() {return _fp16c;}
  154. /// Is AVX2 supported
  155. bool avx2() {return _avx2;}
  156. /// Is HLE (hardware lock elision) supported
  157. bool hle() {return _hle;}
  158. /// Is RTM (restricted transactional memory) supported
  159. bool rtm() {return _rtm;}
  160. /// Is rdseed supported
  161. bool hasRdseed() {return _hasRdseed;}
  162. /// Is SHA supported
  163. bool hasSha() {return _hasSha;}
  164. /// Is AMD 3DNOW supported?
  165. bool amd3dnow() {return _amd3dnow;}
  166. /// Is AMD 3DNOW Ext supported?
  167. bool amd3dnowExt() {return _amd3dnowExt;}
  168. /// Are AMD extensions to MMX supported?
  169. bool amdMmx() {return _amdMmx;}
  170. /// Is fxsave/fxrstor supported?
  171. bool hasFxsr() {return _hasFxsr;}
  172. /// Is cmov supported?
  173. bool hasCmov() {return _hasCmov;}
  174. /// Is rdtsc supported?
  175. bool hasRdtsc() {return _hasRdtsc;}
  176. /// Is cmpxchg8b supported?
  177. bool hasCmpxchg8b() {return _hasCmpxchg8b;}
  178. /// Is cmpxchg8b supported?
  179. bool hasCmpxchg16b() {return _hasCmpxchg16b;}
  180. /// Is SYSENTER/SYSEXIT supported?
  181. bool hasSysEnterSysExit() {return _hasSysEnterSysExit;}
  182. /// Is 3DNow prefetch supported?
  183. bool has3dnowPrefetch() {return _has3dnowPrefetch;}
  184. /// Are LAHF and SAHF supported in 64-bit mode?
  185. bool hasLahfSahf() {return _hasLahfSahf;}
  186. /// Is POPCNT supported?
  187. bool hasPopcnt() {return _hasPopcnt;}
  188. /// Is LZCNT supported?
  189. bool hasLzcnt() {return _hasLzcnt;}
  190. /// Is this an Intel64 or AMD 64?
  191. bool isX86_64() {return _isX86_64;}
  192. /// Is this an IA64 (Itanium) processor?
  193. bool isItanium() { return _isItanium; }
  194. /// Is hyperthreading supported?
  195. bool hyperThreading() { return _hyperThreading; }
  196. /// Returns number of threads per CPU
  197. uint threadsPerCPU() {return _threadsPerCPU;}
  198. /// Returns number of cores in CPU
  199. uint coresPerCPU() {return _coresPerCPU;}
  200. /// Optimisation hints for assembly code.
  201. ///
  202. /// For forward compatibility, the CPU is compared against different
  203. /// microarchitectures. For 32-bit x86, comparisons are made against
  204. /// the Intel PPro/PII/PIII/PM family.
  205. ///
  206. /// The major 32-bit x86 microarchitecture 'dynasties' have been:
  207. ///
  208. /// $(UL
  209. /// $(LI Intel P6 (PentiumPro, PII, PIII, PM, Core, Core2). )
  210. /// $(LI AMD Athlon (K7, K8, K10). )
  211. /// $(LI Intel NetBurst (Pentium 4, Pentium D). )
  212. /// $(LI In-order Pentium (Pentium1, PMMX, Atom) )
  213. /// )
  214. ///
  215. /// Other early CPUs (Nx586, AMD K5, K6, Centaur C3, Transmeta,
  216. /// Cyrix, Rise) were mostly in-order.
  217. ///
  218. /// Some new processors do not fit into the existing categories:
  219. ///
  220. /// $(UL
  221. /// $(LI Intel Atom 230/330 (family 6, model 0x1C) is an in-order core. )
  222. /// $(LI Centaur Isiah = VIA Nano (family 6, model F) is an out-of-order core. )
  223. /// )
  224. ///
  225. /// Within each dynasty, the optimisation techniques are largely
  226. /// identical (eg, use instruction pairing for group 4). Major
  227. /// instruction set improvements occur within each dynasty.
  228. /// Does this CPU perform better on AMD K7 code than PentiumPro..Core2 code?
  229. bool preferAthlon() { return _preferAthlon; }
  230. /// Does this CPU perform better on Pentium4 code than PentiumPro..Core2 code?
  231. bool preferPentium4() { return _preferPentium4; }
  232. /// Does this CPU perform better on Pentium I code than Pentium Pro code?
  233. bool preferPentium1() { return _preferPentium1; }
  234. }
  235. private immutable
  236. {
  237. /* These exist as immutables so that the query property functions can
  238. * be backwards compatible with code that called them with ().
  239. * Also, immutables can only be set by the static this().
  240. */
  241. const(CacheInfo)[5] _dataCaches;
  242. string _vendor;
  243. string _processor;
  244. bool _x87onChip;
  245. bool _mmx;
  246. bool _sse;
  247. bool _sse2;
  248. bool _sse3;
  249. bool _ssse3;
  250. bool _sse41;
  251. bool _sse42;
  252. bool _sse4a;
  253. bool _aes;
  254. bool _hasPclmulqdq;
  255. bool _hasRdrand;
  256. bool _avx;
  257. bool _vaes;
  258. bool _hasVpclmulqdq;
  259. bool _fma;
  260. bool _fp16c;
  261. bool _avx2;
  262. bool _hle;
  263. bool _rtm;
  264. bool _hasRdseed;
  265. bool _hasSha;
  266. bool _amd3dnow;
  267. bool _amd3dnowExt;
  268. bool _amdMmx;
  269. bool _hasFxsr;
  270. bool _hasCmov;
  271. bool _hasRdtsc;
  272. bool _hasCmpxchg8b;
  273. bool _hasCmpxchg16b;
  274. bool _hasSysEnterSysExit;
  275. bool _has3dnowPrefetch;
  276. bool _hasLahfSahf;
  277. bool _hasPopcnt;
  278. bool _hasLzcnt;
  279. bool _isX86_64;
  280. bool _isItanium;
  281. bool _hyperThreading;
  282. uint _threadsPerCPU;
  283. uint _coresPerCPU;
  284. bool _preferAthlon;
  285. bool _preferPentium4;
  286. bool _preferPentium1;
  287. }
  288. __gshared:
  289. // All these values are set only once, and never subsequently modified.
  290. public:
  291. /// $(RED Warning: This field will be turned into a property in a future release.)
  292. ///
  293. /// Processor type (vendor-dependent).
  294. /// This should be visible ONLY for display purposes.
  295. uint stepping, model, family;
  296. /// $(RED This field has been deprecated. Please use $(D cacheLevels) instead.)
  297. uint numCacheLevels = 1;
  298. /// The number of cache levels in the CPU.
  299. @property uint cacheLevels() { return numCacheLevels; }
  300. private:
  301. struct CpuFeatures
  302. {
  303. bool probablyIntel; // true = _probably_ an Intel processor, might be faking
  304. bool probablyAMD; // true = _probably_ an AMD processor
  305. string processorName;
  306. char [12] vendorID;
  307. char [48] processorNameBuffer;
  308. uint features = 0; // mmx, sse, sse2, hyperthreading, etc
  309. uint miscfeatures = 0; // sse3, etc.
  310. uint extfeatures = 0; // HLE, AVX2, RTM, etc.
  311. uint amdfeatures = 0; // 3DNow!, mmxext, etc
  312. uint amdmiscfeatures = 0; // sse4a, sse5, svm, etc
  313. ulong xfeatures = 0; // XFEATURES_ENABLED_MASK
  314. uint maxCores = 1;
  315. uint maxThreads = 1;
  316. }
  317. CpuFeatures cpuFeatures;
  318. /* Hide from the optimizer where cf (a register) is coming from, so that
  319. * cf doesn't get "optimized away". The idea is to reference
  320. * the global data through cf so not so many fixups are inserted
  321. * into the executable image.
  322. */
  323. CpuFeatures* getCpuFeatures() @nogc nothrow
  324. {
  325. pragma(inline, false);
  326. return &cpuFeatures;
  327. }
  328. // Note that this may indicate multi-core rather than hyperthreading.
  329. @property bool hyperThreadingBit() { return (cpuFeatures.features&HTT_BIT)!=0;}
  330. // feature flags CPUID1_EDX
  331. enum : uint
  332. {
  333. FPU_BIT = 1,
  334. TIMESTAMP_BIT = 1<<4, // rdtsc
  335. MDSR_BIT = 1<<5, // RDMSR/WRMSR
  336. CMPXCHG8B_BIT = 1<<8,
  337. SYSENTERSYSEXIT_BIT = 1<<11,
  338. CMOV_BIT = 1<<15,
  339. MMX_BIT = 1<<23,
  340. FXSR_BIT = 1<<24,
  341. SSE_BIT = 1<<25,
  342. SSE2_BIT = 1<<26,
  343. HTT_BIT = 1<<28,
  344. IA64_BIT = 1<<30
  345. }
  346. // feature flags misc CPUID1_ECX
  347. enum : uint
  348. {
  349. SSE3_BIT = 1,
  350. PCLMULQDQ_BIT = 1<<1, // from AVX
  351. MWAIT_BIT = 1<<3,
  352. SSSE3_BIT = 1<<9,
  353. FMA_BIT = 1<<12, // from AVX
  354. CMPXCHG16B_BIT = 1<<13,
  355. SSE41_BIT = 1<<19,
  356. SSE42_BIT = 1<<20,
  357. POPCNT_BIT = 1<<23,
  358. AES_BIT = 1<<25, // AES instructions from AVX
  359. OSXSAVE_BIT = 1<<27, // Used for AVX
  360. AVX_BIT = 1<<28,
  361. FP16C_BIT = 1<<29,
  362. RDRAND_BIT = 1<<30,
  363. }
  364. // Feature flags for cpuid.{EAX = 7, ECX = 0}.EBX.
  365. enum : uint
  366. {
  367. FSGSBASE_BIT = 1 << 0,
  368. BMI1_BIT = 1 << 3,
  369. HLE_BIT = 1 << 4,
  370. AVX2_BIT = 1 << 5,
  371. SMEP_BIT = 1 << 7,
  372. BMI2_BIT = 1 << 8,
  373. ERMS_BIT = 1 << 9,
  374. INVPCID_BIT = 1 << 10,
  375. RTM_BIT = 1 << 11,
  376. RDSEED_BIT = 1 << 18,
  377. SHA_BIT = 1 << 29,
  378. }
  379. // feature flags XFEATURES_ENABLED_MASK
  380. enum : ulong
  381. {
  382. XF_FP_BIT = 0x1,
  383. XF_SSE_BIT = 0x2,
  384. XF_YMM_BIT = 0x4,
  385. }
  386. // AMD feature flags CPUID80000001_EDX
  387. enum : uint
  388. {
  389. AMD_MMX_BIT = 1<<22,
  390. // FXR_OR_CYRIXMMX_BIT = 1<<24, // Cyrix/NS: 6x86MMX instructions.
  391. FFXSR_BIT = 1<<25,
  392. PAGE1GB_BIT = 1<<26, // support for 1GB pages
  393. RDTSCP_BIT = 1<<27,
  394. AMD64_BIT = 1<<29,
  395. AMD_3DNOW_EXT_BIT = 1<<30,
  396. AMD_3DNOW_BIT = 1<<31
  397. }
  398. // AMD misc feature flags CPUID80000001_ECX
  399. enum : uint
  400. {
  401. LAHFSAHF_BIT = 1,
  402. LZCNT_BIT = 1<<5,
  403. SSE4A_BIT = 1<<6,
  404. AMD_3DNOW_PREFETCH_BIT = 1<<8,
  405. }
  406. version (GNU) {
  407. version (X86)
  408. enum supportedX86 = true;
  409. else version (X86_64)
  410. enum supportedX86 = true;
  411. else
  412. enum supportedX86 = false;
  413. } else version (D_InlineAsm_X86) {
  414. enum supportedX86 = true;
  415. } else version (D_InlineAsm_X86_64) {
  416. enum supportedX86 = true;
  417. } else {
  418. enum supportedX86 = false;
  419. }
  420. static if (supportedX86) {
  421. // Note that this code will also work for Itanium in x86 mode.
  422. __gshared uint max_cpuid, max_extended_cpuid;
  423. // CPUID2: "cache and tlb information"
  424. void getcacheinfoCPUID2()
  425. {
  426. // We are only interested in the data caches
  427. void decipherCpuid2(ubyte x) @nogc nothrow {
  428. if (x==0) return;
  429. // Values from http://www.sandpile.org/ia32/cpuid.htm.
  430. // Includes Itanium and non-Intel CPUs.
  431. //
  432. static immutable ubyte [63] ids = [
  433. 0x0A, 0x0C, 0x0D, 0x2C, 0x60, 0x0E, 0x66, 0x67, 0x68,
  434. // level 2 cache
  435. 0x41, 0x42, 0x43, 0x44, 0x45, 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7F,
  436. 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x49, 0x4E,
  437. 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x48, 0x80, 0x81,
  438. // level 3 cache
  439. 0x22, 0x23, 0x25, 0x29, 0x46, 0x47, 0x4A, 0x4B, 0x4C, 0x4D,
  440. 0xD0, 0xD1, 0xD2, 0xD6, 0xD7, 0xD8, 0xDC, 0xDD, 0xDE,
  441. 0xE2, 0xE3, 0xE4, 0xEA, 0xEB, 0xEC
  442. ];
  443. static immutable uint [63] sizes = [
  444. 8, 16, 16, 64, 16, 24, 8, 16, 32,
  445. 128, 256, 512, 1024, 2048, 1024, 128, 256, 512, 1024, 2048, 512,
  446. 256, 512, 1024, 2048, 512, 1024, 4096, 6*1024,
  447. 128, 192, 128, 256, 384, 512, 3072, 512, 128,
  448. 512, 1024, 2048, 4096, 4096, 8192, 6*1024, 8192, 12*1024, 16*1024,
  449. 512, 1024, 2048, 1024, 2048, 4096, 1024+512, 3*1024, 6*1024,
  450. 2*1024, 4*1024, 8*1024, 12*1024, 28*1024, 24*1024
  451. ];
  452. // CPUBUG: Pentium M reports 0x2C but tests show it is only 4-way associative
  453. static immutable ubyte [63] ways = [
  454. 2, 4, 4, 8, 8, 6, 4, 4, 4,
  455. 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 2,
  456. 8, 8, 8, 8, 4, 8, 16, 24,
  457. 4, 6, 2, 4, 6, 4, 12, 8, 8,
  458. 4, 8, 8, 8, 4, 8, 12, 16, 12, 16,
  459. 4, 4, 4, 8, 8, 8, 12, 12, 12,
  460. 16, 16, 16, 24, 24, 24
  461. ];
  462. enum { FIRSTDATA2 = 8, FIRSTDATA3 = 28+9 }
  463. for (size_t i=0; i< ids.length; ++i) {
  464. if (x==ids[i]) {
  465. int level = i< FIRSTDATA2 ? 0: i<FIRSTDATA3 ? 1 : 2;
  466. if (x==0x49 && family==0xF && model==0x6) level=2;
  467. datacache[level].size=sizes[i];
  468. datacache[level].associativity=ways[i];
  469. if (level == 3 || x==0x2C || x==0x0D || (x>=0x48 && x<=0x80)
  470. || x==0x86 || x==0x87
  471. || (x>=0x66 && x<=0x68) || (x>=0x39 && x<=0x3E)){
  472. datacache[level].lineSize = 64;
  473. } else datacache[level].lineSize = 32;
  474. }
  475. }
  476. }
  477. uint[4] a;
  478. bool firstTime = true;
  479. // On a multi-core system, this could theoretically fail, but it's only used
  480. // for old single-core CPUs.
  481. uint numinfos = 1;
  482. do {
  483. version (GNU) asm pure nothrow @nogc {
  484. "cpuid" : "=a" a[0], "=b" a[1], "=c" a[2], "=d" a[3] : "a" 2;
  485. } else asm pure nothrow @nogc {
  486. mov EAX, 2;
  487. cpuid;
  488. mov a, EAX;
  489. mov a+4, EBX;
  490. mov a+8, ECX;
  491. mov a+12, EDX;
  492. }
  493. if (firstTime) {
  494. if (a[0]==0x0000_7001 && a[3]==0x80 && a[1]==0 && a[2]==0) {
  495. // Cyrix MediaGX MMXEnhanced returns: EAX= 00007001, EDX=00000080.
  496. // These are NOT standard Intel values
  497. // (TLB = 32 entry, 4 way associative, 4K pages)
  498. // (L1 cache = 16K, 4way, linesize16)
  499. datacache[0].size=8;
  500. datacache[0].associativity=4;
  501. datacache[0].lineSize=16;
  502. return;
  503. }
  504. // lsb of a is how many times to loop.
  505. numinfos = a[0] & 0xFF;
  506. // and otherwise it should be ignored
  507. a[0] &= 0xFFFF_FF00;
  508. firstTime = false;
  509. }
  510. for (int c=0; c<4;++c) {
  511. // high bit set == no info.
  512. if (a[c] & 0x8000_0000) continue;
  513. decipherCpuid2(cast(ubyte)(a[c] & 0xFF));
  514. decipherCpuid2(cast(ubyte)((a[c]>>8) & 0xFF));
  515. decipherCpuid2(cast(ubyte)((a[c]>>16) & 0xFF));
  516. decipherCpuid2(cast(ubyte)((a[c]>>24) & 0xFF));
  517. }
  518. } while (--numinfos);
  519. }
  520. // CPUID4: "Deterministic cache parameters" leaf
  521. void getcacheinfoCPUID4()
  522. {
  523. int cachenum = 0;
  524. for (;;) {
  525. uint a, b, number_of_sets;
  526. version (GNU) asm pure nothrow @nogc {
  527. "cpuid" : "=a" a, "=b" b, "=c" number_of_sets : "a" 4, "c" cachenum : "edx";
  528. } else asm pure nothrow @nogc {
  529. mov EAX, 4;
  530. mov ECX, cachenum;
  531. cpuid;
  532. mov a, EAX;
  533. mov b, EBX;
  534. mov number_of_sets, ECX;
  535. }
  536. ++cachenum;
  537. if ((a&0x1F)==0) break; // no more caches
  538. immutable uint numthreads = ((a>>14) & 0xFFF) + 1;
  539. immutable uint numcores = ((a>>26) & 0x3F) + 1;
  540. if (numcores > cpuFeatures.maxCores) cpuFeatures.maxCores = numcores;
  541. if ((a&0x1F)!=1 && ((a&0x1F)!=3)) continue; // we only want data & unified caches
  542. ++number_of_sets;
  543. immutable ubyte level = cast(ubyte)(((a>>5)&7)-1);
  544. if (level > datacache.length) continue; // ignore deep caches
  545. datacache[level].associativity = a & 0x200 ? ubyte.max :cast(ubyte)((b>>22)+1);
  546. datacache[level].lineSize = (b & 0xFFF)+ 1; // system coherency line size
  547. immutable uint line_partitions = ((b >> 12)& 0x3FF) + 1;
  548. // Size = number of sets * associativity * cachelinesize * linepartitions
  549. // and must convert to Kb, also dividing by the number of hyperthreads using this cache.
  550. immutable ulong sz = (datacache[level].associativity< ubyte.max)? number_of_sets *
  551. datacache[level].associativity : number_of_sets;
  552. datacache[level].size = cast(size_t)(
  553. (sz * datacache[level].lineSize * line_partitions ) / (numthreads *1024));
  554. if (level == 0 && (a&0xF)==3) {
  555. // Halve the size for unified L1 caches
  556. datacache[level].size/=2;
  557. }
  558. }
  559. }
  560. // CPUID8000_0005 & 6
  561. void getAMDcacheinfo()
  562. {
  563. uint dummy, c5, c6, d6;
  564. version (GNU) asm pure nothrow @nogc {
  565. "cpuid" : "=a" dummy, "=c" c5 : "a" 0x8000_0005 : "ebx", "edx";
  566. } else asm pure nothrow @nogc {
  567. mov EAX, 0x8000_0005; // L1 cache
  568. cpuid;
  569. // EAX has L1_TLB_4M.
  570. // EBX has L1_TLB_4K
  571. // EDX has L1 instruction cache
  572. mov c5, ECX;
  573. }
  574. datacache[0].size = ( (c5>>24) & 0xFF);
  575. datacache[0].associativity = cast(ubyte)( (c5 >> 16) & 0xFF);
  576. datacache[0].lineSize = c5 & 0xFF;
  577. if (max_extended_cpuid >= 0x8000_0006) {
  578. // AMD K6-III or K6-2+ or later.
  579. ubyte numcores = 1;
  580. if (max_extended_cpuid >= 0x8000_0008) {
  581. version (GNU) asm pure nothrow @nogc {
  582. "cpuid" : "=a" dummy, "=c" numcores : "a" 0x8000_0008 : "ebx", "edx";
  583. } else asm pure nothrow @nogc {
  584. mov EAX, 0x8000_0008;
  585. cpuid;
  586. mov numcores, CL;
  587. }
  588. ++numcores;
  589. if (numcores>cpuFeatures.maxCores) cpuFeatures.maxCores = numcores;
  590. }
  591. version (GNU) asm pure nothrow @nogc {
  592. "cpuid" : "=a" dummy, "=c" c6, "=d" d6 : "a" 0x8000_0006 : "ebx";
  593. } else asm pure nothrow @nogc {
  594. mov EAX, 0x8000_0006; // L2/L3 cache
  595. cpuid;
  596. mov c6, ECX; // L2 cache info
  597. mov d6, EDX; // L3 cache info
  598. }
  599. static immutable ubyte [] assocmap = [ 0, 1, 2, 0, 4, 0, 8, 0, 16, 0, 32, 48, 64, 96, 128, 0xFF ];
  600. datacache[1].size = (c6>>16) & 0xFFFF;
  601. datacache[1].associativity = assocmap[(c6>>12)&0xF];
  602. datacache[1].lineSize = c6 & 0xFF;
  603. // The L3 cache value is TOTAL, not per core.
  604. datacache[2].size = ((d6>>18)*512)/numcores; // could be up to 2 * this, -1.
  605. datacache[2].associativity = assocmap[(d6>>12)&0xF];
  606. datacache[2].lineSize = d6 & 0xFF;
  607. }
  608. }
  609. // For Intel CoreI7 and later, use function 0x0B
  610. // to determine number of processors.
  611. void getCpuInfo0B()
  612. {
  613. int level=0;
  614. int threadsPerCore;
  615. uint a, b, c, d;
  616. do {
  617. version (GNU) asm pure nothrow @nogc {
  618. "cpuid" : "=a" a, "=b" b, "=c" c, "=d" d : "a" 0x0B, "c" level;
  619. } else asm pure nothrow @nogc {
  620. mov EAX, 0x0B;
  621. mov ECX, level;
  622. cpuid;
  623. mov a, EAX;
  624. mov b, EBX;
  625. mov c, ECX;
  626. mov d, EDX;
  627. }
  628. if (b!=0) {
  629. // I'm not sure about this. The docs state that there
  630. // are 2 hyperthreads per core if HT is factory enabled.
  631. if (level==0)
  632. threadsPerCore = b & 0xFFFF;
  633. else if (level==1) {
  634. cpuFeatures.maxThreads = b & 0xFFFF;
  635. cpuFeatures.maxCores = cpuFeatures.maxThreads / threadsPerCore;
  636. }
  637. }
  638. ++level;
  639. } while (a!=0 || b!=0);
  640. }
  641. void cpuidX86()
  642. {
  643. auto cf = getCpuFeatures();
  644. uint a, b, c, d;
  645. uint* venptr = cast(uint*)cf.vendorID.ptr;
  646. version (GNU)
  647. {
  648. asm pure nothrow @nogc { "cpuid" : "=a" max_cpuid, "=b" venptr[0], "=d" venptr[1], "=c" venptr[2] : "a" 0; }
  649. asm pure nothrow @nogc { "cpuid" : "=a" max_extended_cpuid : "a" 0x8000_0000 : "ebx", "ecx", "edx"; }
  650. }
  651. else
  652. {
  653. uint a2;
  654. version (D_InlineAsm_X86)
  655. {
  656. asm pure nothrow @nogc {
  657. mov EAX, 0;
  658. cpuid;
  659. mov a, EAX;
  660. mov EAX, venptr;
  661. mov [EAX], EBX;
  662. mov [EAX + 4], EDX;
  663. mov [EAX + 8], ECX;
  664. }
  665. }
  666. else version (D_InlineAsm_X86_64)
  667. {
  668. asm pure nothrow @nogc {
  669. mov EAX, 0;
  670. cpuid;
  671. mov a, EAX;
  672. mov RAX, venptr;
  673. mov [RAX], EBX;
  674. mov [RAX + 4], EDX;
  675. mov [RAX + 8], ECX;
  676. }
  677. }
  678. asm pure nothrow @nogc {
  679. mov EAX, 0x8000_0000;
  680. cpuid;
  681. mov a2, EAX;
  682. }
  683. max_cpuid = a;
  684. max_extended_cpuid = a2;
  685. }
  686. cf.probablyIntel = cf.vendorID == "GenuineIntel";
  687. cf.probablyAMD = cf.vendorID == "AuthenticAMD";
  688. uint apic = 0; // brand index, apic id
  689. version (GNU) asm pure nothrow @nogc {
  690. "cpuid" : "=a" a, "=b" apic, "=c" cf.miscfeatures, "=d" cf.features : "a" 1;
  691. } else {
  692. asm pure nothrow @nogc {
  693. mov EAX, 1; // model, stepping
  694. cpuid;
  695. mov a, EAX;
  696. mov apic, EBX;
  697. mov c, ECX;
  698. mov d, EDX;
  699. }
  700. cf.features = d;
  701. cf.miscfeatures = c;
  702. }
  703. stepping = a & 0xF;
  704. immutable uint fbase = (a >> 8) & 0xF;
  705. immutable uint mbase = (a >> 4) & 0xF;
  706. family = ((fbase == 0xF) || (fbase == 0)) ? fbase + (a >> 20) & 0xFF : fbase;
  707. model = ((fbase == 0xF) || (fbase == 6 && cf.probablyIntel) ) ?
  708. mbase + ((a >> 12) & 0xF0) : mbase;
  709. if (max_cpuid >= 7)
  710. {
  711. version (GNU) asm pure nothrow @nogc {
  712. "cpuid" : "=a" a, "=b" cf.extfeatures, "=c" c : "a" 7, "c" 0 : "edx";
  713. } else {
  714. uint ext;
  715. asm pure nothrow @nogc {
  716. mov EAX, 7; // Structured extended feature leaf.
  717. mov ECX, 0; // Main leaf.
  718. cpuid;
  719. mov ext, EBX; // HLE, AVX2, RTM, etc.
  720. }
  721. cf.extfeatures = ext;
  722. }
  723. }
  724. if (cf.miscfeatures & OSXSAVE_BIT)
  725. {
  726. version (GNU) asm pure nothrow @nogc {
  727. "xgetbv" : "=a" a, "=d" d : "c" 0;
  728. } else asm pure nothrow @nogc {
  729. mov ECX, 0;
  730. xgetbv;
  731. mov d, EDX;
  732. mov a, EAX;
  733. }
  734. cf.xfeatures = cast(ulong)d << 32 | a;
  735. }
  736. cf.amdfeatures = 0;
  737. cf.amdmiscfeatures = 0;
  738. if (max_extended_cpuid >= 0x8000_0001) {
  739. version (GNU) asm pure nothrow @nogc {
  740. "cpuid" : "=a" a, "=c" cf.amdmiscfeatures, "=d" cf.amdfeatures : "a" 0x8000_0001 : "ebx";
  741. } else {
  742. asm pure nothrow @nogc {
  743. mov EAX, 0x8000_0001;
  744. cpuid;
  745. mov c, ECX;
  746. mov d, EDX;
  747. }
  748. cf.amdmiscfeatures = c;
  749. cf.amdfeatures = d;
  750. }
  751. }
  752. // Try to detect fraudulent vendorIDs
  753. if (amd3dnow) cf.probablyIntel = false;
  754. if (!cf.probablyIntel && max_extended_cpuid >= 0x8000_0008) {
  755. //http://support.amd.com/TechDocs/25481.pdf pg.36
  756. cf.maxCores = 1;
  757. if (hyperThreadingBit) {
  758. // determine max number of cores for AMD
  759. version (GNU) asm pure nothrow @nogc {
  760. "cpuid" : "=a" a, "=c" c : "a" 0x8000_0008 : "ebx", "edx";
  761. } else asm pure nothrow @nogc {
  762. mov EAX, 0x8000_0008;
  763. cpuid;
  764. mov c, ECX;
  765. }
  766. cf.maxCores += c & 0xFF;
  767. }
  768. }
  769. if (max_extended_cpuid >= 0x8000_0004) {
  770. uint* pnb = cast(uint*)cf.processorNameBuffer.ptr;
  771. version (GNU)
  772. {
  773. asm pure nothrow @nogc { "cpuid" : "=a" pnb[0], "=b" pnb[1], "=c" pnb[ 2], "=d" pnb[ 3] : "a" 0x8000_0002; }
  774. asm pure nothrow @nogc { "cpuid" : "=a" pnb[4], "=b" pnb[5], "=c" pnb[ 6], "=d" pnb[ 7] : "a" 0x8000_0003; }
  775. asm pure nothrow @nogc { "cpuid" : "=a" pnb[8], "=b" pnb[9], "=c" pnb[10], "=d" pnb[11] : "a" 0x8000_0004; }
  776. }
  777. else version (D_InlineAsm_X86)
  778. {
  779. asm pure nothrow @nogc {
  780. push ESI;
  781. mov ESI, pnb;
  782. mov EAX, 0x8000_0002;
  783. cpuid;
  784. mov [ESI], EAX;
  785. mov [ESI+4], EBX;
  786. mov [ESI+8], ECX;
  787. mov [ESI+12], EDX;
  788. mov EAX, 0x8000_0003;
  789. cpuid;
  790. mov [ESI+16], EAX;
  791. mov [ESI+20], EBX;
  792. mov [ESI+24], ECX;
  793. mov [ESI+28], EDX;
  794. mov EAX, 0x8000_0004;
  795. cpuid;
  796. mov [ESI+32], EAX;
  797. mov [ESI+36], EBX;
  798. mov [ESI+40], ECX;
  799. mov [ESI+44], EDX;
  800. pop ESI;
  801. }
  802. }
  803. else version (D_InlineAsm_X86_64)
  804. {
  805. asm pure nothrow @nogc {
  806. push RSI;
  807. mov RSI, pnb;
  808. mov EAX, 0x8000_0002;
  809. cpuid;
  810. mov [RSI], EAX;
  811. mov [RSI+4], EBX;
  812. mov [RSI+8], ECX;
  813. mov [RSI+12], EDX;
  814. mov EAX, 0x8000_0003;
  815. cpuid;
  816. mov [RSI+16], EAX;
  817. mov [RSI+20], EBX;
  818. mov [RSI+24], ECX;
  819. mov [RSI+28], EDX;
  820. mov EAX, 0x8000_0004;
  821. cpuid;
  822. mov [RSI+32], EAX;
  823. mov [RSI+36], EBX;
  824. mov [RSI+40], ECX;
  825. mov [RSI+44], EDX;
  826. pop RSI;
  827. }
  828. }
  829. // Intel P4 and PM pad at front with spaces.
  830. // Other CPUs pad at end with nulls.
  831. int start = 0, end = 0;
  832. while (cf.processorNameBuffer[start] == ' ') { ++start; }
  833. while (cf.processorNameBuffer[cf.processorNameBuffer.length-end-1] == 0) { ++end; }
  834. cf.processorName = cast(string)(cf.processorNameBuffer[start..$-end]);
  835. } else {
  836. cf.processorName = "Unknown CPU";
  837. }
  838. // Determine cache sizes
  839. // Intel docs specify that they return 0 for 0x8000_0005.
  840. // AMD docs do not specify the behaviour for 0004 and 0002.
  841. // Centaur/VIA and most other manufacturers use the AMD method,
  842. // except Cyrix MediaGX MMX Enhanced uses their OWN form of CPUID2!
  843. // NS Geode GX1 provides CyrixCPUID2 _and_ does the same wrong behaviour
  844. // for CPUID80000005. But Geode GX uses the AMD method
  845. // Deal with Geode GX1 - make it same as MediaGX MMX.
  846. if (max_extended_cpuid==0x8000_0005 && max_cpuid==2) {
  847. max_extended_cpuid = 0x8000_0004;
  848. }
  849. // Therefore, we try the AMD method unless it's an Intel chip.
  850. // If we still have no info, try the Intel methods.
  851. datacache[0].size = 0;
  852. if (max_cpuid<2 || !cf.probablyIntel) {
  853. if (max_extended_cpuid >= 0x8000_0005) {
  854. getAMDcacheinfo();
  855. } else if (cf.probablyAMD) {
  856. // According to AMDProcRecognitionAppNote, this means CPU
  857. // K5 model 0, or Am5x86 (model 4), or Am4x86DX4 (model 4)
  858. // Am5x86 has 16Kb 4-way unified data & code cache.
  859. datacache[0].size = 8;
  860. datacache[0].associativity = 4;
  861. datacache[0].lineSize = 32;
  862. } else {
  863. // Some obscure CPU.
  864. // Values for Cyrix 6x86MX (family 6, model 0)
  865. datacache[0].size = 64;
  866. datacache[0].associativity = 4;
  867. datacache[0].lineSize = 32;
  868. }
  869. }
  870. if ((datacache[0].size == 0) && max_cpuid>=4) {
  871. getcacheinfoCPUID4();
  872. }
  873. if ((datacache[0].size == 0) && max_cpuid>=2) {
  874. getcacheinfoCPUID2();
  875. }
  876. if (datacache[0].size == 0) {
  877. // Pentium, PMMX, late model 486, or an obscure CPU
  878. if (mmx) { // Pentium MMX. Also has 8kB code cache.
  879. datacache[0].size = 16;
  880. datacache[0].associativity = 4;
  881. datacache[0].lineSize = 32;
  882. } else { // Pentium 1 (which also has 8kB code cache)
  883. // or 486.
  884. // Cyrix 6x86: 16, 4way, 32 linesize
  885. datacache[0].size = 8;
  886. datacache[0].associativity = 2;
  887. datacache[0].lineSize = 32;
  888. }
  889. }
  890. if (max_cpuid >= 0x0B) {
  891. // For Intel i7 and later, use function 0x0B to determine
  892. // cores and hyperthreads.
  893. getCpuInfo0B();
  894. } else {
  895. if (hyperThreadingBit) cf.maxThreads = (apic>>>16) & 0xFF;
  896. else cf.maxThreads = cf.maxCores;
  897. }
  898. }
  899. // Return true if the cpuid instruction is supported.
  900. // BUG(WONTFIX): Returns false for Cyrix 6x86 and 6x86L. They will be treated as 486 machines.
  901. bool hasCPUID()
  902. {
  903. version (X86_64)
  904. return true;
  905. else
  906. {
  907. uint flags;
  908. version (GNU)
  909. {
  910. // http://wiki.osdev.org/CPUID#Checking_CPUID_availability
  911. // ASM template supports both AT&T and Intel syntax.
  912. asm nothrow @nogc { "
  913. pushf{l|d} # Save EFLAGS
  914. pushf{l|d} # Store EFLAGS
  915. xor{l $0x00200000, (%%esp)| dword ptr [esp], 0x00200000}
  916. # Invert the ID bit in stored EFLAGS
  917. popf{l|d} # Load stored EFLAGS (with ID bit inverted)
  918. pushf{l|d} # Store EFLAGS again (ID bit may or may not be inverted)
  919. pop {%%}eax # eax = modified EFLAGS (ID bit may or may not be inverted)
  920. xor {(%%esp), %%eax|eax, [esp]}
  921. # eax = whichever bits were changed
  922. popf{l|d} # Restore original EFLAGS
  923. " : "=a" flags;
  924. }
  925. }
  926. else version (D_InlineAsm_X86)
  927. {
  928. asm nothrow @nogc {
  929. pushfd;
  930. pop EAX;
  931. mov flags, EAX;
  932. xor EAX, 0x0020_0000;
  933. push EAX;
  934. popfd;
  935. pushfd;
  936. pop EAX;
  937. xor flags, EAX;
  938. }
  939. }
  940. return (flags & 0x0020_0000) != 0;
  941. }
  942. }
  943. } else { // supported X86
  944. bool hasCPUID() { return false; }
  945. void cpuidX86()
  946. {
  947. datacache[0].size = 8;
  948. datacache[0].associativity = 2;
  949. datacache[0].lineSize = 32;
  950. }
  951. }
  952. /*
  953. // TODO: Implement this function with OS support
  954. void cpuidPPC()
  955. {
  956. enum :int { PPC601, PPC603, PPC603E, PPC604,
  957. PPC604E, PPC620, PPCG3, PPCG4, PPCG5 }
  958. // TODO:
  959. // asm { mfpvr; } returns the CPU version but unfortunately it can
  960. // only be used in kernel mode. So OS support is required.
  961. int cputype = PPC603;
  962. // 601 has a 8KB combined data & code L1 cache.
  963. uint sizes[] = [4, 8, 16, 16, 32, 32, 32, 32, 64];
  964. ubyte ways[] = [8, 2, 4, 4, 4, 8, 8, 8, 8];
  965. uint L2size[]= [0, 0, 0, 0, 0, 0, 0, 256, 512];
  966. uint L3size[]= [0, 0, 0, 0, 0, 0, 0, 2048, 0];
  967. datacache[0].size = sizes[cputype];
  968. datacache[0].associativity = ways[cputype];
  969. datacache[0].lineSize = (cputype==PPCG5)? 128 :
  970. (cputype == PPC620 || cputype == PPCG3)? 64 : 32;
  971. datacache[1].size = L2size[cputype];
  972. datacache[2].size = L3size[cputype];
  973. datacache[1].lineSize = datacache[0].lineSize;
  974. datacache[2].lineSize = datacache[0].lineSize;
  975. }
  976. // TODO: Implement this function with OS support
  977. void cpuidSparc()
  978. {
  979. // UltaSparcIIi : L1 = 16, 2way. L2 = 512, 4 way.
  980. // UltraSparcIII : L1 = 64, 4way. L2= 4096 or 8192.
  981. // UltraSparcIIIi: L1 = 64, 4way. L2= 1024, 4 way
  982. // UltraSparcIV : L1 = 64, 4way. L2 = 16*1024.
  983. // UltraSparcIV+ : L1 = 64, 4way. L2 = 2048, L3=32*1024.
  984. // Sparc64V : L1 = 128, 2way. L2 = 4096 4way.
  985. }
  986. */
  987. shared static this()
  988. {
  989. auto cf = getCpuFeatures();
  990. if (hasCPUID()) {
  991. cpuidX86();
  992. } else {
  993. // it's a 386 or 486, or a Cyrix 6x86.
  994. //Probably still has an external cache.
  995. }
  996. if (datacache[0].size==0) {
  997. // Guess same as Pentium 1.
  998. datacache[0].size = 8;
  999. datacache[0].associativity = 2;
  1000. datacache[0].lineSize = 32;
  1001. }
  1002. numCacheLevels = 1;
  1003. // And now fill up all the unused levels with full memory space.
  1004. for (size_t i=1; i< datacache.length; ++i) {
  1005. if (datacache[i].size==0) {
  1006. // Set all remaining levels of cache equal to full address space.
  1007. datacache[i].size = size_t.max/1024;
  1008. datacache[i].associativity = 1;
  1009. datacache[i].lineSize = datacache[i-1].lineSize;
  1010. }
  1011. else
  1012. ++numCacheLevels;
  1013. }
  1014. // Set the immortals
  1015. _dataCaches = datacache;
  1016. _vendor = cast(string)cf.vendorID;
  1017. _processor = cf.processorName;
  1018. _x87onChip = (cf.features&FPU_BIT)!=0;
  1019. _mmx = (cf.features&MMX_BIT)!=0;
  1020. _sse = (cf.features&SSE_BIT)!=0;
  1021. _sse2 = (cf.features&SSE2_BIT)!=0;
  1022. _sse3 = (cf.miscfeatures&SSE3_BIT)!=0;
  1023. _ssse3 = (cf.miscfeatures&SSSE3_BIT)!=0;
  1024. _sse41 = (cf.miscfeatures&SSE41_BIT)!=0;
  1025. _sse42 = (cf.miscfeatures&SSE42_BIT)!=0;
  1026. _sse4a = (cf.amdmiscfeatures&SSE4A_BIT)!=0;
  1027. _aes = (cf.miscfeatures&AES_BIT)!=0;
  1028. _hasPclmulqdq = (cf.miscfeatures&PCLMULQDQ_BIT)!=0;
  1029. _hasRdrand = (cf.miscfeatures&RDRAND_BIT)!=0;
  1030. enum avx_mask = XF_SSE_BIT|XF_YMM_BIT;
  1031. _avx = (cf.xfeatures & avx_mask) == avx_mask && (cf.miscfeatures&AVX_BIT)!=0;
  1032. _vaes = avx && aes;
  1033. _hasVpclmulqdq = avx && hasPclmulqdq;
  1034. _fma = avx && (cf.miscfeatures&FMA_BIT)!=0;
  1035. _fp16c = avx && (cf.miscfeatures&FP16C_BIT)!=0;
  1036. _avx2 = avx && (cf.extfeatures & AVX2_BIT) != 0;
  1037. _hle = (cf.extfeatures & HLE_BIT) != 0;
  1038. _rtm = (cf.extfeatures & RTM_BIT) != 0;
  1039. _hasRdseed = (cf.extfeatures&RDSEED_BIT)!=0;
  1040. _hasSha = (cf.extfeatures&SHA_BIT)!=0;
  1041. _amd3dnow = (cf.amdfeatures&AMD_3DNOW_BIT)!=0;
  1042. _amd3dnowExt = (cf.amdfeatures&AMD_3DNOW_EXT_BIT)!=0;
  1043. _amdMmx = (cf.amdfeatures&AMD_MMX_BIT)!=0;
  1044. _hasFxsr = (cf.features&FXSR_BIT)!=0;
  1045. _hasCmov = (cf.features&CMOV_BIT)!=0;
  1046. _hasRdtsc = (cf.features&TIMESTAMP_BIT)!=0;
  1047. _hasCmpxchg8b = (cf.features&CMPXCHG8B_BIT)!=0;
  1048. _hasCmpxchg16b = (cf.miscfeatures&CMPXCHG16B_BIT)!=0;
  1049. _hasSysEnterSysExit =
  1050. // The SYSENTER/SYSEXIT features were buggy on Pentium Pro and early PentiumII.
  1051. // (REF: www.geoffchappell.com).
  1052. (cf.probablyIntel && (family < 6 || (family==6 && (model< 3 || (model==3 && stepping<3)))))
  1053. ? false
  1054. : (cf.features & SYSENTERSYSEXIT_BIT)!=0;
  1055. _has3dnowPrefetch = (cf.amdmiscfeatures&AMD_3DNOW_PREFETCH_BIT)!=0;
  1056. _hasLahfSahf = (cf.amdmiscfeatures&LAHFSAHF_BIT)!=0;
  1057. _hasPopcnt = (cf.miscfeatures&POPCNT_BIT)!=0;
  1058. _hasLzcnt = (cf.amdmiscfeatures&LZCNT_BIT)!=0;
  1059. _isX86_64 = (cf.amdfeatures&AMD64_BIT)!=0;
  1060. _isItanium = (cf.features&IA64_BIT)!=0;
  1061. _hyperThreading = cf.maxThreads>cf.maxCores;
  1062. _threadsPerCPU = cf.maxThreads;
  1063. _coresPerCPU = cf.maxCores;
  1064. _preferAthlon = cf.probablyAMD && family >=6;
  1065. _preferPentium4 = cf.probablyIntel && family == 0xF;
  1066. _preferPentium1 = family < 6 || (family==6 && model < 0xF && !cf.probablyIntel);
  1067. }