1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ART_RUNTIME_ARCH_X86_64_INSTRUCTION_SET_FEATURES_X86_64_H_
18 #define ART_RUNTIME_ARCH_X86_64_INSTRUCTION_SET_FEATURES_X86_64_H_
19 
20 #include "arch/x86/instruction_set_features_x86.h"
21 #include "base/macros.h"
22 
23 namespace art HIDDEN {
24 
25 class X86_64InstructionSetFeatures;
26 using X86_64FeaturesUniquePtr = std::unique_ptr<const X86_64InstructionSetFeatures>;
27 
28 // Instruction set features relevant to the X86_64 architecture.
29 class X86_64InstructionSetFeatures final : public X86InstructionSetFeatures {
30  public:
31   // Process a CPU variant string like "atom" or "nehalem" and create InstructionSetFeatures.
FromVariant(const std::string & variant,std::string * error_msg)32   static X86_64FeaturesUniquePtr FromVariant(const std::string& variant, std::string* error_msg) {
33     return Convert(X86InstructionSetFeatures::FromVariant(variant, error_msg, true));
34   }
35 
36   // Parse a bitmap and create an InstructionSetFeatures.
FromBitmap(uint32_t bitmap)37   static X86_64FeaturesUniquePtr FromBitmap(uint32_t bitmap) {
38     return Convert(X86InstructionSetFeatures::FromBitmap(bitmap, true));
39   }
40 
41   // Turn C pre-processor #defines into the equivalent instruction set features.
FromCppDefines()42   static X86_64FeaturesUniquePtr FromCppDefines() {
43     return Convert(X86InstructionSetFeatures::FromCppDefines(true));
44   }
45 
46   // Process /proc/cpuinfo and use kRuntimeISA to produce InstructionSetFeatures.
FromCpuInfo()47   static X86_64FeaturesUniquePtr FromCpuInfo() {
48     return Convert(X86InstructionSetFeatures::FromCpuInfo(true));
49   }
50 
51   // Process the auxiliary vector AT_HWCAP entry and use kRuntimeISA to produce
52   // InstructionSetFeatures.
FromHwcap()53   static X86_64FeaturesUniquePtr FromHwcap() {
54     return Convert(X86InstructionSetFeatures::FromHwcap(true));
55   }
56 
57   // Use assembly tests of the current runtime (ie kRuntimeISA) to determine the
58   // InstructionSetFeatures. This works around kernel bugs in AT_HWCAP and /proc/cpuinfo.
FromAssembly()59   static X86_64FeaturesUniquePtr FromAssembly() {
60     return Convert(X86InstructionSetFeatures::FromAssembly(true));
61   }
62 
63   // Use external cpu_features library.
FromCpuFeatures()64   static X86_64FeaturesUniquePtr FromCpuFeatures() {
65     return Convert(X86InstructionSetFeatures::FromCpuFeatures(true));
66   }
67 
GetInstructionSet()68   InstructionSet GetInstructionSet() const override {
69     return InstructionSet::kX86_64;
70   }
71 
~X86_64InstructionSetFeatures()72   virtual ~X86_64InstructionSetFeatures() {}
73 
74  protected:
75   // Parse a string of the form "ssse3" adding these to a new InstructionSetFeatures.
76   std::unique_ptr<const InstructionSetFeatures>
AddFeaturesFromSplitString(const std::vector<std::string> & features,std::string * error_msg)77       AddFeaturesFromSplitString(const std::vector<std::string>& features,
78                                  std::string* error_msg) const override {
79     return X86InstructionSetFeatures::AddFeaturesFromSplitString(features, true, error_msg);
80   }
81 
82  private:
X86_64InstructionSetFeatures(bool has_SSSE3,bool has_SSE4_1,bool has_SSE4_2,bool has_AVX,bool has_AVX2,bool has_POPCNT)83   X86_64InstructionSetFeatures(bool has_SSSE3,
84                                bool has_SSE4_1,
85                                bool has_SSE4_2,
86                                bool has_AVX,
87                                bool has_AVX2,
88                                bool has_POPCNT)
89       : X86InstructionSetFeatures(has_SSSE3, has_SSE4_1, has_SSE4_2, has_AVX,
90                                   has_AVX2, has_POPCNT) {
91   }
92 
Convert(X86FeaturesUniquePtr && in)93   static X86_64FeaturesUniquePtr Convert(X86FeaturesUniquePtr&& in) {
94     return X86_64FeaturesUniquePtr(in.release()->AsX86_64InstructionSetFeatures());
95   }
96 
97   friend class X86InstructionSetFeatures;
98 
99   DISALLOW_COPY_AND_ASSIGN(X86_64InstructionSetFeatures);
100 };
101 
102 }  // namespace art
103 
104 #endif  // ART_RUNTIME_ARCH_X86_64_INSTRUCTION_SET_FEATURES_X86_64_H_
105