forked from kimwalisch/primesieve
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
POPCNT runtime detection using CPUID for x86 CPUs (kimwalisch#148)
- Loading branch information
1 parent
5cbf2e2
commit 55cdcce
Showing
15 changed files
with
415 additions
and
247 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 was deleted.
Oops, something went wrong.
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,94 @@ | ||
/// | ||
/// @file CPUID.hpp | ||
/// @brief POPCNT detection fo x86 and x86-64 CPUs. | ||
/// | ||
/// Copyright (C) 2024 Kim Walisch, <[email protected]> | ||
/// | ||
/// This file is distributed under the BSD License. See the COPYING | ||
/// file in the top level directory. | ||
/// | ||
|
||
#ifndef CPUID_HPP | ||
#define CPUID_HPP | ||
|
||
// Enable 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(_MSC_VER) | ||
#include <intrin.h> | ||
#endif | ||
|
||
namespace { | ||
|
||
inline void run_CPUID(int eax, int ecx, int* abcd) | ||
{ | ||
#if defined(_MSC_VER) | ||
__cpuidex(abcd, eax, ecx); | ||
#else | ||
int ebx = 0; | ||
int edx = 0; | ||
|
||
#if defined(__i386__) && \ | ||
defined(__PIC__) | ||
/* in case of PIC under 32-bit EBX cannot be clobbered */ | ||
__asm__ ("movl %%ebx, %%edi;" | ||
"cpuid;" | ||
"xchgl %%ebx, %%edi;" | ||
: "=D" (ebx), | ||
"+a" (eax), | ||
"+c" (ecx), | ||
"=d" (edx)); | ||
#else | ||
__asm__ ("cpuid;" | ||
: "+b" (ebx), | ||
"+a" (eax), | ||
"+c" (ecx), | ||
"=d" (edx)); | ||
#endif | ||
|
||
abcd[0] = eax; | ||
abcd[1] = ebx; | ||
abcd[2] = ecx; | ||
abcd[3] = edx; | ||
#endif | ||
} | ||
|
||
#if !defined(HAS_POPCNT) | ||
#define ENABLE_CPUID_POPCNT | ||
|
||
inline bool run_CPUID_POPCNT() | ||
{ | ||
// %ecx POPCNT bit flag | ||
int bit_POPCNT = 1 << 23; | ||
int abcd[4]; | ||
|
||
run_CPUID(1, 0, abcd); | ||
return (abcd[2] & bit_POPCNT) == bit_POPCNT; | ||
} | ||
|
||
/// Initialized at startup | ||
const bool HAS_CPUID_POPCNT = run_CPUID_POPCNT(); | ||
|
||
#endif // ENABLE_CPUID_POPCNT | ||
|
||
} // namespace | ||
|
||
#endif // x86 CPU | ||
|
||
#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 |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
/// returns the primes. When there are no more primes left in | ||
/// the vector PrimeGenerator generates new primes. | ||
/// | ||
/// Copyright (C) 2023 Kim Walisch, <[email protected]> | ||
/// Copyright (C) 2024 Kim Walisch, <[email protected]> | ||
/// | ||
/// This file is distributed under the BSD License. See the COPYING | ||
/// file in the top level directory. | ||
|
@@ -23,6 +23,21 @@ | |
#include <stdint.h> | ||
#include <cstddef> | ||
|
||
#if defined(MULTIARCH_AVX512) | ||
// GCC/Clang function multiversioning for AVX512 is not needed if | ||
// the user compiles with -mavx512f -mavx512vbmi -mavx512vbmi2. | ||
// GCC/Clang function multiversioning generally causes a minor | ||
// overhead, hence we disable it if it is not needed. | ||
#if defined(__AVX512__) || (defined(__AVX512F__) && \ | ||
defined(__AVX512VBMI__) && \ | ||
defined(__AVX512VBMI2__)) | ||
#undef MULTIARCH_AVX512 | ||
#else | ||
#define MULTIARCH_TARGET_DEFAULT | ||
#define MULTIARCH_TARGET_AVX512 | ||
#endif | ||
#endif | ||
|
||
namespace primesieve { | ||
|
||
class PreSieve; | ||
|
@@ -34,22 +49,15 @@ class PrimeGenerator : public Erat | |
void fillPrevPrimes(Vector<uint64_t>& primes, std::size_t* size); | ||
static uint64_t maxCachedPrime(); | ||
|
||
#if defined(MULTIARCH_POPCNT_BMI) | ||
#define MULTIARCH | ||
__attribute__ ((target ("popcnt,bmi"))) | ||
void fillNextPrimes(Vector<uint64_t>& primes, std::size_t* size); | ||
#if defined(MULTIARCH_TARGET_DEFAULT) | ||
__attribute__ ((target ("default"))) | ||
#endif | ||
|
||
#if defined(MULTIARCH_AVX512) | ||
#define MULTIARCH | ||
__attribute__ ((target ("avx512f,avx512vbmi,avx512vbmi2,popcnt"))) | ||
void fillNextPrimes(Vector<uint64_t>& primes, std::size_t* size); | ||
#endif | ||
|
||
#if defined(MULTIARCH) | ||
__attribute__ ((target ("default"))) | ||
#endif | ||
#if defined(MULTIARCH_TARGET_AVX512) | ||
__attribute__ ((target ("avx512f,avx512vbmi,avx512vbmi2"))) | ||
void fillNextPrimes(Vector<uint64_t>& primes, std::size_t* size); | ||
#endif | ||
|
||
private: | ||
bool isInit_ = false; | ||
|
Oops, something went wrong.