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