diff --git a/glm/core/func_common.inl b/glm/core/func_common.inl index e449714d..620d04b3 100644 --- a/glm/core/func_common.inl +++ b/glm/core/func_common.inl @@ -148,18 +148,21 @@ namespace detail */ // roundEven template - GLM_FUNC_QUALIFIER genType roundEven(genType const& x) + GLM_FUNC_QUALIFIER genType roundEven(genType const & x) { GLM_STATIC_ASSERT(detail::type::is_float, "'roundEven' only accept floating-point inputs"); - genType RoundValue(0.5); - if(fract(x) == genType(0.5) && int(x) % 2) - RoundValue = genType(-0.5); - - if(x < genType(0.0)) - return genType(int(x - RoundValue)); - return genType(int(x + RoundValue)); - } + int Integer = int(x); + genType IntegerPart = genType(Integer); + genType FractionalPart = fract(x); + + if(FractionalPart > genType(0.5) || FractionalPart < genType(0.5)) + return round(x); + else if(!(Integer % 2)) + return IntegerPart; + else + return IntegerPart + mix(genType(-1), genType(1), x >= genType(0)); + } VECTORIZE_VEC(roundEven) diff --git a/test/core/core_func_common.cpp b/test/core/core_func_common.cpp index 41dfaa1c..49c64266 100644 --- a/test/core/core_func_common.cpp +++ b/test/core/core_func_common.cpp @@ -167,70 +167,76 @@ int test_roundEven() int Error = 0; { - float A = glm::round(0.0f); + float A = glm::roundEven(-1.5f); + Error += A == -2.0f ? 0 : 1; + Error += 0; + } + + { + float A = glm::roundEven(0.0f); Error += A == 0.0f ? 0 : 1; - float B = glm::round(0.5f); + float B = glm::roundEven(0.5f); Error += B == 0.0f ? 0 : 1; - float C = glm::round(1.0f); + float C = glm::roundEven(1.0f); Error += C == 1.0f ? 0 : 1; - float D = glm::round(0.1f); + float D = glm::roundEven(0.1f); Error += D == 0.0f ? 0 : 1; - float E = glm::round(0.9f); + float E = glm::roundEven(0.9f); Error += E == 1.0f ? 0 : 1; - float F = glm::round(1.5f); + float F = glm::roundEven(1.5f); Error += F == 2.0f ? 0 : 1; - float G = glm::round(1.9f); + float G = glm::roundEven(1.9f); Error += G == 2.0f ? 0 : 1; } { - float A = glm::round(-0.0f); + float A = glm::roundEven(-0.0f); Error += A == 0.0f ? 0 : 1; - float B = glm::round(-0.5f); + float B = glm::roundEven(-0.5f); Error += B == -0.0f ? 0 : 1; - float C = glm::round(-1.0f); + float C = glm::roundEven(-1.0f); Error += C == -1.0f ? 0 : 1; - float D = glm::round(-0.1f); + float D = glm::roundEven(-0.1f); Error += D == 0.0f ? 0 : 1; - float E = glm::round(-0.9f); + float E = glm::roundEven(-0.9f); Error += E == -1.0f ? 0 : 1; - float F = glm::round(-1.5f); + float F = glm::roundEven(-1.5f); Error += F == -2.0f ? 0 : 1; - float G = glm::round(-1.9f); + float G = glm::roundEven(-1.9f); Error += G == -2.0f ? 0 : 1; } { - float A = glm::round(1.5f); + float A = glm::roundEven(1.5f); Error += A == 2.0f ? 0 : 1; - float B = glm::round(2.5f); + float B = glm::roundEven(2.5f); Error += B == 2.0f ? 0 : 1; - float C = glm::round(3.5f); + float C = glm::roundEven(3.5f); Error += C == 4.0f ? 0 : 1; - float D = glm::round(4.5f); + float D = glm::roundEven(4.5f); Error += D == 4.0f ? 0 : 1; - float E = glm::round(5.5f); + float E = glm::roundEven(5.5f); Error += E == 6.0f ? 0 : 1; - float F = glm::round(6.5f); + float F = glm::roundEven(6.5f); Error += F == 6.0f ? 0 : 1; - float G = glm::round(7.5f); + float G = glm::roundEven(7.5f); Error += G == 8.0f ? 0 : 1; } { - float A = glm::round(-1.5f); + float A = glm::roundEven(-1.5f); Error += A == -2.0f ? 0 : 1; - float B = glm::round(-2.5f); + float B = glm::roundEven(-2.5f); Error += B == -2.0f ? 0 : 1; - float C = glm::round(-3.5f); + float C = glm::roundEven(-3.5f); Error += C == -4.0f ? 0 : 1; - float D = glm::round(-4.5f); + float D = glm::roundEven(-4.5f); Error += D == -4.0f ? 0 : 1; - float E = glm::round(-5.5f); + float E = glm::roundEven(-5.5f); Error += E == -6.0f ? 0 : 1; - float F = glm::round(-6.5f); + float F = glm::roundEven(-6.5f); Error += F == -6.0f ? 0 : 1; - float G = glm::round(-7.5f); + float G = glm::roundEven(-7.5f); Error += G == -8.0f ? 0 : 1; }