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_INSTRUCTION_SET_FEATURES_X86_H_
18 #define ART_RUNTIME_ARCH_X86_INSTRUCTION_SET_FEATURES_X86_H_
19 
20 #include "arch/instruction_set_features.h"
21 
22 namespace art {
23 
24 class X86InstructionSetFeatures;
25 using X86FeaturesUniquePtr = std::unique_ptr<const X86InstructionSetFeatures>;
26 
27 // Instruction set features relevant to the X86 architecture.
28 class X86InstructionSetFeatures : public InstructionSetFeatures {
29  public:
30   // Process a CPU variant string like "atom" or "nehalem" and create InstructionSetFeatures.
31   static X86FeaturesUniquePtr FromVariant(const std::string& variant,
32                                           std::string* error_msg,
33                                           bool x86_64 = false);
34 
35   // Parse a bitmap and create an InstructionSetFeatures.
36   static X86FeaturesUniquePtr FromBitmap(uint32_t bitmap, bool x86_64 = false);
37 
38   // Turn C pre-processor #defines into the equivalent instruction set features.
39   static X86FeaturesUniquePtr FromCppDefines(bool x86_64 = false);
40 
41   // Process /proc/cpuinfo and use kRuntimeISA to produce InstructionSetFeatures.
42   static X86FeaturesUniquePtr FromCpuInfo(bool x86_64 = false);
43 
44   // Process the auxiliary vector AT_HWCAP entry and use kRuntimeISA to produce
45   // InstructionSetFeatures.
46   static X86FeaturesUniquePtr FromHwcap(bool x86_64 = false);
47 
48   // Use assembly tests of the current runtime (ie kRuntimeISA) to determine the
49   // InstructionSetFeatures. This works around kernel bugs in AT_HWCAP and /proc/cpuinfo.
50   static X86FeaturesUniquePtr FromAssembly(bool x86_64 = false);
51 
52   bool Equals(const InstructionSetFeatures* other) const OVERRIDE;
53 
54   bool HasAtLeast(const InstructionSetFeatures* other) const OVERRIDE;
55 
GetInstructionSet()56   virtual InstructionSet GetInstructionSet() const OVERRIDE {
57     return InstructionSet::kX86;
58   }
59 
60   uint32_t AsBitmap() const OVERRIDE;
61 
62   std::string GetFeatureString() const OVERRIDE;
63 
~X86InstructionSetFeatures()64   virtual ~X86InstructionSetFeatures() {}
65 
HasSSE4_1()66   bool HasSSE4_1() const { return has_SSE4_1_; }
67 
HasPopCnt()68   bool HasPopCnt() const { return has_POPCNT_; }
69 
70  protected:
71   // Parse a string of the form "ssse3" adding these to a new InstructionSetFeatures.
72   virtual std::unique_ptr<const InstructionSetFeatures>
AddFeaturesFromSplitString(const std::vector<std::string> & features,std::string * error_msg)73       AddFeaturesFromSplitString(const std::vector<std::string>& features,
74                                  std::string* error_msg) const OVERRIDE {
75     return AddFeaturesFromSplitString(features, false, error_msg);
76   }
77 
78   std::unique_ptr<const InstructionSetFeatures>
79       AddFeaturesFromSplitString(const std::vector<std::string>& features,
80                                  bool x86_64,
81                                  std::string* error_msg) const;
82 
X86InstructionSetFeatures(bool has_SSSE3,bool has_SSE4_1,bool has_SSE4_2,bool has_AVX,bool has_AVX2,bool has_POPCNT)83   X86InstructionSetFeatures(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       : InstructionSetFeatures(),
90         has_SSSE3_(has_SSSE3),
91         has_SSE4_1_(has_SSE4_1),
92         has_SSE4_2_(has_SSE4_2),
93         has_AVX_(has_AVX),
94         has_AVX2_(has_AVX2),
95         has_POPCNT_(has_POPCNT) {
96   }
97 
98   static X86FeaturesUniquePtr Create(bool x86_64,
99                                      bool has_SSSE3,
100                                      bool has_SSE4_1,
101                                      bool has_SSE4_2,
102                                      bool has_AVX,
103                                      bool has_AVX2,
104                                      bool has_POPCNT);
105 
106  private:
107   // Bitmap positions for encoding features as a bitmap.
108   enum {
109     kSsse3Bitfield = 1 << 0,
110     kSse4_1Bitfield = 1 << 1,
111     kSse4_2Bitfield = 1 << 2,
112     kAvxBitfield = 1 << 3,
113     kAvx2Bitfield = 1 << 4,
114     kPopCntBitfield = 1 << 5,
115   };
116 
117   const bool has_SSSE3_;   // x86 128bit SIMD - Supplemental SSE.
118   const bool has_SSE4_1_;  // x86 128bit SIMD SSE4.1.
119   const bool has_SSE4_2_;  // x86 128bit SIMD SSE4.2.
120   const bool has_AVX_;     // x86 256bit SIMD AVX.
121   const bool has_AVX2_;    // x86 256bit SIMD AVX 2.0.
122   const bool has_POPCNT_;  // x86 population count
123 
124   DISALLOW_COPY_AND_ASSIGN(X86InstructionSetFeatures);
125 };
126 
127 }  // namespace art
128 
129 #endif  // ART_RUNTIME_ARCH_X86_INSTRUCTION_SET_FEATURES_X86_H_
130