| 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 |
|
| 27 |
|
| 28 |
#include "jit/arm64/vixl/Cpu-Features-vixl.h" |
| 29 |
|
| 30 |
#include <ostream> |
| 31 |
|
| 32 |
#include "jit/arm64/vixl/Cpu-vixl.h" |
| 33 |
#include "jit/arm64/vixl/Globals-vixl.h" |
| 34 |
#include "jit/arm64/vixl/Utils-vixl.h" |
| 35 |
|
| 36 |
// Mozilla change: Don't use VIXL_USE_AARCH64_CPU_HELPERS for the simulator, so |
| 37 |
// that we get consistent simulator behavior across architectures. |
| 38 |
#if defined(__aarch64__) && !defined(JS_SIMULATOR_ARM64) |
| 39 |
#define VIXL_USE_AARCH64_CPU_HELPERS |
| 40 |
#endif |
| 41 |
|
| 42 |
namespace vixl { |
| 43 |
|
| 44 |
static uint64_t MakeFeatureMask(CPUFeatures::Feature feature) { |
| 45 |
if (feature == CPUFeatures::kNone) { |
| 46 |
return 0; |
| 47 |
} else { |
| 48 |
// Check that the shift is well-defined, and that the feature is valid. |
| 49 |
VIXL_STATIC_ASSERT(CPUFeatures::kNumberOfFeatures <= |
| 50 |
(sizeof(uint64_t) * 8)); |
| 51 |
VIXL_ASSERT(feature < CPUFeatures::kNumberOfFeatures); |
| 52 |
return UINT64_C(1) << feature; |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
CPUFeatures::CPUFeatures(Feature feature0, |
| 57 |
Feature feature1, |
| 58 |
Feature feature2, |
| 59 |
Feature feature3) |
| 60 |
: features_(0) { |
| 61 |
Combine(feature0, feature1, feature2, feature3); |
| 62 |
} |
| 63 |
|
| 64 |
CPUFeatures CPUFeatures::All() { |
| 65 |
CPUFeatures all; |
| 66 |
// Check that the shift is well-defined. |
| 67 |
VIXL_STATIC_ASSERT(CPUFeatures::kNumberOfFeatures < (sizeof(uint64_t) * 8)); |
| 68 |
all.features_ = (UINT64_C(1) << kNumberOfFeatures) - 1; |
| 69 |
return all; |
| 70 |
} |
| 71 |
|
| 72 |
CPUFeatures CPUFeatures::InferFromIDRegisters() { |
| 73 |
// This function assumes that kIDRegisterEmulation is available. |
| 74 |
CPUFeatures features(CPUFeatures::kIDRegisterEmulation); |
| 75 |
#ifdef VIXL_USE_AARCH64_CPU_HELPERS |
| 76 |
// Note that the Linux kernel filters these values during emulation, so the |
| 77 |
// results may not exactly match the expected hardware support. |
| 78 |
features.Combine(CPU::InferCPUFeaturesFromIDRegisters()); |
| 79 |
#endif |
| 80 |
return features; |
| 81 |
} |
| 82 |
|
| 83 |
CPUFeatures CPUFeatures::InferFromOS(QueryIDRegistersOption option) { |
| 84 |
#ifdef VIXL_USE_AARCH64_CPU_HELPERS |
| 85 |
return CPU::InferCPUFeaturesFromOS(option); |
| 86 |
#else |
| 87 |
USE(option); |
| 88 |
return CPUFeatures(); |
| 89 |
#endif |
| 90 |
} |
| 91 |
|
| 92 |
void CPUFeatures::Combine(const CPUFeatures& other) { |
| 93 |
features_ |= other.features_; |
| 94 |
} |
| 95 |
|
| 96 |
void CPUFeatures::Combine(Feature feature0, |
| 97 |
Feature feature1, |
| 98 |
Feature feature2, |
| 99 |
Feature feature3) { |
| 100 |
features_ |= MakeFeatureMask(feature0); |
| 101 |
features_ |= MakeFeatureMask(feature1); |
| 102 |
features_ |= MakeFeatureMask(feature2); |
| 103 |
features_ |= MakeFeatureMask(feature3); |
| 104 |
} |
| 105 |
|
| 106 |
void CPUFeatures::Remove(const CPUFeatures& other) { |
| 107 |
features_ &= ~other.features_; |
| 108 |
} |
| 109 |
|
| 110 |
void CPUFeatures::Remove(Feature feature0, |
| 111 |
Feature feature1, |
| 112 |
Feature feature2, |
| 113 |
Feature feature3) { |
| 114 |
features_ &= ~MakeFeatureMask(feature0); |
| 115 |
features_ &= ~MakeFeatureMask(feature1); |
| 116 |
features_ &= ~MakeFeatureMask(feature2); |
| 117 |
features_ &= ~MakeFeatureMask(feature3); |
| 118 |
} |
| 119 |
|
| 120 |
CPUFeatures CPUFeatures::With(const CPUFeatures& other) const { |
| 121 |
CPUFeatures f(*this); |
| 122 |
f.Combine(other); |
| 123 |
return f; |
| 124 |
} |
| 125 |
|
| 126 |
CPUFeatures CPUFeatures::With(Feature feature0, |
| 127 |
Feature feature1, |
| 128 |
Feature feature2, |
| 129 |
Feature feature3) const { |
| 130 |
CPUFeatures f(*this); |
| 131 |
f.Combine(feature0, feature1, feature2, feature3); |
| 132 |
return f; |
| 133 |
} |
| 134 |
|
| 135 |
CPUFeatures CPUFeatures::Without(const CPUFeatures& other) const { |
| 136 |
CPUFeatures f(*this); |
| 137 |
f.Remove(other); |
| 138 |
return f; |
| 139 |
} |
| 140 |
|
| 141 |
CPUFeatures CPUFeatures::Without(Feature feature0, |
| 142 |
Feature feature1, |
| 143 |
Feature feature2, |
| 144 |
Feature feature3) const { |
| 145 |
CPUFeatures f(*this); |
| 146 |
f.Remove(feature0, feature1, feature2, feature3); |
| 147 |
return f; |
| 148 |
} |
| 149 |
|
| 150 |
bool CPUFeatures::Has(const CPUFeatures& other) const { |
| 151 |
return (features_ & other.features_) == other.features_; |
| 152 |
} |
| 153 |
|
| 154 |
bool CPUFeatures::Has(Feature feature0, |
| 155 |
Feature feature1, |
| 156 |
Feature feature2, |
| 157 |
Feature feature3) const { |
| 158 |
uint64_t mask = MakeFeatureMask(feature0) | MakeFeatureMask(feature1) | |
| 159 |
MakeFeatureMask(feature2) | MakeFeatureMask(feature3); |
| 160 |
return (features_ & mask) == mask; |
| 161 |
} |
| 162 |
|
| 163 |
size_t CPUFeatures::Count() const { return CountSetBits(features_); } |
| 164 |
|
| 165 |
std::ostream& operator<<(std::ostream& os, CPUFeatures::Feature feature) { |
| 166 |
// clang-format off |
| 167 |
switch (feature) { |
| 168 |
#define VIXL_FORMAT_FEATURE(SYMBOL, NAME, CPUINFO) \ |
| 169 |
case CPUFeatures::SYMBOL: \ |
| 170 |
return os << NAME; |
| 171 |
VIXL_CPU_FEATURE_LIST(VIXL_FORMAT_FEATURE) |
| 172 |
#undef VIXL_FORMAT_FEATURE |
| 173 |
case CPUFeatures::kNone: |
| 174 |
return os << "none"; |
| 175 |
case CPUFeatures::kNumberOfFeatures: |
| 176 |
VIXL_UNREACHABLE(); |
| 177 |
} |
| 178 |
// clang-format on |
| 179 |
VIXL_UNREACHABLE(); |
| 180 |
return os; |
| 181 |
} |
| 182 |
|
| 183 |
CPUFeatures::const_iterator CPUFeatures::begin() const { |
| 184 |
if (features_ == 0) return const_iterator(this, kNone); |
| 185 |
|
| 186 |
int feature_number = CountTrailingZeros(features_); |
| 187 |
vixl::CPUFeatures::Feature feature = |
| 188 |
static_cast<CPUFeatures::Feature>(feature_number); |
| 189 |
return const_iterator(this, feature); |
| 190 |
} |
| 191 |
|
| 192 |
CPUFeatures::const_iterator CPUFeatures::end() const { |
| 193 |
return const_iterator(this, kNone); |
| 194 |
} |
| 195 |
|
| 196 |
std::ostream& operator<<(std::ostream& os, const CPUFeatures& features) { |
| 197 |
CPUFeatures::const_iterator it = features.begin(); |
| 198 |
while (it != features.end()) { |
| 199 |
os << *it; |
| 200 |
++it; |
| 201 |
if (it != features.end()) os << ", "; |
| 202 |
} |
| 203 |
return os; |
| 204 |
} |
| 205 |
|
| 206 |
bool CPUFeaturesConstIterator::operator==( |
| 207 |
const CPUFeaturesConstIterator& other) const { |
| 208 |
VIXL_ASSERT(IsValid()); |
| 209 |
return (cpu_features_ == other.cpu_features_) && (feature_ == other.feature_); |
| 210 |
} |
| 211 |
|
| 212 |
CPUFeatures::Feature CPUFeaturesConstIterator::operator++() { // Prefix |
| 213 |
VIXL_ASSERT(IsValid()); |
| 214 |
do { |
| 215 |
// Find the next feature. The order is unspecified. |
| 216 |
feature_ = static_cast<CPUFeatures::Feature>(feature_ + 1); |
| 217 |
if (feature_ == CPUFeatures::kNumberOfFeatures) { |
| 218 |
feature_ = CPUFeatures::kNone; |
| 219 |
VIXL_STATIC_ASSERT(CPUFeatures::kNone == -1); |
| 220 |
} |
| 221 |
VIXL_ASSERT(CPUFeatures::kNone <= feature_); |
| 222 |
VIXL_ASSERT(feature_ < CPUFeatures::kNumberOfFeatures); |
| 223 |
// cpu_features_->Has(kNone) is always true, so this will terminate even if |
| 224 |
// the features list is empty. |
| 225 |
} while (!cpu_features_->Has(feature_)); |
| 226 |
return feature_; |
| 227 |
} |
| 228 |
|
| 229 |
CPUFeatures::Feature CPUFeaturesConstIterator::operator++(int) { // Postfix |
| 230 |
CPUFeatures::Feature result = feature_; |
| 231 |
++(*this); |
| 232 |
return result; |
| 233 |
} |
| 234 |
|
| 235 |
} // namespace vixl |