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