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