You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cpu.h 4.4KB

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