| Index: Source/wtf/CPU.cpp
|
| diff --git a/Source/wtf/CPU.cpp b/Source/wtf/CPU.cpp
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..f8fa085e03af4942128926fe2f3ae07bfb2f210b
|
| --- /dev/null
|
| +++ b/Source/wtf/CPU.cpp
|
| @@ -0,0 +1,140 @@
|
| +// Copyright 2014 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "config.h"
|
| +
|
| +#include "CPU.h"
|
| +
|
| +#if CPU(ARM_NEON_OPTIONAL)
|
| +
|
| +#include "wtf/Assertions.h"
|
| +#include <pthread.h>
|
| +
|
| +#if OS(ANDROID)
|
| +#include <cpu-features.h>
|
| +#elif OS(LINUX)
|
| +#include <errno.h>
|
| +#include <fcntl.h>
|
| +#include <string.h>
|
| +#include <unistd.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
|
|
|