Jacob Bramley | 2af191d | 2018-05-16 10:22:44 +0100 | [diff] [blame] | 1 | // Copyright 2018, VIXL authors |
| 2 | // All rights reserved. |
| 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
| 5 | // modification, are permitted provided that the following conditions are met: |
| 6 | // |
| 7 | // * Redistributions of source code must retain the above copyright notice, |
| 8 | // this list of conditions and the following disclaimer. |
| 9 | // * Redistributions in binary form must reproduce the above copyright notice, |
| 10 | // this list of conditions and the following disclaimer in the documentation |
| 11 | // and/or other materials provided with the distribution. |
| 12 | // * Neither the name of ARM Limited nor the names of its contributors may be |
| 13 | // used to endorse or promote products derived from this software without |
| 14 | // specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND |
| 17 | // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 18 | // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 19 | // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE |
| 20 | // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 21 | // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 22 | // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 23 | // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 25 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | |
mmc28a | 1a2c1d3 | 2024-02-01 16:43:49 +0000 | [diff] [blame] | 27 | #include "cpu-features.h" |
| 28 | |
Jacob Bramley | 2af191d | 2018-05-16 10:22:44 +0100 | [diff] [blame] | 29 | #include <ostream> |
| 30 | |
Jacob Bramley | 2af191d | 2018-05-16 10:22:44 +0100 | [diff] [blame] | 31 | #include "globals-vixl.h" |
| 32 | #include "utils-vixl.h" |
| 33 | |
Jacob Bramley | 4893432 | 2019-02-04 18:27:53 +0000 | [diff] [blame] | 34 | #if defined(__aarch64__) && defined(VIXL_INCLUDE_TARGET_AARCH64) |
| 35 | #include "aarch64/cpu-aarch64.h" |
| 36 | #define VIXL_USE_AARCH64_CPU_HELPERS |
| 37 | #endif |
| 38 | |
Jacob Bramley | 2af191d | 2018-05-16 10:22:44 +0100 | [diff] [blame] | 39 | namespace vixl { |
| 40 | |
Jacob Bramley | 2af191d | 2018-05-16 10:22:44 +0100 | [diff] [blame] | 41 | CPUFeatures CPUFeatures::All() { |
| 42 | CPUFeatures all; |
Jacob Bramley | 8c4ceb6 | 2020-07-08 20:53:32 +0100 | [diff] [blame] | 43 | all.features_.set(); |
Jacob Bramley | 2af191d | 2018-05-16 10:22:44 +0100 | [diff] [blame] | 44 | return all; |
| 45 | } |
| 46 | |
Jacob Bramley | 4893432 | 2019-02-04 18:27:53 +0000 | [diff] [blame] | 47 | CPUFeatures CPUFeatures::InferFromIDRegisters() { |
| 48 | // This function assumes that kIDRegisterEmulation is available. |
| 49 | CPUFeatures features(CPUFeatures::kIDRegisterEmulation); |
| 50 | #ifdef VIXL_USE_AARCH64_CPU_HELPERS |
| 51 | // Note that the Linux kernel filters these values during emulation, so the |
| 52 | // results may not exactly match the expected hardware support. |
| 53 | features.Combine(aarch64::CPU::InferCPUFeaturesFromIDRegisters()); |
| 54 | #endif |
| 55 | return features; |
| 56 | } |
| 57 | |
| 58 | CPUFeatures CPUFeatures::InferFromOS(QueryIDRegistersOption option) { |
| 59 | #ifdef VIXL_USE_AARCH64_CPU_HELPERS |
| 60 | return aarch64::CPU::InferCPUFeaturesFromOS(option); |
| 61 | #else |
| 62 | USE(option); |
Jacob Bramley | 2af191d | 2018-05-16 10:22:44 +0100 | [diff] [blame] | 63 | return CPUFeatures(); |
Jacob Bramley | 4893432 | 2019-02-04 18:27:53 +0000 | [diff] [blame] | 64 | #endif |
Jacob Bramley | 2af191d | 2018-05-16 10:22:44 +0100 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | void CPUFeatures::Combine(const CPUFeatures& other) { |
| 68 | features_ |= other.features_; |
| 69 | } |
| 70 | |
Jacob Bramley | 8c4ceb6 | 2020-07-08 20:53:32 +0100 | [diff] [blame] | 71 | void CPUFeatures::Combine(Feature feature) { |
| 72 | if (feature != CPUFeatures::kNone) features_.set(feature); |
Jacob Bramley | 2af191d | 2018-05-16 10:22:44 +0100 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | void CPUFeatures::Remove(const CPUFeatures& other) { |
| 76 | features_ &= ~other.features_; |
| 77 | } |
| 78 | |
Jacob Bramley | 8c4ceb6 | 2020-07-08 20:53:32 +0100 | [diff] [blame] | 79 | void CPUFeatures::Remove(Feature feature) { |
| 80 | if (feature != CPUFeatures::kNone) features_.reset(feature); |
Jacob Bramley | 2af191d | 2018-05-16 10:22:44 +0100 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | bool CPUFeatures::Has(const CPUFeatures& other) const { |
| 84 | return (features_ & other.features_) == other.features_; |
| 85 | } |
| 86 | |
Jacob Bramley | 8c4ceb6 | 2020-07-08 20:53:32 +0100 | [diff] [blame] | 87 | bool CPUFeatures::Has(Feature feature) const { |
| 88 | return (feature == CPUFeatures::kNone) || features_[feature]; |
Jacob Bramley | 2af191d | 2018-05-16 10:22:44 +0100 | [diff] [blame] | 89 | } |
| 90 | |
Jacob Bramley | 8c4ceb6 | 2020-07-08 20:53:32 +0100 | [diff] [blame] | 91 | size_t CPUFeatures::Count() const { return features_.count(); } |
Jacob Bramley | c44ce3d | 2018-06-12 15:39:09 +0100 | [diff] [blame] | 92 | |
Jacob Bramley | 2af191d | 2018-05-16 10:22:44 +0100 | [diff] [blame] | 93 | std::ostream& operator<<(std::ostream& os, CPUFeatures::Feature feature) { |
| 94 | // clang-format off |
| 95 | switch (feature) { |
| 96 | #define VIXL_FORMAT_FEATURE(SYMBOL, NAME, CPUINFO) \ |
| 97 | case CPUFeatures::SYMBOL: \ |
| 98 | return os << NAME; |
| 99 | VIXL_CPU_FEATURE_LIST(VIXL_FORMAT_FEATURE) |
| 100 | #undef VIXL_FORMAT_FEATURE |
| 101 | case CPUFeatures::kNone: |
| 102 | return os << "none"; |
Jacob Bramley | fdf332a | 2018-09-17 11:17:54 +0100 | [diff] [blame] | 103 | case CPUFeatures::kNumberOfFeatures: |
| 104 | VIXL_UNREACHABLE(); |
Jacob Bramley | 2af191d | 2018-05-16 10:22:44 +0100 | [diff] [blame] | 105 | } |
| 106 | // clang-format on |
| 107 | VIXL_UNREACHABLE(); |
| 108 | return os; |
| 109 | } |
| 110 | |
| 111 | CPUFeatures::const_iterator CPUFeatures::begin() const { |
Jacob Bramley | caa40ee | 2020-07-08 20:46:38 +0100 | [diff] [blame] | 112 | // For iterators in general, it's undefined to increment `end()`, but here we |
| 113 | // control the implementation and it is safe to do this. |
| 114 | return ++end(); |
Jacob Bramley | 2af191d | 2018-05-16 10:22:44 +0100 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | CPUFeatures::const_iterator CPUFeatures::end() const { |
| 118 | return const_iterator(this, kNone); |
| 119 | } |
| 120 | |
| 121 | std::ostream& operator<<(std::ostream& os, const CPUFeatures& features) { |
Jacob Bramley | caa40ee | 2020-07-08 20:46:38 +0100 | [diff] [blame] | 122 | bool need_separator = false; |
| 123 | for (CPUFeatures::Feature feature : features) { |
| 124 | if (need_separator) os << ", "; |
| 125 | need_separator = true; |
| 126 | os << feature; |
Jacob Bramley | 2af191d | 2018-05-16 10:22:44 +0100 | [diff] [blame] | 127 | } |
| 128 | return os; |
| 129 | } |
| 130 | |
| 131 | bool CPUFeaturesConstIterator::operator==( |
| 132 | const CPUFeaturesConstIterator& other) const { |
| 133 | VIXL_ASSERT(IsValid()); |
| 134 | return (cpu_features_ == other.cpu_features_) && (feature_ == other.feature_); |
| 135 | } |
| 136 | |
Jacob Bramley | caa40ee | 2020-07-08 20:46:38 +0100 | [diff] [blame] | 137 | CPUFeaturesConstIterator& CPUFeaturesConstIterator::operator++() { // Prefix |
Jacob Bramley | 2af191d | 2018-05-16 10:22:44 +0100 | [diff] [blame] | 138 | VIXL_ASSERT(IsValid()); |
| 139 | do { |
| 140 | // Find the next feature. The order is unspecified. |
Jacob Bramley | fdf332a | 2018-09-17 11:17:54 +0100 | [diff] [blame] | 141 | feature_ = static_cast<CPUFeatures::Feature>(feature_ + 1); |
| 142 | if (feature_ == CPUFeatures::kNumberOfFeatures) { |
| 143 | feature_ = CPUFeatures::kNone; |
| 144 | VIXL_STATIC_ASSERT(CPUFeatures::kNone == -1); |
Jacob Bramley | 2af191d | 2018-05-16 10:22:44 +0100 | [diff] [blame] | 145 | } |
Jacob Bramley | fdf332a | 2018-09-17 11:17:54 +0100 | [diff] [blame] | 146 | VIXL_ASSERT(CPUFeatures::kNone <= feature_); |
| 147 | VIXL_ASSERT(feature_ < CPUFeatures::kNumberOfFeatures); |
Jacob Bramley | 2af191d | 2018-05-16 10:22:44 +0100 | [diff] [blame] | 148 | // cpu_features_->Has(kNone) is always true, so this will terminate even if |
| 149 | // the features list is empty. |
| 150 | } while (!cpu_features_->Has(feature_)); |
Jacob Bramley | caa40ee | 2020-07-08 20:46:38 +0100 | [diff] [blame] | 151 | return *this; |
Jacob Bramley | 2af191d | 2018-05-16 10:22:44 +0100 | [diff] [blame] | 152 | } |
| 153 | |
Jacob Bramley | caa40ee | 2020-07-08 20:46:38 +0100 | [diff] [blame] | 154 | CPUFeaturesConstIterator CPUFeaturesConstIterator::operator++(int) { // Postfix |
| 155 | CPUFeaturesConstIterator result = *this; |
Jacob Bramley | 2af191d | 2018-05-16 10:22:44 +0100 | [diff] [blame] | 156 | ++(*this); |
| 157 | return result; |
| 158 | } |
| 159 | |
| 160 | } // namespace vixl |