2 * Copyright 2016 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 mde, 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);
52 __cpuidex(static_cast<int*>(reg), 7, 0);
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 it's pic register so we must specially
60 // handle the save and restore to avoid clobbering the register
74 : "=c"(f1c_), "=d"(f1d_)
82 "movl %%ebx, %%eax\n\r"
84 : "=a"(f7b_), "=c"(f7c_)
88 #elif FOLLY_X64 || defined(__i386__)
90 __asm__("cpuid" : "=a"(n) : "a"(0) : "ebx", "edx", "ecx");
92 __asm__("cpuid" : "=c"(f1c_), "=d"(f1d_) : "a"(1) : "ebx");
95 __asm__("cpuid" : "=b"(f7b_), "=c"(f7c_) : "a"(7), "c"(0) : "edx");
100 #define X(name, r, bit) \
101 FOLLY_ALWAYS_INLINE bool name() const { \
102 return (r) & (1U << bit); \
105 // cpuid(1): Processor Info and Feature Bits.
106 #define C(name, bit) X(name, f1c_, bit)
137 #define D(name, bit) X(name, f1d_, bit)
169 // cpuid(7): Extended Features.
170 #define B(name, bit) X(name, f7b_, bit)
196 #define C(name, bit) X(name, f7c_, bit)