cpu.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Copyright (c) 2018-present, Facebook, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under both the BSD-style license (found in the
  6. * LICENSE file in the root directory of this source tree) and the GPLv2 (found
  7. * in the COPYING file in the root directory of this source tree).
  8. * You may select, at your option, one of the above-listed licenses.
  9. */
  10. #ifndef ZSTD_COMMON_CPU_H
  11. #define ZSTD_COMMON_CPU_H
  12. /**
  13. * Implementation taken from folly/CpuId.h
  14. * https://github.com/facebook/folly/blob/master/folly/CpuId.h
  15. */
  16. #include <string.h>
  17. #include "mem.h"
  18. #ifdef _MSC_VER
  19. #include <intrin.h>
  20. #endif
  21. typedef struct {
  22. U32 f1c;
  23. U32 f1d;
  24. U32 f7b;
  25. U32 f7c;
  26. } ZSTD_cpuid_t;
  27. MEM_STATIC ZSTD_cpuid_t ZSTD_cpuid(void) {
  28. U32 f1c = 0;
  29. U32 f1d = 0;
  30. U32 f7b = 0;
  31. U32 f7c = 0;
  32. #if defined(_MSC_VER) && (defined(_M_X64) || defined(_M_IX86))
  33. int reg[4];
  34. __cpuid((int*)reg, 0);
  35. {
  36. int const n = reg[0];
  37. if (n >= 1) {
  38. __cpuid((int*)reg, 1);
  39. f1c = (U32)reg[2];
  40. f1d = (U32)reg[3];
  41. }
  42. if (n >= 7) {
  43. __cpuidex((int*)reg, 7, 0);
  44. f7b = (U32)reg[1];
  45. f7c = (U32)reg[2];
  46. }
  47. }
  48. #elif defined(__i386__) && defined(__PIC__) && !defined(__clang__) && defined(__GNUC__)
  49. /* The following block like the normal cpuid branch below, but gcc
  50. * reserves ebx for use of its pic register so we must specially
  51. * handle the save and restore to avoid clobbering the register
  52. */
  53. U32 n;
  54. __asm__(
  55. "pushl %%ebx\n\t"
  56. "cpuid\n\t"
  57. "popl %%ebx\n\t"
  58. : "=a"(n)
  59. : "a"(0)
  60. : "ecx", "edx");
  61. if (n >= 1) {
  62. U32 f1a;
  63. __asm__(
  64. "pushl %%ebx\n\t"
  65. "cpuid\n\t"
  66. "popl %%ebx\n\t"
  67. : "=a"(f1a), "=c"(f1c), "=d"(f1d)
  68. : "a"(1));
  69. }
  70. if (n >= 7) {
  71. __asm__(
  72. "pushl %%ebx\n\t"
  73. "cpuid\n\t"
  74. "movl %%ebx, %%eax\n\r"
  75. "popl %%ebx"
  76. : "=a"(f7b), "=c"(f7c)
  77. : "a"(7), "c"(0)
  78. : "edx");
  79. }
  80. #elif defined(__x86_64__) || defined(_M_X64) || defined(__i386__)
  81. U32 n;
  82. __asm__("cpuid" : "=a"(n) : "a"(0) : "ebx", "ecx", "edx");
  83. if (n >= 1) {
  84. U32 f1a;
  85. __asm__("cpuid" : "=a"(f1a), "=c"(f1c), "=d"(f1d) : "a"(1) : "ebx");
  86. }
  87. if (n >= 7) {
  88. U32 f7a;
  89. __asm__("cpuid"
  90. : "=a"(f7a), "=b"(f7b), "=c"(f7c)
  91. : "a"(7), "c"(0)
  92. : "edx");
  93. }
  94. #endif
  95. {
  96. ZSTD_cpuid_t cpuid;
  97. cpuid.f1c = f1c;
  98. cpuid.f1d = f1d;
  99. cpuid.f7b = f7b;
  100. cpuid.f7c = f7c;
  101. return cpuid;
  102. }
  103. }
  104. #define X(name, r, bit) \
  105. MEM_STATIC int ZSTD_cpuid_##name(ZSTD_cpuid_t const cpuid) { \
  106. return ((cpuid.r) & (1U << bit)) != 0; \
  107. }
  108. /* cpuid(1): Processor Info and Feature Bits. */
  109. #define C(name, bit) X(name, f1c, bit)
  110. C(sse3, 0)
  111. C(pclmuldq, 1)
  112. C(dtes64, 2)
  113. C(monitor, 3)
  114. C(dscpl, 4)
  115. C(vmx, 5)
  116. C(smx, 6)
  117. C(eist, 7)
  118. C(tm2, 8)
  119. C(ssse3, 9)
  120. C(cnxtid, 10)
  121. C(fma, 12)
  122. C(cx16, 13)
  123. C(xtpr, 14)
  124. C(pdcm, 15)
  125. C(pcid, 17)
  126. C(dca, 18)
  127. C(sse41, 19)
  128. C(sse42, 20)
  129. C(x2apic, 21)
  130. C(movbe, 22)
  131. C(popcnt, 23)
  132. C(tscdeadline, 24)
  133. C(aes, 25)
  134. C(xsave, 26)
  135. C(osxsave, 27)
  136. C(avx, 28)
  137. C(f16c, 29)
  138. C(rdrand, 30)
  139. #undef C
  140. #define D(name, bit) X(name, f1d, bit)
  141. D(fpu, 0)
  142. D(vme, 1)
  143. D(de, 2)
  144. D(pse, 3)
  145. D(tsc, 4)
  146. D(msr, 5)
  147. D(pae, 6)
  148. D(mce, 7)
  149. D(cx8, 8)
  150. D(apic, 9)
  151. D(sep, 11)
  152. D(mtrr, 12)
  153. D(pge, 13)
  154. D(mca, 14)
  155. D(cmov, 15)
  156. D(pat, 16)
  157. D(pse36, 17)
  158. D(psn, 18)
  159. D(clfsh, 19)
  160. D(ds, 21)
  161. D(acpi, 22)
  162. D(mmx, 23)
  163. D(fxsr, 24)
  164. D(sse, 25)
  165. D(sse2, 26)
  166. D(ss, 27)
  167. D(htt, 28)
  168. D(tm, 29)
  169. D(pbe, 31)
  170. #undef D
  171. /* cpuid(7): Extended Features. */
  172. #define B(name, bit) X(name, f7b, bit)
  173. B(bmi1, 3)
  174. B(hle, 4)
  175. B(avx2, 5)
  176. B(smep, 7)
  177. B(bmi2, 8)
  178. B(erms, 9)
  179. B(invpcid, 10)
  180. B(rtm, 11)
  181. B(mpx, 14)
  182. B(avx512f, 16)
  183. B(avx512dq, 17)
  184. B(rdseed, 18)
  185. B(adx, 19)
  186. B(smap, 20)
  187. B(avx512ifma, 21)
  188. B(pcommit, 22)
  189. B(clflushopt, 23)
  190. B(clwb, 24)
  191. B(avx512pf, 26)
  192. B(avx512er, 27)
  193. B(avx512cd, 28)
  194. B(sha, 29)
  195. B(avx512bw, 30)
  196. B(avx512vl, 31)
  197. #undef B
  198. #define C(name, bit) X(name, f7c, bit)
  199. C(prefetchwt1, 0)
  200. C(avx512vbmi, 1)
  201. #undef C
  202. #undef X
  203. #endif /* ZSTD_COMMON_CPU_H */