| 1 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 |
|
| 5 |
/* compile-time and runtime tests for whether to use Power ISA-specific |
| 6 |
* extensions */ |
| 7 |
|
| 8 |
#include "ppc.h" |
| 9 |
#include "mozilla/Unused.h" |
| 10 |
|
| 11 |
#include <stdio.h> |
| 12 |
#include <stdlib.h> |
| 13 |
|
| 14 |
#if defined(XP_LINUX) |
| 15 |
// Use the getauxval() function if available. |
| 16 |
// ARCH_3_00 wasn't defined until glibc 2.23, so include just in case. |
| 17 |
# include <sys/auxv.h> |
| 18 |
# ifndef PPC_FEATURE2_ARCH_3_00 |
| 19 |
# define PPC_FEATURE2_ARCH_3_00 0x00800000 |
| 20 |
# endif |
| 21 |
#endif |
| 22 |
|
| 23 |
const unsigned PPC_FLAG_VMX = 1; |
| 24 |
const unsigned PPC_FLAG_VSX = 2; |
| 25 |
const unsigned PPC_FLAG_VSX3 = 4; |
| 26 |
|
| 27 |
static signed get_ppc_cpu_flags(void) { |
| 28 |
// This could be expensive, so cache the result. |
| 29 |
static signed cpu_flags = -1; |
| 30 |
|
| 31 |
if (cpu_flags > -1) { // already checked |
| 32 |
return cpu_flags; |
| 33 |
} |
| 34 |
cpu_flags = 0; |
| 35 |
|
| 36 |
#if defined(XP_LINUX) |
| 37 |
// Try getauxval(). |
| 38 |
unsigned long int cap = getauxval(AT_HWCAP); |
| 39 |
unsigned long int cap2 = getauxval(AT_HWCAP2); |
| 40 |
|
| 41 |
if (cap & PPC_FEATURE_HAS_ALTIVEC) { |
| 42 |
cpu_flags |= PPC_FLAG_VMX; |
| 43 |
} |
| 44 |
if (cap & PPC_FEATURE_HAS_VSX) { |
| 45 |
cpu_flags |= PPC_FLAG_VSX; |
| 46 |
} |
| 47 |
if (cap2 & PPC_FEATURE2_ARCH_3_00) { |
| 48 |
cpu_flags |= PPC_FLAG_VSX3; |
| 49 |
} |
| 50 |
#else |
| 51 |
// Non-Linux detection here. Currently, on systems other than Linux, |
| 52 |
// no CPU SIMD features will be detected. |
| 53 |
#endif |
| 54 |
|
| 55 |
return cpu_flags; |
| 56 |
} |
| 57 |
|
| 58 |
namespace mozilla { |
| 59 |
namespace ppc_private { |
| 60 |
bool vmx_enabled = !!(get_ppc_cpu_flags() & PPC_FLAG_VMX); |
| 61 |
bool vsx_enabled = !!(get_ppc_cpu_flags() & PPC_FLAG_VSX); |
| 62 |
bool vsx3_enabled = !!(get_ppc_cpu_flags() & PPC_FLAG_VSX3); |
| 63 |
} // namespace ppc_private |
| 64 |
} // namespace mozilla |