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