Brandon Maier | dbe88da | 2023-01-12 10:27:45 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) Facebook, Inc. |
| 3 | * All rights reserved. |
| 4 | * |
| 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. |
| 9 | */ |
| 10 | |
| 11 | #ifndef ZSTD_COMMON_CPU_H |
| 12 | #define ZSTD_COMMON_CPU_H |
| 13 | |
| 14 | /* |
| 15 | * Implementation taken from folly/CpuId.h |
| 16 | * https://github.com/facebook/folly/blob/master/folly/CpuId.h |
| 17 | */ |
| 18 | |
| 19 | #include "mem.h" |
| 20 | |
Brandon Maier | dbe88da | 2023-01-12 10:27:45 -0600 | [diff] [blame] | 21 | typedef struct { |
| 22 | U32 f1c; |
| 23 | U32 f1d; |
| 24 | U32 f7b; |
| 25 | U32 f7c; |
| 26 | } ZSTD_cpuid_t; |
| 27 | |
| 28 | MEM_STATIC ZSTD_cpuid_t ZSTD_cpuid(void) { |
| 29 | U32 f1c = 0; |
| 30 | U32 f1d = 0; |
| 31 | U32 f7b = 0; |
| 32 | U32 f7c = 0; |
| 33 | #if defined(__i386__) && defined(__PIC__) && !defined(__clang__) && defined(__GNUC__) |
| 34 | /* The following block like the normal cpuid branch below, but gcc |
| 35 | * reserves ebx for use of its pic register so we must specially |
| 36 | * handle the save and restore to avoid clobbering the register |
| 37 | */ |
| 38 | U32 n; |
| 39 | __asm__( |
| 40 | "pushl %%ebx\n\t" |
| 41 | "cpuid\n\t" |
| 42 | "popl %%ebx\n\t" |
| 43 | : "=a"(n) |
| 44 | : "a"(0) |
| 45 | : "ecx", "edx"); |
| 46 | if (n >= 1) { |
| 47 | U32 f1a; |
| 48 | __asm__( |
| 49 | "pushl %%ebx\n\t" |
| 50 | "cpuid\n\t" |
| 51 | "popl %%ebx\n\t" |
| 52 | : "=a"(f1a), "=c"(f1c), "=d"(f1d) |
| 53 | : "a"(1)); |
| 54 | } |
| 55 | if (n >= 7) { |
| 56 | __asm__( |
| 57 | "pushl %%ebx\n\t" |
| 58 | "cpuid\n\t" |
| 59 | "movl %%ebx, %%eax\n\t" |
| 60 | "popl %%ebx" |
| 61 | : "=a"(f7b), "=c"(f7c) |
| 62 | : "a"(7), "c"(0) |
| 63 | : "edx"); |
| 64 | } |
| 65 | #elif defined(__x86_64__) || defined(_M_X64) || defined(__i386__) |
| 66 | U32 n; |
| 67 | __asm__("cpuid" : "=a"(n) : "a"(0) : "ebx", "ecx", "edx"); |
| 68 | if (n >= 1) { |
| 69 | U32 f1a; |
| 70 | __asm__("cpuid" : "=a"(f1a), "=c"(f1c), "=d"(f1d) : "a"(1) : "ebx"); |
| 71 | } |
| 72 | if (n >= 7) { |
| 73 | U32 f7a; |
| 74 | __asm__("cpuid" |
| 75 | : "=a"(f7a), "=b"(f7b), "=c"(f7c) |
| 76 | : "a"(7), "c"(0) |
| 77 | : "edx"); |
| 78 | } |
| 79 | #endif |
| 80 | { |
| 81 | ZSTD_cpuid_t cpuid; |
| 82 | cpuid.f1c = f1c; |
| 83 | cpuid.f1d = f1d; |
| 84 | cpuid.f7b = f7b; |
| 85 | cpuid.f7c = f7c; |
| 86 | return cpuid; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | #define X(name, r, bit) \ |
| 91 | MEM_STATIC int ZSTD_cpuid_##name(ZSTD_cpuid_t const cpuid) { \ |
| 92 | return ((cpuid.r) & (1U << bit)) != 0; \ |
| 93 | } |
| 94 | |
| 95 | /* cpuid(1): Processor Info and Feature Bits. */ |
| 96 | #define C(name, bit) X(name, f1c, bit) |
| 97 | C(sse3, 0) |
| 98 | C(pclmuldq, 1) |
| 99 | C(dtes64, 2) |
| 100 | C(monitor, 3) |
| 101 | C(dscpl, 4) |
| 102 | C(vmx, 5) |
| 103 | C(smx, 6) |
| 104 | C(eist, 7) |
| 105 | C(tm2, 8) |
| 106 | C(ssse3, 9) |
| 107 | C(cnxtid, 10) |
| 108 | C(fma, 12) |
| 109 | C(cx16, 13) |
| 110 | C(xtpr, 14) |
| 111 | C(pdcm, 15) |
| 112 | C(pcid, 17) |
| 113 | C(dca, 18) |
| 114 | C(sse41, 19) |
| 115 | C(sse42, 20) |
| 116 | C(x2apic, 21) |
| 117 | C(movbe, 22) |
| 118 | C(popcnt, 23) |
| 119 | C(tscdeadline, 24) |
| 120 | C(aes, 25) |
| 121 | C(xsave, 26) |
| 122 | C(osxsave, 27) |
| 123 | C(avx, 28) |
| 124 | C(f16c, 29) |
| 125 | C(rdrand, 30) |
| 126 | #undef C |
| 127 | #define D(name, bit) X(name, f1d, bit) |
| 128 | D(fpu, 0) |
| 129 | D(vme, 1) |
| 130 | D(de, 2) |
| 131 | D(pse, 3) |
| 132 | D(tsc, 4) |
| 133 | D(msr, 5) |
| 134 | D(pae, 6) |
| 135 | D(mce, 7) |
| 136 | D(cx8, 8) |
| 137 | D(apic, 9) |
| 138 | D(sep, 11) |
| 139 | D(mtrr, 12) |
| 140 | D(pge, 13) |
| 141 | D(mca, 14) |
| 142 | D(cmov, 15) |
| 143 | D(pat, 16) |
| 144 | D(pse36, 17) |
| 145 | D(psn, 18) |
| 146 | D(clfsh, 19) |
| 147 | D(ds, 21) |
| 148 | D(acpi, 22) |
| 149 | D(mmx, 23) |
| 150 | D(fxsr, 24) |
| 151 | D(sse, 25) |
| 152 | D(sse2, 26) |
| 153 | D(ss, 27) |
| 154 | D(htt, 28) |
| 155 | D(tm, 29) |
| 156 | D(pbe, 31) |
| 157 | #undef D |
| 158 | |
| 159 | /* cpuid(7): Extended Features. */ |
| 160 | #define B(name, bit) X(name, f7b, bit) |
| 161 | B(bmi1, 3) |
| 162 | B(hle, 4) |
| 163 | B(avx2, 5) |
| 164 | B(smep, 7) |
| 165 | B(bmi2, 8) |
| 166 | B(erms, 9) |
| 167 | B(invpcid, 10) |
| 168 | B(rtm, 11) |
| 169 | B(mpx, 14) |
| 170 | B(avx512f, 16) |
| 171 | B(avx512dq, 17) |
| 172 | B(rdseed, 18) |
| 173 | B(adx, 19) |
| 174 | B(smap, 20) |
| 175 | B(avx512ifma, 21) |
| 176 | B(pcommit, 22) |
| 177 | B(clflushopt, 23) |
| 178 | B(clwb, 24) |
| 179 | B(avx512pf, 26) |
| 180 | B(avx512er, 27) |
| 181 | B(avx512cd, 28) |
| 182 | B(sha, 29) |
| 183 | B(avx512bw, 30) |
| 184 | B(avx512vl, 31) |
| 185 | #undef B |
| 186 | #define C(name, bit) X(name, f7c, bit) |
| 187 | C(prefetchwt1, 0) |
| 188 | C(avx512vbmi, 1) |
| 189 | #undef C |
| 190 | |
| 191 | #undef X |
| 192 | |
| 193 | #endif /* ZSTD_COMMON_CPU_H */ |