From d06ed618d356a713292de95f0ab2c6017c305e17 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Tue, 22 Feb 2022 03:03:44 -0800 Subject: Avoid using an if_constexpr macro It doesn't actually use if constexpr, and compilers are smart enough to optimize. Some functions can use templates instead. --- core/hrtf.cpp | 53 ++++++++++++++++++++++++++++++++++------------------- 1 file changed, 34 insertions(+), 19 deletions(-) (limited to 'core') diff --git a/core/hrtf.cpp b/core/hrtf.cpp index e8d3310a..d94c0569 100644 --- a/core/hrtf.cpp +++ b/core/hrtf.cpp @@ -447,32 +447,47 @@ void MirrorLeftHrirs(const al::span elevs, HrirArray } +template +constexpr std::enable_if_t::value && num_bits < sizeof(T)*8, +T> fixsign(T value) noexcept +{ + constexpr auto signbit = static_cast(1u << (num_bits-1)); + return static_cast((value^signbit) - signbit); +} + +template +constexpr std::enable_if_t::value || num_bits == sizeof(T)*8, +T> fixsign(T value) noexcept +{ return value; } + template -inline T readle(std::istream &data) +inline std::enable_if_t readle(std::istream &data) { static_assert((num_bits&7) == 0, "num_bits must be a multiple of 8"); 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(!data.read(reinterpret_cast(&ret), num_bits/8)) - return static_cast(EOF); - } - else - { - al::byte b[sizeof(T)]{}; - if(!data.read(reinterpret_cast(b), num_bits/8)) - return static_cast(EOF); - std::reverse_copy(std::begin(b), std::end(b), reinterpret_cast(&ret)); - } + if(!data.read(reinterpret_cast(&ret), num_bits/8)) + return static_cast(EOF); - 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); - } - return ret; + return fixsign(ret); +} + +template +inline std::enable_if_t readle(std::istream &data) +{ + static_assert((num_bits&7) == 0, "num_bits must be a multiple of 8"); + static_assert(num_bits <= sizeof(T)*8, "num_bits is too large for the type"); + + T ret{}; + al::byte b[sizeof(T)]{}; + if(!data.read(reinterpret_cast(b), num_bits/8)) + return static_cast(EOF); + std::reverse_copy(std::begin(b), std::end(b), reinterpret_cast(&ret)); + + return fixsign(ret); } template<> -- cgit v1.2.3