#include #define GLM_ENABLE_EXPERIMENTAL #include #include #include #include #include #include #include #include #include #include #include namespace fastCos { static int perf(bool NextFloat) { const float begin = -glm::pi(); const float end = glm::pi(); float result = 0.f; const std::clock_t timestamp1 = std::clock(); for(float i = begin; i < end; i = NextFloat ? glm::nextFloat(i) : i + 0.1f) result = glm::fastCos(i); const std::clock_t timestamp2 = std::clock(); for(float i = begin; i < end; i = NextFloat ? glm::nextFloat(i) : i + 0.1f) 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; std::printf("fastCos Time %d clocks\n", static_cast(time_fast)); std::printf("cos Time %d clocks\n", static_cast(time_default)); (void) result; // Silence set but not used warning return time_fast <= time_default ? 0 : 1; } }//namespace fastCos namespace fastSin { /* 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)); } */ static int perf(bool NextFloat) { const float begin = -glm::pi(); const float end = glm::pi(); float result = 0.f; const std::clock_t timestamp1 = std::clock(); for(float i = begin; i < end; i = NextFloat ? glm::nextFloat(i) : i + 0.1f) result = glm::fastSin(i); const std::clock_t timestamp2 = std::clock(); for(float i = begin; i < end; i = NextFloat ? glm::nextFloat(i) : i + 0.1f) 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; std::printf("fastSin Time %d clocks\n", static_cast(time_fast)); std::printf("sin Time %d clocks\n", static_cast(time_default)); (void) result; // Silence set but not used warning return time_fast <= time_default ? 0 : 1; } }//namespace fastSin namespace fastTan { static int perf(bool NextFloat) { const float begin = -glm::pi(); const float end = glm::pi(); float result = 0.f; const std::clock_t timestamp1 = std::clock(); for(float i = begin; i < end; i = NextFloat ? glm::nextFloat(i) : i + 0.1f) result = glm::fastTan(i); const std::clock_t timestamp2 = std::clock(); for (float i = begin; i < end; i = NextFloat ? glm::nextFloat(i) : i + 0.1f) 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; std::printf("fastTan Time %d clocks\n", static_cast(time_fast)); std::printf("tan Time %d clocks\n", static_cast(time_default)); (void) result; // Silence set but not used warning return time_fast <= time_default ? 0 : 1; } }//namespace fastTan namespace fastAcos { static int perf(bool NextFloat) { const float begin = -glm::pi(); const float end = glm::pi(); float result = 0.f; const std::clock_t timestamp1 = std::clock(); for(float i = begin; i < end; i = NextFloat ? glm::nextFloat(i) : i + 0.1f) result = glm::fastAcos(i); const std::clock_t timestamp2 = std::clock(); for(float i = begin; i < end; i = NextFloat ? glm::nextFloat(i) : i + 0.1f) 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(time_fast)); std::printf("acos Time %d clocks\n", static_cast(time_default)); (void) result; // Silence set but not used warning return time_fast <= time_default ? 0 : 1; } }//namespace fastAcos namespace fastAsin { static int perf(bool NextFloat) { const float begin = -glm::pi(); const float end = glm::pi(); float result = 0.f; const std::clock_t timestamp1 = std::clock(); for(float i = begin; i < end; i = NextFloat ? glm::nextFloat(i) : i + 0.1f) result = glm::fastAsin(i); const std::clock_t timestamp2 = std::clock(); for(float i = begin; i < end; i = NextFloat ? glm::nextFloat(i) : i + 0.1f) 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(time_fast)); std::printf("asin Time %d clocks\n", static_cast(time_default)); (void) result; // Silence set but not used warning return time_fast <= time_default ? 0 : 1; } }//namespace fastAsin namespace fastAtan { static int perf(bool NextFloat) { const float begin = -glm::pi(); const float end = glm::pi(); float result = 0.f; const std::clock_t timestamp1 = std::clock(); for(float i = begin; i < end; i = NextFloat ? glm::nextFloat(i) : i + 0.1f) result = glm::fastAtan(i); const std::clock_t timestamp2 = std::clock(); for(float i = begin; i < end; i = NextFloat ? glm::nextFloat(i) : i + 0.1f) 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(time_fast)); std::printf("atan Time %d clocks\n", static_cast(time_default)); (void) result; // Silence set but not used warning return time_fast <= time_default ? 0 : 1; } }//namespace fastAtan namespace taylorCos { using glm::qualifier; using glm::length_t; # if (GLM_COMPILER & GLM_COMPILER_CLANG) # pragma clang diagnostic push # pragma clang diagnostic ignored "-Wglobal-constructors" # endif glm::vec4 const AngleShift(0.0f, glm::half_pi(), glm::pi(), glm::three_over_two_pi()); # if (GLM_COMPILER & GLM_COMPILER_CLANG) # pragma clang diagnostic pop # endif template static GLM_FUNC_QUALIFIER glm::vec taylorSeriesNewCos(glm::vec const& x) { glm::vec const Powed2(x * x); glm::vec const Powed4(Powed2 * Powed2); glm::vec const Powed6(Powed4 * Powed2); glm::vec const Powed8(Powed4 * Powed4); return static_cast(1) - Powed2 * static_cast(0.5) + Powed4 * static_cast(0.04166666666666666666666666666667) - Powed6 * static_cast(0.00138888888888888888888888888889) + Powed8 * static_cast(2.4801587301587301587301587301587e-5); } /* template static GLM_FUNC_QUALIFIER glm::vec taylorSeriesNewCos6(glm::vec const& x) { glm::vec const Powed2(x * x); glm::vec const Powed4(Powed2 * Powed2); glm::vec const Powed6(Powed4 * Powed2); return static_cast(1) - Powed2 * static_cast(0.5) + Powed4 * static_cast(0.04166666666666666666666666666667) - Powed6 * static_cast(0.00138888888888888888888888888889); } */ /* template static GLM_FUNC_QUALIFIER glm::vec fastAbs(glm::vec x) { int* Pointer = reinterpret_cast(&x[0]); Pointer[0] &= 0x7fffffff; Pointer[1] &= 0x7fffffff; Pointer[2] &= 0x7fffffff; Pointer[3] &= 0x7fffffff; return x; } template static GLM_FUNC_QUALIFIER glm::vec fastCosNew(glm::vec const& x) { glm::vec const Angle0_PI(fastAbs(fmod(x + glm::pi(), glm::two_pi()) - glm::pi())); return taylorSeriesNewCos6(x); // vec const FirstQuarterPi(lessThanEqual(Angle0_PI, vec(glm::half_pi()))); // vec const RevertAngle(mix(vec(glm::pi()), vec(0), FirstQuarterPi)); // vec const ReturnSign(mix(vec(-1), vec(1), FirstQuarterPi)); // vec const SectionAngle(RevertAngle - Angle0_PI); // return ReturnSign * taylorSeriesNewCos(SectionAngle); } */ /* static int perf_fastCosNew(float Begin, float End, std::size_t Samples) { std::vector Results; Results.resize(Samples); float const Steps = (End - Begin) / static_cast(Samples); std::clock_t const TimeStampBegin = std::clock(); for(std::size_t i = 0; i < Samples; ++i) Results[i] = fastCosNew(AngleShift + glm::vec4(Begin + Steps * static_cast(i))); std::clock_t const TimeStampEnd = std::clock(); std::printf("fastCosNew %d clocks\n", static_cast(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 static GLM_FUNC_QUALIFIER glm::vec deterministic_fmod(glm::vec const& x, T y) { return x - y * trunc(x / y); } template static GLM_FUNC_QUALIFIER glm::vec fastCosDeterminisctic(glm::vec const& x) { glm::vec const Angle0_PI(abs(deterministic_fmod(x + glm::pi(), glm::two_pi()) - glm::pi())); glm::vec const FirstQuarterPi(lessThanEqual(Angle0_PI, glm::vec(glm::half_pi()))); glm::vec const RevertAngle(mix(glm::vec(glm::pi()), glm::vec(0), FirstQuarterPi)); glm::vec const ReturnSign(mix(glm::vec(-1), glm::vec(1), FirstQuarterPi)); glm::vec const SectionAngle(RevertAngle - Angle0_PI); return ReturnSign * taylorSeriesNewCos(SectionAngle); } static int perf_fastCosDeterminisctic(float Begin, float End, std::size_t Samples) { std::vector Results; Results.resize(Samples); float const Steps = (End - Begin) / static_cast(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 * static_cast(i))); std::clock_t const TimeStampEnd = std::clock(); std::printf("fastCosDeterminisctic %d clocks\n", static_cast(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 static GLM_FUNC_QUALIFIER glm::vec taylorSeriesRefCos(glm::vec const& x) { return static_cast(1) - (x * x) / glm::factorial(static_cast(2)) + (x * x * x * x) / glm::factorial(static_cast(4)) - (x * x * x * x * x * x) / glm::factorial(static_cast(6)) + (x * x * x * x * x * x * x * x) / glm::factorial(static_cast(8)); } template static GLM_FUNC_QUALIFIER glm::vec fastRefCos(glm::vec const& x) { glm::vec const Angle0_PI(glm::abs(fmod(x + glm::pi(), glm::two_pi()) - glm::pi())); // return taylorSeriesRefCos(Angle0_PI); glm::vec const FirstQuarterPi(lessThanEqual(Angle0_PI, glm::vec(glm::half_pi()))); glm::vec const RevertAngle(mix(glm::vec(glm::pi()), glm::vec(0), FirstQuarterPi)); glm::vec const ReturnSign(mix(glm::vec(-1), glm::vec(1), FirstQuarterPi)); glm::vec const SectionAngle(RevertAngle - Angle0_PI); return ReturnSign * taylorSeriesRefCos(SectionAngle); } static int perf_fastCosRef(float Begin, float End, std::size_t Samples) { std::vector Results; Results.resize(Samples); float const Steps = (End - Begin) / static_cast(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 * static_cast(i))); std::clock_t const TimeStampEnd = std::clock(); std::printf("fastCosRef %d clocks\n", static_cast(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; } static int perf_fastCosOld(float Begin, float End, std::size_t Samples) { std::vector Results; Results.resize(Samples); float const Steps = (End - Begin) / static_cast(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 * static_cast(i))); std::clock_t const TimeStampEnd = std::clock(); std::printf("fastCosOld %d clocks\n", static_cast(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; } static int perf_cos(float Begin, float End, std::size_t Samples) { std::vector Results; Results.resize(Samples); float const Steps = (End - Begin) / static_cast(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 * static_cast(i))); std::clock_t const TimeStampEnd = std::clock(); std::printf("cos %d clocks\n", static_cast(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; } static int perf(std::size_t const Samples) { int Error = 0; float const Begin = -glm::pi(); float const End = glm::pi(); 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; } static int test() { int Error = 0; //for(float Angle = -4.0f * glm::pi(); Angle < 4.0f * glm::pi(); 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 namespace taylor2 { # if (GLM_COMPILER & GLM_COMPILER_CLANG) # pragma clang diagnostic push # pragma clang diagnostic ignored "-Wglobal-constructors" # endif glm::vec4 const AngleShift(0.0f, glm::pi() * 0.5f, glm::pi() * 1.0f, glm::pi() * 1.5f); # if (GLM_COMPILER & GLM_COMPILER_CLANG) # pragma clang diagnostic pop # endif static 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); } static 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); } static 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); } static int perf_taylorCosA(float Begin, float End, std::size_t Samples) { std::vector Results; Results.resize(Samples); float const Steps = (End - Begin) / static_cast(Samples); std::clock_t const TimeStampBegin = std::clock(); for(std::size_t i = 0; i < Samples; ++i) Results[i] = taylorCosA(AngleShift.x + Begin + Steps * static_cast(i)); std::clock_t const TimeStampEnd = std::clock(); std::printf("taylorCosA %d clocks\n", static_cast(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; } static int perf_taylorCosB(float Begin, float End, std::size_t Samples) { std::vector Results; Results.resize(Samples); float const Steps = (End - Begin) / static_cast(Samples); std::clock_t const TimeStampBegin = std::clock(); for(std::size_t i = 0; i < Samples; ++i) Results[i] = taylorCosB(AngleShift.x + Begin + Steps * static_cast(i)); std::clock_t const TimeStampEnd = std::clock(); std::printf("taylorCosB %d clocks\n", static_cast(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; } static int perf_taylorCosC(float Begin, float End, std::size_t Samples) { std::vector Results; Results.resize(Samples); float const Steps = (End - Begin) / static_cast(Samples); std::clock_t const TimeStampBegin = std::clock(); for(std::size_t i = 0; i < Samples; ++i) Results[i] = taylorCosC(AngleShift.x + Begin + Steps * static_cast(i)); std::clock_t const TimeStampEnd = std::clock(); std::printf("taylorCosC %d clocks\n", static_cast(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; } static int perf(std::size_t Samples) { int Error = 0; float const Begin = -glm::pi(); float const End = glm::pi(); Error += perf_taylorCosA(Begin, End, Samples); Error += perf_taylorCosB(Begin, End, Samples); Error += perf_taylorCosC(Begin, End, Samples); return Error; } }//namespace taylor2 int main() { int Error(0); Error += ::taylor2::perf(1000); Error += ::taylorCos::test(); Error += ::taylorCos::perf(1000); ::fastCos::perf(false); ::fastSin::perf(false); ::fastTan::perf(false); ::fastAcos::perf(false); ::fastAsin::perf(false); ::fastAtan::perf(false); return Error; }