ManaPlus
Enumerations | Functions
Cpu Namespace Reference

Enumerations

enum  {
  FEATURE_EMPTY = 0 , FEATURE_MMX = 1 , FEATURE_SSE = 2 , FEATURE_SSE2 = 4 ,
  FEATURE_SSSE3 = 8 , FEATURE_SSE4 = 16 , FEATURE_SSE42 = 32 , FEATURE_AVX = 64 ,
  FEATURE_AVX2 = 128
}
 

Functions

void detect ()
 
void printFlags ()
 
uint32_t getFlags ()
 

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
FEATURE_EMPTY 
FEATURE_MMX 
FEATURE_SSE 
FEATURE_SSE2 
FEATURE_SSSE3 
FEATURE_SSE4 
FEATURE_SSE42 
FEATURE_AVX 
FEATURE_AVX2 

Definition at line 29 of file cpu.h.

30  {
31  FEATURE_EMPTY = 0,
32  FEATURE_MMX = 1,
33  FEATURE_SSE = 2,
34  FEATURE_SSE2 = 4,
35  FEATURE_SSSE3 = 8,
36  FEATURE_SSE4 = 16,
37  FEATURE_SSE42 = 32,
38  FEATURE_AVX = 64,
39  FEATURE_AVX2 = 128
40  };
@ FEATURE_SSE42
Definition: cpu.h:37
@ FEATURE_MMX
Definition: cpu.h:32
@ FEATURE_AVX
Definition: cpu.h:38
@ FEATURE_SSE
Definition: cpu.h:33
@ FEATURE_AVX2
Definition: cpu.h:39
@ FEATURE_SSE2
Definition: cpu.h:34
@ FEATURE_SSE4
Definition: cpu.h:36
@ FEATURE_SSSE3
Definition: cpu.h:35
@ FEATURE_EMPTY
Definition: cpu.h:31

Function Documentation

◆ detect()

void Cpu::detect ( )

Definition at line 46 of file cpu.cpp.

47 {
48 #if (defined(__amd64__) || defined(__i386__)) && defined(__GNUC__) \
49  && (GCC_VERSION >= 40800) && !defined(ANDROID)
50  __builtin_cpu_init();
51  if (__builtin_cpu_supports ("mmx"))
53  if (__builtin_cpu_supports ("sse"))
55  if (__builtin_cpu_supports ("sse2"))
57  if (__builtin_cpu_supports ("ssse3"))
59  if (__builtin_cpu_supports ("sse4.1"))
61  if (__builtin_cpu_supports ("sse4.2"))
63  if (__builtin_cpu_supports ("avx"))
65  if (__builtin_cpu_supports ("avx2"))
67  printFlags();
68 #elif defined(__linux__) || defined(__linux)
69  FILE *file = fopen("/proc/cpuinfo", "r");
70  char buf[1001];
71  if (file == nullptr)
72  return;
73  while (fgets(buf, 1000, file) != nullptr)
74  {
75  std::string str = buf;
76  if (findFirst(str, "flags"))
77  {
78  size_t idx = str.find(':');
79  if (idx == std::string::npos)
80  continue;
81  str = str.substr(idx + 1);
82  trim(str);
83  StringVect vect;
84  splitToStringVector(vect, str, ' ');
85  FOR_EACH (StringVectCIter, it, vect)
86  {
87  const std::string &flag = *it;
88  if (flag == "mmx")
90  else if (flag == "sse")
92  else if (flag == "sse2")
94  else if (flag == "ssse3")
96  else if (flag == "sse4_1")
98  else if (flag == "sse4_2")
100  else if (flag == "avx")
102  else if (flag == "avx2")
104  }
105  fclose(file);
106  printFlags();
107  return;
108  }
109  }
110  fclose(file);
111  if (logger != nullptr)
112  logger->log("cpu features was not detected");
113 #else // OTHER
114 
115 #ifdef USE_SDL2
116  if (SDL_HasMMX())
118  if (SDL_HasSSE())
120  if (SDL_HasSSE2())
122  if (SDL_HasSSE3())
124  if (SDL_HasSSE41())
126  if (SDL_HasSSE42())
128 
129 #if SDL_VERSION_ATLEAST(2, 0, 2)
130  if (SDL_HasAVX())
132 #endif // SDL_VERSION_ATLEAST(2, 0, 2)
133 #if SDL_VERSION_ATLEAST(2, 0, 4)
134  if (SDL_HasAVX2())
136 #endif // SDL_VERSION_ATLEAST(2, 0, 4)
137 
138  printFlags();
139 #else // USE_SDL2
140 
141  if (logger)
142  logger->log("cpu features not supported");
143 #endif // USE_SDL2
144 #endif // (defined(__amd64__) || defined(__i386__)) && defined(__GNUC__)
145  // && (GCC_VERSION >= 40800) && !defined(ANDROID)
146 
147 #if SDL_VERSION_ATLEAST(2, 0, 1)
148  logger->log("System ram size: %d", SDL_GetSystemRAM());
149 #endif // SDL_VERSION_ATLEAST(2, 0, 1)
150 }
void log(const char *const log_text,...)
Definition: logger.cpp:269
#define FOR_EACH(type, iter, array)
Definition: foreach.h:25
Logger * logger
Definition: logger.cpp:89
std::string trim(std::string const &str)
void printFlags()
Definition: cpu.cpp:152
void splitToStringVector(StringVect &tokens, const std::string &text, const char separator)
bool findFirst(const std::string &str1, const std::string &str2)
StringVect::const_iterator StringVectCIter
Definition: stringvector.h:31
std::vector< std::string > StringVect
Definition: stringvector.h:29

