From cff3693387c6d389f83ca25c7a150ae3395a434b Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Fri, 22 Jan 2021 05:50:27 -0800 Subject: Use if constexpr when possible --- alc/backends/opensl.cpp | 3 ++- alc/backends/wave.cpp | 3 ++- alc/hrtf.cpp | 4 ++-- common/albit.h | 5 +++-- common/opthelpers.h | 6 ++++++ 5 files changed, 15 insertions(+), 6 deletions(-) diff --git a/alc/backends/opensl.cpp b/alc/backends/opensl.cpp index 926911f0..917e097f 100644 --- a/alc/backends/opensl.cpp +++ b/alc/backends/opensl.cpp @@ -37,6 +37,7 @@ #include "alu.h" #include "compat.h" #include "core/logging.h" +#include "opthelpers.h" #include "ringbuffer.h" #include "threads.h" @@ -104,7 +105,7 @@ constexpr SLuint32 GetTypeRepresentation(DevFmtType type) noexcept constexpr SLuint32 GetByteOrderEndianness() noexcept { - if /*constexpr*/(al::endian::native == al::endian::little) + if_constexpr(al::endian::native == al::endian::little) return SL_BYTEORDER_LITTLEENDIAN; return SL_BYTEORDER_BIGENDIAN; } diff --git a/alc/backends/wave.cpp b/alc/backends/wave.cpp index afff1d56..4f738230 100644 --- a/alc/backends/wave.cpp +++ b/alc/backends/wave.cpp @@ -42,6 +42,7 @@ #include "alu.h" #include "compat.h" #include "core/logging.h" +#include "opthelpers.h" #include "strutils.h" #include "threads.h" #include "vector.h" @@ -149,7 +150,7 @@ int WaveBackend::mixerProc() mDevice->renderSamples(mBuffer.data(), mDevice->UpdateSize, frameStep); done += mDevice->UpdateSize; - if /*constexpr*/(al::endian::native != al::endian::little) + if_constexpr(al::endian::native != al::endian::little) { const uint bytesize{mDevice->bytesFromFmt()}; diff --git a/alc/hrtf.cpp b/alc/hrtf.cpp index 3f4ddf61..60d0aead 100644 --- a/alc/hrtf.cpp +++ b/alc/hrtf.cpp @@ -472,7 +472,7 @@ inline T readle(std::istream &data) static_assert(num_bits <= sizeof(T)*8, "num_bits is too large for the type"); T ret{}; - if /*constexpr*/(al::endian::native == al::endian::little) + if_constexpr(al::endian::native == al::endian::little) { if(!data.read(reinterpret_cast(&ret), num_bits/8)) return static_cast(EOF); @@ -485,7 +485,7 @@ inline T readle(std::istream &data) std::reverse_copy(std::begin(b), std::end(b), reinterpret_cast(&ret)); } - if /*constexpr*/(std::is_signed::value && num_bits < sizeof(T)*8) + if_constexpr(std::is_signed::value && num_bits < sizeof(T)*8) { constexpr auto signbit = static_cast(1u << (num_bits-1)); return static_cast((ret^signbit) - signbit); diff --git a/common/albit.h b/common/albit.h index c54bb31a..2c83ca08 100644 --- a/common/albit.h +++ b/common/albit.h @@ -5,6 +5,7 @@ #include #if !defined(__GNUC__) && (defined(_WIN32) || defined(_WIN64)) #include +#include "opthelpers.h" #endif namespace al { @@ -104,7 +105,7 @@ inline std::enable_if_t::value && std::is_unsigned::value int> countr_zero(T v) { unsigned long idx{std::numeric_limits::digits}; - if /*constexpr*/(std::numeric_limits::digits <= 32) + if_constexpr(std::numeric_limits::digits <= 32) _BitScanForward(&idx, static_cast(v)); else // std::numeric_limits::digits > 32 _BitScanForward64(&idx, v); @@ -118,7 +119,7 @@ inline std::enable_if_t::value && std::is_unsigned::value int> countr_zero(T v) { unsigned long idx{std::numeric_limits::digits}; - if /*constexpr*/(std::numeric_limits::digits <= 32) + if_constexpr(std::numeric_limits::digits <= 32) _BitScanForward(&idx, static_cast(v)); else if(!_BitScanForward(&idx, static_cast(v))) { diff --git a/common/opthelpers.h b/common/opthelpers.h index 8f16fd3f..df0be2f5 100644 --- a/common/opthelpers.h +++ b/common/opthelpers.h @@ -36,4 +36,10 @@ #define ASSUME(x) ((void)0) #endif +#if __cplusplus >= 201709L || defined(__cpp_if_constexpr) +#define if_constexpr if constexpr +#else +#define if_constexpr if +#endif + #endif /* OPTHELPERS_H */ -- cgit v1.2.3