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