serenity/AK/CPUFeatures.h
Dan Klishch 645a220f3b LibCrypto: Use static member function pointer to choose SHA1 SIMD kernel
It turns out we cannot use function multi-versioning with "sha" feature
or even just plain ifunc resolvers without preprocessor guards. So,
instead of feeding ifdef-soup monster, we just use static member
function pointer.

Moving the kernel into the SHA1 class makes it possible to not pass
class members as parameters to it. This, however, requires us to
disambiguate different target "clones" of the kernel using some kind
of template.
2024-07-12 18:30:07 -04:00

60 lines
1.2 KiB
C++

/*
* Copyright (c) 2024, Dan Klishch <danilklishch@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/EnumBits.h>
#include <AK/Types.h>
namespace AK {
enum class CPUFeatures : u64 {
None = 0ULL,
Invalid = 1ULL << 63,
#if !defined(KERNEL) && (ARCH(I386) || ARCH(X86_64))
# define AK_CAN_CODEGEN_FOR_X86_SSE42 1
X86_SSE42 = 1ULL << 0,
# define AK_CAN_CODEGEN_FOR_X86_SHA 1
X86_SHA = 1ULL << 1,
#else
# define AK_CAN_CODEGEN_FOR_X86_SSE42 0
X86_SSE42 = Invalid,
# define AK_CAN_CODEGEN_FOR_X86_SHA 0
X86_SHA = Invalid,
#endif
};
AK_ENUM_BITWISE_OPERATORS(CPUFeatures);
#ifndef KERNEL
namespace Detail {
CPUFeatures detect_cpu_features_uncached();
}
inline CPUFeatures detect_cpu_features()
{
static CPUFeatures const s_cached_features = Detail::detect_cpu_features_uncached();
return s_cached_features;
}
#else
inline CPUFeatures detect_cpu_features()
{
return CPUFeatures::None;
}
#endif
constexpr bool is_valid_feature(CPUFeatures feature)
{
return !has_flag(feature, CPUFeatures::Invalid);
}
}
#ifdef USING_AK_GLOBALLY
using AK::CPUFeatures;
using AK::detect_cpu_features;
#endif