glm/test/gtx/gtx_fast_trigonometry.cpp

467 lines
15 KiB
C++
Raw Normal View History

2014-11-25 23:27:12 +00:00
///////////////////////////////////////////////////////////////////////////////////
/// OpenGL Mathematics (glm.g-truc.net)
///
2014-12-19 22:54:25 +00:00
/// Copyright (c) 2005 - 2015 G-Truc Creation (www.g-truc.net)
2014-11-25 23:27:12 +00:00
/// Permission is hereby granted, free of charge, to any person obtaining a copy
/// of this software and associated documentation files (the "Software"), to deal
/// in the Software without restriction, including without limitation the rights
/// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
/// copies of the Software, and to permit persons to whom the Software is
/// furnished to do so, subject to the following conditions:
///
/// The above copyright notice and this permission notice shall be included in
/// all copies or substantial portions of the Software.
///
/// Restrictions:
/// By making use of the Software for military purposes, you choose to make
/// a Bunny unhappy.
///
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
/// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
/// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
/// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
/// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
/// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
/// THE SOFTWARE.
///
/// @file test/gtx/gtx_fast_trigonometry.cpp
/// @date 2013-10-25 / 2014-11-25
/// @author Christophe Riccio
///////////////////////////////////////////////////////////////////////////////////
#include <glm/gtc/type_precision.hpp>
#include <glm/gtx/fast_trigonometry.hpp>
#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>
#include <glm/gtc/vec1.hpp>
#include <glm/trigonometric.hpp>
#include <cmath>
2014-11-05 13:44:51 +00:00
#include <ctime>
#include <cstdio>
#include <vector>
2014-09-15 23:35:29 +00:00
namespace fastCos
2014-09-15 23:35:29 +00:00
{
2014-11-05 13:44:51 +00:00
int perf()
{
const float begin = -glm::pi<float>();
const float end = glm::pi<float>();
float result = 0.f;
2014-11-05 13:44:51 +00:00
const std::clock_t timestamp1 = std::clock();
for(float i = begin; i < end; i = glm::next_float(i))
2014-11-05 13:44:51 +00:00
result = glm::fastCos(i);
2014-11-05 13:44:51 +00:00
const std::clock_t timestamp2 = std::clock();
for(float i = begin; i < end; i = glm::next_float(i))
2014-11-05 13:44:51 +00:00
result = glm::cos(i);
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-05 14:24:47 +00:00
return time_fast < time_default ? 0 : 1;
2014-11-05 13:44:51 +00:00
}
}//namespace fastCos
2014-09-15 23:35:29 +00:00
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));
}
*/
2014-11-05 14:02:17 +00:00
int perf()
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;
2014-11-05 14:02:17 +00:00
const std::clock_t timestamp1 = std::clock();
for(float i = begin; i < end; i = glm::next_float(i))
2014-11-05 14:02:17 +00:00
result = glm::fastSin(i);
2014-11-05 14:02:17 +00:00
const std::clock_t timestamp2 = std::clock();
for(float i = begin; i < end; i = glm::next_float(i))
2014-11-05 14:02:17 +00:00
result = glm::sin(i);
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-05 14:02:17 +00:00
return time_fast < time_default ? 0 : 1;
2014-09-15 23:35:29 +00:00
}
}//namespace fastSin
2014-09-15 23:35:29 +00:00
namespace fastTan
{
2014-11-05 14:02:26 +00:00
int perf()
{
const float begin = -glm::pi<float>();
const float end = glm::pi<float>();
float result = 0.f;
2014-11-05 14:02:26 +00:00
const std::clock_t timestamp1 = std::clock();
for(float i = begin; i < end; i = glm::next_float(i))
2014-11-05 14:02:26 +00:00
result = glm::fastTan(i);
2014-11-05 14:02:26 +00:00
const std::clock_t timestamp2 = std::clock();
for (float i = begin; i < end; i = glm::next_float(i))
2014-11-05 14:02:26 +00:00
result = glm::tan(i);
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-05 14:02:26 +00:00
return time_fast < time_default ? 0 : 1;
}
}//namespace fastTan
2014-11-05 14:02:26 +00:00
namespace fastAcos
{
2014-11-05 14:05:33 +00:00
int perf()
{
const float begin = -glm::pi<float>();
const float end = glm::pi<float>();
float result = 0.f;
2014-11-05 14:05:33 +00:00
const std::clock_t timestamp1 = std::clock();
for(float i = begin; i < end; i = glm::next_float(i))
2014-11-05 14:05:33 +00:00
result = glm::fastAcos(i);
2014-11-05 14:05:33 +00:00
const std::clock_t timestamp2 = std::clock();
for(float i = begin; i < end; i = glm::next_float(i))
2014-11-05 14:05:33 +00:00
result = glm::acos(i);
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;
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-05 14:05:33 +00:00
return time_fast < time_default ? 0 : 1;
}
}//namespace fastAcos
2014-11-05 14:05:33 +00:00
namespace fastAsin
{
2014-11-05 14:06:07 +00:00
int perf()
{
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))
2014-11-05 14:06:07 +00:00
result = glm::fastAsin(i);
const std::clock_t timestamp2 = std::clock();
for(float i = begin; i < end; i = glm::next_float(i))
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-05 14:06:07 +00:00
return time_fast < time_default ? 0 : 1;
}
}//namespace fastAsin
2014-11-05 14:06:07 +00:00
namespace fastAtan
{
2014-11-05 14:20:53 +00:00
int perf()
{
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))
2014-11-05 14:20:53 +00:00
result = glm::fastAtan(i);
const std::clock_t timestamp2 = std::clock();
for(float i = begin; i < end; i = glm::next_float(i))
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-05 14:20:53 +00:00
return time_fast < time_default ? 0 : 1;
}
}//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 <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(glm::abs(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();
2014-11-29 19:46:58 +00:00
# ifdef NDEBUG
Error += ::fastCos::perf();
Error += ::fastSin::perf();
Error += ::fastTan::perf();
Error += ::fastAcos::perf();
Error += ::fastAsin::perf();
Error += ::fastAtan::perf();
2014-11-29 19:46:58 +00:00
# endif//NDEBUG
2014-09-15 23:35:29 +00:00
return Error;
}