CpuId.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * Copyright 2012-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #pragma once
  17. #include <cstdint>
  18. #include <folly/Portability.h>
  19. #ifdef _MSC_VER
  20. #include <intrin.h>
  21. #endif
  22. namespace folly {
  23. /**
  24. * Identification of an Intel CPU.
  25. * Supports CPUID feature flags (EAX=1) and extended features (EAX=7, ECX=0).
  26. * Values from
  27. * http://www.intel.com/content/www/us/en/processors/processor-identification-cpuid-instruction-note.html
  28. */
  29. class CpuId {
  30. public:
  31. // Always inline in order for this to be usable from a __ifunc__.
  32. // In shared library mode, a __ifunc__ runs at relocation time, while the
  33. // PLT hasn't been fully populated yet; thus, ifuncs cannot use symbols
  34. // with potentially external linkage. (This issue is less likely in opt
  35. // mode since inlining happens more likely, and it doesn't happen for
  36. // statically linked binaries which don't depend on the PLT)
  37. FOLLY_ALWAYS_INLINE CpuId() {
  38. #if defined(_MSC_VER) && (FOLLY_X64 || defined(_M_IX86))
  39. int reg[4];
  40. __cpuid(static_cast<int*>(reg), 0);
  41. const int n = reg[0];
  42. if (n >= 1) {
  43. __cpuid(static_cast<int*>(reg), 1);
  44. f1c_ = uint32_t(reg[2]);
  45. f1d_ = uint32_t(reg[3]);
  46. }
  47. if (n >= 7) {
  48. __cpuidex(static_cast<int*>(reg), 7, 0);
  49. f7b_ = uint32_t(reg[1]);
  50. f7c_ = uint32_t(reg[2]);
  51. }
  52. #elif defined(__i386__) && defined(__PIC__) && !defined(__clang__) && \
  53. defined(__GNUC__)
  54. // The following block like the normal cpuid branch below, but gcc
  55. // reserves ebx for use of its pic register so we must specially
  56. // handle the save and restore to avoid clobbering the register
  57. uint32_t n;
  58. __asm__(
  59. "pushl %%ebx\n\t"
  60. "cpuid\n\t"
  61. "popl %%ebx\n\t"
  62. : "=a"(n)
  63. : "a"(0)
  64. : "ecx", "edx");
  65. if (n >= 1) {
  66. uint32_t f1a;
  67. __asm__(
  68. "pushl %%ebx\n\t"
  69. "cpuid\n\t"
  70. "popl %%ebx\n\t"
  71. : "=a"(f1a), "=c"(f1c_), "=d"(f1d_)
  72. : "a"(1)
  73. :);
  74. }
  75. if (n >= 7) {
  76. __asm__(
  77. "pushl %%ebx\n\t"
  78. "cpuid\n\t"
  79. "movl %%ebx, %%eax\n\r"
  80. "popl %%ebx"
  81. : "=a"(f7b_), "=c"(f7c_)
  82. : "a"(7), "c"(0)
  83. : "edx");
  84. }
  85. #elif FOLLY_X64 || defined(__i386__)
  86. uint32_t n;
  87. __asm__("cpuid" : "=a"(n) : "a"(0) : "ebx", "ecx", "edx");
  88. if (n >= 1) {
  89. uint32_t f1a;
  90. __asm__("cpuid" : "=a"(f1a), "=c"(f1c_), "=d"(f1d_) : "a"(1) : "ebx");
  91. }
  92. if (n >= 7) {
  93. uint32_t f7a;
  94. __asm__("cpuid"
  95. : "=a"(f7a), "=b"(f7b_), "=c"(f7c_)
  96. : "a"(7), "c"(0)
  97. : "edx");
  98. }
  99. #endif
  100. }
  101. #define X(name, r, bit) \
  102. FOLLY_ALWAYS_INLINE bool name() const { \
  103. return ((r) & (1U << bit)) != 0; \
  104. }
  105. // cpuid(1): Processor Info and Feature Bits.
  106. #define C(name, bit) X(name, f1c_, bit)
  107. C(sse3, 0)
  108. C(pclmuldq, 1)
  109. C(dtes64, 2)
  110. C(monitor, 3)
  111. C(dscpl, 4)
  112. C(vmx, 5)
  113. C(smx, 6)
  114. C(eist, 7)
  115. C(tm2, 8)
  116. C(ssse3, 9)
  117. C(cnxtid, 10)
  118. C(fma, 12)
  119. C(cx16, 13)
  120. C(xtpr, 14)
  121. C(pdcm, 15)
  122. C(pcid, 17)
  123. C(dca, 18)
  124. C(sse41, 19)
  125. C(sse42, 20)
  126. C(x2apic, 21)
  127. C(movbe, 22)
  128. C(popcnt, 23)
  129. C(tscdeadline, 24)
  130. C(aes, 25)
  131. C(xsave, 26)
  132. C(osxsave, 27)
  133. C(avx, 28)
  134. C(f16c, 29)
  135. C(rdrand, 30)
  136. #undef C
  137. #define D(name, bit) X(name, f1d_, bit)
  138. D(fpu, 0)
  139. D(vme, 1)
  140. D(de, 2)
  141. D(pse, 3)
  142. D(tsc, 4)
  143. D(msr, 5)
  144. D(pae, 6)
  145. D(mce, 7)
  146. D(cx8, 8)
  147. D(apic, 9)
  148. D(sep, 11)
  149. D(mtrr, 12)
  150. D(pge, 13)
  151. D(mca, 14)
  152. D(cmov, 15)
  153. D(pat, 16)
  154. D(pse36, 17)
  155. D(psn, 18)
  156. D(clfsh, 19)
  157. D(ds, 21)
  158. D(acpi, 22)
  159. D(mmx, 23)
  160. D(fxsr, 24)
  161. D(sse, 25)
  162. D(sse2, 26)
  163. D(ss, 27)
  164. D(htt, 28)
  165. D(tm, 29)
  166. D(pbe, 31)
  167. #undef D
  168. // cpuid(7): Extended Features.
  169. #define B(name, bit) X(name, f7b_, bit)
  170. B(bmi1, 3)
  171. B(hle, 4)
  172. B(avx2, 5)
  173. B(smep, 7)
  174. B(bmi2, 8)
  175. B(erms, 9)
  176. B(invpcid, 10)
  177. B(rtm, 11)
  178. B(mpx, 14)
  179. B(avx512f, 16)
  180. B(avx512dq, 17)
  181. B(rdseed, 18)
  182. B(adx, 19)
  183. B(smap, 20)
  184. B(avx512ifma, 21)
  185. B(pcommit, 22)
  186. B(clflushopt, 23)
  187. B(clwb, 24)
  188. B(avx512pf, 26)
  189. B(avx512er, 27)
  190. B(avx512cd, 28)
  191. B(sha, 29)
  192. B(avx512bw, 30)
  193. B(avx512vl, 31)
  194. #undef B
  195. #define C(name, bit) X(name, f7c_, bit)
  196. C(prefetchwt1, 0)
  197. C(avx512vbmi, 1)
  198. #undef C
  199. #undef X
  200. private:
  201. uint32_t f1c_ = 0;
  202. uint32_t f1d_ = 0;
  203. uint32_t f7b_ = 0;
  204. uint32_t f7c_ = 0;
  205. };
  206. } // namespace folly