diff options
author | Chris Robinson <[email protected]> | 2022-12-05 14:51:03 -0800 |
---|---|---|
committer | Chris Robinson <[email protected]> | 2022-12-05 14:51:03 -0800 |
commit | df6d61dd40b602af55f903564358b083bb8b37e4 (patch) | |
tree | 84113a098867e2ba82aa1f156911e94755fde727 /common | |
parent | 73df39b8f8cefa0c0fca0e287b548063e9e62636 (diff) |
Use standard likely/unlikely attributes when available
Diffstat (limited to 'common')
-rw-r--r-- | common/alcomplex.cpp | 4 | ||||
-rw-r--r-- | common/comptr.h | 2 | ||||
-rw-r--r-- | common/intrusive_ptr.h | 4 | ||||
-rw-r--r-- | common/opthelpers.h | 36 | ||||
-rw-r--r-- | common/polyphase_resampler.cpp | 16 |
5 files changed, 24 insertions, 38 deletions
diff --git a/common/alcomplex.cpp b/common/alcomplex.cpp index c08ac751..cce92665 100644 --- a/common/alcomplex.cpp +++ b/common/alcomplex.cpp @@ -101,7 +101,7 @@ complex_fft(const al::span<std::complex<Real>> buffer, const Real sign) */ const size_t log2_size{static_cast<size_t>(al::countr_zero(fftsize))}; - if(unlikely(log2_size >= gBitReverses.size())) + if(log2_size >= gBitReverses.size()) [[alunlikely]] { for(size_t idx{1u};idx < fftsize-1;++idx) { @@ -116,7 +116,7 @@ complex_fft(const al::span<std::complex<Real>> buffer, const Real sign) std::swap(buffer[idx], buffer[revidx]); } } - else for(auto &rev : gBitReverses[log2_size]) + else for(auto &rev : gBitReverses[log2_size]) [[allikely]] std::swap(buffer[rev.first], buffer[rev.second]); /* Iterative form of Danielson-Lanczos lemma */ diff --git a/common/comptr.h b/common/comptr.h index 3dc574e8..83f339ca 100644 --- a/common/comptr.h +++ b/common/comptr.h @@ -44,7 +44,7 @@ public: } ComPtr& operator=(ComPtr&& rhs) { - if(likely(&rhs != this)) + if(&rhs != this) [[allikely]] { if(mPtr) mPtr->Release(); mPtr = std::exchange(rhs.mPtr, nullptr); diff --git a/common/intrusive_ptr.h b/common/intrusive_ptr.h index 9e206a6b..ba932b95 100644 --- a/common/intrusive_ptr.h +++ b/common/intrusive_ptr.h @@ -18,7 +18,7 @@ public: unsigned int release() noexcept { auto ref = DecrementRef(mRef); - if UNLIKELY(ref == 0) + if(ref == 0) [[alunlikely]] delete static_cast<T*>(this); return ref; } @@ -71,7 +71,7 @@ public: } intrusive_ptr& operator=(intrusive_ptr&& rhs) noexcept { - if(likely(&rhs != this)) + if(&rhs != this) [[allikely]] { if(mPtr) mPtr->release(); mPtr = std::exchange(rhs.mPtr, nullptr); diff --git a/common/opthelpers.h b/common/opthelpers.h index f110303e..5fe455da 100644 --- a/common/opthelpers.h +++ b/common/opthelpers.h @@ -19,41 +19,27 @@ #define force_inline inline #endif -#if defined(__GNUC__) || HAS_BUILTIN(__builtin_expect) -/* likely() optimizes for the case where the condition is true. The condition - * is not required to be true, but it can result in more optimal code for the - * true path at the expense of a less optimal false path. - */ -template<typename T> -force_inline constexpr bool likely(T&& expr) noexcept -{ return __builtin_expect(static_cast<bool>(std::forward<T>(expr)), true); } -/* The opposite of likely(), optimizing for the case where the condition is - * false. - */ -template<typename T> -force_inline constexpr bool unlikely(T&& expr) noexcept -{ return __builtin_expect(static_cast<bool>(std::forward<T>(expr)), false); } - +#if __has_attribute(likely) +#define allikely likely +#define alunlikely unlikely #else - -template<typename T> -force_inline constexpr bool likely(T&& expr) noexcept -{ return static_cast<bool>(std::forward<T>(expr)); } -template<typename T> -force_inline constexpr bool unlikely(T&& expr) noexcept -{ return static_cast<bool>(std::forward<T>(expr)); } +#define allikely +#define alunlikely #endif -#define LIKELY(x) (likely(x)) -#define UNLIKELY(x) (unlikely(x)) -#if HAS_BUILTIN(__builtin_assume) +#define LIKELY(x) (x) [[allikely]] +#define UNLIKELY(x) (x) [[alunlikely]] + /* Unlike LIKELY, ASSUME requires the condition to be true or else it invokes * undefined behavior. It's essentially an assert without actually checking the * condition at run-time, allowing for stronger optimizations than LIKELY. */ +#if HAS_BUILTIN(__builtin_assume) #define ASSUME __builtin_assume #elif defined(_MSC_VER) #define ASSUME __assume +#elif __has_attribute(assume) +#define ASSUME(x) [[assume(x)]] #elif defined(__GNUC__) #define ASSUME(x) do { if(x) break; __builtin_unreachable(); } while(0) #else diff --git a/common/polyphase_resampler.cpp b/common/polyphase_resampler.cpp index bb8f69a4..76723915 100644 --- a/common/polyphase_resampler.cpp +++ b/common/polyphase_resampler.cpp @@ -21,7 +21,7 @@ using uint = unsigned int; */ double Sinc(const double x) { - if(unlikely(std::abs(x) < Epsilon)) + if(std::abs(x) < Epsilon) [[alunlikely]] return 1.0; return std::sin(al::numbers::pi*x) / (al::numbers::pi*x); } @@ -96,7 +96,7 @@ constexpr uint Gcd(uint x, uint y) constexpr uint CalcKaiserOrder(const double rejection, const double transition) { const double w_t{2.0 * al::numbers::pi * transition}; - if LIKELY(rejection > 21.0) + if(rejection > 21.0) [[allikely]] return static_cast<uint>(std::ceil((rejection - 7.95) / (2.285 * w_t))); return static_cast<uint>(std::ceil(5.79 / w_t)); } @@ -104,7 +104,7 @@ constexpr uint CalcKaiserOrder(const double rejection, const double transition) // Calculates the beta value of the Kaiser window. Rejection is in dB. constexpr double CalcKaiserBeta(const double rejection) { - if LIKELY(rejection > 50.0) + if(rejection > 50.0) [[allikely]] return 0.1102 * (rejection - 8.7); if(rejection >= 21.0) return (0.5842 * std::pow(rejection - 21.0, 0.4)) + @@ -171,13 +171,13 @@ void PPhaseResampler::init(const uint srcRate, const uint dstRate) // polyphase filter implementation. void PPhaseResampler::process(const uint inN, const double *in, const uint outN, double *out) { - if UNLIKELY(outN == 0) + if(outN == 0) [[alunlikely]] return; // Handle in-place operation. std::vector<double> workspace; double *work{out}; - if UNLIKELY(work == in) + if(work == in) [[alunlikely]] { workspace.resize(outN); work = workspace.data(); @@ -195,17 +195,17 @@ void PPhaseResampler::process(const uint inN, const double *in, const uint outN, // Only take input when 0 <= j_s < inN. double r{0.0}; - if LIKELY(j_f < m) + if(j_f < m) [[allikely]] { size_t filt_len{(m-j_f+p-1) / p}; - if LIKELY(j_s+1 > inN) + if(j_s+1 > inN) [[allikely]] { size_t skip{std::min<size_t>(j_s+1 - inN, filt_len)}; j_f += p*skip; j_s -= skip; filt_len -= skip; } - if(size_t todo{std::min<size_t>(j_s+1, filt_len)}) + if(size_t todo{std::min<size_t>(j_s+1, filt_len)}) [[allikely]] { do { r += f[j_f] * in[j_s]; |