2016-11-12 13:19:22 +00:00
|
|
|
#define GLM_ENABLE_EXPERIMENTAL
|
2013-10-27 18:25:03 +00:00
|
|
|
#include <glm/gtc/type_precision.hpp>
|
2013-10-27 22:38:22 +00:00
|
|
|
#include <glm/gtx/fast_trigonometry.hpp>
|
2015-06-27 16:36:17 +00:00
|
|
|
#include <glm/gtx/integer.hpp>
|
|
|
|
#include <glm/gtx/common.hpp>
|
2014-09-15 23:35:29 +00:00
|
|
|
#include <glm/gtc/constants.hpp>
|
2014-11-16 00:13:01 +00:00
|
|
|
#include <glm/gtc/ulp.hpp>
|
2015-06-27 16:36:17 +00:00
|
|
|
#include <glm/gtc/vec1.hpp>
|
|
|
|
#include <glm/trigonometric.hpp>
|
|
|
|
#include <cmath>
|
2014-11-05 13:44:51 +00:00
|
|
|
#include <ctime>
|
|
|
|
#include <cstdio>
|
2015-06-27 16:36:17 +00:00
|
|
|
#include <vector>
|
2014-09-15 23:35:29 +00:00
|
|
|
|
2014-11-14 20:36:59 +00:00
|
|
|
namespace fastCos
|
2014-09-15 23:35:29 +00:00
|
|
|
{
|
2015-09-06 19:36:05 +00:00
|
|
|
int perf(bool NextFloat)
|
2014-11-05 13:44:51 +00:00
|
|
|
{
|
|
|
|
const float begin = -glm::pi<float>();
|
|
|
|
const float end = glm::pi<float>();
|
|
|
|
float result = 0.f;
|
2015-06-27 16:36:17 +00:00
|
|
|
|
2014-11-05 13:44:51 +00:00
|
|
|
const std::clock_t timestamp1 = std::clock();
|
2015-09-06 19:36:05 +00:00
|
|
|
for(float i = begin; i < end; i = NextFloat ? glm::next_float(i) : i += 0.1f)
|
2014-11-05 13:44:51 +00:00
|
|
|
result = glm::fastCos(i);
|
2015-06-27 16:36:17 +00:00
|
|
|
|
2014-11-05 13:44:51 +00:00
|
|
|
const std::clock_t timestamp2 = std::clock();
|
2015-09-06 19:36:05 +00:00
|
|
|
for(float i = begin; i < end; i = NextFloat ? glm::next_float(i) : i += 0.1f)
|
2014-11-05 13:44:51 +00:00
|
|
|
result = glm::cos(i);
|
2015-06-27 16:36:17 +00:00
|
|
|
|
2014-11-05 13:44:51 +00:00
|
|
|
const std::clock_t timestamp3 = std::clock();
|
2014-11-05 14:24:47 +00:00
|
|
|
const std::clock_t time_fast = timestamp2 - timestamp1;
|
|
|
|
const std::clock_t time_default = timestamp3 - timestamp2;
|
|
|
|
std::printf("fastCos Time %d clocks\n", static_cast<unsigned int>(time_fast));
|
|
|
|
std::printf("cos Time %d clocks\n", static_cast<unsigned int>(time_default));
|
2014-11-14 23:05:52 +00:00
|
|
|
|
2015-09-06 19:52:22 +00:00
|
|
|
return time_fast <= time_default ? 0 : 1;
|
2014-11-05 13:44:51 +00:00
|
|
|
}
|
2014-11-14 20:36:59 +00:00
|
|
|
}//namespace fastCos
|
2014-09-15 23:35:29 +00:00
|
|
|
|
2014-11-14 20:36:59 +00:00
|
|
|
namespace fastSin
|
|
|
|
{
|
2015-02-15 11:38:23 +00:00
|
|
|
/*
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2015-09-06 19:36:05 +00:00
|
|
|
int perf(bool NextFloat)
|
2014-09-15 23:35:29 +00:00
|
|
|
{
|
2014-11-05 14:02:17 +00:00
|
|
|
const float begin = -glm::pi<float>();
|
|
|
|
const float end = glm::pi<float>();
|
|
|
|
float result = 0.f;
|
2015-06-27 16:36:17 +00:00
|
|
|
|
2014-11-05 14:02:17 +00:00
|
|
|
const std::clock_t timestamp1 = std::clock();
|
2015-09-06 19:36:05 +00:00
|
|
|
for(float i = begin; i < end; i = NextFloat ? glm::next_float(i) : i += 0.1f)
|
2014-11-05 14:02:17 +00:00
|
|
|
result = glm::fastSin(i);
|
2015-06-27 16:36:17 +00:00
|
|
|
|
2014-11-05 14:02:17 +00:00
|
|
|
const std::clock_t timestamp2 = std::clock();
|
2015-09-06 19:36:05 +00:00
|
|
|
for(float i = begin; i < end; i = NextFloat ? glm::next_float(i) : i += 0.1f)
|
2014-11-05 14:02:17 +00:00
|
|
|
result = glm::sin(i);
|
2015-06-27 16:36:17 +00:00
|
|
|
|
2014-11-05 14:02:17 +00:00
|
|
|
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("fastSin Time %d clocks\n", static_cast<unsigned int>(time_fast));
|
|
|
|
std::printf("sin Time %d clocks\n", static_cast<unsigned int>(time_default));
|
2014-11-14 23:05:52 +00:00
|
|
|
|
2015-09-06 19:52:22 +00:00
|
|
|
return time_fast <= time_default ? 0 : 1;
|
2014-09-15 23:35:29 +00:00
|
|
|
}
|
2014-11-14 20:36:59 +00:00
|
|
|
}//namespace fastSin
|
2014-09-15 23:35:29 +00:00
|
|
|
|
2014-11-14 20:36:59 +00:00
|
|
|
namespace fastTan
|
|
|
|
{
|
2015-09-06 19:36:05 +00:00
|
|
|
int perf(bool NextFloat)
|
2014-11-05 14:02:26 +00:00
|
|
|
{
|
|
|
|
const float begin = -glm::pi<float>();
|
|
|
|
const float end = glm::pi<float>();
|
|
|
|
float result = 0.f;
|
2015-06-27 16:36:17 +00:00
|
|
|
|
2014-11-05 14:02:26 +00:00
|
|
|
const std::clock_t timestamp1 = std::clock();
|
2015-09-06 19:36:05 +00:00
|
|
|
for(float i = begin; i < end; i = NextFloat ? glm::next_float(i) : i += 0.1f)
|
2014-11-05 14:02:26 +00:00
|
|
|
result = glm::fastTan(i);
|
2015-06-27 16:36:17 +00:00
|
|
|
|
2014-11-05 14:02:26 +00:00
|
|
|
const std::clock_t timestamp2 = std::clock();
|
2015-09-06 19:36:05 +00:00
|
|
|
for (float i = begin; i < end; i = NextFloat ? glm::next_float(i) : i += 0.1f)
|
2014-11-05 14:02:26 +00:00
|
|
|
result = glm::tan(i);
|
2015-06-27 16:36:17 +00:00
|
|
|
|
2014-11-05 14:02:26 +00:00
|
|
|
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("fastTan Time %d clocks\n", static_cast<unsigned int>(time_fast));
|
|
|
|
std::printf("tan Time %d clocks\n", static_cast<unsigned int>(time_default));
|
2014-11-14 23:05:52 +00:00
|
|
|
|
2015-09-06 19:52:22 +00:00
|
|
|
return time_fast <= time_default ? 0 : 1;
|
2014-11-05 14:02:26 +00:00
|
|
|
}
|
2014-11-14 20:36:59 +00:00
|
|
|
}//namespace fastTan
|
2014-11-05 14:02:26 +00:00
|
|
|
|
2014-11-14 20:36:59 +00:00
|
|
|
namespace fastAcos
|
|
|
|
{
|
2015-09-06 19:36:05 +00:00
|
|
|
int perf(bool NextFloat)
|
2014-11-05 14:05:33 +00:00
|
|
|
{
|
|
|
|
const float begin = -glm::pi<float>();
|
|
|
|
const float end = glm::pi<float>();
|
|
|
|
float result = 0.f;
|
2015-06-27 16:36:17 +00:00
|
|
|
|
2014-11-05 14:05:33 +00:00
|
|
|
const std::clock_t timestamp1 = std::clock();
|
2015-09-06 19:36:05 +00:00
|
|
|
for(float i = begin; i < end; i = NextFloat ? glm::next_float(i) : i += 0.1f)
|
2014-11-05 14:05:33 +00:00
|
|
|
result = glm::fastAcos(i);
|
2015-06-27 16:36:17 +00:00
|
|
|
|
2014-11-05 14:05:33 +00:00
|
|
|
const std::clock_t timestamp2 = std::clock();
|
2015-09-06 19:36:05 +00:00
|
|
|
for(float i = begin; i < end; i = NextFloat ? glm::next_float(i) : i += 0.1f)
|
2014-11-05 14:05:33 +00:00
|
|
|
result = glm::acos(i);
|
2015-06-27 16:36:17 +00:00
|
|
|
|
2014-11-05 14:05:33 +00:00
|
|
|
const std::clock_t timestamp3 = std::clock();
|
|
|
|
const std::clock_t time_fast = timestamp2 - timestamp1;
|
|
|
|
const std::clock_t time_default = timestamp3 - timestamp2;
|
2015-06-27 16:36:17 +00:00
|
|
|
|
2014-11-05 14:05:33 +00:00
|
|
|
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));
|
2014-11-14 23:05:52 +00:00
|
|
|
|
2015-09-06 19:52:22 +00:00
|
|
|
return time_fast <= time_default ? 0 : 1;
|
2014-11-05 14:05:33 +00:00
|
|
|
}
|
2014-11-14 20:36:59 +00:00
|
|
|
}//namespace fastAcos
|
2014-11-05 14:05:33 +00:00
|
|
|
|
2014-11-14 20:36:59 +00:00
|
|
|
namespace fastAsin
|
|
|
|
{
|
2015-09-06 19:36:05 +00:00
|
|
|
int perf(bool NextFloat)
|
2014-11-05 14:06:07 +00:00
|
|
|
{
|
|
|
|
const float begin = -glm::pi<float>();
|
|
|
|
const float end = glm::pi<float>();
|
|
|
|
float result = 0.f;
|
|
|
|
const std::clock_t timestamp1 = std::clock();
|
2015-09-06 19:36:05 +00:00
|
|
|
for(float i = begin; i < end; i = NextFloat ? glm::next_float(i) : i += 0.1f)
|
2014-11-05 14:06:07 +00:00
|
|
|
result = glm::fastAsin(i);
|
|
|
|
const std::clock_t timestamp2 = std::clock();
|
2015-09-06 19:36:05 +00:00
|
|
|
for(float i = begin; i < end; i = NextFloat ? glm::next_float(i) : i += 0.1f)
|
2014-11-05 14:06:07 +00:00
|
|
|
result = glm::asin(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("fastAsin Time %d clocks\n", static_cast<unsigned int>(time_fast));
|
|
|
|
std::printf("asin Time %d clocks\n", static_cast<unsigned int>(time_default));
|
2014-11-14 23:05:52 +00:00
|
|
|
|
2015-09-06 19:52:22 +00:00
|
|
|
return time_fast <= time_default ? 0 : 1;
|
2014-11-05 14:06:07 +00:00
|
|
|
}
|
2014-11-14 20:36:59 +00:00
|
|
|
}//namespace fastAsin
|
2014-11-05 14:06:07 +00:00
|
|
|
|
2014-11-14 20:36:59 +00:00
|
|
|
namespace fastAtan
|
|
|
|
{
|
2015-09-06 19:36:05 +00:00
|
|
|
int perf(bool NextFloat)
|
2014-11-05 14:20:53 +00:00
|
|
|
{
|
|
|
|
const float begin = -glm::pi<float>();
|
|
|
|
const float end = glm::pi<float>();
|
|
|
|
float result = 0.f;
|
|
|
|
const std::clock_t timestamp1 = std::clock();
|
2015-09-06 19:36:05 +00:00
|
|
|
for(float i = begin; i < end; i = NextFloat ? glm::next_float(i) : i += 0.1f)
|
2014-11-05 14:20:53 +00:00
|
|
|
result = glm::fastAtan(i);
|
|
|
|
const std::clock_t timestamp2 = std::clock();
|
2015-09-06 19:36:05 +00:00
|
|
|
for(float i = begin; i < end; i = NextFloat ? glm::next_float(i) : i += 0.1f)
|
2014-11-05 14:20:53 +00:00
|
|
|
result = glm::atan(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("fastAtan Time %d clocks\n", static_cast<unsigned int>(time_fast));
|
|
|
|
std::printf("atan Time %d clocks\n", static_cast<unsigned int>(time_default));
|
2014-11-14 23:05:52 +00:00
|
|
|
|
2015-09-06 19:52:22 +00:00
|
|
|
return time_fast <= time_default ? 0 : 1;
|
2014-11-05 14:20:53 +00:00
|
|
|
}
|
2014-11-14 20:36:59 +00:00
|
|
|
}//namespace fastAtan
|
2013-10-27 18:25:03 +00:00
|
|
|
|
2015-06-27 16:36:17 +00:00
|
|
|
namespace taylorCos
|
|
|
|
{
|
2017-08-15 19:23:47 +00:00
|
|
|
using glm::qualifier;
|
2017-08-15 23:22:50 +00:00
|
|
|
using glm::length_t;
|
2016-12-29 00:59:01 +00:00
|
|
|
|
2017-08-07 15:28:56 +00:00
|
|
|
glm::vec4 const AngleShift(0.0f, glm::half_pi<float>(), glm::pi<float>(), glm::three_over_two_pi<float>());
|
2015-06-27 16:36:17 +00:00
|
|
|
|
2017-08-15 23:22:50 +00:00
|
|
|
template<length_t L, typename T, qualifier Q>
|
|
|
|
GLM_FUNC_QUALIFIER glm::vec<L, T, Q> taylorSeriesNewCos(glm::vec<L, T, Q> const& x)
|
2015-06-27 16:36:17 +00:00
|
|
|
{
|
2017-08-15 23:22:50 +00:00
|
|
|
glm::vec<L, T, Q> const Powed2(x * x);
|
|
|
|
glm::vec<L, T, Q> const Powed4(Powed2 * Powed2);
|
|
|
|
glm::vec<L, T, Q> const Powed6(Powed4 * Powed2);
|
|
|
|
glm::vec<L, T, Q> const Powed8(Powed4 * Powed4);
|
2015-06-27 16:36:17 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-08-15 23:22:50 +00:00
|
|
|
template<length_t L, typename T, qualifier Q>
|
|
|
|
GLM_FUNC_QUALIFIER glm::vec<L, T, Q> taylorSeriesNewCos6(glm::vec<L, T, Q> const& x)
|
2015-06-27 16:36:17 +00:00
|
|
|
{
|
2017-08-15 23:22:50 +00:00
|
|
|
glm::vec<L, T, Q> const Powed2(x * x);
|
|
|
|
glm::vec<L, T, Q> const Powed4(Powed2 * Powed2);
|
|
|
|
glm::vec<L, T, Q> const Powed6(Powed4 * Powed2);
|
2015-06-27 16:36:17 +00:00
|
|
|
|
|
|
|
return static_cast<T>(1)
|
|
|
|
- Powed2 * static_cast<T>(0.5)
|
|
|
|
+ Powed4 * static_cast<T>(0.04166666666666666666666666666667)
|
|
|
|
- Powed6 * static_cast<T>(0.00138888888888888888888888888889);
|
|
|
|
}
|
|
|
|
|
2017-08-15 23:22:50 +00:00
|
|
|
template<glm::length_t L, qualifier Q>
|
|
|
|
GLM_FUNC_QUALIFIER glm::vec<L, float, Q> fastAbs(glm::vec<L, float, Q> x)
|
2015-06-27 16:36:17 +00:00
|
|
|
{
|
|
|
|
int* Pointer = reinterpret_cast<int*>(&x[0]);
|
2015-07-23 23:47:54 +00:00
|
|
|
Pointer[0] &= 0x7fffffff;
|
|
|
|
Pointer[1] &= 0x7fffffff;
|
|
|
|
Pointer[2] &= 0x7fffffff;
|
|
|
|
Pointer[3] &= 0x7fffffff;
|
2015-06-27 16:36:17 +00:00
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2017-08-15 23:22:50 +00:00
|
|
|
template<glm::length_t L, typename T, qualifier Q>
|
|
|
|
GLM_FUNC_QUALIFIER glm::vec<L, T, Q> fastCosNew(glm::vec<L, T, Q> const& x)
|
2015-06-27 16:36:17 +00:00
|
|
|
{
|
2017-08-15 23:22:50 +00:00
|
|
|
glm::vec<L, T, Q> const Angle0_PI(fastAbs(fmod(x + glm::pi<T>(), glm::two_pi<T>()) - glm::pi<T>()));
|
2015-06-27 16:36:17 +00:00
|
|
|
return taylorSeriesNewCos6(x);
|
|
|
|
/*
|
2017-08-15 23:22:50 +00:00
|
|
|
vec<L, bool, Q> const FirstQuarterPi(lessThanEqual(Angle0_PI, vec<L, T, Q>(glm::half_pi<T>())));
|
2015-06-27 16:36:17 +00:00
|
|
|
|
2017-08-15 23:22:50 +00:00
|
|
|
vec<L, T, Q> const RevertAngle(mix(vec<L, T, Q>(glm::pi<T>()), vec<L, T, Q>(0), FirstQuarterPi));
|
|
|
|
vec<L, T, Q> const ReturnSign(mix(vec<L, T, Q>(-1), vec<L, T, Q>(1), FirstQuarterPi));
|
|
|
|
vec<L, T, Q> const SectionAngle(RevertAngle - Angle0_PI);
|
2015-06-27 16:36:17 +00:00
|
|
|
|
|
|
|
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)
|
2015-07-23 23:32:51 +00:00
|
|
|
Results[i] = fastCosNew(AngleShift + glm::vec4(Begin + Steps * i));
|
2015-06-27 16:36:17 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-08-15 23:22:50 +00:00
|
|
|
template<length_t L, typename T, qualifier Q>
|
|
|
|
GLM_FUNC_QUALIFIER glm::vec<L, T, Q> deterministic_fmod(glm::vec<L, T, Q> const& x, T y)
|
2015-06-27 16:36:17 +00:00
|
|
|
{
|
|
|
|
return x - y * trunc(x / y);
|
|
|
|
}
|
|
|
|
|
2017-08-15 23:22:50 +00:00
|
|
|
template<length_t L, typename T, qualifier Q>
|
|
|
|
GLM_FUNC_QUALIFIER glm::vec<L, T, Q> fastCosDeterminisctic(glm::vec<L, T, Q> const& x)
|
2015-06-27 16:36:17 +00:00
|
|
|
{
|
2017-08-15 23:22:50 +00:00
|
|
|
glm::vec<L, T, Q> const Angle0_PI(abs(deterministic_fmod(x + glm::pi<T>(), glm::two_pi<T>()) - glm::pi<T>()));
|
|
|
|
glm::vec<L, bool, Q> const FirstQuarterPi(lessThanEqual(Angle0_PI, glm::vec<L, T, Q>(glm::half_pi<T>())));
|
2015-06-27 16:36:17 +00:00
|
|
|
|
2017-08-15 23:22:50 +00:00
|
|
|
glm::vec<L, T, Q> const RevertAngle(mix(glm::vec<L, T, Q>(glm::pi<T>()), glm::vec<L, T, Q>(0), FirstQuarterPi));
|
|
|
|
glm::vec<L, T, Q> const ReturnSign(mix(glm::vec<L, T, Q>(-1), glm::vec<L, T, Q>(1), FirstQuarterPi));
|
|
|
|
glm::vec<L, T, Q> const SectionAngle(RevertAngle - Angle0_PI);
|
2015-06-27 16:36:17 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-08-15 23:22:50 +00:00
|
|
|
template<length_t L, typename T, qualifier Q>
|
|
|
|
GLM_FUNC_QUALIFIER glm::vec<L, T, Q> taylorSeriesRefCos(glm::vec<L, T, Q> const& x)
|
2015-06-27 16:36:17 +00:00
|
|
|
{
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2017-08-15 23:22:50 +00:00
|
|
|
template<length_t L, typename T, qualifier Q>
|
|
|
|
GLM_FUNC_QUALIFIER glm::vec<L, T, Q> fastRefCos(glm::vec<L, T, Q> const& x)
|
2015-06-27 16:36:17 +00:00
|
|
|
{
|
2017-08-15 23:22:50 +00:00
|
|
|
glm::vec<L, T, Q> const Angle0_PI(glm::abs(fmod(x + glm::pi<T>(), glm::two_pi<T>()) - glm::pi<T>()));
|
2015-06-27 16:36:17 +00:00
|
|
|
// return taylorSeriesRefCos(Angle0_PI);
|
|
|
|
|
2017-08-15 23:22:50 +00:00
|
|
|
glm::vec<L, bool, Q> const FirstQuarterPi(lessThanEqual(Angle0_PI, glm::vec<L, T, Q>(glm::half_pi<T>())));
|
2015-06-27 16:36:17 +00:00
|
|
|
|
2017-08-15 23:22:50 +00:00
|
|
|
glm::vec<L, T, Q> const RevertAngle(mix(glm::vec<L, T, Q>(glm::pi<T>()), glm::vec<L, T, Q>(0), FirstQuarterPi));
|
|
|
|
glm::vec<L, T, Q> const ReturnSign(mix(glm::vec<L, T, Q>(-1), glm::vec<L, T, Q>(1), FirstQuarterPi));
|
|
|
|
glm::vec<L, T, Q> const SectionAngle(RevertAngle - Angle0_PI);
|
2015-06-27 16:36:17 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-09-06 19:36:05 +00:00
|
|
|
int perf(std::size_t const Samples)
|
2015-06-27 16:36:17 +00:00
|
|
|
{
|
|
|
|
int Error = 0;
|
|
|
|
|
|
|
|
float const Begin = -glm::pi<float>();
|
|
|
|
float const End = glm::pi<float>();
|
|
|
|
|
|
|
|
Error += perf_cos(Begin, End, Samples);
|
|
|
|
Error += perf_fastCosOld(Begin, End, Samples);
|
|
|
|
Error += perf_fastCosRef(Begin, End, Samples);
|
2015-07-23 23:47:54 +00:00
|
|
|
//Error += perf_fastCosNew(Begin, End, Samples);
|
2015-06-27 16:36:17 +00:00
|
|
|
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;
|
|
|
|
|
2017-08-18 18:09:46 +00:00
|
|
|
//float const Cos2 = taylorCos::fastCosNew(glm::fvec1(radAngle)).x;
|
|
|
|
//Error += glm::abs(Cos2 - Cos0) < 0.1f ? 0 : 1;
|
2015-06-27 16:36:17 +00:00
|
|
|
|
|
|
|
assert(!Error);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Error;
|
|
|
|
}
|
|
|
|
}//namespace taylorCos
|
|
|
|
|
2016-11-20 23:06:05 +00:00
|
|
|
namespace taylor2
|
|
|
|
{
|
|
|
|
glm::vec4 const AngleShift(0.0f, glm::pi<float>() * 0.5f, glm::pi<float>() * 1.0f, glm::pi<float>() * 1.5f);
|
|
|
|
|
|
|
|
float taylorCosA(float x)
|
|
|
|
{
|
|
|
|
return 1.f
|
|
|
|
- (x * x) * (1.f / 2.f)
|
|
|
|
+ (x * x * x * x) * (1.f / 24.f)
|
|
|
|
- (x * x * x * x * x * x) * (1.f / 720.f)
|
|
|
|
+ (x * x * x * x * x * x * x * x) * (1.f / 40320.f);
|
|
|
|
}
|
|
|
|
|
|
|
|
float taylorCosB(float x)
|
|
|
|
{
|
|
|
|
return 1.f
|
|
|
|
- (x * x) * (1.f / 2.f)
|
|
|
|
+ (x * x * x * x) * (1.f / 24.f)
|
|
|
|
- (x * x * x * x * x * x) * (1.f / 720.f)
|
|
|
|
+ (x * x * x * x * x * x * x * x) * (1.f / 40320.f);
|
|
|
|
}
|
|
|
|
|
|
|
|
float taylorCosC(float x)
|
|
|
|
{
|
|
|
|
return 1.f
|
|
|
|
- (x * x) * (1.f / 2.f)
|
|
|
|
+ ((x * x) * (x * x)) * (1.f / 24.f)
|
|
|
|
- (((x * x) * (x * x)) * (x * x)) * (1.f / 720.f)
|
|
|
|
+ (((x * x) * (x * x)) * ((x * x) * (x * x))) * (1.f / 40320.f);
|
|
|
|
}
|
|
|
|
|
|
|
|
int perf_taylorCosA(float Begin, float End, std::size_t Samples)
|
|
|
|
{
|
|
|
|
std::vector<float> 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] = taylorCosA(AngleShift.x + Begin + Steps * i);
|
|
|
|
|
|
|
|
std::clock_t const TimeStampEnd = std::clock();
|
|
|
|
|
|
|
|
std::printf("taylorCosA %ld clocks\n", TimeStampEnd - TimeStampBegin);
|
|
|
|
|
|
|
|
int Error = 0;
|
|
|
|
for(std::size_t i = 0; i < Samples; ++i)
|
|
|
|
Error += Results[i] >= -1.0f && Results[i] <= 1.0f ? 0 : 1;
|
|
|
|
return Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
int perf_taylorCosB(float Begin, float End, std::size_t Samples)
|
|
|
|
{
|
|
|
|
std::vector<float> 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] = taylorCosB(AngleShift.x + Begin + Steps * i);
|
|
|
|
|
|
|
|
std::clock_t const TimeStampEnd = std::clock();
|
|
|
|
|
|
|
|
std::printf("taylorCosB %ld clocks\n", TimeStampEnd - TimeStampBegin);
|
|
|
|
|
|
|
|
int Error = 0;
|
|
|
|
for(std::size_t i = 0; i < Samples; ++i)
|
|
|
|
Error += Results[i] >= -1.0f && Results[i] <= 1.0f ? 0 : 1;
|
|
|
|
return Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
int perf_taylorCosC(float Begin, float End, std::size_t Samples)
|
|
|
|
{
|
|
|
|
std::vector<float> 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] = taylorCosC(AngleShift.x + Begin + Steps * i);
|
|
|
|
|
|
|
|
std::clock_t const TimeStampEnd = std::clock();
|
|
|
|
|
|
|
|
std::printf("taylorCosC %ld clocks\n", TimeStampEnd - TimeStampBegin);
|
|
|
|
|
|
|
|
int Error = 0;
|
|
|
|
for(std::size_t i = 0; i < Samples; ++i)
|
|
|
|
Error += Results[i] >= -1.0f && Results[i] <= 1.0f ? 0 : 1;
|
|
|
|
return Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
int perf(std::size_t Samples)
|
|
|
|
{
|
|
|
|
int Error = 0;
|
|
|
|
|
|
|
|
float const Begin = -glm::pi<float>();
|
|
|
|
float const End = glm::pi<float>();
|
|
|
|
|
|
|
|
Error += perf_taylorCosA(Begin, End, Samples);
|
|
|
|
Error += perf_taylorCosB(Begin, End, Samples);
|
|
|
|
Error += perf_taylorCosC(Begin, End, Samples);
|
|
|
|
|
|
|
|
return Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
}//namespace taylor2
|
|
|
|
|
2013-10-27 18:25:03 +00:00
|
|
|
int main()
|
|
|
|
{
|
|
|
|
int Error(0);
|
|
|
|
|
2016-11-20 23:06:05 +00:00
|
|
|
Error += ::taylor2::perf(1000);
|
2015-06-27 16:36:17 +00:00
|
|
|
Error += ::taylorCos::test();
|
2015-09-06 19:36:05 +00:00
|
|
|
Error += ::taylorCos::perf(1000);
|
2015-06-27 16:36:17 +00:00
|
|
|
|
2014-11-29 19:46:58 +00:00
|
|
|
# ifdef NDEBUG
|
2015-09-06 19:52:22 +00:00
|
|
|
::fastCos::perf(false);
|
|
|
|
::fastSin::perf(false);
|
|
|
|
::fastTan::perf(false);
|
|
|
|
::fastAcos::perf(false);
|
|
|
|
::fastAsin::perf(false);
|
|
|
|
::fastAtan::perf(false);
|
2014-11-29 19:46:58 +00:00
|
|
|
# endif//NDEBUG
|
2014-09-15 23:35:29 +00:00
|
|
|
|
2013-10-27 18:25:03 +00:00
|
|
|
return Error;
|
|
|
|
}
|