LeechCraft  0.6.70-10870-g558588d6ec
Modular cross-platform feature rich live environment.
cpufeatures.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * LeechCraft - modular cross-platform feature rich internet client.
3  * Copyright (C) 2006-2014 Georg Rudoy
4  *
5  * Boost Software License - Version 1.0 - August 17th, 2003
6  *
7  * Permission is hereby granted, free of charge, to any person or organization
8  * obtaining a copy of the software and accompanying documentation covered by
9  * this license (the "Software") to use, reproduce, display, distribute,
10  * execute, and transmit the Software, and to prepare derivative works of the
11  * Software, and to permit third-parties to whom the Software is furnished to
12  * do so, all subject to the following:
13  *
14  * The copyright notices in the Software and this entire statement, including
15  * the above license grant, this restriction and the following disclaimer,
16  * must be included in all copies of the Software, in whole or in part, and
17  * all derivative works of the Software, unless such copies or derivative
18  * works are solely in the form of machine-executable object code generated by
19  * a source language processor.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
24  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
25  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
26  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  **********************************************************************/
29 
30 #include "cpufeatures.h"
31 #include <mutex>
32 #include <QStringList>
33 #include <QtDebug>
34 
35 #if defined (Q_PROCESSOR_X86)
36 #define HAS_CPUID
37 #endif
38 
39 #ifdef HAS_CPUID
40 #include <cpuid.h>
41 #endif
42 
43 #include <util/sll/unreachable.h>
44 
45 namespace LeechCraft
46 {
47 namespace Util
48 {
50  {
51 #ifdef HAS_CPUID
52  uint32_t eax = 0, ebx = 0, ecx = 0, edx = 0;
53  if (!__get_cpuid (1, &eax, &ebx, &ecx, &edx))
54  qWarning () << Q_FUNC_INFO
55  << "failed to get CPUID eax = 1";
56  else
57  Ecx1_ = ecx;
58 
59  if (__get_cpuid_max (0, nullptr) < 7)
60  qWarning () << Q_FUNC_INFO
61  << "cpuid max less than 7";
62  else
63  {
64  __cpuid_count (7, 0, eax, ebx, ecx, edx);
65  Ebx7_ = ebx;
66  }
67 #endif
68 
69  static std::once_flag dbgFlag;
70  std::call_once (dbgFlag,
71  [this] { DumpDetectedFeatures (); });
72  }
73 
75  {
76  switch (feature)
77  {
78  case Feature::SSSE3:
79  return "ssse3";
80  case Feature::SSE41:
81  return "sse4.1";
82  case Feature::AVX:
83  return "avx";
84  case Feature::XSave:
85  return "xsave";
86  case Feature::AVX2:
87  return "avx2";
88  case Feature::None:
89  return "";
90  }
91 
93  }
94 
95  bool CpuFeatures::HasFeature (Feature feature) const
96  {
97  switch (feature)
98  {
99  case Feature::SSSE3:
100  return Ecx1_ & (1 << 9);
101  case Feature::SSE41:
102  return Ecx1_ & (1 << 19);
103  case Feature::AVX:
104  return Ecx1_ & (1 << 28);
105  case Feature::XSave:
106  return Ecx1_ & (1 << 26);
107  case Feature::AVX2:
108  return HasFeature (Feature::XSave) && (Ebx7_ & (1 << 5));
109  case Feature::None:
110  return true;
111  }
112 
114  }
115 
116  void CpuFeatures::DumpDetectedFeatures () const
117  {
118  if (qgetenv ("DUMP_CPUFEATURES").isEmpty ())
119  return;
120 
121  QStringList detected;
122  QStringList undetected;
123 
124  for (int i = 0; i < static_cast<int> (Feature::None); ++i)
125  {
126  const auto feature = static_cast<Feature> (i);
127  const auto& featureName = GetFeatureName (feature);
128  if (HasFeature (feature))
129  detected << featureName;
130  else
131  undetected << featureName;
132  }
133 
134  qDebug () << Q_FUNC_INFO;
135  qDebug () << "detected the following CPU features:" << detected.join (" ").toUtf8 ().constData ();
136  qDebug () << "couldn't detect the following CPU features:" << undetected.join (" ").toUtf8 ().constData ();
137  }
138 }
139 }
bool HasFeature(Feature) const
Definition: cpufeatures.cpp:95
void Unreachable()
Definition: unreachable.h:36
static QString GetFeatureName(Feature)
Definition: cpufeatures.cpp:74