Added GTC_integer, deprecated GTX_bit

This commit is contained in:
Christophe Riccio
2014-10-26 19:22:19 +01:00
parent eaa3b72b99
commit d3b368b65c
8 changed files with 192 additions and 209 deletions

94
glm/gtc/integer.hpp Normal file
View File

@@ -0,0 +1,94 @@
///////////////////////////////////////////////////////////////////////////////////
/// OpenGL Mathematics (glm.g-truc.net)
///
/// Copyright (c) 2005 - 2014 G-Truc Creation (www.g-truc.net)
/// Permission is hereby granted, free of charge, to any person obtaining a copy
/// of this software and associated documentation files (the "Software"), to deal
/// in the Software without restriction, including without limitation the rights
/// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
/// copies of the Software, and to permit persons to whom the Software is
/// furnished to do so, subject to the following conditions:
///
/// The above copyright notice and this permission notice shall be included in
/// all copies or substantial portions of the Software.
///
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
/// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
/// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
/// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
/// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
/// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
/// THE SOFTWARE.
///
/// @ref gtc_bit
/// @file glm/gtc/integer.hpp
/// @date 2014-10-25 / 2014-10-25
/// @author Christophe Riccio
///
/// @see core (dependence)
/// @see gtc_bitfield (dependence)
///
/// @defgroup gtc_integer GLM_GTC_integer
/// @ingroup gtc
///
/// @brief Allow to perform bit operations on integer values
///
/// <glm/gtc/integer.hpp> need to be included to use these functionalities.
///////////////////////////////////////////////////////////////////////////////////
#pragma once
// Dependencies
#include "../detail/setup.hpp"
#include "../detail/precision.hpp"
#include <limits>
#if(defined(GLM_MESSAGES) && !defined(GLM_EXT_INCLUDED))
# pragma message("GLM: GLM_GTC_integer extension included")
#endif
namespace glm
{
/// @addtogroup gtc_integer
/// @{
/// Return true if the value is a power of two number.
///
/// @see gtc_integer
template <typename genType>
GLM_FUNC_DECL bool isPowerOfTwo(genType Value);
/// Return true if the value is a power of two number.
///
/// @see gtc_integer
template <typename T, precision P, template <typename, precision> class vecType>
GLM_FUNC_DECL vecType<bool, P> isPowerOfTwo(vecType<T, P> const & value);
/// Find the highest bit set to 1 in a integer variable and return its value.
///
/// @see gtc_integer
template <typename genType>
GLM_FUNC_DECL genType highestBitValue(genType const & value);
/// Return the power of two number which value is just higher the input value.
///
/// @see gtc_integer
template <typename genType>
GLM_FUNC_DECL genType powerOfTwoAbove(genType const & value);
/// Return the power of two number which value is just lower the input value.
///
/// @see gtc_integer
template <typename genType>
GLM_FUNC_DECL genType powerOfTwoBelow(genType const & value);
/// Return the power of two number which value is the closet to the input value.
///
/// @see gtc_integer
template <typename genType>
GLM_FUNC_DECL genType powerOfTwoNearest(genType const & value);
/// @}
} //namespace glm
#include "integer.inl"

47
glm/gtc/integer.inl Normal file
View File

@@ -0,0 +1,47 @@
///////////////////////////////////////////////////////////////////////////////////
/// OpenGL Mathematics (glm.g-truc.net)
///
/// Copyright (c) 2005 - 2014 G-Truc Creation (www.g-truc.net)
/// Permission is hereby granted, free of charge, to any person obtaining a copy
/// of this software and associated documentation files (the "Software"), to deal
/// in the Software without restriction, including without limitation the rights
/// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
/// copies of the Software, and to permit persons to whom the Software is
/// furnished to do so, subject to the following conditions:
///
/// The above copyright notice and this permission notice shall be included in
/// all copies or substantial portions of the Software.
///
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
/// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
/// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
/// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
/// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
/// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
/// THE SOFTWARE.
///
/// @ref gtc_integer
/// @file glm/gtc/integer.inl
/// @date 2014-10-25 / 2014-10-25
/// @author Christophe Riccio
///////////////////////////////////////////////////////////////////////////////////
namespace glm
{
////////////////
// isPowerOfTwo
template <typename genType>
GLM_FUNC_QUALIFIER bool isPowerOfTwo(genType Value)
{
genType Result = glm::abs(Value);
return !(Result & (Result - 1));
}
template <typename T, precision P, template <typename, precision> class vecType>
GLM_FUNC_QUALIFIER vecType<bool, P> isPowerOfTwo(vecType<T, P> const & value)
{
genType Result = glm::abs(Value);
return !(Result & (Result - 1));
}
}//namespace glm

View File

