Path: blob/main/sys/contrib/zstd/lib/common/cpu.h
103756 views
/*1* Copyright (c) Facebook, Inc.2* All rights reserved.3*4* This source code is licensed under both the BSD-style license (found in the5* LICENSE file in the root directory of this source tree) and the GPLv2 (found6* in the COPYING file in the root directory of this source tree).7* You may select, at your option, one of the above-listed licenses.8*/910#ifndef ZSTD_COMMON_CPU_H11#define ZSTD_COMMON_CPU_H1213/**14* Implementation taken from folly/CpuId.h15* https://github.com/facebook/folly/blob/master/folly/CpuId.h16*/1718#include "mem.h"1920#ifdef _MSC_VER21#include <intrin.h>22#endif2324typedef struct {25U32 f1c;26U32 f1d;27U32 f7b;28U32 f7c;29} ZSTD_cpuid_t;3031MEM_STATIC ZSTD_cpuid_t ZSTD_cpuid(void) {32U32 f1c = 0;33U32 f1d = 0;34U32 f7b = 0;35U32 f7c = 0;36#if defined(_MSC_VER) && (defined(_M_X64) || defined(_M_IX86))37int reg[4];38__cpuid((int*)reg, 0);39{40int const n = reg[0];41if (n >= 1) {42__cpuid((int*)reg, 1);43f1c = (U32)reg[2];44f1d = (U32)reg[3];45}46if (n >= 7) {47__cpuidex((int*)reg, 7, 0);48f7b = (U32)reg[1];49f7c = (U32)reg[2];50}51}52#elif defined(__i386__) && defined(__PIC__) && !defined(__clang__) && defined(__GNUC__)53/* The following block like the normal cpuid branch below, but gcc54* reserves ebx for use of its pic register so we must specially55* handle the save and restore to avoid clobbering the register56*/57U32 n;58__asm__(59"pushl %%ebx\n\t"60"cpuid\n\t"61"popl %%ebx\n\t"62: "=a"(n)63: "a"(0)64: "ecx", "edx");65if (n >= 1) {66U32 f1a;67__asm__(68"pushl %%ebx\n\t"69"cpuid\n\t"70"popl %%ebx\n\t"71: "=a"(f1a), "=c"(f1c), "=d"(f1d)72: "a"(1));73}74if (n >= 7) {75__asm__(76"pushl %%ebx\n\t"77"cpuid\n\t"78"movl %%ebx, %%eax\n\t"79"popl %%ebx"80: "=a"(f7b), "=c"(f7c)81: "a"(7), "c"(0)82: "edx");83}84#elif defined(__x86_64__) || defined(_M_X64) || defined(__i386__)85U32 n;86__asm__("cpuid" : "=a"(n) : "a"(0) : "ebx", "ecx", "edx");87if (n >= 1) {88U32 f1a;89__asm__("cpuid" : "=a"(f1a), "=c"(f1c), "=d"(f1d) : "a"(1) : "ebx");90}91if (n >= 7) {92U32 f7a;93__asm__("cpuid"94: "=a"(f7a), "=b"(f7b), "=c"(f7c)95: "a"(7), "c"(0)96: "edx");97}98#endif99{100ZSTD_cpuid_t cpuid;101cpuid.f1c = f1c;102cpuid.f1d = f1d;103cpuid.f7b = f7b;104cpuid.f7c = f7c;105return cpuid;106}107}108109#define X(name, r, bit) \110MEM_STATIC int ZSTD_cpuid_##name(ZSTD_cpuid_t const cpuid) { \111return ((cpuid.r) & (1U << bit)) != 0; \112}113114/* cpuid(1): Processor Info and Feature Bits. */115#define C(name, bit) X(name, f1c, bit)116C(sse3, 0)117C(pclmuldq, 1)118C(dtes64, 2)119C(monitor, 3)120C(dscpl, 4)121C(vmx, 5)122C(smx, 6)123C(eist, 7)124C(tm2, 8)125C(ssse3, 9)126C(cnxtid, 10)127C(fma, 12)128C(cx16, 13)129C(xtpr, 14)130C(pdcm, 15)131C(pcid, 17)132C(dca, 18)133C(sse41, 19)134C(sse42, 20)135C(x2apic, 21)136C(movbe, 22)137C(popcnt, 23)138C(tscdeadline, 24)139C(aes, 25)140C(xsave, 26)141C(osxsave, 27)142C(avx, 28)143C(f16c, 29)144C(rdrand, 30)145#undef C146#define D(name, bit) X(name, f1d, bit)147D(fpu, 0)148D(vme, 1)149D(de, 2)150D(pse, 3)151D(tsc, 4)152D(msr, 5)153D(pae, 6)154D(mce, 7)155D(cx8, 8)156D(apic, 9)157D(sep, 11)158D(mtrr, 12)159D(pge, 13)160D(mca, 14)161D(cmov, 15)162D(pat, 16)163D(pse36, 17)164D(psn, 18)165D(clfsh, 19)166D(ds, 21)167D(acpi, 22)168D(mmx, 23)169D(fxsr, 24)170D(sse, 25)171D(sse2, 26)172D(ss, 27)173D(htt, 28)174D(tm, 29)175D(pbe, 31)176#undef D177178/* cpuid(7): Extended Features. */179#define B(name, bit) X(name, f7b, bit)180B(bmi1, 3)181B(hle, 4)182B(avx2, 5)183B(smep, 7)184B(bmi2, 8)185B(erms, 9)186B(invpcid, 10)187B(rtm, 11)188B(mpx, 14)189B(avx512f, 16)190B(avx512dq, 17)191B(rdseed, 18)192B(adx, 19)193B(smap, 20)194B(avx512ifma, 21)195B(pcommit, 22)196B(clflushopt, 23)197B(clwb, 24)198B(avx512pf, 26)199B(avx512er, 27)200B(avx512cd, 28)201B(sha, 29)202B(avx512bw, 30)203B(avx512vl, 31)204#undef B205#define C(name, bit) X(name, f7c, bit)206C(prefetchwt1, 0)207C(avx512vbmi, 1)208#undef C209210#undef X211212#endif /* ZSTD_COMMON_CPU_H */213214215