|
| 1 | +/*************************************************************************** |
| 2 | + * Copyright (c) Johan Mabille, Sylvain Corlay, Wolf Vollprecht and * |
| 3 | + * Martin Renou * |
| 4 | + * Copyright (c) QuantStack * |
| 5 | + * Copyright (c) Serge Guelton * |
| 6 | + * * |
| 7 | + * Distributed under the terms of the BSD 3-Clause License. * |
| 8 | + * * |
| 9 | + * The full license is in the file LICENSE, distributed with this software. * |
| 10 | + ***************************************************************************/ |
| 11 | + |
| 12 | +#ifndef XSIMD_CPU_FEATURES_ARM_HPP |
| 13 | +#define XSIMD_CPU_FEATURES_ARM_HPP |
| 14 | + |
| 15 | +#include "./xsimd_config.hpp" |
| 16 | + |
| 17 | +#if XSIMD_TARGET_ARM && XSIMD_HAVE_LINUX_GETAUXVAL |
| 18 | +#include "../utils/bits.hpp" |
| 19 | +#include "./xsimd_getauxval.hpp" |
| 20 | + |
| 21 | +// HWCAP_XXX masks to use on getauxval results. |
| 22 | +// Header does not exists on all architectures and masks are architecture |
| 23 | +// specific. |
| 24 | +#include <asm/hwcap.h> |
| 25 | + |
| 26 | +// Port possibly missing mask. Should only be defined on Arm64. |
| 27 | +#if XSIMD_TARGET_ARM64 && !defined(HWCAP2_I8MM) |
| 28 | +#define HWCAP2_I8MM (1 << 13) |
| 29 | +#endif |
| 30 | +#endif // XSIMD_TARGET_ARM && XSIMD_HAVE_LINUX_GETAUXVAL |
| 31 | + |
| 32 | +namespace xsimd |
| 33 | +{ |
| 34 | + /** |
| 35 | + * An opinionated CPU feature detection utility for ARM. |
| 36 | + * |
| 37 | + * Combines compile-time knowledge with runtime detection when available. |
| 38 | + * On Linux, runtime detection uses getauxval to query the auxiliary vector. |
| 39 | + * On other platforms, only compile-time information is used. |
| 40 | + * |
| 41 | + * This is well defined on all architectures. |
| 42 | + * It will always return false on non-ARM architectures. |
| 43 | + */ |
| 44 | + class arm_cpu_features |
| 45 | + { |
| 46 | + public: |
| 47 | + arm_cpu_features() noexcept = default; |
| 48 | + |
| 49 | + inline bool neon() const noexcept |
| 50 | + { |
| 51 | +#if XSIMD_TARGET_ARM && !XSIMD_TARGET_ARM64 && XSIMD_HAVE_LINUX_GETAUXVAL |
| 52 | + return hwcap().has_feature(HWCAP_NEON); |
| 53 | +#else |
| 54 | + return static_cast<bool>(XSIMD_WITH_NEON); |
| 55 | +#endif |
| 56 | + } |
| 57 | + |
| 58 | + constexpr bool neon64() const noexcept |
| 59 | + { |
| 60 | + return static_cast<bool>(XSIMD_WITH_NEON64); |
| 61 | + } |
| 62 | + |
| 63 | + inline bool sve() const noexcept |
| 64 | + { |
| 65 | +#if XSIMD_TARGET_ARM64 && XSIMD_HAVE_LINUX_GETAUXVAL |
| 66 | + return hwcap().has_feature(HWCAP_SVE); |
| 67 | +#else |
| 68 | + return false; |
| 69 | +#endif |
| 70 | + } |
| 71 | + |
| 72 | + inline bool i8mm() const noexcept |
| 73 | + { |
| 74 | +#if XSIMD_TARGET_ARM64 && XSIMD_HAVE_LINUX_GETAUXVAL |
| 75 | + return hwcap2().has_feature(HWCAP2_I8MM); |
| 76 | +#else |
| 77 | + return false; |
| 78 | +#endif |
| 79 | + } |
| 80 | + |
| 81 | + private: |
| 82 | +#if XSIMD_TARGET_ARM && XSIMD_HAVE_LINUX_GETAUXVAL |
| 83 | + enum class status |
| 84 | + { |
| 85 | + hwcap_valid = 0, |
| 86 | + hwcap2_valid = 1, |
| 87 | + }; |
| 88 | + |
| 89 | + using status_bitset = utils::uint_bitset<status, std::uint32_t>; |
| 90 | + |
| 91 | + mutable status_bitset m_status {}; |
| 92 | + |
| 93 | + mutable xsimd::linux_auxval m_hwcap {}; |
| 94 | + |
| 95 | + inline xsimd::linux_auxval const& hwcap() const noexcept |
| 96 | + { |
| 97 | + if (!m_status.bit_is_set<status::hwcap_valid>()) |
| 98 | + { |
| 99 | + m_hwcap = xsimd::linux_auxval::read(AT_HWCAP); |
| 100 | + m_status.set_bit<status::hwcap_valid>(); |
| 101 | + } |
| 102 | + return m_hwcap; |
| 103 | + } |
| 104 | + |
| 105 | +#if XSIMD_TARGET_ARM64 |
| 106 | + mutable xsimd::linux_auxval m_hwcap2 {}; |
| 107 | + |
| 108 | + inline xsimd::linux_auxval const& hwcap2() const noexcept |
| 109 | + { |
| 110 | + if (!m_status.bit_is_set<status::hwcap2_valid>()) |
| 111 | + { |
| 112 | + m_hwcap2 = xsimd::linux_auxval::read(AT_HWCAP2); |
| 113 | + m_status.set_bit<status::hwcap2_valid>(); |
| 114 | + } |
| 115 | + return m_hwcap2; |
| 116 | + } |
| 117 | +#endif |
| 118 | +#endif // XSIMD_TARGET_ARM && XSIMD_HAVE_LINUX_GETAUXVAL |
| 119 | + }; |
| 120 | +} |
| 121 | +#endif |
0 commit comments