blob: f23d827b71a3195752e12a2661ba155a942900a9 [file] [log] [blame]
simon.hosie953b1c12016-04-06 14:02:26 -07001/*
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include <stdlib.h>
12#include <string.h>
13#include <features.h>
14#if __GLIBC_PREREQ(2, 16)
15#include <sys/auxv.h>
16#else
17#include <fcntl.h>
18#include <unistd.h>
19#include <errno.h>
20#include <link.h>
21#endif
22#include "webrtc/system_wrappers/include/cpu_features_wrapper.h"
23
24#if defined(WEBRTC_ARCH_ARM_FAMILY)
25#include <asm/hwcap.h>
26
27uint64_t WebRtc_GetCPUFeaturesARM(void) {
28 uint64_t result = 0;
29 int architecture = 0;
30 unsigned long hwcap = 0;
31 const char* platform = NULL;
32#if __GLIBC_PREREQ(2, 16)
33 hwcap = getauxval(AT_HWCAP);
34 platform = (const char*)getauxval(AT_PLATFORM);
35#else
36 ElfW(auxv_t) auxv;
37 int fd = open("/proc/self/auxv", O_RDONLY);
38 if (fd >= 0) {
39 while (hwcap == 0 || platform == NULL) {
40 if (read(fd, &auxv, sizeof(auxv)) < (ssize_t)sizeof(auxv)) {
41 if (errno == EINTR)
42 continue;
43 break;
44 }
45 switch (auxv.a_type) {
46 case AT_HWCAP:
47 hwcap = auxv.a_un.a_val;
48 break;
49 case AT_PLATFORM:
50 platform = (const char*)auxv.a_un.a_val;
51 break;
52 }
53 }
54 close(fd);
55 }
56#endif // __GLIBC_PREREQ(2,16)
57#if defined(__aarch64__)
58 architecture = 8;
59 if ((hwcap & HWCAP_FP) != 0)
60 result |= kCPUFeatureVFPv3;
61 if ((hwcap & HWCAP_ASIMD) != 0)
62 result |= kCPUFeatureNEON;
63#else
64 if (platform != NULL) {
65 /* expect a string in the form "v6l" or "v7l", etc.
66 */
67 if (platform[0] == 'v' && '0' <= platform[1] && platform[1] <= '9' &&
68 (platform[2] == 'l' || platform[2] == 'b')) {
69 architecture = platform[1] - '0';
70 }
71 }
72 if ((hwcap & HWCAP_VFPv3) != 0)
73 result |= kCPUFeatureVFPv3;
74 if ((hwcap & HWCAP_NEON) != 0)
75 result |= kCPUFeatureNEON;
76#endif
77 if (architecture >= 7)
78 result |= kCPUFeatureARMv7;
79 if (architecture >= 6)
80 result |= kCPUFeatureLDREXSTREX;
81 return result;
82}
83#endif // WEBRTC_ARCH_ARM_FAMILY