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