2 * Copyright 2017 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <folly/Portability.h>
29 * Identification of an Intel CPU.
30 * Supports CPUID feature flags (EAX=1) and extended features (EAX=7, ECX=0).
31 * Values from http://www.intel.com/content/www/us/en/processors/processor-identification-cpuid-instruction-note.html
35 // Always inline in order for this to be usable from a __ifunc__.
36 // In shared library mode, a __ifunc__ runs at relocation time, while the
37 // PLT hasn't been fully populated yet; thus, ifuncs cannot use symbols
38 // with potentially external linkage. (This issue is less likely in opt
39 // mode since inlining happens more likely, and it doesn't happen for
40 // statically linked binaries which don't depend on the PLT)
41 FOLLY_ALWAYS_INLINE CpuId() {
44 __cpuid(static_cast<int*>(reg), 0);
47 __cpuid(static_cast<int*>(reg), 1);
48 f1c_ = uint32_t(reg[2]);
49 f1d_ = uint32_t(reg[3]);
52 __cpuidex(static_cast<int*>(reg), 7, 0);
53 f7b_ = uint32_t(reg[1]);
54 f7c_ = uint32_t(reg[2]);
56 #elif defined(__i386__) && defined(__PIC__) && !defined(__clang__) && \
58 // The following block like the normal cpuid branch below, but gcc
59 // reserves ebx for use of its pic register so we must specially
60 // handle the save and restore to avoid clobbering the register
75 : "=a"(f1a), "=c"(f1c_), "=d"(f1d_)
83 "movl %%ebx, %%eax\n\r"
85 : "=a"(f7b_), "=c"(f7c_)
89 #elif FOLLY_X64 || defined(__i386__)
91 __asm__("cpuid" : "=a"(n) : "a"(0) : "ebx", "ecx", "edx");
94 __asm__("cpuid" : "=a"(f1a), "=c"(f1c_), "=d"(f1d_) : "a"(1) : "ebx");
99 : "=a"(f7a), "=b"(f7b_), "=c"(f7c_)
106 #define X(name, r, bit) \
107 FOLLY_ALWAYS_INLINE bool name() const { \
108 return ((r) & (1U << bit)) != 0; \
111 // cpuid(1): Processor Info and Feature Bits.
112 #define C(name, bit) X(name, f1c_, bit)
143 #define D(name, bit) X(name, f1d_, bit)
175 // cpuid(7): Extended Features.
176 #define B(name, bit) X(name, f7b_, bit)
202 #define C(name, bit) X(name, f7c_, bit)