blob: 2d79dde11189fbaf8ab09493febccc8ed9af33e1 [file] [log] [blame]
simon.hosie953b1c12016-04-06 21:02:261/*
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 19:55:3511#include <features.h>
simon.hosie953b1c12016-04-06 21:02:2612#include <stdlib.h>
13#include <string.h>
Karl Wiberg7f557b42020-09-04 12:23:0414
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 15:34:0619#endif
Karl Wiberg7f557b42020-09-04 12:23:0420
21#if WEBRTC_GLIBC_PREREQ(2, 16)
simon.hosie953b1c12016-04-06 21:02:2622#include <sys/auxv.h>
23#else
Mirko Bonadeid156a0d2020-09-05 21:18:2624#include <errno.h>
Mirko Bonadei66e73382020-09-05 19:55:3525#include <fcntl.h>
Mirko Bonadeid156a0d2020-09-05 21:18:2626#include <link.h>
Mirko Bonadei66e73382020-09-05 19:55:3527#include <unistd.h>
simon.hosie953b1c12016-04-06 21:02:2628#endif
Karl Wiberg7f557b42020-09-04 12:23:0429
Niels Möllera12c42a2018-07-25 14:05:4830#include "rtc_base/system/arch.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3131#include "system_wrappers/include/cpu_features_wrapper.h"
simon.hosie953b1c12016-04-06 21:02:2632
33#if defined(WEBRTC_ARCH_ARM_FAMILY)
34#include <asm/hwcap.h>
35
Mirko Bonadeibef022b2020-09-06 14:07:1536namespace webrtc {
37
38uint64_t GetCPUFeaturesARM(void) {
simon.hosie953b1c12016-04-06 21:02:2639 uint64_t result = 0;
40 int architecture = 0;
Mirko Bonadei66e73382020-09-05 19:55:3541 uint64_t hwcap = 0;
simon.hosie953b1c12016-04-06 21:02:2642 const char* platform = NULL;
Karl Wiberg7f557b42020-09-04 12:23:0443#if WEBRTC_GLIBC_PREREQ(2, 16)
simon.hosie953b1c12016-04-06 21:02:2644 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 12:23:0467#endif // WEBRTC_GLIBC_PREREQ(2, 16)
simon.hosie953b1c12016-04-06 21:02:2668#if defined(__aarch64__)
Mirko Bonadei32913752021-10-30 15:56:2469 (void)platform;
simon.hosie953b1c12016-04-06 21:02:2670 architecture = 8;
71 if ((hwcap & HWCAP_FP) != 0)
72 result |= kCPUFeatureVFPv3;
73 if ((hwcap & HWCAP_ASIMD) != 0)
74 result |= kCPUFeatureNEON;
75#else
76 if (platform != NULL) {
77 /* expect a string in the form "v6l" or "v7l", etc.
78 */
79 if (platform[0] == 'v' && '0' <= platform[1] && platform[1] <= '9' &&
80 (platform[2] == 'l' || platform[2] == 'b')) {
81 architecture = platform[1] - '0';
82 }
83 }
84 if ((hwcap & HWCAP_VFPv3) != 0)
85 result |= kCPUFeatureVFPv3;
86 if ((hwcap & HWCAP_NEON) != 0)
87 result |= kCPUFeatureNEON;
88#endif
89 if (architecture >= 7)
90 result |= kCPUFeatureARMv7;
91 if (architecture >= 6)
92 result |= kCPUFeatureLDREXSTREX;
93 return result;
94}
Mirko Bonadeibef022b2020-09-06 14:07:1595
96} // namespace webrtc
simon.hosie953b1c12016-04-06 21:02:2697#endif // WEBRTC_ARCH_ARM_FAMILY