Files
platform-external-webrtc/system_wrappers/source/cpu_features_linux.c
Mirko Bonadei d156a0d061 Revert "Merge cpu_features build targets into //system_wrappers."
This reverts commit 2b242d8fba59ccf67e4c5bcf5a1ea80917a23e1c.

Reason for revert: Breaks downstream project.

Original change's description:
> Merge cpu_features build targets into //system_wrappers.
> 
> Before this CL, functions declared in cpu_features_wrapper.h where
> not defined in the same build target, causing brittle builds that
> might fail at link time if the binary was not depending on
> //system_wrappers (the target with the definitions), violating [1].
> 
> This CL moves everything into //system_wrappers and also moves
> cpu_features_wrapper.h definitions from C to C++ (in order to be able
> to add the definitions to a C++ build target like //system_wrappers).
> 
> [1] - https://webrtc.googlesource.com/src/+/refs/heads/master/style-guide.md#h-cc-pairs
> 
> Bug: None
> Change-Id: I5a0009cddb17206b19f2a71eeba722faacc4bcae
> Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/183380
> Commit-Queue: Mirko Bonadei <mbonadei@webrtc.org>
> Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
> Cr-Commit-Position: refs/heads/master@{#32039}

TBR=mbonadei@webrtc.org,kwiberg@webrtc.org

Change-Id: I4daa7582e55a0343eef72f08ed023c73e0b6456b
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Bug: None
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/183443
Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org>
Commit-Queue: Mirko Bonadei <mbonadei@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#32040}
2020-09-05 21:18:40 +00:00

93 lines
2.4 KiB
C

/*
* Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include <stdlib.h>
#include <string.h>
#include <features.h>
#ifdef __GLIBC_PREREQ
#define WEBRTC_GLIBC_PREREQ(a, b) __GLIBC_PREREQ(a, b)
#else
#define WEBRTC_GLIBC_PREREQ(a, b) 0
#endif
#if WEBRTC_GLIBC_PREREQ(2, 16)
#include <sys/auxv.h>
#else
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <link.h>
#endif
#include "rtc_base/system/arch.h"
#include "system_wrappers/include/cpu_features_wrapper.h"
#if defined(WEBRTC_ARCH_ARM_FAMILY)
#include <asm/hwcap.h>
uint64_t WebRtc_GetCPUFeaturesARM(void) {
uint64_t result = 0;
int architecture = 0;
unsigned long hwcap = 0;
const char* platform = NULL;
#if WEBRTC_GLIBC_PREREQ(2, 16)
hwcap = getauxval(AT_HWCAP);
platform = (const char*)getauxval(AT_PLATFORM);
#else
ElfW(auxv_t) auxv;
int fd = open("/proc/self/auxv", O_RDONLY);
if (fd >= 0) {
while (hwcap == 0 || platform == NULL) {
if (read(fd, &auxv, sizeof(auxv)) < (ssize_t)sizeof(auxv)) {
if (errno == EINTR)
continue;
break;
}
switch (auxv.a_type) {
case AT_HWCAP:
hwcap = auxv.a_un.a_val;
break;
case AT_PLATFORM:
platform = (const char*)auxv.a_un.a_val;
break;
}
}
close(fd);
}
#endif // WEBRTC_GLIBC_PREREQ(2, 16)
#if defined(__aarch64__)
architecture = 8;
if ((hwcap & HWCAP_FP) != 0)
result |= kCPUFeatureVFPv3;
if ((hwcap & HWCAP_ASIMD) != 0)
result |= kCPUFeatureNEON;
#else
if (platform != NULL) {
/* expect a string in the form "v6l" or "v7l", etc.
*/
if (platform[0] == 'v' && '0' <= platform[1] && platform[1] <= '9' &&
(platform[2] == 'l' || platform[2] == 'b')) {
architecture = platform[1] - '0';
}
}
if ((hwcap & HWCAP_VFPv3) != 0)
result |= kCPUFeatureVFPv3;
if ((hwcap & HWCAP_NEON) != 0)
result |= kCPUFeatureNEON;
#endif
if (architecture >= 7)
result |= kCPUFeatureARMv7;
if (architecture >= 6)
result |= kCPUFeatureLDREXSTREX;
return result;
}
#endif // WEBRTC_ARCH_ARM_FAMILY