From 7c67703bca0daef11c2213bae73da891645d899c Mon Sep 17 00:00:00 2001 From: Christophe Riccio Date: Mon, 9 May 2011 12:33:00 +0100 Subject: [PATCH] Extended bit field functions: #4 --- glm/gtx/bit.hpp | 16 ++++++++++++++++ glm/gtx/bit.inl | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/glm/gtx/bit.hpp b/glm/gtx/bit.hpp index f6c689f2..676b1ad0 100644 --- a/glm/gtx/bit.hpp +++ b/glm/gtx/bit.hpp @@ -101,6 +101,22 @@ namespace glm template genType bitRotateLeft(genType const & In, std::size_t Shift); + //! Set to 1 a range of bits. + //! From GLM_GTX_bit extension. + template + genIUType fillBitfieldWithOne( + genIUType const & Value, + int const & FromBit, + int const & ToBit); + + //! Set to 0 a range of bits. + //! From GLM_GTX_bit extension. + template + genIUType fillBitfieldWithZero( + genIUType const & Value, + int const & FromBit, + int const & ToBit); + ///@} }//namespace bit diff --git a/glm/gtx/bit.inl b/glm/gtx/bit.inl index 5c3ec8e2..f1d29c35 100644 --- a/glm/gtx/bit.inl +++ b/glm/gtx/bit.inl @@ -738,6 +738,40 @@ GLM_FUNC_QUALIFIER detail::tvec4 bitRotateLeft bitRotateLeft(Value[3], Shift)); } +template +GLM_FUNC_QUALIFIER genIUType fillBitfieldWithOne +( + genIUType const & Value, + int const & FromBit, + int const & ToBit +) +{ + assert(FromBit <= ToBit); + assert(ToBit <= sizeof(genIUType) * std::size_t(8)); + + genIUType Result = Value; + for(std::size_t i = 0; i <= ToBit; ++i) + Result |= (1 << i); + return Result; +} + +template +GLM_FUNC_QUALIFIER genIUType fillBitfieldWithZero +( + genIUType const & Value, + int const & FromBit, + int const & ToBit +) +{ + assert(FromBit <= ToBit); + assert(ToBit <= sizeof(genIUType) * std::size_t(8)); + + genIUType Result = Value; + for(std::size_t i = 0; i <= ToBit; ++i) + Result &= ~(1 << i); + return Result; +} + }//namespace bit }//namespace gtx }//namespace glm