diff options
| author | assiduous <assiduous@diligentgraphics.com> | 2021-01-25 22:41:17 +0000 |
|---|---|---|
| committer | assiduous <assiduous@diligentgraphics.com> | 2021-01-25 22:50:04 +0000 |
| commit | 753060f232745ea2a3fa197a176c3a8675699053 (patch) | |
| tree | df878e249249d0a6e104b62a09e31fe67a978830 /Common/interface | |
| parent | GraphicsAccessories: added PipelineTypeFromShaderStages function (diff) | |
| download | DiligentCore-753060f232745ea2a3fa197a176c3a8675699053.tar.gz DiligentCore-753060f232745ea2a3fa197a176c3a8675699053.zip | |
Basic math: reworked ExtractBit and renamed to ExtractLSB; added tests
Diffstat (limited to 'Common/interface')
| -rw-r--r-- | Common/interface/BasicMath.hpp | 20 |
1 files changed, 13 insertions, 7 deletions
diff --git a/Common/interface/BasicMath.hpp b/Common/interface/BasicMath.hpp index 78e9beaf..5f37c164 100644 --- a/Common/interface/BasicMath.hpp +++ b/Common/interface/BasicMath.hpp @@ -2189,18 +2189,24 @@ inline Uint32 BitInterleave16(Uint16 _x, Uint16 _y) return x | (y << 1u); } +/// Returns the least-signficant bit and clears it in the input argument template <typename T> -inline T ExtractBit(T& bits) +typename std::enable_if<std::is_integral<T>::value, T>::type ExtractLSB(T& bits) { - static_assert(std::is_enum<T>::value || std::is_integral<T>::value, "T must be enum or integer type"); + if (bits == T{0}) + return 0; - using U = std::conditional_t<(sizeof(T) > sizeof(Uint32)), Uint64, Uint32>; - VERIFY_EXPR(static_cast<U>(bits) > 0); + const T bit = bits & ~(bits - T{1}); + bits &= ~bit; - const U result = static_cast<U>(bits) & ~(static_cast<U>(bits) - U{1}); - bits = static_cast<T>(static_cast<U>(bits) & ~result); + return bit; +} - return static_cast<T>(result); +/// Returns the enum value representing the least-signficant bit and clears it in the input argument +template <typename T> +typename std::enable_if<std::is_enum<T>::value, T>::type ExtractLSB(T& bits) +{ + return static_cast<T>(ExtractLSB(reinterpret_cast<std::underlying_type<T>::type&>(bits))); } } // namespace Diligent |
