-
-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1 parent
34179cc
commit 17a49bd
Showing
10 changed files
with
233 additions
and
173 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/// | ||
/// @file cpu_supports_avx512_vbmi2.hpp | ||
/// @brief Detect if the x86 CPU supports AVX512 VBMI2. | ||
/// | ||
/// Copyright (C) 2024 Kim Walisch, <kim.walisch@gmail.com> | ||
/// | ||
/// This file is distributed under the BSD License. See the COPYING | ||
/// file in the top level directory. | ||
/// | ||
|
||
#ifndef CPU_SUPPORTS_AVX512_VBMI2_HPP | ||
#define CPU_SUPPORTS_AVX512_VBMI2_HPP | ||
|
||
#include "cpuid.hpp" | ||
|
||
#if defined(_MSC_VER) | ||
#include <immintrin.h> | ||
#endif | ||
|
||
// %ebx bit flags | ||
#define bit_AVX512F (1 << 16) | ||
|
||
// %ecx bit flags | ||
#define bit_AVX512VBMI (1 << 1) | ||
#define bit_AVX512VBMI2 (1 << 6) | ||
|
||
// xgetbv bit flags | ||
#define XSTATE_SSE (1 << 1) | ||
#define XSTATE_YMM (1 << 2) | ||
#define XSTATE_ZMM (7 << 5) | ||
|
||
namespace { | ||
|
||
// Get Value of Extended Control Register | ||
inline int get_xcr0() | ||
{ | ||
int xcr0; | ||
|
||
#if defined(_MSC_VER) | ||
xcr0 = (int) _xgetbv(0); | ||
#else | ||
__asm__ ("xgetbv" : "=a" (xcr0) : "c" (0) : "%edx" ); | ||
#endif | ||
|
||
return xcr0; | ||
} | ||
|
||
inline bool run_cpuid_avx512_vbmi2() | ||
{ | ||
int abcd[4]; | ||
|
||
run_cpuid(1, 0, abcd); | ||
|
||
int osxsave_mask = (1 << 27); | ||
|
||
// Ensure OS supports extended processor state management | ||
if ((abcd[2] & osxsave_mask) != osxsave_mask) | ||
return false; | ||
|
||
int ymm_mask = XSTATE_SSE | XSTATE_YMM; | ||
int zmm_mask = XSTATE_SSE | XSTATE_YMM | XSTATE_ZMM; | ||
|
||
int xcr0 = get_xcr0(); | ||
|
||
// Check AVX OS support | ||
if ((xcr0 & ymm_mask) != ymm_mask) | ||
return false; | ||
|
||
// Check AVX512 OS support | ||
if ((xcr0 & zmm_mask) != zmm_mask) | ||
return false; | ||
|
||
run_cpuid(7, 0, abcd); | ||
|
||
// PrimeGenerator::fillNextPrimes() requires AVX512F, AVX512VBMI & AVX512VBMI2 | ||
return ((abcd[1] & bit_AVX512F) == bit_AVX512F && | ||
(abcd[2] & (bit_AVX512VBMI | bit_AVX512VBMI2)) == (bit_AVX512VBMI | bit_AVX512VBMI2)); | ||
} | ||
|
||
/// Initialized at startup | ||
bool cpu_supports_avx512_vbmi2 = run_cpuid_avx512_vbmi2(); | ||
|
||
} // namespace | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/// | ||
/// @file cpu_supports_popcnt.hpp | ||
/// @brief POPCNT detection fo x86 and x86-64 CPUs. | ||
/// | ||
/// Copyright (C) 2024 Kim Walisch, <kim.walisch@gmail.com> | ||
/// | ||
/// This file is distributed under the BSD License. See the COPYING | ||
/// file in the top level directory. | ||
/// | ||
|
||
#ifndef CPU_SUPPORTS_POPCNT_HPP | ||
#define CPU_SUPPORTS_POPCNT_HPP | ||
|
||
// Enable CPUID on x86 and x86-64 CPUs | ||
#if defined(__x86_64__) || \ | ||
defined(__i386__) || \ | ||
defined(_M_X64) || \ | ||
defined(_M_IX86) | ||
|
||
// Both GCC and Clang (even Clang on Windows) define the __POPCNT__ | ||
// macro if the user compiles with -mpopcnt. The __POPCNT__ | ||
// macro is even defined if the user compiles with other flags | ||
// such as -mavx or -march=native. | ||
#if defined(__POPCNT__) | ||
#define HAS_POPCNT | ||
// The MSVC compiler does not support a POPCNT macro, but if the user | ||
// compiles with e.g. /arch:AVX or /arch:AVX512 then MSVC defines | ||
// the __AVX__ macro and POPCNT is also supported. | ||
#elif defined(_MSC_VER) && defined(__AVX__) | ||
#define HAS_POPCNT | ||
#endif | ||
|
||
#if !defined(HAS_POPCNT) | ||
|
||
#include "cpuid.hpp" | ||
#define ENABLE_CPUID_POPCNT | ||
|
||
namespace { | ||
|
||
inline bool run_cpuid_supports_popcnt() | ||
{ | ||
int abcd[4]; | ||
run_cpuid(1, 0, abcd); | ||
|
||
// %ecx POPCNT bit flag | ||
int bit_POPCNT = 1 << 23; | ||
return (abcd[2] & bit_POPCNT) == bit_POPCNT; | ||
} | ||
|
||
/// Initialized at startup | ||
bool cpu_supports_popcnt = run_cpuid_supports_popcnt(); | ||
|
||
} // namespace | ||
|
||
#endif // !defined(HAS_POPCNT) | ||
#endif // CPUID | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters