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