References FEATURE_AVX, FEATURE_AVX2, FEATURE_MMX, FEATURE_SSE, FEATURE_SSE2, FEATURE_SSE4, FEATURE_SSE42, FEATURE_SSSE3, findFirst(), FOR_EACH, Logger::log(), logger, anonymous_namespace{cpu.cpp}::mCpuFlags, printFlags(), splitToStringVector(), and Catch::trim().

Referenced by Client::gameInit(), and main().

◆ getFlags()

uint32_t Cpu::getFlags ( )

Definition at line 176 of file cpu.cpp.

177 {
178  return mCpuFlags;
179 }

References anonymous_namespace{cpu.cpp}::mCpuFlags.

Referenced by DyePalette::initFunctions().

◆ printFlags()

void Cpu::printFlags ( )

Definition at line 152 of file cpu.cpp.

153 {
154  if (logger == nullptr)
155  return;
156  std::string str("CPU features:");
157  if ((mCpuFlags & FEATURE_MMX) != 0U)
158  str.append(" mmx");
159  if ((mCpuFlags & FEATURE_SSE) != 0U)
160  str.append(" sse");
161  if ((mCpuFlags & FEATURE_SSE2) != 0U)
162  str.append(" sse2");
163  if ((mCpuFlags & FEATURE_SSSE3) != 0U)
164  str.append(" ssse3");
165  if ((mCpuFlags & FEATURE_SSE4) != 0U)
166  str.append(" sse4");
167  if ((mCpuFlags & FEATURE_SSE42) != 0U)
168  str.append(" sse4_2");
169  if ((mCpuFlags & FEATURE_AVX) != 0U)
170  str.append(" avx");
171  if ((mCpuFlags & FEATURE_AVX2) != 0U)
172  str.append(" avx2");
173  logger->log(str);
174 }

References FEATURE_AVX, FEATURE_AVX2, FEATURE_MMX, FEATURE_SSE, FEATURE_SSE2, FEATURE_SSE4, FEATURE_SSE42, FEATURE_SSSE3, Logger::log(), logger, and anonymous_namespace{cpu.cpp}::mCpuFlags.

Referenced by detect().