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