Browse Source

Extended bit field functions: #4

Christophe Riccio 14 years ago
parent
commit
7c67703bca
2 changed files with 50 additions and 0 deletions
  1. 16 0
      glm/gtx/bit.hpp
  2. 34 0
      glm/gtx/bit.inl

+ 16 - 0
glm/gtx/bit.hpp

@@ -101,6 +101,22 @@ namespace glm
 		template <typename genType>
 		genType bitRotateLeft(genType const & In, std::size_t Shift);
 
+		//! Set to 1 a range of bits.
+		//! From GLM_GTX_bit extension.
+		template <typename genIUType>
+		genIUType fillBitfieldWithOne(
+			genIUType const & Value,
+			int const & FromBit, 
+			int const & ToBit);
+
+		//! Set to 0 a range of bits.
+		//! From GLM_GTX_bit extension.
+		template <typename genIUType>
+		genIUType fillBitfieldWithZero(
+			genIUType const & Value,
+			int const & FromBit, 
+			int const & ToBit);
+
 		///@}
 
 	}//namespace bit

+ 34 - 0
glm/gtx/bit.inl

@@ -738,6 +738,40 @@ GLM_FUNC_QUALIFIER detail::tvec4<valType> bitRotateLeft
 		bitRotateLeft(Value[3], Shift));
 }
 
+template <typename genIUType>
+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 <typename genIUType>
+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