Skip to content

Commit

Permalink
- Fixed integer pow from GTX_integer with null exponent #658
Browse files Browse the repository at this point in the history
  • Loading branch information
Groovounet committed Jul 24, 2017
1 parent ad74473 commit 50a527c
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 3 deletions.
2 changes: 1 addition & 1 deletion glm/gtx/integer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 6 additions & 2 deletions glm/gtx/integer.inl
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
43 changes: 43 additions & 0 deletions test/gtx/gtx_integer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,56 @@ 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;

Error += test_nlz();
// Error += test_floor_log2();
Error += test_log2();
Error += test_pow_uint();
Error += test_pow_int();

return Error;
}
Expand Down

0 comments on commit 50a527c

Please sign in to comment.