Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AVX2 feature detection #93

Closed
dmex opened this issue Oct 29, 2024 · 1 comment
Closed

AVX2 feature detection #93

dmex opened this issue Oct 29, 2024 · 1 comment
Labels
not a bug This issue is incorrect

Comments

@dmex
Copy link

dmex commented Oct 29, 2024

The CRT has feature detection for AVX before calling winmain and this code duplicates the CRT:

[[nodiscard]] bool HasAVX2() {
const static bool fHasAVX2 = []() {
int arrInfo[4] { };
__cpuid(arrInfo, 0);
if (arrInfo[0] >= 7) {
__cpuid(arrInfo, 7);
return (arrInfo[1] & (1 << 5)) != 0;
}
return false;
}();
return fHasAVX2;
}

You can include the isa_availability header and just check the flags:

#include <isa_availability.h>

extern int __isa_available;
#define ISA_AVAILABLE_X86 0
#define ISA_AVAILABLE_SSE2 1
#define ISA_AVAILABLE_SSE42 2
#define ISA_AVAILABLE_AVX 3
#define ISA_AVAILABLE_ENFSTRG 4
#define ISA_AVAILABLE_AVX2 5
#define ISA_AVAILABLE_AVX512 6

extern long __isa_enabled;
#define ISA_ENABLED_X86 0x00000001
#define ISA_ENABLED_SSE2 0x00000002
#define ISA_ENABLED_SSE42 0x00000004
#define ISA_ENABLED_AVX 0x00000008
#define ISA_ENABLED_ENFSTRG 0x00000010
#define ISA_ENABLED_AVX2 0x00000020
#define ISA_ENABLED_AVX512 0x00000040

Example usage (from the STL: https://github.com/microsoft/STL/blob/main/stl/src/vector_algorithms.cpp)

bool _Use_avx2() noexcept {
    return __isa_enabled & (1 << __ISA_AVAILABLE_AVX2);
}
@jovibor
Copy link
Owner

jovibor commented Oct 29, 2024

Well, you're absolutely right.
Except that this header is not standard compliant, and may be a subject to change in the future, see here.
Also, this header was replaced in the STL internal usages recently by the <__msvc_bit_utils.hpp> helper header, see here and here.
Also, it's one more additional dependency, which I would rather avoid.
And eventually, it's just a matter of preference.

@jovibor jovibor added the not a bug This issue is incorrect label Oct 30, 2024
@jovibor jovibor closed this as completed Oct 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
not a bug This issue is incorrect
Projects
None yet
Development

No branches or pull requests

2 participants