blob: 335bed4da3194ccf39b571b50a23bc2cfe5a1223 [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
Mirko Bonadei66e73382020-09-05 21:55:35 +020011#include <features.h>
simon.hosie953b1c12016-04-06 14:02:26 -070012#include <stdlib.h>
13#include <string.h>
Karl Wiberg7f557b42020-09-04 14:23:04 +020014
15#ifdef __GLIBC_PREREQ
16#define WEBRTC_GLIBC_PREREQ(a, b) __GLIBC_PREREQ(a, b)
17#else
18#define WEBRTC_GLIBC_PREREQ(a, b) 0
hillmab0727392017-03-06 07:34:06 -080019#endif
Karl Wiberg7f557b42020-09-04 14:23:04 +020020
21#if WEBRTC_GLIBC_PREREQ(2, 16)
simon.hosie953b1c12016-04-06 14:02:26 -070022#include <sys/auxv.h>
23#else
Mirko Bonadeid156a0d2020-09-05 21:18:26 +000024#include <errno.h>
Mirko Bonadei66e73382020-09-05 21:55:35 +020025#include <fcntl.h>
Mirko Bonadeid156a0d2020-09-05 21:18:26 +000026#include <link.h>
Mirko Bonadei66e73382020-09-05 21:55:35 +020027#include <unistd.h>
simon.hosie953b1c12016-04-06 14:02:26 -070028#endif
Karl Wiberg7f557b42020-09-04 14:23:04 +020029
Niels Möllera12c42a2018-07-25 16:05:48 +020030#include "rtc_base/system/arch.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020031#include "system_wrappers/include/cpu_features_wrapper.h"
simon.hosie953b1c12016-04-06 14:02:26 -070032
33#if defined(WEBRTC_ARCH_ARM_FAMILY)
34#include <asm/hwcap.h>
35
Mirko Bonadeibef022b2020-09-06 16:07:15 +020036namespace webrtc {
37
38uint64_t GetCPUFeaturesARM(void) {
simon.hosie953b1c12016-04-06 14:02:26 -070039 uint64_t result = 0;
40 int architecture = 0;
Mirko Bonadei66e73382020-09-05 21:55:35 +020041 uint64_t hwcap = 0;
simon.hosie953b1c12016-04-06 14:02:26 -070042 const char* platform = NULL;
Karl Wiberg7f557b42020-09-04 14:23:04 +020043#if WEBRTC_GLIBC_PREREQ(2, 16)
simon.hosie953b1c12016-04-06 14:02:26 -070044 hwcap = getauxval(AT_HWCAP);
45 platform = (const char*)getauxval(AT_PLATFORM);
46#else
47 ElfW(auxv_t) auxv;
48 int fd = open("/proc/self/auxv", O_RDONLY);
49 if (fd >= 0) {
50 while (hwcap == 0 || platform == NULL) {
51 if (read(fd, &auxv, sizeof(auxv)) < (ssize_t)sizeof(auxv)) {
52 if (errno == EINTR)
53 continue;
54 break;
55 }
56 switch (auxv.a_type) {
57 case AT_HWCAP:
58 hwcap = auxv.a_un.a_val;
59 break;
60 case AT_PLATFORM:
61 platform = (const char*)auxv.a_un.a_val;
62 break;
63 }
64 }
65 close(fd);
66 }
Karl Wiberg7f557b42020-09-04 14:23:04 +020067#endif // WEBRTC_GLIBC_PREREQ(2, 16)
simon.hosie953b1c12016-04-06 14:02:26 -070068#if defined(__aarch64__)
69 architecture = 8;
70 if ((hwcap & HWCAP_FP) != 0)
71 result |= kCPUFeatureVFPv3;
72 if ((hwcap & HWCAP_ASIMD) != 0)
73 result |= kCPUFeatureNEON;
74#else
75 if (platform != NULL) {
76 /* expect a string in the form "v6l" or "v7l", etc.
77 */
78 if (platform[0] == 'v' && '0' <= platform[1] && platform[1] <= '9' &&
79 (platform[2] == 'l' || platform[2] == 'b')) {
80 architecture = platform[1] - '0';
81 }
82 }
83 if ((hwcap & HWCAP_VFPv3) != 0)
84 result |= kCPUFeatureVFPv3;
85 if ((hwcap & HWCAP_NEON) != 0)
86 result |= kCPUFeatureNEON;
87#endif
88 if (architecture >= 7)
89 result |= kCPUFeatureARMv7;
90 if (architecture >= 6)
91 result |= kCPUFeatureLDREXSTREX;
92 return result;
93}
Mirko Bonadeibef022b2020-09-06 16:07:15 +020094
95} // namespace webrtc
simon.hosie953b1c12016-04-06 14:02:26 -070096#endif // WEBRTC_ARCH_ARM_FAMILY