blob: 37739c12f66978278c574ec8882efc0542dc83df [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>
Mirko Bonadeid156a0d2020-09-05 21:18:26 +000013#include <features.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 Bonadei2b242d82020-09-05 21:55:35 +020024#include <fcntl.h>
Mirko Bonadei2b242d82020-09-05 21:55:35 +020025#include <unistd.h>
Mirko Bonadeid156a0d2020-09-05 21:18:26 +000026#include <errno.h>
27#include <link.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
36uint64_t WebRtc_GetCPUFeaturesARM(void) {
37 uint64_t result = 0;
38 int architecture = 0;
Mirko Bonadeid156a0d2020-09-05 21:18:26 +000039 unsigned long hwcap = 0;
simon.hosie953b1c12016-04-06 14:02:26 -070040 const char* platform = NULL;
Karl Wiberg7f557b42020-09-04 14:23:04 +020041#if WEBRTC_GLIBC_PREREQ(2, 16)
simon.hosie953b1c12016-04-06 14:02:26 -070042 hwcap = getauxval(AT_HWCAP);
43 platform = (const char*)getauxval(AT_PLATFORM);
44#else
45 ElfW(auxv_t) auxv;
46 int fd = open("/proc/self/auxv", O_RDONLY);
47 if (fd >= 0) {
48 while (hwcap == 0 || platform == NULL) {
49 if (read(fd, &auxv, sizeof(auxv)) < (ssize_t)sizeof(auxv)) {
50 if (errno == EINTR)
51 continue;
52 break;
53 }
54 switch (auxv.a_type) {
55 case AT_HWCAP:
56 hwcap = auxv.a_un.a_val;
57 break;
58 case AT_PLATFORM:
59 platform = (const char*)auxv.a_un.a_val;
60 break;
61 }
62 }
63 close(fd);
64 }
Karl Wiberg7f557b42020-09-04 14:23:04 +020065#endif // WEBRTC_GLIBC_PREREQ(2, 16)
simon.hosie953b1c12016-04-06 14:02:26 -070066#if defined(__aarch64__)
67 architecture = 8;
68 if ((hwcap & HWCAP_FP) != 0)
69 result |= kCPUFeatureVFPv3;
70 if ((hwcap & HWCAP_ASIMD) != 0)
71 result |= kCPUFeatureNEON;
72#else
73 if (platform != NULL) {
74 /* expect a string in the form "v6l" or "v7l", etc.
75 */
76 if (platform[0] == 'v' && '0' <= platform[1] && platform[1] <= '9' &&
77 (platform[2] == 'l' || platform[2] == 'b')) {
78 architecture = platform[1] - '0';
79 }
80 }
81 if ((hwcap & HWCAP_VFPv3) != 0)
82 result |= kCPUFeatureVFPv3;
83 if ((hwcap & HWCAP_NEON) != 0)
84 result |= kCPUFeatureNEON;
85#endif
86 if (architecture >= 7)
87 result |= kCPUFeatureARMv7;
88 if (architecture >= 6)
89 result |= kCPUFeatureLDREXSTREX;
90 return result;
91}
92#endif // WEBRTC_ARCH_ARM_FAMILY