gtx_fast_trigonometry.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. ///////////////////////////////////////////////////////////////////////////////////
  2. /// OpenGL Mathematics (glm.g-truc.net)
  3. ///
  4. /// Copyright (c) 2005 - 2015 G-Truc Creation (www.g-truc.net)
  5. /// Permission is hereby granted, free of charge, to any person obtaining a copy
  6. /// of this software and associated documentation files (the "Software"), to deal
  7. /// in the Software without restriction, including without limitation the rights
  8. /// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. /// copies of the Software, and to permit persons to whom the Software is
  10. /// furnished to do so, subject to the following conditions:
  11. ///
  12. /// The above copyright notice and this permission notice shall be included in
  13. /// all copies or substantial portions of the Software.
  14. ///
  15. /// Restrictions:
  16. /// By making use of the Software for military purposes, you choose to make
  17. /// a Bunny unhappy.
  18. ///
  19. /// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. /// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. /// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. /// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. /// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. /// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. /// THE SOFTWARE.
  26. ///
  27. /// @file test/gtx/gtx_fast_trigonometry.cpp
  28. /// @date 2013-10-25 / 2014-11-25
  29. /// @author Christophe Riccio
  30. ///////////////////////////////////////////////////////////////////////////////////
  31. #include <glm/gtc/type_precision.hpp>
  32. #include <glm/gtx/fast_trigonometry.hpp>
  33. #include <glm/gtc/constants.hpp>
  34. #include <glm/gtc/ulp.hpp>
  35. #include <ctime>
  36. #include <cstdio>
  37. namespace fastCos
  38. {
  39. int perf()
  40. {
  41. const float begin = -glm::pi<float>();
  42. const float end = glm::pi<float>();
  43. float result = 0.f;
  44. const std::clock_t timestamp1 = std::clock();
  45. for(float i=begin; i<end; i = glm::next_float(i, end))
  46. result = glm::fastCos(i);
  47. const std::clock_t timestamp2 = std::clock();
  48. for(float i=begin; i<end; i = glm::next_float(i, end))
  49. result = glm::cos(i);
  50. const std::clock_t timestamp3 = std::clock();
  51. const std::clock_t time_fast = timestamp2 - timestamp1;
  52. const std::clock_t time_default = timestamp3 - timestamp2;
  53. std::printf("fastCos Time %d clocks\n", static_cast<unsigned int>(time_fast));
  54. std::printf("cos Time %d clocks\n", static_cast<unsigned int>(time_default));
  55. return time_fast < time_default ? 0 : 1;
  56. }
  57. }//namespace fastCos
  58. namespace fastSin
  59. {
  60. /*
  61. float sin(float x) {
  62. float temp;
  63. temp = (x + M_PI) / ((2 * M_PI) - M_PI);
  64. return limited_sin((x + M_PI) - ((2 * M_PI) - M_PI) * temp));
  65. }
  66. */
  67. int perf()
  68. {
  69. const float begin = -glm::pi<float>();
  70. const float end = glm::pi<float>();
  71. float result = 0.f;
  72. const std::clock_t timestamp1 = std::clock();
  73. for (float i = begin; i<end; i = glm::next_float(i, end))
  74. result = glm::fastSin(i);
  75. const std::clock_t timestamp2 = std::clock();
  76. for (float i = begin; i<end; i = glm::next_float(i, end))
  77. result = glm::sin(i);
  78. const std::clock_t timestamp3 = std::clock();
  79. const std::clock_t time_fast = timestamp2 - timestamp1;
  80. const std::clock_t time_default = timestamp3 - timestamp2;
  81. std::printf("fastSin Time %d clocks\n", static_cast<unsigned int>(time_fast));
  82. std::printf("sin Time %d clocks\n", static_cast<unsigned int>(time_default));
  83. return time_fast < time_default ? 0 : 1;
  84. }
  85. }//namespace fastSin
  86. namespace fastTan
  87. {
  88. int perf()
  89. {
  90. const float begin = -glm::pi<float>();
  91. const float end = glm::pi<float>();
  92. float result = 0.f;
  93. const std::clock_t timestamp1 = std::clock();
  94. for (float i = begin; i<end; i = glm::next_float(i, end))
  95. result = glm::fastTan(i);
  96. const std::clock_t timestamp2 = std::clock();
  97. for (float i = begin; i<end; i = glm::next_float(i, end))
  98. result = glm::tan(i);
  99. const std::clock_t timestamp3 = std::clock();
  100. const std::clock_t time_fast = timestamp2 - timestamp1;
  101. const std::clock_t time_default = timestamp3 - timestamp2;
  102. std::printf("fastTan Time %d clocks\n", static_cast<unsigned int>(time_fast));
  103. std::printf("tan Time %d clocks\n", static_cast<unsigned int>(time_default));
  104. return time_fast < time_default ? 0 : 1;
  105. }
  106. }//namespace fastTan
  107. namespace fastAcos
  108. {
  109. int perf()
  110. {
  111. const float begin = -glm::pi<float>();
  112. const float end = glm::pi<float>();
  113. float result = 0.f;
  114. const std::clock_t timestamp1 = std::clock();
  115. for (float i = begin; i<end; i = glm::next_float(i, end))
  116. result = glm::fastAcos(i);
  117. const std::clock_t timestamp2 = std::clock();
  118. for (float i = begin; i<end; i = glm::next_float(i, end))
  119. result = glm::acos(i);
  120. const std::clock_t timestamp3 = std::clock();
  121. const std::clock_t time_fast = timestamp2 - timestamp1;
  122. const std::clock_t time_default = timestamp3 - timestamp2;
  123. std::printf("fastAcos Time %d clocks\n", static_cast<unsigned int>(time_fast));
  124. std::printf("acos Time %d clocks\n", static_cast<unsigned int>(time_default));
  125. return time_fast < time_default ? 0 : 1;
  126. }
  127. }//namespace fastAcos
  128. namespace fastAsin
  129. {
  130. int perf()
  131. {
  132. const float begin = -glm::pi<float>();
  133. const float end = glm::pi<float>();
  134. float result = 0.f;
  135. const std::clock_t timestamp1 = std::clock();
  136. for (float i = begin; i<end; i = glm::next_float(i, end))
  137. result = glm::fastAsin(i);
  138. const std::clock_t timestamp2 = std::clock();
  139. for (float i = begin; i<end; i = glm::next_float(i, end))
  140. result = glm::asin(i);
  141. const std::clock_t timestamp3 = std::clock();
  142. const std::clock_t time_fast = timestamp2 - timestamp1;
  143. const std::clock_t time_default = timestamp3 - timestamp2;
  144. std::printf("fastAsin Time %d clocks\n", static_cast<unsigned int>(time_fast));
  145. std::printf("asin Time %d clocks\n", static_cast<unsigned int>(time_default));
  146. return time_fast < time_default ? 0 : 1;
  147. }
  148. }//namespace fastAsin
  149. namespace fastAtan
  150. {
  151. int perf()
  152. {
  153. const float begin = -glm::pi<float>();
  154. const float end = glm::pi<float>();
  155. float result = 0.f;
  156. const std::clock_t timestamp1 = std::clock();
  157. for (float i = begin; i<end; i = glm::next_float(i, end))
  158. result = glm::fastAtan(i);
  159. const std::clock_t timestamp2 = std::clock();
  160. for (float i = begin; i<end; i = glm::next_float(i, end))
  161. result = glm::atan(i);
  162. const std::clock_t timestamp3 = std::clock();
  163. const std::clock_t time_fast = timestamp2 - timestamp1;
  164. const std::clock_t time_default = timestamp3 - timestamp2;
  165. std::printf("fastAtan Time %d clocks\n", static_cast<unsigned int>(time_fast));
  166. std::printf("atan Time %d clocks\n", static_cast<unsigned int>(time_default));
  167. return time_fast < time_default ? 0 : 1;
  168. }
  169. }//namespace fastAtan
  170. int main()
  171. {
  172. int Error(0);
  173. # ifdef NDEBUG
  174. Error += ::fastCos::perf();
  175. Error += ::fastSin::perf();
  176. Error += ::fastTan::perf();
  177. Error += ::fastAcos::perf();
  178. Error += ::fastAsin::perf();
  179. Error += ::fastAtan::perf();
  180. # endif//NDEBUG
  181. return Error;
  182. }