blob: 004bbd26a44b2fd67a503051e23d3b37babe4ec4 [file] [log] [blame]
Jacob Bramley2af191d2018-05-16 10:22:44 +01001// 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#ifndef VIXL_CPU_FEATURES_H
28#define VIXL_CPU_FEATURES_H
29
Jacob Bramley8c4ceb62020-07-08 20:53:32 +010030#include <bitset>
Jacob Bramley2af191d2018-05-16 10:22:44 +010031#include <ostream>
32
33#include "globals-vixl.h"
34
35namespace vixl {
36
37
Jacob Bramley3d8d3942020-07-06 19:38:59 +010038// VIXL aims to handle and detect all architectural features that are likely to
39// influence code-generation decisions at EL0 (user-space).
40//
41// - There may be multiple VIXL feature flags for a given architectural
42// extension. This occurs where the extension allow components to be
43// implemented independently, or where kernel support is needed, and is likely
44// to be fragmented.
45//
46// For example, Pointer Authentication (kPAuth*) has a separate feature flag
47// for access to PACGA, and to indicate that the QARMA algorithm is
48// implemented.
49//
50// - Conversely, some extensions have configuration options that do not affect
51// EL0, so these are presented as a single VIXL feature.
52//
53// For example, the RAS extension (kRAS) has several variants, but the only
54// feature relevant to VIXL is the addition of the ESB instruction so we only
55// need a single flag.
56//
57// - VIXL offers separate flags for separate features even if they're
58// architecturally linked.
59//
60// For example, the architecture requires kFPHalf and kNEONHalf to be equal,
61// but they have separate hardware ID register fields so VIXL presents them as
62// separate features.
63//
64// - VIXL can detect every feature for which it can generate code.
65//
66// - VIXL can detect some features for which it cannot generate code.
67//
68// The CPUFeatures::Feature enum — derived from the macro list below — is
69// frequently extended. New features may be added to the list at any point, and
70// no assumptions should be made about the numerical values assigned to each
71// enum constant. The symbolic names can be considered to be stable.
72//
73// The debug descriptions are used only for debug output. The 'cpuinfo' strings
74// are informative; VIXL does not use /proc/cpuinfo for feature detection.
75
Jacob Bramley2af191d2018-05-16 10:22:44 +010076// clang-format off
77#define VIXL_CPU_FEATURE_LIST(V) \
78 /* If set, the OS traps and emulates MRS accesses to relevant (EL1) ID_* */ \
79 /* registers, so that the detailed feature registers can be read */ \
80 /* directly. */ \
Jacob Bramley3d8d3942020-07-06 19:38:59 +010081 \
82 /* Constant name Debug description Linux 'cpuinfo' string. */ \
Jacob Bramley2af191d2018-05-16 10:22:44 +010083 V(kIDRegisterEmulation, "ID register emulation", "cpuid") \
84 \
85 V(kFP, "FP", "fp") \
86 V(kNEON, "NEON", "asimd") \
87 V(kCRC32, "CRC32", "crc32") \
Jacob Bramley3d8d3942020-07-06 19:38:59 +010088 V(kDGH, "DGH", "dgh") \
89 /* Speculation control features. */ \
90 V(kCSV2, "CSV2", NULL) \
91 V(kSCXTNUM, "SCXTNUM", NULL) \
92 V(kCSV3, "CSV3", NULL) \
93 V(kSB, "SB", "sb") \
94 V(kSPECRES, "SPECRES", NULL) \
95 V(kSSBS, "SSBS", NULL) \
96 V(kSSBSControl, "SSBS (PSTATE control)", "ssbs") \
Jacob Bramley2af191d2018-05-16 10:22:44 +010097 /* Cryptographic support instructions. */ \
98 V(kAES, "AES", "aes") \
99 V(kSHA1, "SHA1", "sha1") \
100 V(kSHA2, "SHA2", "sha2") \
101 /* A form of PMULL{2} with a 128-bit (1Q) result. */ \
102 V(kPmull1Q, "Pmull1Q", "pmull") \
103 /* Atomic operations on memory: CAS, LDADD, STADD, SWP, etc. */ \
104 V(kAtomics, "Atomics", "atomics") \
105 /* Limited ordering regions: LDLAR, STLLR and their variants. */ \
106 V(kLORegions, "LORegions", NULL) \
107 /* Rounding doubling multiply add/subtract: SQRDMLAH and SQRDMLSH. */ \
108 V(kRDM, "RDM", "asimdrdm") \
Jacob Bramley4482be72018-09-14 15:57:57 +0100109 /* Scalable Vector Extension. */ \
110 V(kSVE, "SVE", "sve") \
Jacob Bramley3d8d3942020-07-06 19:38:59 +0100111 V(kSVEF64MM, "SVE F64MM", "svef64mm") \
112 V(kSVEF32MM, "SVE F32MM", "svef32mm") \
113 V(kSVEI8MM, "SVE I8MM", "svei8imm") \
114 V(kSVEBF16, "SVE BFloat16", "svebf16") \
Jacob Bramley2af191d2018-05-16 10:22:44 +0100115 /* SDOT and UDOT support (in NEON). */ \
116 V(kDotProduct, "DotProduct", "asimddp") \
Jacob Bramley3d8d3942020-07-06 19:38:59 +0100117 /* Int8 matrix multiplication (in NEON). */ \
118 V(kI8MM, "NEON I8MM", "i8mm") \
Jacob Bramley2af191d2018-05-16 10:22:44 +0100119 /* Half-precision (FP16) support for FP and NEON, respectively. */ \
120 V(kFPHalf, "FPHalf", "fphp") \
121 V(kNEONHalf, "NEONHalf", "asimdhp") \
Jacob Bramley3d8d3942020-07-06 19:38:59 +0100122 /* BFloat16 support (in both FP and NEON.) */ \
123 V(kBF16, "FP/NEON BFloat 16", "bf16") \
Jacob Bramleyca789742018-09-13 14:25:46 +0100124 /* The RAS extension, including the ESB instruction. */ \
125 V(kRAS, "RAS", NULL) \
Jacob Bramley2af191d2018-05-16 10:22:44 +0100126 /* Data cache clean to the point of persistence: DC CVAP. */ \
127 V(kDCPoP, "DCPoP", "dcpop") \
TatWai Chong684f5f72018-12-25 17:49:56 -0800128 /* Data cache clean to the point of deep persistence: DC CVADP. */ \
Jacob Bramley17b2e542019-11-07 18:26:05 +0000129 V(kDCCVADP, "DCCVADP", "dcpodp") \
Jacob Bramley2af191d2018-05-16 10:22:44 +0100130 /* Cryptographic support instructions. */ \
131 V(kSHA3, "SHA3", "sha3") \
132 V(kSHA512, "SHA512", "sha512") \
133 V(kSM3, "SM3", "sm3") \
134 V(kSM4, "SM4", "sm4") \
135 /* Pointer authentication for addresses. */ \
Jacob Bramley17b2e542019-11-07 18:26:05 +0000136 V(kPAuth, "PAuth", "paca") \
Jacob Bramley2af191d2018-05-16 10:22:44 +0100137 /* Pointer authentication for addresses uses QARMA. */ \
138 V(kPAuthQARMA, "PAuthQARMA", NULL) \
139 /* Generic authentication (using the PACGA instruction). */ \
Jacob Bramley17b2e542019-11-07 18:26:05 +0000140 V(kPAuthGeneric, "PAuthGeneric", "pacg") \
Jacob Bramley2af191d2018-05-16 10:22:44 +0100141 /* Generic authentication uses QARMA. */ \
142 V(kPAuthGenericQARMA, "PAuthGenericQARMA", NULL) \
Jacob Bramley4482be72018-09-14 15:57:57 +0100143 /* JavaScript-style FP -> integer conversion instruction: FJCVTZS. */ \
Jacob Bramley2af191d2018-05-16 10:22:44 +0100144 V(kJSCVT, "JSCVT", "jscvt") \
Jacob Bramley4482be72018-09-14 15:57:57 +0100145 /* Complex number support for NEON: FCMLA and FCADD. */ \
146 V(kFcma, "Fcma", "fcma") \
Jacob Bramley2af191d2018-05-16 10:22:44 +0100147 /* RCpc-based model (for weaker release consistency): LDAPR and variants. */ \
148 V(kRCpc, "RCpc", "lrcpc") \
Jacob Bramley4482be72018-09-14 15:57:57 +0100149 V(kRCpcImm, "RCpc (imm)", "ilrcpc") \
150 /* Flag manipulation instructions: SETF{8,16}, CFINV, RMIF. */ \
151 V(kFlagM, "FlagM", "flagm") \
152 /* Unaligned single-copy atomicity. */ \
153 V(kUSCAT, "USCAT", "uscat") \
154 /* FP16 fused multiply-add or -subtract long: FMLAL{2}, FMLSL{2}. */ \
155 V(kFHM, "FHM", "asimdfhm") \
156 /* Data-independent timing (for selected instructions). */ \
Martyn Capewellcb963f72018-10-22 15:25:28 +0100157 V(kDIT, "DIT", "dit") \
158 /* Branch target identification. */ \
Jacob Bramley3d8d3942020-07-06 19:38:59 +0100159 V(kBTI, "BTI", "bti") \
Alexander Gilday84ee1442018-11-06 15:28:07 +0000160 /* Flag manipulation instructions: {AX,XA}FLAG */ \
Jacob Bramley3d8d3942020-07-06 19:38:59 +0100161 V(kAXFlag, "AXFlag", "flagm2") \
TatWai Chong04edf682018-12-27 16:01:02 -0800162 /* Random number generation extension, */ \
Jacob Bramley3d8d3942020-07-06 19:38:59 +0100163 V(kRNG, "RNG", "rng") \
TatWai Chong04471812019-03-19 14:29:00 -0700164 /* Floating-point round to {32,64}-bit integer. */ \
Jacob Bramley3d8d3942020-07-06 19:38:59 +0100165 V(kFrintToFixedSizedInt,"Frint (bounded)", "frint") \
166 /* Memory Tagging Extension. */ \
167 V(kMTEInstructions, "MTE (EL0 instructions)", NULL) \
168 V(kMTE, "MTE", NULL) \
169 /* PAuth extensions. */ \
170 V(kPAuthEnhancedPAC, "PAuth EnhancedPAC", NULL) \
171 V(kPAuthEnhancedPAC2, "PAuth EnhancedPAC2", NULL) \
172 V(kPAuthFPAC, "PAuth FPAC", NULL) \
Jacob Bramley1a3aac02020-07-06 19:39:26 +0100173 V(kPAuthFPACCombined, "PAuth FPACCombined", NULL) \
174 /* Scalable Vector Extension 2. */ \
175 V(kSVE2, "SVE2", "sve2") \
176 V(kSVESM4, "SVE SM4", "svesm4") \
177 V(kSVESHA3, "SVE SHA3", "svesha3") \
178 V(kSVEBitPerm, "SVE BitPerm", "svebitperm") \
179 V(kSVEAES, "SVE AES", "sveaes") \
Lioncashefec2fb2022-01-18 14:57:43 -0500180 V(kSVEPmull128, "SVE Pmull128", "svepmull") \
181 /* Alternate floating-point behavior */ \
lioncashd37468b2022-01-27 10:26:27 -0500182 V(kAFP, "AFP", "afp") \
183 /* Enhanced Counter Virtualization */ \
184 V(kECV, "ECV", "ecv")
Jacob Bramley2af191d2018-05-16 10:22:44 +0100185// clang-format on
186
187
188class CPUFeaturesConstIterator;
189
190// A representation of the set of features known to be supported by the target
191// device. Each feature is represented by a simple boolean flag.
192//
Jacob Bramley5997b462018-06-05 14:05:30 +0100193// - When the Assembler is asked to assemble an instruction, it asserts (in
194// debug mode) that the necessary features are available.
Jacob Bramley2af191d2018-05-16 10:22:44 +0100195//
Jacob Bramley2af191d2018-05-16 10:22:44 +0100196// - TODO: The MacroAssembler relies on the Assembler's assertions, but in
197// some cases it may be useful for macros to generate a fall-back sequence
198// in case features are not available.
199//
Jacob Bramleyc44ce3d2018-06-12 15:39:09 +0100200// - The Simulator assumes by default that all features are available, but it
201// is possible to configure it to fail if the simulated code uses features
202// that are not enabled.
203//
204// The Simulator also offers pseudo-instructions to allow features to be
205// enabled and disabled dynamically. This is useful when you want to ensure
206// that some features are constrained to certain areas of code.
207//
208// - The base Disassembler knows nothing about CPU features, but the
209// PrintDisassembler can be configured to annotate its output with warnings
210// about unavailable features. The Simulator uses this feature when
211// instruction trace is enabled.
212//
213// - The Decoder-based components -- the Simulator and PrintDisassembler --
214// rely on a CPUFeaturesAuditor visitor. This visitor keeps a list of
215// features actually encountered so that a large block of code can be
216// examined (either directly or through simulation), and the required
217// features analysed later.
218//
Jacob Bramley2af191d2018-05-16 10:22:44 +0100219// Expected usage:
220//
221// // By default, VIXL uses CPUFeatures::AArch64LegacyBaseline(), for
222// // compatibility with older version of VIXL.
223// MacroAssembler masm;
224//
225// // Generate code only for the current CPU.
226// masm.SetCPUFeatures(CPUFeatures::InferFromOS());
227//
228// // Turn off feature checking entirely.
229// masm.SetCPUFeatures(CPUFeatures::All());
230//
231// Feature set manipulation:
232//
233// CPUFeatures f; // The default constructor gives an empty set.
234// // Individual features can be added (or removed).
235// f.Combine(CPUFeatures::kFP, CPUFeatures::kNEON, CPUFeatures::AES);
236// f.Remove(CPUFeatures::kNEON);
237//
238// // Some helpers exist for extensions that provide several features.
239// f.Remove(CPUFeatures::All());
240// f.Combine(CPUFeatures::AArch64LegacyBaseline());
241//
242// // Chained construction is also possible.
243// CPUFeatures g =
244// f.With(CPUFeatures::kPmull1Q).Without(CPUFeatures::kCRC32);
245//
246// // Features can be queried. Where multiple features are given, they are
247// // combined with logical AND.
248// if (h.Has(CPUFeatures::kNEON)) { ... }
249// if (h.Has(CPUFeatures::kFP, CPUFeatures::kNEON)) { ... }
250// if (h.Has(g)) { ... }
251// // If the empty set is requested, the result is always 'true'.
252// VIXL_ASSERT(h.Has(CPUFeatures()));
253//
254// // For debug and reporting purposes, features can be enumerated (or
255// // printed directly):
256// std::cout << CPUFeatures::kNEON; // Prints something like "NEON".
257// std::cout << f; // Prints something like "FP, NEON, CRC32".
258class CPUFeatures {
259 public:
260 // clang-format off
261 // Individual features.
262 // These should be treated as opaque tokens. User code should not rely on
263 // specific numeric values or ordering.
264 enum Feature {
265 // Refer to VIXL_CPU_FEATURE_LIST (above) for the list of feature names that
266 // this class supports.
267
Jacob Bramleyfdf332a2018-09-17 11:17:54 +0100268 kNone = -1,
Jacob Bramley2af191d2018-05-16 10:22:44 +0100269#define VIXL_DECLARE_FEATURE(SYMBOL, NAME, CPUINFO) SYMBOL,
270 VIXL_CPU_FEATURE_LIST(VIXL_DECLARE_FEATURE)
271#undef VIXL_DECLARE_FEATURE
Jacob Bramleyfdf332a2018-09-17 11:17:54 +0100272 kNumberOfFeatures
Jacob Bramley2af191d2018-05-16 10:22:44 +0100273 };
274 // clang-format on
275
276 // By default, construct with no features enabled.
Jacob Bramley8c4ceb62020-07-08 20:53:32 +0100277 CPUFeatures() : features_{} {}
Jacob Bramley2af191d2018-05-16 10:22:44 +0100278
279 // Construct with some features already enabled.
Jacob Bramley8c4ceb62020-07-08 20:53:32 +0100280 template <typename T, typename... U>
281 CPUFeatures(T first, U... others) : features_{} {
282 Combine(first, others...);
283 }
Jacob Bramley2af191d2018-05-16 10:22:44 +0100284
285 // Construct with all features enabled. This can be used to disable feature
286 // checking: `Has(...)` returns true regardless of the argument.
287 static CPUFeatures All();
288
Jacob Bramley7b8fc822018-06-26 16:48:21 +0100289 // Construct an empty CPUFeatures. This is equivalent to the default
290 // constructor, but is provided for symmetry and convenience.
291 static CPUFeatures None() { return CPUFeatures(); }
292
Jacob Bramley2af191d2018-05-16 10:22:44 +0100293 // The presence of these features was assumed by version of VIXL before this
294 // API was added, so using this set by default ensures API compatibility.
295 static CPUFeatures AArch64LegacyBaseline() {
296 return CPUFeatures(kFP, kNEON, kCRC32);
297 }
298
Jacob Bramley48934322019-02-04 18:27:53 +0000299 // Construct a new CPUFeatures object using ID registers. This assumes that
300 // kIDRegisterEmulation is present.
301 static CPUFeatures InferFromIDRegisters();
302
303 enum QueryIDRegistersOption {
304 kDontQueryIDRegisters,
305 kQueryIDRegistersIfAvailable
306 };
307
Jacob Bramley2af191d2018-05-16 10:22:44 +0100308 // Construct a new CPUFeatures object based on what the OS reports.
Jacob Bramley48934322019-02-04 18:27:53 +0000309 static CPUFeatures InferFromOS(
310 QueryIDRegistersOption option = kQueryIDRegistersIfAvailable);
Jacob Bramley2af191d2018-05-16 10:22:44 +0100311
312 // Combine another CPUFeatures object into this one. Features that already
313 // exist in this set are left unchanged.
314 void Combine(const CPUFeatures& other);
315
Jacob Bramley8c4ceb62020-07-08 20:53:32 +0100316 // Combine a specific feature into this set. If it already exists in the set,
317 // the set is left unchanged.
318 void Combine(Feature feature);
319
320 // Combine multiple features (or feature sets) into this set.
321 template <typename T, typename... U>
322 void Combine(T first, U... others) {
323 Combine(first);
324 Combine(others...);
325 }
Jacob Bramley2af191d2018-05-16 10:22:44 +0100326
327 // Remove features in another CPUFeatures object from this one.
328 void Remove(const CPUFeatures& other);
329
Jacob Bramley8c4ceb62020-07-08 20:53:32 +0100330 // Remove a specific feature from this set. This has no effect if the feature
331 // doesn't exist in the set.
332 void Remove(Feature feature0);
Jacob Bramley2af191d2018-05-16 10:22:44 +0100333
Jacob Bramley8c4ceb62020-07-08 20:53:32 +0100334 // Remove multiple features (or feature sets) from this set.
335 template <typename T, typename... U>
336 void Remove(T first, U... others) {
337 Remove(first);
338 Remove(others...);
339 }
Jacob Bramley2af191d2018-05-16 10:22:44 +0100340
Jacob Bramley8c4ceb62020-07-08 20:53:32 +0100341 // Chaining helpers for convenient construction by combining other CPUFeatures
342 // or individual Features.
343 template <typename... T>
344 CPUFeatures With(T... others) const {
345 CPUFeatures f(*this);
346 f.Combine(others...);
347 return f;
348 }
349
350 template <typename... T>
351 CPUFeatures Without(T... others) const {
352 CPUFeatures f(*this);
353 f.Remove(others...);
354 return f;
355 }
356
357 // Test whether the `other` feature set is equal to or a subset of this one.
Jacob Bramley2af191d2018-05-16 10:22:44 +0100358 bool Has(const CPUFeatures& other) const;
Jacob Bramley8c4ceb62020-07-08 20:53:32 +0100359
360 // Test whether a single feature exists in this set.
361 // Note that `Has(kNone)` always returns true.
362 bool Has(Feature feature) const;
363
364 // Test whether all of the specified features exist in this set.
365 template <typename T, typename... U>
366 bool Has(T first, U... others) const {
367 return Has(first) && Has(others...);
368 }
Jacob Bramley2af191d2018-05-16 10:22:44 +0100369
Jacob Bramleyc44ce3d2018-06-12 15:39:09 +0100370 // Return the number of enabled features.
371 size_t Count() const;
Jacob Bramley2fb52cb2019-04-16 15:22:31 +0100372 bool HasNoFeatures() const { return Count() == 0; }
Jacob Bramleyc44ce3d2018-06-12 15:39:09 +0100373
374 // Check for equivalence.
375 bool operator==(const CPUFeatures& other) const {
376 return Has(other) && other.Has(*this);
377 }
378 bool operator!=(const CPUFeatures& other) const { return !(*this == other); }
379
Jacob Bramley2af191d2018-05-16 10:22:44 +0100380 typedef CPUFeaturesConstIterator const_iterator;
381
382 const_iterator begin() const;
383 const_iterator end() const;
384
385 private:
Jacob Bramley8c4ceb62020-07-08 20:53:32 +0100386 // Each bit represents a feature. This set will be extended as needed.
387 std::bitset<kNumberOfFeatures> features_;
Jacob Bramley2af191d2018-05-16 10:22:44 +0100388
389 friend std::ostream& operator<<(std::ostream& os,
390 const vixl::CPUFeatures& features);
391};
392
393std::ostream& operator<<(std::ostream& os, vixl::CPUFeatures::Feature feature);
394std::ostream& operator<<(std::ostream& os, const vixl::CPUFeatures& features);
395
396// This is not a proper C++ iterator type, but it simulates enough of
397// ForwardIterator that simple loops can be written.
398class CPUFeaturesConstIterator {
399 public:
400 CPUFeaturesConstIterator(const CPUFeatures* cpu_features = NULL,
401 CPUFeatures::Feature start = CPUFeatures::kNone)
402 : cpu_features_(cpu_features), feature_(start) {
403 VIXL_ASSERT(IsValid());
404 }
405
406 bool operator==(const CPUFeaturesConstIterator& other) const;
407 bool operator!=(const CPUFeaturesConstIterator& other) const {
408 return !(*this == other);
409 }
Jacob Bramleycaa40ee2020-07-08 20:46:38 +0100410 CPUFeaturesConstIterator& operator++();
411 CPUFeaturesConstIterator operator++(int);
Jacob Bramley2af191d2018-05-16 10:22:44 +0100412
413 CPUFeatures::Feature operator*() const {
414 VIXL_ASSERT(IsValid());
415 return feature_;
416 }
417
418 // For proper support of C++'s simplest "Iterator" concept, this class would
419 // have to define member types (such as CPUFeaturesIterator::pointer) to make
420 // it appear as if it iterates over Feature objects in memory. That is, we'd
421 // need CPUFeatures::iterator to behave like std::vector<Feature>::iterator.
422 // This is at least partially possible -- the std::vector<bool> specialisation
423 // does something similar -- but it doesn't seem worthwhile for a
424 // special-purpose debug helper, so they are omitted here.
425 private:
426 const CPUFeatures* cpu_features_;
427 CPUFeatures::Feature feature_;
428
429 bool IsValid() const {
Jacob Bramley5523f6c2019-06-28 11:37:26 +0100430 if (cpu_features_ == NULL) {
431 return feature_ == CPUFeatures::kNone;
432 }
433 return cpu_features_->Has(feature_);
Jacob Bramley2af191d2018-05-16 10:22:44 +0100434 }
435};
436
437// A convenience scope for temporarily modifying a CPU features object. This
438// allows features to be enabled for short sequences.
439//
440// Expected usage:
441//
442// {
443// CPUFeaturesScope cpu(&masm, CPUFeatures::kCRC32);
444// // This scope can now use CRC32, as well as anything else that was enabled
445// // before the scope.
446//
447// ...
448//
449// // At the end of the scope, the original CPU features are restored.
450// }
451class CPUFeaturesScope {
452 public:
453 // Start a CPUFeaturesScope on any object that implements
454 // `CPUFeatures* GetCPUFeatures()`.
455 template <typename T>
Jacob Bramley8c4ceb62020-07-08 20:53:32 +0100456 explicit CPUFeaturesScope(T* cpu_features_wrapper)
Jacob Bramley2af191d2018-05-16 10:22:44 +0100457 : cpu_features_(cpu_features_wrapper->GetCPUFeatures()),
Jacob Bramley8c4ceb62020-07-08 20:53:32 +0100458 old_features_(*cpu_features_) {}
Jacob Bramley2af191d2018-05-16 10:22:44 +0100459
Jacob Bramley8c4ceb62020-07-08 20:53:32 +0100460 // Start a CPUFeaturesScope on any object that implements
461 // `CPUFeatures* GetCPUFeatures()`, with the specified features enabled.
462 template <typename T, typename U, typename... V>
463 CPUFeaturesScope(T* cpu_features_wrapper, U first, V... features)
Jacob Bramley2af191d2018-05-16 10:22:44 +0100464 : cpu_features_(cpu_features_wrapper->GetCPUFeatures()),
465 old_features_(*cpu_features_) {
Jacob Bramley8c4ceb62020-07-08 20:53:32 +0100466 cpu_features_->Combine(first, features...);
Jacob Bramley2af191d2018-05-16 10:22:44 +0100467 }
468
469 ~CPUFeaturesScope() { *cpu_features_ = old_features_; }
470
471 // For advanced usage, the CPUFeatures object can be accessed directly.
472 // The scope will restore the original state when it ends.
473
474 CPUFeatures* GetCPUFeatures() const { return cpu_features_; }
475
476 void SetCPUFeatures(const CPUFeatures& cpu_features) {
477 *cpu_features_ = cpu_features;
478 }
479
480 private:
481 CPUFeatures* const cpu_features_;
482 const CPUFeatures old_features_;
483};
484
485
486} // namespace vixl
487
488#endif // VIXL_CPU_FEATURES_H