Skip to content

Commit 82e0d2b

Browse files
committed
cpuid_ppc.cpp: fix for macOS
1 parent f871218 commit 82e0d2b

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

src/lib/utils/cpuid/cpuid_ppc/cpuid_ppc.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,44 @@
1313
#include <botan/internal/os_utils.h>
1414
#endif
1515

16+
/*
17+
* On macOS and OpenBSD ppc, use sysctl to detect AltiVec
18+
*/
19+
#if defined(BOTAN_TARGET_OS_IS_MACOS)
20+
#include <sys/sysctl.h>
21+
#elif defined(BOTAN_TARGET_OS_IS_OPENBSD)
22+
#include <sys/param.h>
23+
#include <sys/sysctl.h>
24+
#include <machine/cpu.h>
25+
#endif
26+
1627
namespace Botan {
1728

1829
uint32_t CPUID::CPUID_Data::detect_cpu_features(uint32_t allowed) {
1930
uint32_t feat = 0;
2031

32+
#if defined(BOTAN_TARGET_OS_IS_MACOS) || defined(BOTAN_TARGET_OS_IS_OPENBSD)
33+
// On macOS and OpenBSD, use sysctl
34+
int sels[2] = {
35+
#if defined(BOTAN_TARGET_OS_IS_OPENBSD)
36+
CTL_MACHDEP, CPU_ALTIVEC
37+
#else
38+
CTL_HW, HW_VECTORUNIT
39+
#endif
40+
};
41+
42+
int vector_type = 0;
43+
size_t length = sizeof(vector_type);
44+
int error = ::sysctl(sels, 2, &vector_type, &length, NULL, 0);
45+
46+
if(error == 0 && vector_type > 0) {
47+
feat |= CPUFeature::Bit::ALTIVEC;
48+
}
49+
50+
return feat;
51+
52+
#else // defined(BOTAN_TARGET_OS_IS_MACOS) || defined(BOTAN_TARGET_OS_IS_OPENBSD)
53+
2154
#if defined(BOTAN_HAS_OS_UTILS)
2255

2356
if(auto auxval = OS::get_auxval_hwcap()) {
@@ -80,6 +113,8 @@ uint32_t CPUID::CPUID_Data::detect_cpu_features(uint32_t allowed) {
80113
#endif
81114

82115
return feat;
116+
117+
#endif // defined(BOTAN_TARGET_OS_IS_MACOS) || defined(BOTAN_TARGET_OS_IS_OPENBSD)
83118
}
84119

85120
} // namespace Botan

0 commit comments

Comments
 (0)