Преглед изворни кода

Fixed roundEven implementation, tests are now passing

Christophe Riccio пре 14 година
родитељ
комит
6c00ffa1b4
2 измењених фајлова са 46 додато и 37 уклоњено
  1. 12 9
      glm/core/func_common.inl
  2. 34 28
      test/core/core_func_common.cpp

+ 12 - 9
glm/core/func_common.inl

@@ -148,18 +148,21 @@ namespace detail
 */
     // roundEven
     template <typename genType>
-    GLM_FUNC_QUALIFIER genType roundEven(genType const& x)
+    GLM_FUNC_QUALIFIER genType roundEven(genType const & x)
     {
 		GLM_STATIC_ASSERT(detail::type<genType>::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)
 

+ 34 - 28
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;
 	}