| 1 |
// Mostly copied from libdeflate/lib/x86/cpu_features.c , except that we're |
| 2 |
// interested in different features (SSE4.2, AVX2). |
| 3 |
// |
| 4 |
// It's currently just used to print a more informative error message when e.g. |
| 5 |
// the AVX2 build is run on a machine that doesn't support it. As a |
| 6 |
// consequence, it must be compiled with minimal flags (just -O2, |
| 7 |
// -DCPU_CHECK_xxx, and warnings). |
| 8 |
// |
| 9 |
// Original license text follows. |
| 10 |
|
| 11 |
/* |
| 12 |
* x86/cpu_features.c - feature detection for x86 processors |
| 13 |
* |
| 14 |
* Copyright 2016 Eric Biggers |
| 15 |
* |
| 16 |
* Permission is hereby granted, free of charge, to any person |
| 17 |
* obtaining a copy of this software and associated documentation |
| 18 |
* files (the "Software"), to deal in the Software without |
| 19 |
* restriction, including without limitation the rights to use, |
| 20 |
* copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 21 |
* copies of the Software, and to permit persons to whom the |
| 22 |
* Software is furnished to do so, subject to the following |
| 23 |
* conditions: |
| 24 |
* |
| 25 |
* The above copyright notice and this permission notice shall be |
| 26 |
* included in all copies or substantial portions of the Software. |
| 27 |
* |
| 28 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 29 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 30 |
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 31 |
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 32 |
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 33 |
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 34 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 35 |
* OTHER DEALINGS IN THE SOFTWARE. |
| 36 |
*/ |
| 37 |
|
| 38 |
#if defined(CPU_CHECK_SSE42) || defined(CPU_CHECK_AVX2) |
| 39 |
|
| 40 |
# include <stdio.h> |
| 41 |
# include <stdlib.h> |
| 42 |
# include <stdint.h> |
| 43 |
# include <inttypes.h> |
| 44 |
|
| 45 |
/* With old GCC versions we have to manually save and restore the x86_32 PIC |
| 46 |
* register (ebx). See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=47602 */ |
| 47 |
# if defined(__i386__) && defined(__PIC__) |
| 48 |
# define EBX_CONSTRAINT "=r" |
| 49 |
# else |
| 50 |
# define EBX_CONSTRAINT "=b" |
| 51 |
# endif |
| 52 |
|
| 53 |
/* Execute the CPUID instruction. */ |
| 54 |
static inline void cpuid(uint32_t leaf, uint32_t subleaf, uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d) { |
| 55 |
__asm__(".ifnc %%ebx, %1; mov %%ebx, %1; .endif\n" |
| 56 |
"cpuid \n" |
| 57 |
".ifnc %%ebx, %1; xchg %%ebx, %1; .endif\n" |
| 58 |
: "=a" (*a), EBX_CONSTRAINT (*b), "=c" (*c), "=d" (*d) |
| 59 |
: "a" (leaf), "c" (subleaf)); |
| 60 |
} |
| 61 |
|
| 62 |
# ifdef CPU_CHECK_AVX2 |
| 63 |
/* Read an extended control register. */ |
| 64 |
static inline uint64_t read_xcr(uint32_t index) { |
| 65 |
uint32_t edx, eax; |
| 66 |
|
| 67 |
/* Execute the "xgetbv" instruction. Old versions of binutils do not |
| 68 |
* recognize this instruction, so list the raw bytes instead. */ |
| 69 |
__asm__ (".byte 0x0f, 0x01, 0xd0" : "=d" (edx), "=a" (eax) : "c" (index)); |
| 70 |
# ifdef __cplusplus |
| 71 |
return (static_cast<uint64_t>(edx) << 32) | eax; |
| 72 |
# else |
| 73 |
return (((uint64_t)edx) << 32) | eax; |
| 74 |
# endif |
| 75 |
} |
| 76 |
# endif |
| 77 |
|
| 78 |
extern int RealMain(int argc, char** argv); |
| 79 |
|
| 80 |
# define unlikely(expr) __builtin_expect(!!(expr), 0) |
| 81 |
|
| 82 |
int main(int argc, char** argv) { |
| 83 |
uint32_t dummy1, dummy2, dummy3, dummy4; |
| 84 |
uint32_t max_function; |
| 85 |
uint32_t features_2; |
| 86 |
# ifdef CPU_CHECK_AVX2 |
| 87 |
uint32_t features_3, features_4; |
| 88 |
# endif |
| 89 |
|
| 90 |
/* Get maximum supported function */ |
| 91 |
cpuid(0, 0, &max_function, &dummy2, &dummy3, &dummy4); |
| 92 |
if (unlikely(max_function < 1)) { |
| 93 |
// er, is this possible given that the 64-bit build executes at all? well, |
| 94 |
// leave it in for now. |
| 95 |
goto CpuCheck_ret_SSE42_FAIL; |
| 96 |
} |
| 97 |
|
| 98 |
/* Standard feature flags */ |
| 99 |
cpuid(1, 0, &dummy1, &dummy2, &features_2, &dummy4); |
| 100 |
|
| 101 |
// bit 20 = SSE4.2 |
| 102 |
if (unlikely(!(features_2 & 0x100000))) { |
| 103 |
goto CpuCheck_ret_SSE42_FAIL; |
| 104 |
} |
| 105 |
|
| 106 |
# ifdef CPU_CHECK_AVX2 |
| 107 |
// os_saves_ymm_regs must be true for AVX2 |
| 108 |
if (unlikely((!(features_2 & 0x8000000)) || |
| 109 |
((read_xcr(0) & 0x6) != 0x6))) { |
| 110 |
goto CpuCheck_ret_AVX2_FAIL; |
| 111 |
} |
| 112 |
if (unlikely(max_function < 7)) { |
| 113 |
goto CpuCheck_ret_AVX2_FAIL; |
| 114 |
} |
| 115 |
cpuid(7, 0, &dummy1, &features_3, &features_4, &dummy4); |
| 116 |
// bit 3 = BMI |
| 117 |
// bit 5 = AVX2 |
| 118 |
// bit 8 = BMI2 |
| 119 |
if (unlikely((features_3 & 0x128) != 0x128)) { |
| 120 |
goto CpuCheck_ret_AVX2_FAIL; |
| 121 |
} |
| 122 |
# endif |
| 123 |
|
| 124 |
return RealMain(argc, argv); |
| 125 |
CpuCheck_ret_SSE42_FAIL: |
| 126 |
fputs("Error: This plink2 build requires a processor which supports SSE4.2\ninstructions. Try a plain 64-bit build instead.\n", stderr); |
| 127 |
exit(13); // 13 = kPglRetUnsupportedInstructions |
| 128 |
# ifdef CPU_CHECK_AVX2 |
| 129 |
CpuCheck_ret_AVX2_FAIL: |
| 130 |
// SSE4.2 doesn't deliver enough of an advantage to justify more clutter on |
| 131 |
// the main downloads page, but technically sophisticated users should be |
| 132 |
// encouraged to build from source in this case. |
| 133 |
fputs("Error: This plink2 build requires a processor which supports AVX2/Haswell\ninstructions, but only SSE4.2 is available. Try a plain 64-bit build instead,\nor use the build_dynamic/ Makefile to produce a binary that takes advantage of\nSSE4.2 instructions but not AVX2.\n", stderr); |
| 134 |
exit(13); |
| 135 |
# endif |
| 136 |
} |
| 137 |
#endif // CPU_CHECK_SSE42 || CPU_CHECK_AVX2 |