ClickHouse/src/Functions/TargetSpecific.cpp

65 lines
1.5 KiB
C++
Raw Normal View History

#include <Functions/TargetSpecific.h>
2020-04-02 13:48:14 +00:00
2020-04-14 15:46:53 +00:00
#if defined(__GNUC__)
2020-04-06 07:31:26 +00:00
# include <cpuid.h>
2020-05-15 10:10:34 +00:00
# include <x86intrin.h>
2020-04-06 07:31:26 +00:00
#else
# error "Only CLANG and GCC compilers are supported for dynamic dispatch"
#endif
namespace DB
2020-04-02 13:48:14 +00:00
{
2020-05-15 10:10:34 +00:00
__attribute__ ((target("xsave")))
2020-05-16 06:59:08 +00:00
UInt64 xgetbv(UInt32 ecx)
{
2020-05-15 10:10:34 +00:00
return _xgetbv(ecx);
}
2020-05-16 06:59:08 +00:00
UInt32 GetSupportedArches()
{
UInt32 eax, ebx, ecx, edx;
if (!__get_cpuid(1, &eax, &ebx, &ecx, &edx))
2020-04-06 07:31:26 +00:00
return 0;
2020-05-16 06:59:08 +00:00
UInt32 res = 0;
2020-04-06 07:31:26 +00:00
if (ecx & bit_SSE4_2)
2020-05-16 06:59:08 +00:00
res |= static_cast<UInt32>(TargetArch::SSE4);
/// (xgetbv(0) & 0x6) == 0x6 checks that XMM state and YMM state are enabled.
if ((ecx & bit_OSXSAVE) && (ecx & bit_AVX) && (xgetbv(0) & 0x6) == 0x6)
{
res |= static_cast<UInt32>(TargetArch::AVX);
if (__get_cpuid(7, &eax, &ebx, &ecx, &edx) && (ebx & bit_AVX2))
{
res |= static_cast<UInt32>(TargetArch::AVX2);
if (ebx & bit_AVX512F)
{
res |= static_cast<UInt32>(TargetArch::AVX512F);
2020-05-15 10:10:34 +00:00
}
2020-04-06 07:31:26 +00:00
}
}
return res;
}
2020-04-02 13:48:14 +00:00
bool IsArchSupported(TargetArch arch)
{
2020-05-16 06:59:08 +00:00
static UInt32 arches = GetSupportedArches();
return arch == TargetArch::Default || (arches & static_cast<UInt32>(arch));
2020-04-02 13:48:14 +00:00
}
2020-05-15 10:10:34 +00:00
String ToString(TargetArch arch)
{
2020-05-16 06:59:08 +00:00
switch (arch)
{
2020-05-15 10:10:34 +00:00
case TargetArch::Default: return "default";
case TargetArch::SSE4: return "sse4";
case TargetArch::AVX: return "avx";
case TargetArch::AVX2: return "avx2";
2020-05-15 12:00:20 +00:00
case TargetArch::AVX512F: return "avx512f";
2020-05-15 10:10:34 +00:00
}
__builtin_unreachable();
}
2020-05-16 06:59:08 +00:00
}