diff --git a/glm/gtx/integer.hpp b/glm/gtx/integer.hpp index d96b3d82..9420581c 100644 --- a/glm/gtx/integer.hpp +++ b/glm/gtx/integer.hpp @@ -31,7 +31,7 @@ namespace glm //! Returns x raised to the y power. //! From GLM_GTX_integer extension. - GLM_FUNC_DECL int pow(int x, int y); + GLM_FUNC_DECL int pow(int x, uint y); //! Returns the positive square root of x. //! From GLM_GTX_integer extension. diff --git a/glm/gtx/integer.inl b/glm/gtx/integer.inl index bddfd2ca..186072f6 100644 --- a/glm/gtx/integer.inl +++ b/glm/gtx/integer.inl @@ -4,10 +4,11 @@ namespace glm { // pow - GLM_FUNC_QUALIFIER int pow(int x, int y) + GLM_FUNC_QUALIFIER int pow(int x, uint y) { if(y == 0) - return 1; + return x >= 0 ? 1 : -1; + int result = x; for(int i = 1; i < y; ++i) result *= x; @@ -111,6 +112,9 @@ namespace detail GLM_FUNC_QUALIFIER uint pow(uint x, uint y) { + if (y == 0) + return 1u; + uint result = x; for(uint i = 1; i < y; ++i) result *= x; diff --git a/readme.md b/readme.md index e2d75ca3..5882061a 100644 --- a/readme.md +++ b/readme.md @@ -85,6 +85,7 @@ glm::mat4 camera(float Translate, glm::vec2 const & Rotate) - Fixed references to GLM_FORCE_RADIANS which was removed #642 - Fixed glm::fastInverseSqrt to use fast inverse square #640 - Fixed axisAngle NaN #638 +- Fixed integer pow from GTX_integer with null exponent #658 #### Deprecation: - Requires Visual Studio 2013, GCC 4.7, Clang 3.4, Cuda 7, ICC 2013 or a C++11 compiler diff --git a/test/gtx/gtx_integer.cpp b/test/gtx/gtx_integer.cpp index f810953f..ae3be761 100644 --- a/test/gtx/gtx_integer.cpp +++ b/test/gtx/gtx_integer.cpp @@ -52,6 +52,47 @@ int test_nlz() return Error; } +int test_pow_uint() +{ + int Error = 0; + + glm::uint const p0 = glm::pow(2u, 0u); + Error += p0 == 1u ? 0 : 1; + + glm::uint const p1 = glm::pow(2u, 1u); + Error += p1 == 2u ? 0 : 1; + + glm::uint const p2 = glm::pow(2u, 2u); + Error += p2 == 4u ? 0 : 1; + + return Error; +} + +int test_pow_int() +{ + int Error = 0; + + int const p0 = glm::pow(2, 0u); + Error += p0 == 1 ? 0 : 1; + + int const p1 = glm::pow(2, 1u); + Error += p1 == 2 ? 0 : 1; + + int const p2 = glm::pow(2, 2u); + Error += p2 == 4 ? 0 : 1; + + int const p0n = glm::pow(-2, 0u); + Error += p0n == -1 ? 0 : 1; + + int const p1n = glm::pow(-2, 1u); + Error += p1n == -2 ? 0 : 1; + + int const p2n = glm::pow(-2, 2u); + Error += p2n == 4 ? 0 : 1; + + return Error; +} + int main() { int Error = 0; @@ -59,6 +100,8 @@ int main() Error += test_nlz(); // Error += test_floor_log2(); Error += test_log2(); + Error += test_pow_uint(); + Error += test_pow_int(); return Error; }