Browse Source

Added experiment for new fastCos. More sRGB tests.

Christophe Riccio 10 years ago
parent
commit
08eedc6209

+ 13 - 3
glm/gtx/fast_trigonometry.inl

@@ -33,6 +33,16 @@
 namespace glm{
 namespace detail
 {
+	template <typename T, precision P, template <typename, precision> class vecType>
+	GLM_FUNC_QUALIFIER vecType<T, P> taylorCos(vecType<T, P> const & x)
+	{
+		return static_cast<T>(1)
+			- (x * x) / 2.f
+			+ (x * x * x * x) / 24.f
+			- (x * x * x * x * x * x) / 720.f
+			+ (x * x * x * x * x * x * x * x) / 40320.f;
+	}
+
 	template <typename T>
 	GLM_FUNC_QUALIFIER T cos_52s(T x)
 	{
@@ -66,11 +76,11 @@ namespace detail
 	{
 		T const angle(wrapAngle<T>(x));
 
-		if(angle<half_pi<T>())
+		if(angle < half_pi<T>())
 			return detail::cos_52s(angle);
-		if(angle<pi<T>())
+		if(angle < pi<T>())
 			return -detail::cos_52s(pi<T>() - angle);
-		if(angle<(T(3) * half_pi<T>()))
+		if(angle < (T(3) * half_pi<T>()))
 			return -detail::cos_52s(angle - pi<T>());
 
 		return detail::cos_52s(two_pi<T>() - angle);

+ 16 - 0
test/core/core_func_common.cpp

@@ -168,6 +168,22 @@ namespace mod_
 	{
 		int Error(0);
 
+		{
+			float A(1.5f);
+			float B(1.0f);
+			float C = glm::mod(A, B);
+
+			Error += glm::abs(C - 0.5f) < 0.00001f ? 0 : 1;
+		}
+
+		{
+			float A(-0.2f);
+			float B(1.0f);
+			float C = glm::mod(A, B);
+
+			Error += glm::abs(C - 0.8f) < 0.00001f ? 0 : 1;
+		}
+
 		{
 			float A(3.0);
 			float B(2.0f);

+ 4 - 9
test/core/core_func_trigonometric.cpp

@@ -30,17 +30,12 @@
 ///////////////////////////////////////////////////////////////////////////////////
 
 #include <glm/trigonometric.hpp>
-/*
-float sin(float x) {
-	float temp;
-	temp = (x + M_PI) / ((2 * M_PI) - M_PI);
-	return limited_sin((x + M_PI) - ((2 * M_PI) - M_PI) * temp));
-}
-*/
+
 int main()
 {
-	int Failed = 0;
+	int Error = 0;
+
 
-	return Failed;
+	return Error;
 }
 

+ 19 - 5
test/gtc/gtc_color.cpp

@@ -39,20 +39,34 @@ namespace srgb
 	{
 		int Error(0);
 
-		glm::vec4 const ColorSourceRGB(1.0, 0.5, 0.0, 1.0);
+		glm::vec3 const ColorSourceRGB(1.0, 0.5, 0.0);
 
 		{
-			glm::vec4 const ColorSRGB = glm::convertRgbToSrgb(ColorSourceRGB);
-			glm::vec4 const ColorRGB = glm::convertSrgbToRgb(ColorSRGB);
+			glm::vec3 const ColorSRGB = glm::convertRgbToSrgb(ColorSourceRGB);
+			glm::vec3 const ColorRGB = glm::convertSrgbToRgb(ColorSRGB);
 			Error += glm::all(glm::epsilonEqual(ColorSourceRGB, ColorRGB, 0.00001f)) ? 0 : 1;
 		}
 
 		{
-			glm::vec4 const ColorSRGB = glm::convertRgbToSrgb(ColorSourceRGB, 2.8f);
-			glm::vec4 const ColorRGB = glm::convertSrgbToRgb(ColorSRGB, 2.8f);
+			glm::vec3 const ColorSRGB = glm::convertRgbToSrgb(ColorSourceRGB, 2.8f);
+			glm::vec3 const ColorRGB = glm::convertSrgbToRgb(ColorSRGB, 2.8f);
 			Error += glm::all(glm::epsilonEqual(ColorSourceRGB, ColorRGB, 0.00001f)) ? 0 : 1;
 		}
 
+		glm::vec4 const ColorSourceRGBA(1.0, 0.5, 0.0, 1.0);
+
+		{
+			glm::vec4 const ColorSRGB = glm::convertRgbToSrgb(ColorSourceRGBA);
+			glm::vec4 const ColorRGB = glm::convertSrgbToRgb(ColorSRGB);
+			Error += glm::all(glm::epsilonEqual(ColorSourceRGBA, ColorRGB, 0.00001f)) ? 0 : 1;
+		}
+
+		{
+			glm::vec4 const ColorSRGB = glm::convertRgbToSrgb(ColorSourceRGBA, 2.8f);
+			glm::vec4 const ColorRGB = glm::convertSrgbToRgb(ColorSRGB, 2.8f);
+			Error += glm::all(glm::epsilonEqual(ColorSourceRGBA, ColorRGB, 0.00001f)) ? 0 : 1;
+		}
+
 		return Error;
 	}
 }//namespace srgb

+ 290 - 12
test/gtx/gtx_fast_trigonometry.cpp

@@ -31,10 +31,16 @@
 
 #include <glm/gtc/type_precision.hpp>
 #include <glm/gtx/fast_trigonometry.hpp>
+#include <glm/gtx/integer.hpp>
+#include <glm/gtx/common.hpp>
 #include <glm/gtc/constants.hpp>
 #include <glm/gtc/ulp.hpp>
+#include <glm/gtc/vec1.hpp>
+#include <glm/trigonometric.hpp>
+#include <cmath>
 #include <ctime>
 #include <cstdio>
+#include <vector>
 
 namespace fastCos
 {
@@ -43,12 +49,15 @@ namespace fastCos
 		const float begin = -glm::pi<float>();
 		const float end = glm::pi<float>();
 		float result = 0.f;
+
 		const std::clock_t timestamp1 = std::clock();
-		for(float i=begin; i<end; i = glm::next_float(i, end))
+		for(float i = begin; i < end; i = glm::next_float(i))
 			result = glm::fastCos(i);
+
 		const std::clock_t timestamp2 = std::clock();
-		for(float i=begin; i<end; i = glm::next_float(i, end))
+		for(float i = begin; i < end; i = glm::next_float(i))
 			result = glm::cos(i);
+
 		const std::clock_t timestamp3 = std::clock();
 		const std::clock_t time_fast = timestamp2 - timestamp1;
 		const std::clock_t time_default = timestamp3 - timestamp2;
@@ -74,12 +83,15 @@ namespace fastSin
 		const float begin = -glm::pi<float>();
 		const float end = glm::pi<float>();
 		float result = 0.f;
+
 		const std::clock_t timestamp1 = std::clock();
-		for (float i = begin; i<end; i = glm::next_float(i, end))
+		for(float i = begin; i < end; i = glm::next_float(i))
 			result = glm::fastSin(i);
+
 		const std::clock_t timestamp2 = std::clock();
-		for (float i = begin; i<end; i = glm::next_float(i, end))
+		for(float i = begin; i < end; i = glm::next_float(i))
 			result = glm::sin(i);
+
 		const std::clock_t timestamp3 = std::clock();
 		const std::clock_t time_fast = timestamp2 - timestamp1;
 		const std::clock_t time_default = timestamp3 - timestamp2;
@@ -97,12 +109,15 @@ namespace fastTan
 		const float begin = -glm::pi<float>();
 		const float end = glm::pi<float>();
 		float result = 0.f;
+
 		const std::clock_t timestamp1 = std::clock();
-		for (float i = begin; i<end; i = glm::next_float(i, end))
+		for(float i = begin; i < end; i = glm::next_float(i))
 			result = glm::fastTan(i);
+
 		const std::clock_t timestamp2 = std::clock();
-		for (float i = begin; i<end; i = glm::next_float(i, end))
+		for (float i = begin; i < end; i = glm::next_float(i))
 			result = glm::tan(i);
+
 		const std::clock_t timestamp3 = std::clock();
 		const std::clock_t time_fast = timestamp2 - timestamp1;
 		const std::clock_t time_default = timestamp3 - timestamp2;
@@ -120,15 +135,19 @@ namespace fastAcos
 		const float begin = -glm::pi<float>();
 		const float end = glm::pi<float>();
 		float result = 0.f;
+
 		const std::clock_t timestamp1 = std::clock();
-		for (float i = begin; i<end; i = glm::next_float(i, end))
+		for(float i = begin; i < end; i = glm::next_float(i))
 			result = glm::fastAcos(i);
+
 		const std::clock_t timestamp2 = std::clock();
-		for (float i = begin; i<end; i = glm::next_float(i, end))
+		for(float i = begin; i < end; i = glm::next_float(i))
 			result = glm::acos(i);
+
 		const std::clock_t timestamp3 = std::clock();
 		const std::clock_t time_fast = timestamp2 - timestamp1;
 		const std::clock_t time_default = timestamp3 - timestamp2;
+
 		std::printf("fastAcos Time %d clocks\n", static_cast<unsigned int>(time_fast));
 		std::printf("acos Time %d clocks\n", static_cast<unsigned int>(time_default));
 
@@ -144,10 +163,10 @@ namespace fastAsin
 		const float end = glm::pi<float>();
 		float result = 0.f;
 		const std::clock_t timestamp1 = std::clock();
-		for (float i = begin; i<end; i = glm::next_float(i, end))
+		for(float i = begin; i < end; i = glm::next_float(i))
 			result = glm::fastAsin(i);
 		const std::clock_t timestamp2 = std::clock();
-		for (float i = begin; i<end; i = glm::next_float(i, end))
+		for(float i = begin; i < end; i = glm::next_float(i))
 			result = glm::asin(i);
 		const std::clock_t timestamp3 = std::clock();
 		const std::clock_t time_fast = timestamp2 - timestamp1;
@@ -167,10 +186,10 @@ namespace fastAtan
 		const float end = glm::pi<float>();
 		float result = 0.f;
 		const std::clock_t timestamp1 = std::clock();
-		for (float i = begin; i<end; i = glm::next_float(i, end))
+		for(float i = begin; i < end; i = glm::next_float(i))
 			result = glm::fastAtan(i);
 		const std::clock_t timestamp2 = std::clock();
-		for (float i = begin; i<end; i = glm::next_float(i, end))
+		for(float i = begin; i < end; i = glm::next_float(i))
 			result = glm::atan(i);
 		const std::clock_t timestamp3 = std::clock();
 		const std::clock_t time_fast = timestamp2 - timestamp1;
@@ -182,10 +201,269 @@ namespace fastAtan
 	}
 }//namespace fastAtan
 
+namespace taylorCos
+{
+	glm::vec4 const AngleShift(0.0f, glm::pi<float>() * 0.5f, glm::pi<float>() * 1.0f, glm::pi<float>() * 1.5f);
+
+	template <typename T, glm::precision P, template <typename, glm::precision> class vecType>
+	GLM_FUNC_QUALIFIER vecType<T, P> taylorSeriesNewCos(vecType<T, P> const & x)
+	{
+		vecType<T, P> const Powed2(x * x);
+		vecType<T, P> const Powed4(Powed2 * Powed2);
+		vecType<T, P> const Powed6(Powed4 * Powed2);
+		vecType<T, P> const Powed8(Powed4 * Powed4);
+
+		return static_cast<T>(1)
+			- Powed2 * static_cast<T>(0.5)
+			+ Powed4 * static_cast<T>(0.04166666666666666666666666666667)
+			- Powed6 * static_cast<T>(0.00138888888888888888888888888889)
+			+ Powed8 * static_cast<T>(2.4801587301587301587301587301587e-5);
+	}
+
+	template <typename T, glm::precision P, template <typename, glm::precision> class vecType>
+	GLM_FUNC_QUALIFIER vecType<T, P> taylorSeriesNewCos6(vecType<T, P> const & x)
+	{
+		vecType<T, P> const Powed2(x * x);
+		vecType<T, P> const Powed4(Powed2 * Powed2);
+		vecType<T, P> const Powed6(Powed4 * Powed2);
+
+		return static_cast<T>(1)
+			- Powed2 * static_cast<T>(0.5)
+			+ Powed4 * static_cast<T>(0.04166666666666666666666666666667)
+			- Powed6 * static_cast<T>(0.00138888888888888888888888888889);
+	}
+
+	template <glm::precision P, template <typename, glm::precision> class vecType>
+	GLM_FUNC_QUALIFIER vecType<float, P> fastAbs(vecType<float, P> x)
+	{
+		int* Pointer = reinterpret_cast<int*>(&x[0]);
+		*(((int *) &Pointer[0]) + 1) &= 0x7fffffff;
+		*(((int *) &Pointer[1]) + 1) &= 0x7fffffff;
+		*(((int *) &Pointer[2]) + 1) &= 0x7fffffff;
+		*(((int *) &Pointer[3]) + 1) &= 0x7fffffff;
+		return x;
+	}
+
+	template <typename T, glm::precision P, template <typename, glm::precision> class vecType>
+	GLM_FUNC_QUALIFIER vecType<T, P> fastCosNew(vecType<T, P> const & x)
+	{
+		vecType<T, P> const Angle0_PI(fastAbs(fmod(x + glm::pi<T>(), glm::two_pi<T>()) - glm::pi<T>()));
+		return taylorSeriesNewCos6(x);
+/*
+		vecType<bool, P> const FirstQuarterPi(lessThanEqual(Angle0_PI, vecType<T, P>(glm::half_pi<T>())));
+
+		vecType<T, P> const RevertAngle(mix(vecType<T, P>(glm::pi<T>()), vecType<T, P>(0), FirstQuarterPi));
+		vecType<T, P> const ReturnSign(mix(vecType<T, P>(-1), vecType<T, P>(1), FirstQuarterPi));
+		vecType<T, P> const SectionAngle(RevertAngle - Angle0_PI);
+
+		return ReturnSign * taylorSeriesNewCos(SectionAngle);
+*/
+	}
+
+	int perf_fastCosNew(float Begin, float End, std::size_t Samples)
+	{
+		std::vector<glm::vec4> Results;
+		Results.resize(Samples);
+
+		float Steps = (End - Begin) / Samples;
+
+		std::clock_t const TimeStampBegin = std::clock();
+
+		for(std::size_t i = 0; i < Samples; ++i)
+			Results[i] = taylorCos::fastCosNew(AngleShift + glm::vec4(Begin + Steps * i));
+
+		std::clock_t const TimeStampEnd = std::clock();
+
+		std::printf("fastCosNew %ld clocks\n", TimeStampEnd - TimeStampBegin);
+
+		int Error = 0;
+		for(std::size_t i = 0; i < Samples; ++i)
+			Error += Results[i].x >= -1.0f && Results[i].x <= 1.0f ? 0 : 1;
+		return Error;
+	}
+
+	template <typename T, glm::precision P, template <typename, glm::precision> class vecType>
+	GLM_FUNC_QUALIFIER vecType<T, P> deterministic_fmod(vecType<T, P> const & x, T y)
+	{
+		return x - y * trunc(x / y);
+	}
+
+	template <typename T, glm::precision P, template <typename, glm::precision> class vecType>
+	GLM_FUNC_QUALIFIER vecType<T, P> fastCosDeterminisctic(vecType<T, P> const & x)
+	{
+		vecType<T, P> const Angle0_PI(abs(deterministic_fmod(x + glm::pi<T>(), glm::two_pi<T>()) - glm::pi<T>()));
+		vecType<bool, P> const FirstQuarterPi(lessThanEqual(Angle0_PI, vecType<T, P>(glm::half_pi<T>())));
+
+		vecType<T, P> const RevertAngle(mix(vecType<T, P>(glm::pi<T>()), vecType<T, P>(0), FirstQuarterPi));
+		vecType<T, P> const ReturnSign(mix(vecType<T, P>(-1), vecType<T, P>(1), FirstQuarterPi));
+		vecType<T, P> const SectionAngle(RevertAngle - Angle0_PI);
+
+		return ReturnSign * taylorSeriesNewCos(SectionAngle);
+	}
+
+	int perf_fastCosDeterminisctic(float Begin, float End, std::size_t Samples)
+	{
+		std::vector<glm::vec4> Results;
+		Results.resize(Samples);
+
+		float Steps = (End - Begin) / Samples;
+
+		std::clock_t const TimeStampBegin = std::clock();
+
+		for(std::size_t i = 0; i < Samples; ++i)
+			Results[i] = taylorCos::fastCosDeterminisctic(AngleShift + glm::vec4(Begin + Steps * i));
+
+		std::clock_t const TimeStampEnd = std::clock();
+
+		std::printf("fastCosDeterminisctic %ld clocks\n", TimeStampEnd - TimeStampBegin);
+
+		int Error = 0;
+		for(std::size_t i = 0; i < Samples; ++i)
+			Error += Results[i].x >= -1.0f && Results[i].x <= 1.0f ? 0 : 1;
+		return Error;
+	}
+
+	template <typename T, glm::precision P, template <typename, glm::precision> class vecType>
+	GLM_FUNC_QUALIFIER vecType<T, P> taylorSeriesRefCos(vecType<T, P> const & x)
+	{
+		return static_cast<T>(1)
+			- (x * x) / glm::factorial(static_cast<T>(2))
+			+ (x * x * x * x) / glm::factorial(static_cast<T>(4))
+			- (x * x * x * x * x * x) / glm::factorial(static_cast<T>(6))
+			+ (x * x * x * x * x * x * x * x) / glm::factorial(static_cast<T>(8));
+	}
+
+	template <typename T, glm::precision P, template <typename, glm::precision> class vecType>
+	GLM_FUNC_QUALIFIER vecType<T, P> fastRefCos(vecType<T, P> const & x)
+	{
+		vecType<T, P> const Angle0_PI(glm::abs(fmod(x + glm::pi<T>(), glm::two_pi<T>()) - glm::pi<T>()));
+//		return taylorSeriesRefCos(Angle0_PI);
+
+		vecType<bool, P> const FirstQuarterPi(lessThanEqual(Angle0_PI, vecType<T, P>(glm::half_pi<T>())));
+
+		vecType<T, P> const RevertAngle(mix(vecType<T, P>(glm::pi<T>()), vecType<T, P>(0), FirstQuarterPi));
+		vecType<T, P> const ReturnSign(mix(vecType<T, P>(-1), vecType<T, P>(1), FirstQuarterPi));
+		vecType<T, P> const SectionAngle(RevertAngle - Angle0_PI);
+
+		return ReturnSign * taylorSeriesRefCos(SectionAngle);
+	}
+
+	int perf_fastCosRef(float Begin, float End, std::size_t Samples)
+	{
+		std::vector<glm::vec4> Results;
+		Results.resize(Samples);
+
+		float Steps = (End - Begin) / Samples;
+
+		std::clock_t const TimeStampBegin = std::clock();
+
+		for(std::size_t i = 0; i < Samples; ++i)
+			Results[i] = taylorCos::fastRefCos(AngleShift + glm::vec4(Begin + Steps * i));
+
+		std::clock_t const TimeStampEnd = std::clock();
+
+		std::printf("fastCosRef %ld clocks\n", TimeStampEnd - TimeStampBegin);
+
+		int Error = 0;
+		for(std::size_t i = 0; i < Samples; ++i)
+			Error += Results[i].x >= -1.0f && Results[i].x <= 1.0f ? 0 : 1;
+		return Error;
+	}
+
+	int perf_fastCosOld(float Begin, float End, std::size_t Samples)
+	{
+		std::vector<glm::vec4> Results;
+		Results.resize(Samples);
+
+		float Steps = (End - Begin) / Samples;
+
+		std::clock_t const TimeStampBegin = std::clock();
+
+		for(std::size_t i = 0; i < Samples; ++i)
+			Results[i] = glm::fastCos(AngleShift + glm::vec4(Begin + Steps * i));
+
+		std::clock_t const TimeStampEnd = std::clock();
+
+		std::printf("fastCosOld %ld clocks\n", TimeStampEnd - TimeStampBegin);
+
+		int Error = 0;
+		for(std::size_t i = 0; i < Samples; ++i)
+			Error += Results[i].x >= -1.0f && Results[i].x <= 1.0f ? 0 : 1;
+		return Error;
+	}
+
+	int perf_cos(float Begin, float End, std::size_t Samples)
+	{
+		std::vector<glm::vec4> Results;
+		Results.resize(Samples);
+
+		float Steps = (End - Begin) / Samples;
+
+		std::clock_t const TimeStampBegin = std::clock();
+
+		for(std::size_t i = 0; i < Samples; ++i)
+			Results[i] = glm::cos(AngleShift + glm::vec4(Begin + Steps * i));
+
+		std::clock_t const TimeStampEnd = std::clock();
+
+		std::printf("cos %ld clocks\n", TimeStampEnd - TimeStampBegin);
+
+		int Error = 0;
+		for(std::size_t i = 0; i < Samples; ++i)
+			Error += Results[i].x >= -1.0f && Results[i].x <= 1.0f ? 0 : 1;
+		return Error;
+	}
+
+	int perf()
+	{
+		int Error = 0;
+
+		float const Begin = -glm::pi<float>();
+		float const End = glm::pi<float>();
+		std::size_t const Samples = 10000000;
+
+		Error += perf_cos(Begin, End, Samples);
+		Error += perf_fastCosOld(Begin, End, Samples);
+		Error += perf_fastCosRef(Begin, End, Samples);
+		//Error += perf_fastCosNew(Begin, End, Samples);
+		Error += perf_fastCosDeterminisctic(Begin, End, Samples);
+
+		return Error;
+	}
+
+	int test()
+	{
+		int Error = 0;
+
+		//for(float Angle = -4.0f * glm::pi<float>(); Angle < 4.0f * glm::pi<float>(); Angle += 0.1f)
+		//for(float Angle = -720.0f; Angle < 720.0f; Angle += 0.1f)
+		for(float Angle = 0.0f; Angle < 180.0f; Angle += 0.1f)
+		{
+			float const modAngle = std::fmod(glm::abs(Angle), 360.f);
+			assert(modAngle >= 0.0f && modAngle <= 360.f);
+			float const radAngle = glm::radians(modAngle);
+			float const Cos0 = std::cos(radAngle);
+
+			float const Cos1 = taylorCos::fastRefCos(glm::fvec1(radAngle)).x;
+			Error += glm::abs(Cos1 - Cos0) < 0.1f ? 0 : 1;
+
+			float const Cos2 = taylorCos::fastCosNew(glm::fvec1(radAngle)).x;
+			//Error += glm::abs(Cos2 - Cos0) < 0.1f ? 0 : 1;
+
+			assert(!Error);
+		}
+
+		return Error;
+	}
+}//namespace taylorCos
+
 int main()
 {
 	int Error(0);
 
+	Error += ::taylorCos::test();
+	Error += ::taylorCos::perf();
+
 #	ifdef NDEBUG
 		Error += ::fastCos::perf();
 		Error += ::fastSin::perf();