Browse Source

Added vector retionnal with max ULPs arguments and fixed double support

Christophe Riccio 7 years ago
parent
commit
b2a7f1093c
4 changed files with 56 additions and 19 deletions
  1. 0 13
      glm/detail/qualifier.hpp
  2. 48 0
      glm/detail/type_float.hpp
  3. 4 3
      glm/ext/scalar_relational.inl
  4. 4 3
      glm/ext/vector_relational.inl

+ 0 - 13
glm/detail/qualifier.hpp

@@ -206,18 +206,5 @@ namespace detail
 			return genType(1);
 		}
 	};
-
-	// https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/
-	union float_t
-	{
-		GLM_CONSTEXPR float_t(float Num = 0.0f) : f(Num) {}
-		// Portable extraction of components.
-		GLM_CONSTEXPR bool negative() const { return i < 0; }
-		GLM_CONSTEXPR int mantissa() const { return i & ((1 << 23) - 1); }
-		GLM_CONSTEXPR int exponent() const { return (i >> 23) & 0xFF; }
-
-		int const i;
-		float const f;
-	};
 }//namespace detail
 }//namespace glm

+ 48 - 0
glm/detail/type_float.hpp

@@ -0,0 +1,48 @@
+#pragma once
+
+#include "setup.hpp"
+
+namespace glm{
+namespace detail
+{
+	template <typename T>
+	union float_t
+	{};
+
+	// https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/
+	template <>
+	union float_t<float>
+	{
+		typedef int int_type;
+		typedef float float_type;
+
+		GLM_CONSTEXPR float_t(float_type Num = 0.0f) : f(Num) {}
+
+		// Portable extraction of components.
+		GLM_CONSTEXPR bool negative() const { return i < 0; }
+		GLM_CONSTEXPR int_type mantissa() const { return i & ((1 << 23) - 1); }
+		GLM_CONSTEXPR int_type exponent() const { return (i >> 23) & ((1 << 8) - 1); }
+
+		int_type const i;
+		float_type const f;
+	};
+
+	template <>
+	union float_t<double>
+	{
+		typedef detail::int64 int_type;
+		typedef double float_type;
+
+		GLM_CONSTEXPR float_t(float_type Num = static_cast<float_type>(0)) : f(Num) {}
+
+		// Portable extraction of components.
+		GLM_CONSTEXPR bool negative() const { return i < 0; }
+		GLM_CONSTEXPR int_type mantissa() const { return i & ((int_type(1) << 52) - 1); }
+		GLM_CONSTEXPR int_type exponent() const { return (i >> 52) & ((int_type(1) << 11) - 1); }
+
+		int_type const i;
+		float_type const f;
+	};
+}//namespace detail
+}//namespace glm
+

+ 4 - 3
glm/ext/scalar_relational.inl

@@ -1,6 +1,7 @@
 #include "../common.hpp"
 #include "../ext/scalar_int_sized.hpp"
 #include "../ext/scalar_uint_sized.hpp"
+#include "../detail/type_float.hpp"
 
 namespace glm
 {
@@ -19,8 +20,8 @@ namespace glm
 	template<typename genType>
 	GLM_FUNC_QUALIFIER GLM_CONSTEXPR bool equal(genType const& x, genType const& y, int MaxULPs)
 	{
-		detail::float_t const a(x);
-		detail::float_t const b(y);
+		detail::float_t<genType> const a(x);
+		detail::float_t<genType> const b(y);
 
 		// Different signs means they do not match.
 		if(a.negative() != b.negative())
@@ -30,7 +31,7 @@ namespace glm
 		}
 
 		// Find the difference in ULPs.
-		int const DiffULPs = abs(a.i - b.i);
+		typename detail::float_t<genType>::int_type const DiffULPs = abs(a.i - b.i);
 		return DiffULPs <= MaxULPs;
 	}
 

+ 4 - 3
glm/ext/vector_relational.inl

@@ -1,6 +1,7 @@
 #include "../vector_relational.hpp"
 #include "../common.hpp"
 #include "../detail/qualifier.hpp"
+#include "../detail/type_float.hpp"
 
 namespace glm
 {
@@ -41,8 +42,8 @@ namespace glm
 		vec<L, bool, Q> Result;
 		for(length_t i = 0; i < L; ++i)
 		{
-			detail::float_t const a(x[i]);
-			detail::float_t const b(y[i]);
+			detail::float_t<T> const a(x[i]);
+			detail::float_t<T> const b(y[i]);
 
 			// Different signs means they do not match.
 			if(a.negative() != b.negative())
@@ -52,7 +53,7 @@ namespace glm
 			}
 
 			// Find the difference in ULPs.
-			int const DiffULPs = abs(a.i - b.i);
+			typename detail::float_t<T>::int_type const DiffULPs = abs(a.i - b.i);
 			Result[i] = DiffULPs <= MaxULPs;
 		}
 		return Result;