2 * Copyright (c) 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
29 MEM_STATIC ZSTD_cpuid_t
ZSTD_cpuid(void) {
34 #if defined(__i386__) && defined(__PIC__) && !defined(__clang__) && defined(__GNUC__)
35 /* The following block like the normal cpuid branch below, but gcc
36 * reserves ebx for use of its pic register so we must specially
37 * handle the save and restore to avoid clobbering the register
53 : "=a"(f1a
), "=c"(f1c
), "=d"(f1d
)
60 "movl %%ebx, %%eax\n\t"
62 : "=a"(f7b
), "=c"(f7c
)
66 #elif defined(__x86_64__) || defined(_M_X64) || defined(__i386__)
68 __asm__("cpuid" : "=a"(n
) : "a"(0) : "ebx", "ecx", "edx");
71 __asm__("cpuid" : "=a"(f1a
), "=c"(f1c
), "=d"(f1d
) : "a"(1) : "ebx");
76 : "=a"(f7a
), "=b"(f7b
), "=c"(f7c
)
91 #define X(name, r, bit) \
92 MEM_STATIC int ZSTD_cpuid_##name(ZSTD_cpuid_t const cpuid) { \
93 return ((cpuid.r) & (1U << bit)) != 0; \
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)
194 #endif /* ZSTD_COMMON_CPU_H */