From c853df16382c5d427697a4725e5ee2c372fbc66a Mon Sep 17 00:00:00 2001 From: Christophe Riccio Date: Thu, 10 Mar 2016 21:17:46 +0100 Subject: [PATCH] Added uround to GTC_integer, fast round on positive values --- glm/gtc/integer.hpp | 13 +++++++++++++ glm/gtc/integer.inl | 18 ++++++++++++++++++ readme.md | 1 + test/core/core_func_common.cpp | 2 +- test/gtc/gtc_integer.cpp | 19 +++++++++++++++++++ 5 files changed, 52 insertions(+), 1 deletion(-) diff --git a/glm/gtc/integer.hpp b/glm/gtc/integer.hpp index 96c19348..ef0df86e 100644 --- a/glm/gtc/integer.hpp +++ b/glm/gtc/integer.hpp @@ -99,6 +99,19 @@ namespace glm template class vecType> GLM_FUNC_DECL vecType mod(vecType const & x, vecType const & y); + /// Returns a value equal to the nearest integer to x. + /// The fraction 0.5 will round in a direction chosen by the + /// implementation, presumably the direction that is fastest. + /// + /// @param x The values of the argument must be greater or equal to zero. + /// @tparam T floating point scalar types. + /// @tparam vecType vector types. + /// + /// @see GLSL round man page + /// @see gtc_integer + template class vecType> + GLM_FUNC_DECL vecType uround(vecType const & x); + /// @} } //namespace glm diff --git a/glm/gtc/integer.inl b/glm/gtc/integer.inl index 8ddf1238..d57f906e 100644 --- a/glm/gtc/integer.inl +++ b/glm/gtc/integer.inl @@ -62,4 +62,22 @@ namespace detail }; # endif//GLM_HAS_BITSCAN_WINDOWS }//namespace detail + + template + GLM_FUNC_QUALIFIER uint uround(genType x) + { + GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'round' only accept floating-point inputs"); + assert(static_cast(0.0) <= x); + + return static_cast(x + static_cast(0.5)); + } + + template class vecType> + GLM_FUNC_QUALIFIER vecType uround(vecType const& x) + { + GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'uround' only accept floating-point inputs"); + assert(all(lessThanEqual(vecType(0), x))); + + return vecType(x + static_cast(0.5)); + } }//namespace glm diff --git a/readme.md b/readme.md index dee81544..d842fb7e 100644 --- a/readme.md +++ b/readme.md @@ -61,6 +61,7 @@ glm::mat4 camera(float Translate, glm::vec2 const & Rotate) - Added 16bit pack and unpack to GTC_packing - Added 8bit pack and unpack to GTC_packing - Added missing bvec* && and || operators +- Added uround to GTC_integer, fast round on positive values ##### Improvements: - Improved GTC_random linearRand documentation diff --git a/test/core/core_func_common.cpp b/test/core/core_func_common.cpp index a610dd51..ed6cb2df 100644 --- a/test/core/core_func_common.cpp +++ b/test/core/core_func_common.cpp @@ -599,7 +599,7 @@ namespace round_ int test() { int Error = 0; - + { float A = glm::round(0.0f); Error += A == 0.0f ? 0 : 1; diff --git a/test/gtc/gtc_integer.cpp b/test/gtc/gtc_integer.cpp index 40832822..98614780 100644 --- a/test/gtc/gtc_integer.cpp +++ b/test/gtc/gtc_integer.cpp @@ -210,11 +210,30 @@ namespace log2_ } }//namespace log2_ +namespace uround +{ + int test() + { + int Error = 0; + + for(float f = 0.0f; f < 3.1f; f += 0.05f) + { + int RoundFast = glm::uround(f); + int RoundSTD = std::round(f); + Error += RoundFast == RoundSTD ? 0 : 1; + assert(!Error); + } + + return Error; + } +}//namespace uround + int main() { int Error(0); Error += ::log2_::test(); + Error += ::uround::test(); # ifdef NDEBUG std::size_t const Samples(1000);