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
38 __cpuid(static_cast<int*>(reg), 0);
41 __cpuid(static_cast<int*>(reg), 1);
46 __cpuidex(static_cast<int*>(reg), 7, 0);
50 #elif defined(__i386__) && defined(__PIC__) && !defined(__clang__) && \
52 // The following block like the normal cpuid branch below, but gcc
53 // reserves ebx for use of it's pic register so we must specially
54 // handle the save and restore to avoid clobbering the register
68 : "=c"(f1c_), "=d"(f1d_)
76 "movl %%ebx, %%eax\n\r"
78 : "=a"(f7b_), "=c"(f7c_)
82 #elif FOLLY_X64 || defined(__i386__)
84 __asm__("cpuid" : "=a"(n) : "a"(0) : "ebx", "edx", "ecx");
86 __asm__("cpuid" : "=c"(f1c_), "=d"(f1d_) : "a"(1) : "ebx");
89 __asm__("cpuid" : "=b"(f7b_), "=c"(f7c_) : "a"(7), "c"(0) : "edx");
94 #define X(name, r, bit) bool name() const { return (r) & (1U << bit); }
96 // cpuid(1): Processor Info and Feature Bits.
97 #define C(name, bit) X(name, f1c_, bit)
128 #define D(name, bit) X(name, f1d_, bit)
160 // cpuid(7): Extended Features.
161 #define B(name, bit) X(name, f7b_, bit)
187 #define C(name, bit) X(name, f7c_, bit)