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