Index: Source/wtf/CPU.cpp |
diff --git a/Source/wtf/CPU.cpp b/Source/wtf/CPU.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..ebe4a9035340f94fff4b8aa8cef69e6b73432282 |
--- /dev/null |
+++ b/Source/wtf/CPU.cpp |
@@ -0,0 +1,162 @@ |
+/* |
+ * Copyright (C) 2014 Samsung Electronics. All rights reserved. |
+ * |
+ * Redistribution and use in source and binary forms, with or without |
+ * modification, are permitted provided that the following conditions |
+ * are met: |
+ * 1. Redistributions of source code must retain the above copyright |
+ * notice, this list of conditions and the following disclaimer. |
+ * 2. Redistributions in binary form must reproduce the above copyright |
+ * notice, this list of conditions and the following disclaimer in the |
+ * documentation and/or other materials provided with the distribution. |
+ * |
+ * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY |
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR |
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
+ */ |
+ |
+#include "config.h" |
+ |
+#include "CPU.h" |
+ |
+#if CPU(ARM_NEON_OPTIONAL) |
+#include "wtf/Assertions.h" |
+#include <pthread.h> |
+ |
+#if OS(LINUX) |
+#include <errno.h> |
+#include <fcntl.h> |
+#include <string.h> |
+#include <unistd.h> |
+#endif |
+ |
+#if OS(ANDROID) |
+#include <cpu-features.h> |
+#endif |
+ |
+static pthread_once_t sOnce; |
+static bool sHasArmNeon; |
+ |
+// A function used to determine at runtime if the target CPU supports |
+// the ARM NEON instruction set. This implementation is Linux and Android specific. |
+static bool checkNEON(void) |
+{ |
+ // If we fail any of the following, assume we don't have NEON instructions |
+ // This allows us to return immediately in case of error. |
+ bool result = false; |
+ |
+#if OS(ANDROID) |
+ // Use the Android NDK's cpu-features helper library to detect NEON at runtime. |
+ // See http://crbug.com/164154 for skia to see why this is needed in Chromium for Android. |
+ result = (android_getCpuFeatures() & ANDROID_CPU_ARM_FEATURE_NEON) != 0; |
+#elif OS(LINUX) |
+ // There is no user-accessible CPUID instruction on ARM that we can use. |
+ // Instead, we must parse /proc/cpuinfo and look for the 'neon' feature. |
+ // For example, here's a typical output (Nexus S running ICS 4.0.3): |
+ /* |
+ Processor : ARMv7 Processor rev 2 (v7l) |
+ BogoMIPS : 994.65 |
+ Features : swp half thumb fastmult vfp edsp thumbee neon vfpv3 |
+ CPU implementer : 0x41 |
+ CPU architecture: 7 |
+ CPU variant : 0x2 |
+ CPU part : 0xc08 |
+ CPU revision : 2 |
+ |
+ Hardware : herring |
+ Revision : 000b |
+ Serial : 3833c77d6dc000ec |
+ */ |
+ char buffer[4096]; |
+ |
+ do { |
+ // open /proc/cpuinfo |
+ int fd = TEMP_FAILURE_RETRY(open("/proc/cpuinfo", O_RDONLY)); |
+ if (fd < 0) { |
+ WTF_LOG_ERROR("Could not open /proc/cpuinfo: %s", strerror(errno)); |
+ break; |
+ } |
+ |
+ // Read the file. To simplify our search, we're going to place two |
+ // sentinel '\n' characters: one at the start of the buffer, and one at |
+ // the end. This means we reserve the first and last buffer bytes. |
+ buffer[0] = '\n'; |
+ int size = TEMP_FAILURE_RETRY(read(fd, buffer+1, sizeof(buffer)-2)); |
+ close(fd); |
+ |
+ if (size < 0) { |
+ WTF_LOG_ERROR("Could not read /proc/cpuinfo: %s", strerror(errno)); |
+ break; |
+ } |
+ |
+ WTF_LOG(CPU, "START /proc/cpuinfo:\n%.*s\nEND /proc/cpuinfo", size, buffer+1); |
+ |
+ // Compute buffer limit, and place final sentinel |
+ char* bufferEnd = buffer + 1 + size; |
+ bufferEnd[0] = '\n'; |
+ |
+ // Now, find a line that starts with "Features", i.e. look for |
+ // '\nFeatures ' in our buffer. |
+ const char features[] = "\nFeatures\t"; |
+ const size_t featuresLength = sizeof(features)-1; |
+ |
+ char* line = (char*) memmem(buffer, bufferEnd - buffer, features, featuresLength); |
+ if (!line) { |
+ WTF_LOG(CPU, "Could not find a line starting with 'Features' in /proc/cpuinfo ?"); |
+ break; |
+ } |
+ |
+ // Skip the "\nFeatures\t" prefix |
+ line += featuresLength; |
+ |
+ // Find the end of the current line |
+ char* lineEnd = (char*) memchr(line, '\n', bufferEnd - line); |
+ if (!lineEnd) |
+ lineEnd = bufferEnd; |
+ |
+ // Now find an instance of 'neon' in the flags list. We want to |
+ // ensure it's only 'neon' and not something fancy like 'noneon' |
+ // so check that it follows a space. |
+ const char neon[] = " neon"; |
+ const size_t neonLength = sizeof(neon)-1; |
+ const char* flag = (const char*) memmem(line, lineEnd - line, neon, neonLength); |
+ if (!flag) |
+ break; |
+ |
+ // Ensure it is followed by a space or a newline. |
+ if (flag[neonLength] != ' ' && flag[neonLength] != '\n') |
+ break; |
+ |
+ // Fine, we support Arm NEON ! |
+ result = true; |
+ |
+ } while (0); |
+#endif // OS(ANDROID) |
+ |
+ if (result) |
+ WTF_LOG(CPU, "Device supports ARM NEON instructions!\n"); |
+ else |
+ WTF_LOG(CPU, "Device does NOT support ARM NEON instructions!\n"); |
+ return result; |
+} |
+ |
+// called through pthread_once() |
+void probeNEONFeature(void) |
+{ |
+ sHasArmNeon = checkNEON(); |
+} |
+ |
+bool isSupportedNEON(void) |
+{ |
+ pthread_once(&sOnce, probeNEONFeature); |
+ return sHasArmNeon; |
+} |
+#endif |