Skip to content

Commit

Permalink
Refactor cpu_features_wrapper.h functions from C to C++.
Browse files Browse the repository at this point in the history
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 <[email protected]>
Commit-Queue: Mirko Bonadei <[email protected]>
Cr-Commit-Position: refs/heads/master@{#32045}
  • Loading branch information
MirkoBonadei authored and Commit Bot committed Sep 7, 2020
1 parent d381eed commit bef022b
Show file tree
Hide file tree
Showing 21 changed files with 55 additions and 45 deletions.
4 changes: 2 additions & 2 deletions common_audio/fir_filter_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions common_audio/resampler/sinc_resampler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions common_audio/resampler/sinc_resampler_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion common_audio/third_party/ooura/fft_size_128/ooura_fft.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion modules/audio_coding/codecs/isac/fix/source/isacfix.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions modules/audio_processing/aec3/adaptive_fir_filter_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions modules/audio_processing/aec3/aec3_common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion modules/audio_processing/aec3/aec3_fft.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions modules/audio_processing/aec3/fft_data_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions modules/audio_processing/aec3/matched_filter_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
12 changes: 6 additions & 6 deletions modules/audio_processing/aec3/vector_math_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion modules/audio_processing/agc2/rnn_vad/common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion modules/audio_processing/agc2/rnn_vad/test_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion modules/audio_processing/agc2/signal_classifier.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion modules/desktop_capture/differ_block.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion modules/video_processing/util/denoiser_filter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
12 changes: 7 additions & 5 deletions system_wrappers/include/cpu_features_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

#include <stdint.h>

namespace webrtc {

// List of features in x86.
typedef enum { kSSE2, kSSE3, kAVX2 } CPUFeature;

Expand All @@ -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_
9 changes: 5 additions & 4 deletions system_wrappers/source/cpu_features.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#include <intrin.h>
#endif

namespace webrtc {

// No CPU feature is available => straight C path.
int GetCPUInfoNoASM(CPUFeature feature) {
(void)feature;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
6 changes: 5 additions & 1 deletion system_wrappers/source/cpu_features_android.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@

#include <cpu-features.h>

uint64_t WebRtc_GetCPUFeaturesARM(void) {
namespace webrtc {

uint64_t GetCPUFeaturesARM(void) {
return android_getCpuFeatures();
}

} // namespace webrtc
6 changes: 5 additions & 1 deletion system_wrappers/source/cpu_features_linux.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -89,4 +91,6 @@ uint64_t WebRtc_GetCPUFeaturesARM(void) {
result |= kCPUFeatureLDREXSTREX;
return result;
}

} // namespace webrtc
#endif // WEBRTC_ARCH_ARM_FAMILY

0 comments on commit bef022b

Please sign in to comment.