Skip to content

Commit 89f6fe1

Browse files
committed
kram - add linux simd checks
1 parent f165d67 commit 89f6fe1

1 file changed

Lines changed: 49 additions & 3 deletions

File tree

kramc/KramMain.cpp

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ using namespace STL_NAMESPACE;
1414
void checkSimdSupport()
1515
{
1616
// Check for AVX2, FMA, F16C support on Intel.
17-
// AVX2 implies the other 2, but still have to enable on compile.
18-
// arm64 just has everything needed. No holes to check, or legacy simd.
17+
// Still need to set compile flags, and arm64 emulators are also spotty.
18+
// arm64 native has everything needed. No holes to check, or legacy simd.
1919

2020
#if SIMD_AVX2
2121
#if KRAM_MAC
@@ -90,6 +90,7 @@ void checkSimdSupport()
9090
// Make sure compile has enabled these on AVX2.
9191
// Rosetta2 and Prism often don't emulate these.
9292
// (f.e. BMI and F16C)
93+
9394
bool hasAVX = strstr(bufferFeatures.data(), "AVX") != nullptr;
9495
bool hasF16C = strstr(bufferFeatures.data(), "F16C") != nullptr;
9596
bool hasFMA = strstr(bufferFeatures.data(), "FMA") != nullptr;
@@ -108,7 +109,6 @@ void checkSimdSupport()
108109
}
109110
}
110111

111-
// TODO: can add brand to this if find the sysctlbyname query
112112
if (!hasSimdSupport) {
113113
KLOGE("Main", "Missing simd support for %s%s%s%s on %s",
114114
missingFeatures[0], missingFeatures[1], missingFeatures[2], missingFeatures[3],
@@ -232,6 +232,52 @@ void checkSimdSupport()
232232
brandId);
233233
exit(1);
234234
}
235+
236+
#elif KRAM_LINUX
237+
238+
// This should apply to all clang and gcc builds. So may want
239+
// to use on all platforms.
240+
241+
// Common CPU features that can be checked with __builtin_cpu_supports include:
242+
// sse, sse2, sse3, ssse3, sse4.1, sse4.2
243+
// avx, avx2, avx512f
244+
// fma
245+
// bmi, bmi2
246+
// popcnt
247+
// lzcnt
248+
// mmx
249+
250+
251+
bool hasSimdSupport = true;
252+
253+
bool hasAVX2 = __builtin_cpu_supports("avx2");
254+
255+
bool hasFMA = __builtin_cpu_supports("fma");
256+
bool hasAVX = __builtin_cpu_supports("avx");
257+
bool hasF16C = __builtin_cpu_supports("f16c");
258+
259+
if (!hasAVX2) {
260+
missingFeatures[missingFeaturesCount++] = "AVX2 ";
261+
hasSimdSupport = false;
262+
}
263+
if (!hasAVX) {
264+
missingFeatures[missingFeaturesCount++] = "AVX ";
265+
hasSimdSupport = false;
266+
}
267+
if (!hasFMA) {
268+
missingFeatures[missingFeaturesCount++] = "FMA ";
269+
hasSimdSupport = false;
270+
}
271+
if (!hasF16C) {
272+
missingFeatures[missingFeaturesCount++] = "F16C ";
273+
hasSimdSupport = false;
274+
}
275+
276+
if (!hasSimdSupport) {
277+
KLOGE("Main", "Missing simd support for %s%s%s%s",
278+
missingFeatures[0], missingFeatures[1], missingFeatures[2], missingFeatures[3]);
279+
exit(1);
280+
}
235281

236282
#endif
237283
#endif

0 commit comments

Comments
 (0)