2 * Copyright (c) 2018-present, Facebook, Inc.
5 * This source code is licensed under both the BSD-style license (found in the
6 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7 * in the COPYING file in the root directory of this source tree).
8 * You may select, at your option, one of the above-listed licenses.
11 #ifndef ZSTD_COMMON_CPU_H
12 #define ZSTD_COMMON_CPU_H
15 * Implementation taken from folly/CpuId.h
16 * https://github.com/facebook/folly/blob/master/folly/CpuId.h
34 MEM_STATIC ZSTD_cpuid_t ZSTD_cpuid(void) {
41 __cpuid((int*)reg, 0);
45 __cpuid((int*)reg, 1);
50 __cpuidex((int*)reg, 7, 0);
55 #elif defined(__i386__) && defined(__PIC__) && !defined(__clang__) && defined(__GNUC__)
56 /* The following block like the normal cpuid branch below, but gcc
57 * reserves ebx for use of its pic register so we must specially
58 * handle the save and restore to avoid clobbering the register
74 : "=a"(f1a), "=c"(f1c), "=d"(f1d)
82 "movl %%ebx, %%eax\n\r"
84 : "=a"(f7b), "=c"(f7c)
88 #elif defined(__x86_64__) || defined(_M_X64) || defined(__i386__)
90 __asm__("cpuid" : "=a"(n) : "a"(0) : "ebx", "ecx", "edx");
93 __asm__("cpuid" : "=a"(f1a), "=c"(f1c), "=d"(f1d) : "a"(1) : "ebx");
98 : "=a"(f7a), "=b"(f7b), "=c"(f7c)
113 #define X(name, r, bit) \
114 MEM_STATIC int ZSTD_cpuid_##name(ZSTD_cpuid_t const cpuid) { \
115 return ((cpuid.r) & (1U << bit)) != 0; \
118 /* cpuid(1): Processor Info and Feature Bits. */
119 #define C(name, bit) X(name, f1c, bit)
150 #define D(name, bit) X(name, f1d, bit)
182 /* cpuid(7): Extended Features. */
183 #define B(name, bit) X(name, f7b, bit)
209 #define C(name, bit) X(name, f7c, bit)
216 #endif /* ZSTD_COMMON_CPU_H */