-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add runtime detection of CPU extensions
Signed-off-by: Roman Gershman <roman@dragonflydb.io>
- Loading branch information
Showing
4 changed files
with
117 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Copyright 2024, Roman Gershman. All rights reserved. | ||
// See LICENSE for licensing terms. | ||
// | ||
|
||
#include "base/cpu_features.h" | ||
|
||
#include <cstdint> | ||
|
||
namespace base { | ||
|
||
namespace { | ||
|
||
// aarch64 is currently a noop. | ||
#ifdef __x86_64__ | ||
|
||
// See <cpuid.h> for constants reference | ||
constexpr unsigned BIT_AVX2 = (1 << 5); | ||
constexpr unsigned BIT_AVX512F = (1 << 16); | ||
|
||
constexpr unsigned BIT_XSAVE = (1 << 26); | ||
constexpr unsigned BIT_OSXSAVE = (1 << 27); | ||
|
||
// A struct to hold the result of a call to cpuid. | ||
typedef struct { | ||
uint32_t eax, ebx, ecx, edx; | ||
} Leaf; | ||
|
||
Leaf GetCpuidLeaf(uint32_t leaf_id) { | ||
Leaf leaf; | ||
__asm__ __volatile__("cpuid" | ||
: "=a"(leaf.eax), "=b"(leaf.ebx), "=c"(leaf.ecx), "=d"(leaf.edx) | ||
: "a"(leaf_id), "c"(0)); | ||
return leaf; | ||
} | ||
|
||
uint32_t GetXCR0() { | ||
uint32_t xcr0; | ||
__asm__("xgetbv" : "=a"(xcr0) : "c"(0) : "%edx"); | ||
return xcr0; | ||
} | ||
|
||
} | ||
|
||
CpuFeatures GetCpuFeatures() { | ||
CpuFeatures res; | ||
Leaf leaf = GetCpuidLeaf(0); | ||
const uint32_t max_cpuid_leaf = leaf.eax; | ||
|
||
if (max_cpuid_leaf < 7) | ||
return res; | ||
|
||
leaf = GetCpuidLeaf(1); | ||
|
||
bool has_xcr0 = (leaf.ecx & BIT_OSXSAVE) && (leaf.ecx & BIT_XSAVE); | ||
if (!has_xcr0) | ||
return res; | ||
|
||
leaf = GetCpuidLeaf(7); | ||
const uint32_t xcr0 = GetXCR0(); | ||
|
||
if ((xcr0 & 6) != 6) | ||
return res; | ||
|
||
// See https://en.wikichip.org/wiki/x86/avx-512 for explanation about the variants | ||
res.has_avx2 = leaf.ebx & BIT_AVX2; | ||
res.has_avx512f = leaf.ebx & BIT_AVX512F; | ||
|
||
return res; | ||
} | ||
|
||
#endif | ||
|
||
} // namespace base |
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,35 @@ | ||
// Copyright 2024, Roman Gershman. All rights reserved. | ||
// See LICENSE for licensing terms. | ||
// | ||
|
||
#pragma once | ||
|
||
// Much slimmer version of https://github.com/google/cpu_features/ | ||
// Assumes only relatively recent cpu families (found in the cloud) | ||
namespace base { | ||
|
||
struct CpuFeatures { | ||
#ifdef __x86_64__ | ||
bool has_avx2 = false; | ||
bool has_avx512f = false; | ||
#endif | ||
|
||
#ifdef __aarch64__ | ||
// TBD | ||
#endif | ||
}; | ||
|
||
#ifdef __x86_64__ | ||
CpuFeatures GetCpuFeatures(); | ||
|
||
#else | ||
|
||
// Stub for now. | ||
inline CpuFeatures GetCpuFeatures() { | ||
return CpuFeatures{}; | ||
|
||
} | ||
|
||
#endif | ||
|
||
} // namespace base |
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