Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(984)

Unified Diff: base/cpu.cc

Issue 12511002: Adding check for OS support to AVX (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: using file_util::ReadFileToString Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/cpu.cc
diff --git a/base/cpu.cc b/base/cpu.cc
index d6976bab33b316aed50e3a133edf1a7e8a060a97..a7dc97bb080d311d04701093b6425b28cecc24f7 100644
--- a/base/cpu.cc
+++ b/base/cpu.cc
@@ -11,6 +11,16 @@
#include "build/build_config.h"
#if defined(ARCH_CPU_X86_FAMILY)
+#if defined(OS_LINUX)
+#include "base/file_util.h"
+#endif
+
+#if defined (OS_MACOSX)
apatrick_chromium 2013/03/06 19:19:21 nit: space between "defined" and "("
+#include "base/version.h"
apatrick_chromium 2013/03/06 19:19:21 #include "base/memory/scoped_ptr.h"
+#include <sys/param.h>
+#include <sys/sysctl.h>
+#endif
+
apatrick_chromium 2013/03/06 19:19:21 #if defined(OS_WIN) #include <windows.h> #endif
#if defined(_MSC_VER)
#include <intrin.h>
#endif
@@ -32,6 +42,7 @@ CPU::CPU()
has_ssse3_(false),
has_sse41_(false),
has_sse42_(false),
+ has_avx_(false),
cpu_vendor_("unknown") {
Initialize();
}
@@ -81,6 +92,56 @@ void __cpuidex(int cpu_info[4], int info_type, int info_index) {
#endif
#endif // _MSC_VER
+
+#if defined(OS_WIN)
+bool OSHasAVXSupport() {
+ OSVERSIONINFO version_info;
apatrick_chromium 2013/03/06 19:19:21 Consider OSVERSIONINFOEX to get service pack versi
+
+ if (!GetVersionEx(&version_info))
apatrick_chromium 2013/03/06 19:19:21 You need to set version_info.dwOSVersionInfoSize t
+ return false;
+
+ // Windows 7 SP1 added support for AVX.
apatrick_chromium 2013/03/06 19:19:21 This tests for Windows 7 or later rather than Wind
+ return version_info.dwMajorVersion > 6 ||
+ version_info.dwMajorVersion == 6 &&
+ version_info.dwMinorVersion > 1;
+}
+#elif defined(OS_LINUX)
+bool OSHasAVXSupport() {
+ std::string buffer;
+ if (!file_util::ReadFileToString("/proc/cpuinfo", &buffer))
DaleCurtis 2013/03/06 18:32:37 Is there a different way you can write this check?
apatrick_chromium 2013/03/06 19:19:21 First argument should be of type base::FilePath.
+ return false;
+
+ // Scan through /proc/cpuinfo to see if the kernel supports AVX.
+ return buffer.find("avx") != std::string::npos;
+}
+#elif defined(OS_MACOSX)
apatrick_chromium 2013/03/06 19:19:21 nit: two spaces between #elif and defined
+bool OSHasAVXSupport() {
+ size_t length;
+ char* kernel_version;
+ const static int mib[2] = {CTL_KERN, KERN_OSRELEASE};
apatrick_chromium 2013/03/06 19:19:21 This is passed to sysctl so it can't be const. htt
+
+ if (sysctl(mib, 2, NULL, &length, NULL, 0) < 0)
apatrick_chromium 2013/03/06 19:19:21 2 -> arraysize(mib)
+ return false;
+
+ scoped_ptr<char[]> buffer = new char[length];
+ if (!buffer)
+ return false;
+
+ if (sysctl(mib, 2, buffer, &length, NULL, 0) < 0)
DaleCurtis 2013/03/06 18:32:37 Perhaps the same sandbox issue.
apatrick_chromium 2013/03/06 19:19:21 buffer.get() 2 -> arraysize(mib)
+ return false;
+
+ base::Version kernel_version(buffer);
apatrick_chromium 2013/03/06 19:19:21 kernel_version is declared twice. Also Version is
+ if (!kernel_version.IsValid())
+ return false;
+
+ // AVX support was added in 10.6.8, which has kernel 10.8.
+ return kernel_version.CompareTo(base::Version("10.8")) >= 0;
+}
+#else
+bool OSHasAVXSupport() {
+ return false;
+}
+#endif
#endif // ARCH_CPU_X86_FAMILY
void CPU::Initialize() {
@@ -117,7 +178,7 @@ void CPU::Initialize() {
has_ssse3_ = (cpu_info[2] & 0x00000200) != 0;
has_sse41_ = (cpu_info[2] & 0x00080000) != 0;
has_sse42_ = (cpu_info[2] & 0x00100000) != 0;
- has_avx_ = (cpu_info[2] & 0x10000000) != 0;
+ has_avx_ = (cpu_info[2] & 0x10000000) != 0 && OSHasAVXSupport();
}
// Get the brand string of the cpu.
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698