Skip to content

Commit

Permalink
ENH: Detect CPU features on FreeBSD/powerpc64*
Browse files Browse the repository at this point in the history
1. FreeBSD uses elf_aux_info() instead of getauxval.
2. Switch to using compiler macros for detecting POWER platform
FreeBSD sets the machine name (what uname -m prints) on all powerpc* to
just powerpc. To identify actual architecture, uname -p should be used,
but this is not present in uname() function. Thus, the only way to
correctly detect platform is to use what uname prints and also check
compiler defines.
  • Loading branch information
pkubaj authored and charris committed Jan 8, 2023
1 parent c341fcd commit 90128b6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
16 changes: 14 additions & 2 deletions numpy/core/src/common/npy_cpu_features.c
Expand Up @@ -513,7 +513,10 @@ npy__cpu_init_features(void)

#elif defined(NPY_CPU_PPC64) || defined(NPY_CPU_PPC64LE)

#ifdef __linux__
#if defined(__linux__) || defined(__FreeBSD__)
#ifdef __FreeBSD__
#include <machine/cpu.h> // defines PPC_FEATURE_HAS_VSX
#endif
#include <sys/auxv.h>
#ifndef AT_HWCAP2
#define AT_HWCAP2 26
Expand All @@ -533,12 +536,21 @@ static void
npy__cpu_init_features(void)
{
memset(npy__cpu_have, 0, sizeof(npy__cpu_have[0]) * NPY_CPU_FEATURE_MAX);
#if defined(__linux__) || defined(__FreeBSD__)
#ifdef __linux__
unsigned int hwcap = getauxval(AT_HWCAP);
if ((hwcap & PPC_FEATURE_HAS_VSX) == 0)
return;

hwcap = getauxval(AT_HWCAP2);
#else
unsigned long hwcap;
elf_aux_info(AT_HWCAP, &hwcap, sizeof(hwcap));
if ((hwcap & PPC_FEATURE_HAS_VSX) == 0)
return;

elf_aux_info(AT_HWCAP2, &hwcap, sizeof(hwcap));
#endif // __linux__
if (hwcap & PPC_FEATURE2_ARCH_3_1)
{
npy__cpu_have[NPY_CPU_FEATURE_VSX] =
Expand All @@ -551,7 +563,7 @@ npy__cpu_init_features(void)
npy__cpu_have[NPY_CPU_FEATURE_VSX2] = (hwcap & PPC_FEATURE2_ARCH_2_07) != 0;
npy__cpu_have[NPY_CPU_FEATURE_VSX3] = (hwcap & PPC_FEATURE2_ARCH_3_00) != 0;
npy__cpu_have[NPY_CPU_FEATURE_VSX4] = (hwcap & PPC_FEATURE2_ARCH_3_1) != 0;
// TODO: AIX, FreeBSD
// TODO: AIX, OpenBSD
#else
npy__cpu_have[NPY_CPU_FEATURE_VSX] = 1;
#if defined(NPY_CPU_PPC64LE) || defined(NPY_HAVE_VSX2)
Expand Down
8 changes: 6 additions & 2 deletions numpy/distutils/ccompiler_opt.py
Expand Up @@ -959,8 +959,12 @@ def __init__(self):
detect_arch = (
("cc_on_x64", ".*(x|x86_|amd)64.*", ""),
("cc_on_x86", ".*(win32|x86|i386|i686).*", ""),
("cc_on_ppc64le", ".*(powerpc|ppc)64(el|le).*", ""),
("cc_on_ppc64", ".*(powerpc|ppc)64.*", ""),
("cc_on_ppc64le", ".*(powerpc|ppc)64(el|le).*|.*powerpc.*",
"defined(__powerpc64__) && "
"defined(__LITTLE_ENDIAN__)"),
("cc_on_ppc64", ".*(powerpc|ppc).*|.*powerpc.*",
"defined(__powerpc64__) && "
"defined(__BIG_ENDIAN__)"),
("cc_on_aarch64", ".*(aarch64|arm64).*", ""),
("cc_on_armhf", ".*arm.*", "defined(__ARM_ARCH_7__) || "
"defined(__ARM_ARCH_7A__)"),
Expand Down

0 comments on commit 90128b6

Please sign in to comment.