From d3b368b65c4a3cd7e2d4b340a80e9652cf49510a Mon Sep 17 00:00:00 2001 From: Christophe Riccio Date: Sun, 26 Oct 2014 19:22:19 +0100 Subject: [PATCH] Added GTC_integer, deprecated GTX_bit --- glm/gtc/integer.hpp | 94 +++++++++++ glm/gtc/integer.inl | 47 ++++++ glm/gtx/bit.hpp | 41 +---- glm/gtx/bit.inl | 155 ------------------ readme.txt | 1 + test/gtc/CMakeLists.txt | 1 + test/{gtx/gtx_bit.cpp => gtc/gtc_integer.cpp} | 61 +++++-- test/gtx/CMakeLists.txt | 1 - 8 files changed, 192 insertions(+), 209 deletions(-) create mode 100644 glm/gtc/integer.hpp create mode 100644 glm/gtc/integer.inl delete mode 100644 glm/gtx/bit.inl rename test/{gtx/gtx_bit.cpp => gtc/gtc_integer.cpp} (53%) diff --git a/glm/gtc/integer.hpp b/glm/gtc/integer.hpp new file mode 100644 index 00000000..a072b5b5 --- /dev/null +++ b/glm/gtc/integer.hpp @@ -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 +/// +/// need to be included to use these functionalities. +/////////////////////////////////////////////////////////////////////////////////// + +#pragma once + +// Dependencies +#include "../detail/setup.hpp" +#include "../detail/precision.hpp" +#include + +#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 + GLM_FUNC_DECL bool isPowerOfTwo(genType Value); + + /// Return true if the value is a power of two number. + /// + /// @see gtc_integer + template class vecType> + GLM_FUNC_DECL vecType isPowerOfTwo(vecType const & value); + + /// Find the highest bit set to 1 in a integer variable and return its value. + /// + /// @see gtc_integer + template + 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 + 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 + 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 + GLM_FUNC_DECL genType powerOfTwoNearest(genType const & value); + + /// @} +} //namespace glm + +#include "integer.inl" diff --git a/glm/gtc/integer.inl b/glm/gtc/integer.inl new file mode 100644 index 00000000..db14286c --- /dev/null +++ b/glm/gtc/integer.inl @@ -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 + GLM_FUNC_QUALIFIER bool isPowerOfTwo(genType Value) + { + genType Result = glm::abs(Value); + return !(Result & (Result - 1)); + } + + template class vecType> + GLM_FUNC_QUALIFIER vecType isPowerOfTwo(vecType const & value) + { + genType Result = glm::abs(Value); + return !(Result & (Result - 1)); + } +}//namespace glm diff --git a/glm/gtx/bit.hpp b/glm/gtx/bit.hpp index c9314ed1..dbafced9 100644 --- a/glm/gtx/bit.hpp +++ b/glm/gtx/bit.hpp @@ -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 +#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 - GLM_FUNC_DECL genType highestBitValue(genType const & value); - - //! Return true if the value is a power of two number. - /// @see gtx_bit - template - 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 - 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 - 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 - GLM_FUNC_DECL genType powerOfTwoNearest(genType const & value); - - /// @} -} //namespace glm - -#include "bit.inl" diff --git a/glm/gtx/bit.inl b/glm/gtx/bit.inl deleted file mode 100644 index 6bbb1cb0..00000000 --- a/glm/gtx/bit.inl +++ /dev/null @@ -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 - -namespace glm -{ - // highestBitValue - template - 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 - GLM_FUNC_QUALIFIER tvec2 highestBitValue - ( - tvec2 const & value - ) - { - return tvec2( - highestBitValue(value[0]), - highestBitValue(value[1])); - } - - template - GLM_FUNC_QUALIFIER tvec3 highestBitValue - ( - tvec3 const & value - ) - { - return tvec3( - highestBitValue(value[0]), - highestBitValue(value[1]), - highestBitValue(value[2])); - } - - template - GLM_FUNC_QUALIFIER tvec4 highestBitValue - ( - tvec4 const & value - ) - { - return tvec4( - highestBitValue(value[0]), - highestBitValue(value[1]), - highestBitValue(value[2]), - highestBitValue(value[3])); - } - - // isPowerOfTwo - template - GLM_FUNC_QUALIFIER bool isPowerOfTwo(genType const & Value) - { - //detail::If::is_signed>::apply(abs, Value); - //return !(Value & (Value - 1)); - - // For old complier? - genType Result = Value; - if(std::numeric_limits::is_signed) - Result = abs(Result); - return !(Result & (Result - 1)); - } - - template - GLM_FUNC_QUALIFIER tvec2 isPowerOfTwo - ( - tvec2 const & value - ) - { - return tvec2( - isPowerOfTwo(value[0]), - isPowerOfTwo(value[1])); - } - - template - GLM_FUNC_QUALIFIER tvec3 isPowerOfTwo - ( - tvec3 const & value - ) - { - return tvec3( - isPowerOfTwo(value[0]), - isPowerOfTwo(value[1]), - isPowerOfTwo(value[2])); - } - - template - GLM_FUNC_QUALIFIER tvec4 isPowerOfTwo - ( - tvec4 const & value - ) - { - return tvec4( - isPowerOfTwo(value[0]), - isPowerOfTwo(value[1]), - isPowerOfTwo(value[2]), - isPowerOfTwo(value[3])); - } - - // powerOfTwoAbove - template - GLM_FUNC_QUALIFIER genType powerOfTwoAbove(genType const & value) - { - return isPowerOfTwo(value) ? value : highestBitValue(value) << 1; - } - - VECTORIZE_VEC(powerOfTwoAbove) - - // powerOfTwoBelow - template - GLM_FUNC_QUALIFIER genType powerOfTwoBelow - ( - genType const & value - ) - { - return isPowerOfTwo(value) ? value : highestBitValue(value); - } - - VECTORIZE_VEC(powerOfTwoBelow) - - // powerOfTwoNearest - template - 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 diff --git a/readme.txt b/readme.txt index 73827a83..bf01a523 100644 --- a/readme.txt +++ b/readme.txt @@ -78,6 +78,7 @@ GLM 0.9.6.0: 2014-XX-XX - Added not function (from GLSL specification) on VC12 - Optimized bitfield operations - Added GTC_bitfield extension, promoted GTX_bit +- Added GTC_integer extension, promoted GTX_bit ================================================================================ GLM 0.9.5.4: 2014-06-21 diff --git a/test/gtc/CMakeLists.txt b/test/gtc/CMakeLists.txt index 6b590df0..3483c101 100644 --- a/test/gtc/CMakeLists.txt +++ b/test/gtc/CMakeLists.txt @@ -1,6 +1,7 @@ glmCreateTestGTC(gtc_bitfield) glmCreateTestGTC(gtc_constants) glmCreateTestGTC(gtc_epsilon) +glmCreateTestGTC(gtc_integer) glmCreateTestGTC(gtc_matrix_access) glmCreateTestGTC(gtc_matrix_integer) glmCreateTestGTC(gtc_matrix_inverse) diff --git a/test/gtx/gtx_bit.cpp b/test/gtc/gtc_integer.cpp similarity index 53% rename from test/gtx/gtx_bit.cpp rename to test/gtc/gtc_integer.cpp index ba98a40d..2981897d 100644 --- a/test/gtx/gtx_bit.cpp +++ b/test/gtc/gtc_integer.cpp @@ -1,16 +1,15 @@ /////////////////////////////////////////////////////////////////////////////////////////////////// // OpenGL Mathematics Copyright (c) 2005 - 2014 G-Truc Creation (www.g-truc.net) /////////////////////////////////////////////////////////////////////////////////////////////////// -// Created : 2010-09-16 +// Created : 2014-10-25 // Updated : 2014-10-25 // Licence : This source is under MIT licence -// File : test/gtx/bit.cpp +// File : test/gtc/integer.cpp /////////////////////////////////////////////////////////////////////////////////////////////////// -#include +#include #include - namespace isPowerOfTwo { template @@ -20,29 +19,61 @@ namespace isPowerOfTwo bool Return; }; - type const DataI32[] = + int test_int() { - {0x00000001, true}, - {0x00000002, true}, - {0x00000004, true}, - {0xffffffff, true}, - {0x00000000, true}, - {0x00000003, true} - }; + type const DataI32[] = + { + {0x00000001, true}, + {0x00000002, true}, + {0x00000004, true}, + {0xffffffff, true}, + {0x00000000, true}, + {0x00000003, false} + }; - int test() - { int Error(0); for(std::size_t i = 0, n = sizeof(DataI32) / sizeof(type); i < n; ++i) { bool Result = glm::isPowerOfTwo(DataI32[i].Value); Error += DataI32[i].Return == Result ? 0 : 1; - assert(!Error); } return Error; } + + int test_uint() + { + type const DataU32[] = + { + {0x00000001, true}, + {0x00000002, true}, + {0x00000004, true}, + {0x80000000, true}, + {0x00000000, true}, + {0x00000003, false} + }; + + int Error(0); + + for(std::size_t i = 0, n = sizeof(DataU32) / sizeof(type); i < n; ++i) + { + bool Result = glm::isPowerOfTwo(DataU32[i].Value); + Error += DataU32[i].Return == Result ? 0 : 1; + } + + return Error; + } + + int test() + { + int Error(0); + + Error += test_int(); + Error += test_uint(); + + return Error; + } }//isPowerOfTwo int main() diff --git a/test/gtx/CMakeLists.txt b/test/gtx/CMakeLists.txt index ce4744ad..1b72c6ff 100644 --- a/test/gtx/CMakeLists.txt +++ b/test/gtx/CMakeLists.txt @@ -1,5 +1,4 @@ glmCreateTestGTC(gtx_associated_min_max) -glmCreateTestGTC(gtx_bit) glmCreateTestGTC(gtx_closest_point) glmCreateTestGTC(gtx_color_space_YCoCg) glmCreateTestGTC(gtx_color_space)