ab4eb27ff48c763e5834a8250437238f7d342c9c
[folly.git] / folly / CpuId.h
1 /*
2  * Copyright 2013 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
22 namespace folly {
23
24 /**
25  * Identification of an Intel CPU.
26  * Supports CPUID (EAX=1) feature flags.
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 #if defined(__x86_64__) || defined(__i386__)
33     __asm__("cpuid" : "=c"(c_), "=d"(d_) : "a"(1) : "ebx");
34 #else
35     // On non-Intel, none of these features exist; at least not in the same form
36     // as they do on Intel
37     c_ = 0;
38     d_ = 0;
39 #endif
40   }
41 #define X(name, r, bit) bool name() const { return r & (1U << bit); }
42 #define C(name, bit) X(name, c_, bit)
43 #define D(name, bit) X(name, d_, bit)
44   C(sse3, 0)
45   C(pclmuldq, 1)
46   C(dtes64, 2)
47   C(monitor, 3)
48   C(dscpl, 4)
49   C(vmx, 5)
50   C(smx, 6)
51   C(eist, 7)
52   C(tm2, 8)
53   C(ssse3, 9)
54   C(cnxtid, 10)
55   // 11 is reserved
56   C(fma, 12)
57   C(cx16, 13)
58   C(xtpr, 14)
59   C(pdcm, 15)
60   // 16 is reserved
61   C(pcid, 17)
62   C(dca, 18)
63   C(sse41, 19)
64   C(sse42, 20)
65   C(x2apic, 21)
66   C(movbe, 22)
67   C(popcnt, 23)
68   C(tscdeadline, 24)
69   C(aes, 25)
70   C(xsave, 26)
71   C(osxsave, 27)
72   C(avx, 28)
73   C(f16c, 29)
74   C(rdrand, 30)
75   // 31 is not used
76   D(fpu, 0)
77   D(vme, 1)
78   D(de, 2)
79   D(pse, 3)
80   D(tsc, 4)
81   D(msr, 5)
82   D(pae, 6)
83   D(mce, 7)
84   D(cx8, 8)
85   D(apic, 9)
86   // 10 is reserved
87   D(sep, 11)
88   D(mtrr, 12)
89   D(pge, 13)
90   D(mca, 14)
91   D(cmov, 15)
92   D(pat, 16)
93   D(pse36, 17)
94   D(psn, 18)
95   D(clfsh, 19)
96   // 20 is reserved
97   D(ds, 21)
98   D(acpi, 22)
99   D(mmx, 23)
100   D(fxsr, 24)
101   D(sse, 25)
102   D(sse2, 26)
103   D(ss, 27)
104   D(htt, 28)
105   D(tm, 29)
106   // 30 is reserved
107   D(pbe, 31)
108 #undef D
109 #undef C
110 #undef X
111  private:
112   uint32_t c_;  // ECX
113   uint32_t d_;  // EDX
114 };
115
116 }  // namespace folly
117
118 #endif /* FOLLY_CPUID_H_ */
119