From 68ec048b7021ac1f37a917532302465816a276da Mon Sep 17 00:00:00 2001 From: Christophe Riccio Date: Fri, 29 Apr 2016 17:24:35 +0200 Subject: [PATCH] Fixed roundPowerOfTwo and floorPowerOfTwo #503 --- glm/gtc/round.inl | 6 +++--- test/gtc/gtc_round.cpp | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/glm/gtc/round.inl b/glm/gtc/round.inl index c816089f..988c64a1 100644 --- a/glm/gtc/round.inl +++ b/glm/gtc/round.inl @@ -276,7 +276,7 @@ namespace detail template GLM_FUNC_QUALIFIER genType floorPowerOfTwo(genType value) { - return isPowerOfTwo(value) ? value : findMSB(value); + return isPowerOfTwo(value) ? value : static_cast(1) << findMSB(value); } template class vecType> @@ -294,8 +294,8 @@ namespace detail if(isPowerOfTwo(value)) return value; - genIUType const prev = findMSB(value); - genIUType const next = prev << 1; + genIUType const prev = static_cast(1) << findMSB(value); + genIUType const next = prev << static_cast(1); return (next - value) < (value - prev) ? next : prev; } diff --git a/test/gtc/gtc_round.cpp b/test/gtc/gtc_round.cpp index 97b2abfd..ba57a786 100644 --- a/test/gtc/gtc_round.cpp +++ b/test/gtc/gtc_round.cpp @@ -303,6 +303,21 @@ namespace roundPowerOfTwo glm::uint32 const A = glm::roundPowerOfTwo(7u); Error += A == 8u ? 0 : 1; + glm::uint32 const B = glm::roundPowerOfTwo(15u); + Error += B == 16u ? 0 : 1; + + glm::uint32 const C = glm::roundPowerOfTwo(31u); + Error += C == 32u ? 0 : 1; + + glm::uint32 const D = glm::roundPowerOfTwo(9u); + Error += D == 8u ? 0 : 1; + + glm::uint32 const E = glm::roundPowerOfTwo(17u); + Error += E == 16u ? 0 : 1; + + glm::uint32 const F = glm::roundPowerOfTwo(33u); + Error += F == 32u ? 0 : 1; + return Error; } }//namespace roundPowerOfTwo @@ -316,6 +331,12 @@ namespace floorPowerOfTwo glm::uint32 const A = glm::floorPowerOfTwo(7u); Error += A == 4u ? 0 : 1; + glm::uint32 const B = glm::floorPowerOfTwo(15u); + Error += B == 8u ? 0 : 1; + + glm::uint32 const C = glm::floorPowerOfTwo(31u); + Error += C == 16u ? 0 : 1; + return Error; } }//namespace floorPowerOfTwo