@@ -39,44 +39,9 @@
#pragma once
// Dependencies
#include "../detail/type_int.hpp"
#include "../detail/setup.hpp"
#include "../detail/precision.hpp"
#include "../gtc/bitfield.hpp"
#include <cstddef>
#include "../gtc/integer.hpp"
#if(defined(GLM_MESSAGES) && !defined(GLM_EXT_INCLUDED))
# pragma message("GLM: GLM_GTX_bit extension included")
#if(defined(GLM_MESSAGES))
# pragma message("GLM: GLM_GTX_bit extension is deprecated, include GLM_GTC_bitfield and GLM_GTC_integer instead")
#endif
namespace glm
{
//! Find the highest bit set to 1 in a integer variable and return its value.
/// @see gtx_bit
template <typename genType>
GLM_FUNC_DECL genType highestBitValue(genType const & value);
//! Return true if the value is a power of two number.
/// @see gtx_bit
template <typename genType>
GLM_FUNC_DECL bool isPowerOfTwo(genType const & value);
//! Return the power of two number which value is just higher the input value.
/// @see gtx_bit
template <typename genType>
GLM_FUNC_DECL genType powerOfTwoAbove(genType const & value);
//! Return the power of two number which value is just lower the input value.
/// @see gtx_bit
template <typename genType>
GLM_FUNC_DECL genType powerOfTwoBelow(genType const & value);
//! Return the power of two number which value is the closet to the input value.
/// @see gtx_bit
template <typename genType>
GLM_FUNC_DECL genType powerOfTwoNearest(genType const & value);
/// @}
} //namespace glm
#include "bit.inl"

View File

@@ -1,155 +0,0 @@
///////////////////////////////////////////////////////////////////////////////////////////////////
// OpenGL Mathematics Copyright (c) 2005 - 2014 G-Truc Creation (www.g-truc.net)
///////////////////////////////////////////////////////////////////////////////////////////////////
// Created : 2007-03-14
// Updated : 2013-12-25
// Licence : This source is under MIT License
// File : glm/gtx/bit.inl
///////////////////////////////////////////////////////////////////////////////////////////////////
#include "../detail/_vectorize.hpp"
#include <limits>
namespace glm
{
// highestBitValue
template <typename genType>
GLM_FUNC_QUALIFIER genType highestBitValue
(
genType const & value
)
{
genType tmp = value;
genType result = genType(0);
while(tmp)
{
result = (tmp & (~tmp + 1)); // grab lowest bit
tmp &= ~result; // clear lowest bit
}
return result;
}
template <typename T, precision P>
GLM_FUNC_QUALIFIER tvec2<int, P> highestBitValue
(
tvec2<T, P> const & value
)
{
return tvec2<int, P>(
highestBitValue(value[0]),
highestBitValue(value[1]));
}
template <typename T, precision P>
GLM_FUNC_QUALIFIER tvec3<int, P> highestBitValue
(
tvec3<T, P> const & value
)
{
return tvec3<int, P>(
highestBitValue(value[0]),
highestBitValue(value[1]),
highestBitValue(value[2]));
}
template <typename T, precision P>
GLM_FUNC_QUALIFIER tvec4<int, P> highestBitValue
(
tvec4<T, P> const & value
)
{
return tvec4<int, P>(
highestBitValue(value[0]),
highestBitValue(value[1]),
highestBitValue(value[2]),
highestBitValue(value[3]));
}
// isPowerOfTwo
template <typename genType>
GLM_FUNC_QUALIFIER bool isPowerOfTwo(genType const & Value)
{
//detail::If<std::numeric_limits<genType>::is_signed>::apply(abs, Value);
//return !(Value & (Value - 1));
// For old complier?
genType Result = Value;
if(std::numeric_limits<genType>::is_signed)
Result = abs(Result);
return !(Result & (Result - 1));
}
template <typename T, precision P>
GLM_FUNC_QUALIFIER tvec2<bool, P> isPowerOfTwo
(
tvec2<T, P> const & value
)
{
return tvec2<bool, P>(
isPowerOfTwo(value[0]),
isPowerOfTwo(value[1]));
}
template <typename T, precision P>
GLM_FUNC_QUALIFIER tvec3<bool, P> isPowerOfTwo
(
tvec3<T, P> const & value
)
{
return tvec3<bool, P>(
isPowerOfTwo(value[0]),
isPowerOfTwo(value[1]),
isPowerOfTwo(value[2]));
}
template <typename T, precision P>
GLM_FUNC_QUALIFIER tvec4<bool, P> isPowerOfTwo
(
tvec4<T, P> const & value
)
{
return tvec4<bool, P>(
isPowerOfTwo(value[0]),
isPowerOfTwo(value[1]),
isPowerOfTwo(value[2]),
isPowerOfTwo(value[3]));
}
// powerOfTwoAbove
template <typename genType>
GLM_FUNC_QUALIFIER genType powerOfTwoAbove(genType const & value)
{
return isPowerOfTwo(value) ? value : highestBitValue(value) << 1;
}
VECTORIZE_VEC(powerOfTwoAbove)
// powerOfTwoBelow
template <typename genType>
GLM_FUNC_QUALIFIER genType powerOfTwoBelow
(
genType const & value
)
{
return isPowerOfTwo(value) ? value : highestBitValue(value);
}
VECTORIZE_VEC(powerOfTwoBelow)
// powerOfTwoNearest
template <typename genType>
GLM_FUNC_QUALIFIER genType powerOfTwoNearest
(
genType const & value
)
{
if(isPowerOfTwo(value))
return value;
genType prev = highestBitValue(value);
genType next = prev << 1;
return (next - value) < (value - prev) ? next : prev;
}
VECTORIZE_VEC(powerOfTwoNearest)
}//namespace glm