blob: 9c5645068a5472fc8de430e8a17eddef8d240479 [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>
hillmab0727392017-03-06 07:34:06 -080014#ifndef __GLIBC_PREREQ
15#define __GLIBC_PREREQ(a, b) 0
16#endif
simon.hosie953b1c12016-04-06 14:02:26 -070017#if __GLIBC_PREREQ(2, 16)
18#include <sys/auxv.h>
19#else
20#include <fcntl.h>
21#include <unistd.h>
22#include <errno.h>
23#include <link.h>
24#endif
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "system_wrappers/include/cpu_features_wrapper.h"
simon.hosie953b1c12016-04-06 14:02:26 -070026
27#if defined(WEBRTC_ARCH_ARM_FAMILY)
28#include <asm/hwcap.h>
29
30uint64_t WebRtc_GetCPUFeaturesARM(void) {
31 uint64_t result = 0;
32 int architecture = 0;
33 unsigned long hwcap = 0;
34 const char* platform = NULL;
35#if __GLIBC_PREREQ(2, 16)
36 hwcap = getauxval(AT_HWCAP);
37 platform = (const char*)getauxval(AT_PLATFORM);
38#else
39 ElfW(auxv_t) auxv;
40 int fd = open("/proc/self/auxv", O_RDONLY);
41 if (fd >= 0) {
42 while (hwcap == 0 || platform == NULL) {
43 if (read(fd, &auxv, sizeof(auxv)) < (ssize_t)sizeof(auxv)) {
44 if (errno == EINTR)
45 continue;
46 break;
47 }
48 switch (auxv.a_type) {
49 case AT_HWCAP:
50 hwcap = auxv.a_un.a_val;
51 break;
52 case AT_PLATFORM:
53 platform = (const char*)auxv.a_un.a_val;
54 break;
55 }
56 }
57 close(fd);
58 }
59#endif // __GLIBC_PREREQ(2,16)
60#if defined(__aarch64__)
61 architecture = 8;
62 if ((hwcap & HWCAP_FP) != 0)
63 result |= kCPUFeatureVFPv3;
64 if ((hwcap & HWCAP_ASIMD) != 0)
65 result |= kCPUFeatureNEON;
66#else
67 if (platform != NULL) {
68 /* expect a string in the form "v6l" or "v7l", etc.
69 */
70 if (platform[0] == 'v' && '0' <= platform[1] && platform[1] <= '9' &&
71 (platform[2] == 'l' || platform[2] == 'b')) {
72 architecture = platform[1] - '0';
73 }
74 }
75 if ((hwcap & HWCAP_VFPv3) != 0)
76 result |= kCPUFeatureVFPv3;
77 if ((hwcap & HWCAP_NEON) != 0)
78 result |= kCPUFeatureNEON;
79#endif
80 if (architecture >= 7)
81 result |= kCPUFeatureARMv7;
82 if (architecture >= 6)
83 result |= kCPUFeatureLDREXSTREX;
84 return result;
85}
86#endif // WEBRTC_ARCH_ARM_FAMILY