|
| 1 | +// Copyright (C) 2024 Joel Rosdahl and other contributors |
| 2 | +// |
| 3 | +// See doc/AUTHORS.adoc for a complete list of contributors. |
| 4 | +// |
| 5 | +// This program is free software; you can redistribute it and/or modify it |
| 6 | +// under the terms of the GNU General Public License as published by the Free |
| 7 | +// Software Foundation; either version 3 of the License, or (at your option) |
| 8 | +// any later version. |
| 9 | +// |
| 10 | +// This program is distributed in the hope that it will be useful, but WITHOUT |
| 11 | +// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 13 | +// more details. |
| 14 | +// |
| 15 | +// You should have received a copy of the GNU General Public License along with |
| 16 | +// this program; if not, write to the Free Software Foundation, Inc., 51 |
| 17 | +// Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | + |
| 19 | +#include "cpu.hpp" |
| 20 | + |
| 21 | +#ifdef _MSC_VER |
| 22 | +# include <intrin.h> |
| 23 | +#endif |
| 24 | + |
| 25 | +#ifdef HAVE_CPUID_H |
| 26 | +# include <cpuid.h> |
| 27 | +#endif |
| 28 | + |
| 29 | +namespace util { |
| 30 | + |
| 31 | +bool |
| 32 | +cpu_supports_avx2() |
| 33 | +{ |
| 34 | + // CPUID with EAX=7 ECX=0 returns AVX2 support in bit 5 of EBX. |
| 35 | + int registers[4]; // EAX, EBX, ECX, EDX |
| 36 | +#if defined(_MSC_VER) && defined(_M_X64) |
| 37 | + __cpuidex(registers, 7, 0); |
| 38 | +#elif defined(HAVE_CPUID_H) |
| 39 | + __cpuid_count(7, 0, registers[0], registers[1], registers[2], registers[3]); |
| 40 | +#elif __x86_64__ |
| 41 | + __asm__ __volatile__("cpuid" |
| 42 | + : "=a"(registers[0]), |
| 43 | + "=b"(registers[1]), |
| 44 | + "=c"(registers[2]), |
| 45 | + "=d"(registers[3]) |
| 46 | + : "a"(7), "c"(0)); |
| 47 | +#else |
| 48 | + registers[1] = 0; |
| 49 | +#endif |
| 50 | + return registers[1] & (1 << 5); |
| 51 | +} |
| 52 | + |
| 53 | +} // namespace util |
0 commit comments