Refactor cpu_features_wrapper.h functions from C to C++.

As mentioned on https://webrtc-review.googlesource.com/c/src/+/183380,
then relanded as https://webrtc-review.googlesource.com/c/src/+/183444,
functions in cpu_features_wrapper.h should be refactored to use
C++ features like namespaces and drop the WebRtc_ prefix.

Bug: None
Change-Id: I3e83e1668f9bf48a5d8e85d809f006666b7fa45e
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/183445
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Commit-Queue: Mirko Bonadei <mbonadei@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#32045}
This commit is contained in:
Mirko Bonadei 2020-09-06 16:07:15 +02:00 committed by Commit Bot
parent d381eede92
commit bef022bde0
21 changed files with 55 additions and 45 deletions

View file

@ -36,10 +36,10 @@ FIRFilter* CreateFirFilter(const float* coefficients,
// If we know the minimum architecture at compile time, avoid CPU detection.
#if defined(WEBRTC_ARCH_X86_FAMILY)
// x86 CPU detection required.
if (WebRtc_GetCPUInfo(kAVX2)) {
if (GetCPUInfo(kAVX2)) {
filter =
new FIRFilterAVX2(coefficients, coefficients_length, max_input_length);
} else if (WebRtc_GetCPUInfo(kSSE2)) {
} else if (GetCPUInfo(kSSE2)) {
filter =
new FIRFilterSSE2(coefficients, coefficients_length, max_input_length);
} else {

View file

@ -127,9 +127,9 @@ void SincResampler::InitializeCPUSpecificFeatures() {
convolve_proc_ = Convolve_NEON;
#elif defined(WEBRTC_ARCH_X86_FAMILY)
// Using AVX2 instead of SSE2 when AVX2 supported.
if (WebRtc_GetCPUInfo(kAVX2))
if (GetCPUInfo(kAVX2))
convolve_proc_ = Convolve_AVX2;
else if (WebRtc_GetCPUInfo(kSSE2))
else if (GetCPUInfo(kSSE2))
convolve_proc_ = Convolve_SSE;
else
convolve_proc_ = Convolve_C;

View file

@ -121,9 +121,9 @@ TEST(SincResamplerTest, DISABLED_SetRatioBench) {
// will be tested by the parameterized SincResampler tests below.
TEST(SincResamplerTest, Convolve) {
#if defined(WEBRTC_ARCH_X86_FAMILY)
ASSERT_TRUE(WebRtc_GetCPUInfo(kSSE2));
ASSERT_TRUE(GetCPUInfo(kSSE2));
#elif defined(WEBRTC_ARCH_ARM_V7)
ASSERT_TRUE(WebRtc_GetCPUFeaturesARM() & kCPUFeatureNEON);
ASSERT_TRUE(GetCPUFeaturesARM() & kCPUFeatureNEON);
#endif
// Initialize a dummy resampler.
@ -182,9 +182,9 @@ TEST(SincResamplerTest, ConvolveBenchmark) {
printf("Convolve_C took %.2fms.\n", total_time_c_us / 1000);
#if defined(WEBRTC_ARCH_X86_FAMILY)
ASSERT_TRUE(WebRtc_GetCPUInfo(kSSE2));
ASSERT_TRUE(GetCPUInfo(kSSE2));
#elif defined(WEBRTC_ARCH_ARM_V7)
ASSERT_TRUE(WebRtc_GetCPUFeaturesARM() & kCPUFeatureNEON);
ASSERT_TRUE(GetCPUFeaturesARM() & kCPUFeatureNEON);
#endif
// Benchmark with unaligned input pointer.

View file

@ -323,7 +323,7 @@ OouraFft::OouraFft(bool sse2_available) {
OouraFft::OouraFft() {
#if defined(WEBRTC_ARCH_X86_FAMILY)
use_sse2_ = (WebRtc_GetCPUInfo(kSSE2) != 0);
use_sse2_ = (GetCPUInfo(kSSE2) != 0);
#else
use_sse2_ = false;
#endif

View file

@ -26,7 +26,6 @@
#include "modules/audio_coding/codecs/isac/fix/source/filterbank_internal.h"
#include "modules/audio_coding/codecs/isac/fix/source/lpc_masking_model.h"
#include "modules/audio_coding/codecs/isac/fix/source/structs.h"
#include "system_wrappers/include/cpu_features_wrapper.h"
// Declare function pointers.
FilterMaLoopFix WebRtcIsacfix_FilterMaLoopFix;

View file

@ -53,7 +53,7 @@ TEST(AdaptiveFirFilter, UpdateErlNeonOptimization) {
// Verifies that the optimized method for echo return loss computation is
// bitexact to the reference counterpart.
TEST(AdaptiveFirFilter, UpdateErlSse2Optimization) {
bool use_sse2 = (WebRtc_GetCPUInfo(kSSE2) != 0);
bool use_sse2 = (GetCPUInfo(kSSE2) != 0);
if (use_sse2) {
const size_t kNumPartitions = 12;
std::vector<std::array<float, kFftLengthBy2Plus1>> H2(kNumPartitions);
@ -78,7 +78,7 @@ TEST(AdaptiveFirFilter, UpdateErlSse2Optimization) {
// Verifies that the optimized method for echo return loss computation is
// bitexact to the reference counterpart.
TEST(AdaptiveFirFilter, UpdateErlAvx2Optimization) {
bool use_avx2 = (WebRtc_GetCPUInfo(kAVX2) != 0);
bool use_avx2 = (GetCPUInfo(kAVX2) != 0);
if (use_avx2) {
const size_t kNumPartitions = 12;
std::vector<std::array<float, kFftLengthBy2Plus1>> H2(kNumPartitions);

View file

@ -179,7 +179,7 @@ TEST_P(AdaptiveFirFilterOneTwoFourEightRenderChannels,
constexpr int kSampleRateHz = 48000;
constexpr size_t kNumBands = NumBandsForRate(kSampleRateHz);
bool use_sse2 = (WebRtc_GetCPUInfo(kSSE2) != 0);
bool use_sse2 = (GetCPUInfo(kSSE2) != 0);
if (use_sse2) {
for (size_t num_partitions : {2, 5, 12, 30, 50}) {
std::unique_ptr<RenderDelayBuffer> render_delay_buffer(
@ -254,7 +254,7 @@ TEST_P(AdaptiveFirFilterOneTwoFourEightRenderChannels,
constexpr int kSampleRateHz = 48000;
constexpr size_t kNumBands = NumBandsForRate(kSampleRateHz);
bool use_avx2 = (WebRtc_GetCPUInfo(kAVX2) != 0);
bool use_avx2 = (GetCPUInfo(kAVX2) != 0);
if (use_avx2) {
for (size_t num_partitions : {2, 5, 12, 30, 50}) {
std::unique_ptr<RenderDelayBuffer> render_delay_buffer(
@ -326,7 +326,7 @@ TEST_P(AdaptiveFirFilterOneTwoFourEightRenderChannels,
TEST_P(AdaptiveFirFilterOneTwoFourEightRenderChannels,
ComputeFrequencyResponseSse2Optimization) {
const size_t num_render_channels = GetParam();
bool use_sse2 = (WebRtc_GetCPUInfo(kSSE2) != 0);
bool use_sse2 = (GetCPUInfo(kSSE2) != 0);
if (use_sse2) {
for (size_t num_partitions : {2, 5, 12, 30, 50}) {
std::vector<std::vector<FftData>> H(
@ -361,7 +361,7 @@ TEST_P(AdaptiveFirFilterOneTwoFourEightRenderChannels,
TEST_P(AdaptiveFirFilterOneTwoFourEightRenderChannels,
ComputeFrequencyResponseAvx2Optimization) {
const size_t num_render_channels = GetParam();
bool use_avx2 = (WebRtc_GetCPUInfo(kAVX2) != 0);
bool use_avx2 = (GetCPUInfo(kAVX2) != 0);
if (use_avx2) {
for (size_t num_partitions : {2, 5, 12, 30, 50}) {
std::vector<std::vector<FftData>> H(

View file

@ -20,9 +20,9 @@ namespace webrtc {
Aec3Optimization DetectOptimization() {
#if defined(WEBRTC_ARCH_X86_FAMILY)
if (WebRtc_GetCPUInfo(kAVX2) != 0) {
if (GetCPUInfo(kAVX2) != 0) {
return Aec3Optimization::kAvx2;
} else if (WebRtc_GetCPUInfo(kSSE2) != 0) {
} else if (GetCPUInfo(kSSE2) != 0) {
return Aec3Optimization::kSse2;
}
#endif

View file

@ -73,7 +73,7 @@ const float kSqrtHanning128[kFftLength] = {
bool IsSse2Available() {
#if defined(WEBRTC_ARCH_X86_FAMILY)
return WebRtc_GetCPUInfo(kSSE2) != 0;
return GetCPUInfo(kSSE2) != 0;
#else
return false;
#endif

View file

@ -20,7 +20,7 @@ namespace webrtc {
// Verifies that the optimized methods are bitexact to their reference
// counterparts.
TEST(FftData, TestSse2Optimizations) {
if (WebRtc_GetCPUInfo(kSSE2) != 0) {
if (GetCPUInfo(kSSE2) != 0) {
FftData x;
for (size_t k = 0; k < x.re.size(); ++k) {
@ -43,7 +43,7 @@ TEST(FftData, TestSse2Optimizations) {
// Verifies that the optimized methods are bitexact to their reference
// counterparts.
TEST(FftData, TestAvx2Optimizations) {
if (WebRtc_GetCPUInfo(kAVX2) != 0) {
if (GetCPUInfo(kAVX2) != 0) {
FftData x;
for (size_t k = 0; k < x.re.size(); ++k) {

View file

@ -93,7 +93,7 @@ TEST(MatchedFilter, TestNeonOptimizations) {
// Verifies that the optimized methods for SSE2 are bitexact to their reference
// counterparts.
TEST(MatchedFilter, TestSse2Optimizations) {
bool use_sse2 = (WebRtc_GetCPUInfo(kSSE2) != 0);
bool use_sse2 = (GetCPUInfo(kSSE2) != 0);
if (use_sse2) {
Random random_generator(42U);
constexpr float kSmoothing = 0.7f;
@ -134,7 +134,7 @@ TEST(MatchedFilter, TestSse2Optimizations) {
}
TEST(MatchedFilter, TestAvx2Optimizations) {
bool use_avx2 = (WebRtc_GetCPUInfo(kAVX2) != 0);
bool use_avx2 = (GetCPUInfo(kAVX2) != 0);
if (use_avx2) {
Random random_generator(42U);
constexpr float kSmoothing = 0.7f;

View file

@ -80,7 +80,7 @@ TEST(VectorMath, Accumulate) {
#if defined(WEBRTC_ARCH_X86_FAMILY)
TEST(VectorMath, Sse2Sqrt) {
if (WebRtc_GetCPUInfo(kSSE2) != 0) {
if (GetCPUInfo(kSSE2) != 0) {
std::array<float, kFftLengthBy2Plus1> x;
std::array<float, kFftLengthBy2Plus1> z;
std::array<float, kFftLengthBy2Plus1> z_sse2;
@ -102,7 +102,7 @@ TEST(VectorMath, Sse2Sqrt) {
}
TEST(VectorMath, Avx2Sqrt) {
if (WebRtc_GetCPUInfo(kAVX2) != 0) {
if (GetCPUInfo(kAVX2) != 0) {
std::array<float, kFftLengthBy2Plus1> x;
std::array<float, kFftLengthBy2Plus1> z;
std::array<float, kFftLengthBy2Plus1> z_avx2;
@ -124,7 +124,7 @@ TEST(VectorMath, Avx2Sqrt) {
}
TEST(VectorMath, Sse2Multiply) {
if (WebRtc_GetCPUInfo(kSSE2) != 0) {
if (GetCPUInfo(kSSE2) != 0) {
std::array<float, kFftLengthBy2Plus1> x;
std::array<float, kFftLengthBy2Plus1> y;
std::array<float, kFftLengthBy2Plus1> z;
@ -145,7 +145,7 @@ TEST(VectorMath, Sse2Multiply) {
}
TEST(VectorMath, Avx2Multiply) {
if (WebRtc_GetCPUInfo(kAVX2) != 0) {
if (GetCPUInfo(kAVX2) != 0) {
std::array<float, kFftLengthBy2Plus1> x;
std::array<float, kFftLengthBy2Plus1> y;
std::array<float, kFftLengthBy2Plus1> z;
@ -166,7 +166,7 @@ TEST(VectorMath, Avx2Multiply) {
}
TEST(VectorMath, Sse2Accumulate) {
if (WebRtc_GetCPUInfo(kSSE2) != 0) {
if (GetCPUInfo(kSSE2) != 0) {
std::array<float, kFftLengthBy2Plus1> x;
std::array<float, kFftLengthBy2Plus1> z;
std::array<float, kFftLengthBy2Plus1> z_sse2;
@ -186,7 +186,7 @@ TEST(VectorMath, Sse2Accumulate) {
}
TEST(VectorMath, Avx2Accumulate) {
if (WebRtc_GetCPUInfo(kAVX2) != 0) {
if (GetCPUInfo(kAVX2) != 0) {
std::array<float, kFftLengthBy2Plus1> x;
std::array<float, kFftLengthBy2Plus1> z;
std::array<float, kFftLengthBy2Plus1> z_avx2;

View file

@ -18,7 +18,7 @@ namespace rnn_vad {
Optimization DetectOptimization() {
#if defined(WEBRTC_ARCH_X86_FAMILY)
if (WebRtc_GetCPUInfo(kSSE2) != 0) {
if (GetCPUInfo(kSSE2) != 0) {
return Optimization::kSse2;
}
#endif

View file

@ -109,7 +109,7 @@ bool IsOptimizationAvailable(Optimization optimization) {
switch (optimization) {
case Optimization::kSse2:
#if defined(WEBRTC_ARCH_X86_FAMILY)
return WebRtc_GetCPUInfo(kSSE2) != 0;
return GetCPUInfo(kSSE2) != 0;
#else
return false;
#endif

View file

@ -26,7 +26,7 @@ namespace {
bool IsSse2Available() {
#if defined(WEBRTC_ARCH_X86_FAMILY)
return WebRtc_GetCPUInfo(kSSE2) != 0;
return GetCPUInfo(kSSE2) != 0;
#else
return false;
#endif

View file

@ -35,7 +35,7 @@ bool VectorDifference(const uint8_t* image1, const uint8_t* image2) {
// TODO(hclam): Implement a NEON version.
diff_proc = &VectorDifference_C;
#else
bool have_sse2 = WebRtc_GetCPUInfo(kSSE2) != 0;
bool have_sse2 = GetCPUInfo(kSSE2) != 0;
// For x86 processors, check if SSE2 is supported.
if (have_sse2 && kBlockSize == 32) {
diff_proc = &VectorDifference_SSE2_W32;

View file

@ -41,7 +41,7 @@ std::unique_ptr<DenoiserFilter> DenoiserFilter::Create(
filter.reset(new DenoiserFilterSSE2());
#else
// x86 CPU detection required.
if (WebRtc_GetCPUInfo(kSSE2)) {
if (GetCPUInfo(kSSE2)) {
filter.reset(new DenoiserFilterSSE2());
} else {
filter.reset(new DenoiserFilterC());

View file

@ -13,6 +13,8 @@
#include <stdint.h>
namespace webrtc {
// List of features in x86.
typedef enum { kSSE2, kSSE3, kAVX2 } CPUFeature;
@ -24,17 +26,17 @@ enum {
kCPUFeatureLDREXSTREX = (1 << 3)
};
typedef int (*WebRtc_CPUInfo)(CPUFeature feature);
// Returns true if the CPU supports the feature.
extern WebRtc_CPUInfo WebRtc_GetCPUInfo;
int GetCPUInfo(CPUFeature feature);
// No CPU feature is available => straight C path.
extern WebRtc_CPUInfo WebRtc_GetCPUInfoNoASM;
int GetCPUInfoNoASM(CPUFeature feature);
// Return the features in an ARM device.
// It detects the features in the hardware platform, and returns supported
// values in the above enum definition as a bitmask.
extern uint64_t WebRtc_GetCPUFeaturesARM(void);
uint64_t GetCPUFeaturesARM(void);
} // namespace webrtc
#endif // SYSTEM_WRAPPERS_INCLUDE_CPU_FEATURES_WRAPPER_H_

View file

@ -17,6 +17,8 @@
#include <intrin.h>
#endif
namespace webrtc {
// No CPU feature is available => straight C path.
int GetCPUInfoNoASM(CPUFeature feature) {
(void)feature;
@ -65,7 +67,7 @@ static inline void __cpuid(int cpu_info[4], int info_type) {
#if defined(WEBRTC_ARCH_X86_FAMILY)
// Actual feature detection for x86.
static int GetCPUInfo(CPUFeature feature) {
int GetCPUInfo(CPUFeature feature) {
int cpu_info[4];
__cpuid(cpu_info, 1);
if (feature == kSSE2) {
@ -102,11 +104,10 @@ static int GetCPUInfo(CPUFeature feature) {
}
#else
// Default to straight C for other platforms.
static int GetCPUInfo(CPUFeature feature) {
int GetCPUInfo(CPUFeature feature) {
(void)feature;
return 0;
}
#endif
WebRtc_CPUInfo WebRtc_GetCPUInfo = GetCPUInfo;
WebRtc_CPUInfo WebRtc_GetCPUInfoNoASM = GetCPUInfoNoASM;
} // namespace webrtc

View file

@ -10,6 +10,10 @@
#include <cpu-features.h>
uint64_t WebRtc_GetCPUFeaturesARM(void) {
namespace webrtc {
uint64_t GetCPUFeaturesARM(void) {
return android_getCpuFeatures();
}
} // namespace webrtc

View file

@ -33,7 +33,9 @@
#if defined(WEBRTC_ARCH_ARM_FAMILY)
#include <asm/hwcap.h>
uint64_t WebRtc_GetCPUFeaturesARM(void) {
namespace webrtc {
uint64_t GetCPUFeaturesARM(void) {
uint64_t result = 0;
int architecture = 0;
uint64_t hwcap = 0;
@ -89,4 +91,6 @@ uint64_t WebRtc_GetCPUFeaturesARM(void) {
result |= kCPUFeatureLDREXSTREX;
return result;
}
} // namespace webrtc
#endif // WEBRTC_ARCH_ARM_FAMILY