gtx_fast_trigonometry.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. int perf()
  61. {
  62. const float begin = -glm::pi<float>();
  63. const float end = glm::pi<float>();
  64. float result = 0.f;
  65. const std::clock_t timestamp1 = std::clock();
  66. for (float i = begin; i<end; i = glm::next_float(i, end))
  67. result = glm::fastSin(i);
  68. const std::clock_t timestamp2 = std::clock();
  69. for (float i = begin; i<end; i = glm::next_float(i, end))
  70. result = glm::sin(i);
  71. const std::clock_t timestamp3 = std::clock();
  72. const std::clock_t time_fast = timestamp2 - timestamp1;
  73. const std::clock_t time_default = timestamp3 - timestamp2;
  74. std::printf("fastSin Time %d clocks\n", static_cast<unsigned int>(time_fast));
  75. std::printf("sin Time %d clocks\n", static_cast<unsigned int>(time_default));
  76. return time_fast < time_default ? 0 : 1;
  77. }
  78. }//namespace fastSin
  79. namespace fastTan
  80. {
  81. int perf()
  82. {
  83. const float begin = -glm::pi<float>();
  84. const float end = glm::pi<float>();
  85. float result = 0.f;
  86. const std::clock_t timestamp1 = std::clock();
  87. for (float i = begin; i<end; i = glm::next_float(i, end))
  88. result = glm::fastTan(i);
  89. const std::clock_t timestamp2 = std::clock();
  90. for (float i = begin; i<end; i = glm::next_float(i, end))
  91. result = glm::tan(i);
  92. const std::clock_t timestamp3 = std::clock();
  93. const std::clock_t time_fast = timestamp2 - timestamp1;
  94. const std::clock_t time_default = timestamp3 - timestamp2;
  95. std::printf("fastTan Time %d clocks\n", static_cast<unsigned int>(time_fast));
  96. std::printf("tan Time %d clocks\n", static_cast<unsigned int>(time_default));
  97. return time_fast < time_default ? 0 : 1;
  98. }
  99. }//namespace fastTan
  100. namespace fastAcos
  101. {
  102. int perf()
  103. {
  104. const float begin = -glm::pi<float>();
  105. const float end = glm::pi<float>();
  106. float result = 0.f;
  107. const std::clock_t timestamp1 = std::clock();
  108. for (float i = begin; i<end; i = glm::next_float(i, end))
  109. result = glm::fastAcos(i);
  110. const std::clock_t timestamp2 = std::clock();
  111. for (float i = begin; i<end; i = glm::next_float(i, end))
  112. result = glm::acos(i);
  113. const std::clock_t timestamp3 = std::clock();
  114. const std::clock_t time_fast = timestamp2 - timestamp1;
  115. const std::clock_t time_default = timestamp3 - timestamp2;
  116. std::printf("fastAcos Time %d clocks\n", static_cast<unsigned int>(time_fast));
  117. std::printf("acos Time %d clocks\n", static_cast<unsigned int>(time_default));
  118. return time_fast < time_default ? 0 : 1;
  119. }
  120. }//namespace fastAcos
  121. namespace fastAsin
  122. {
  123. int perf()
  124. {
  125. const float begin = -glm::pi<float>();
  126. const float end = glm::pi<float>();
  127. float result = 0.f;
  128. const std::clock_t timestamp1 = std::clock();
  129. for (float i = begin; i<end; i = glm::next_float(i, end))
  130. result = glm::fastAsin(i);
  131. const std::clock_t timestamp2 = std::clock();
  132. for (float i = begin; i<end; i = glm::next_float(i, end))
  133. result = glm::asin(i);
  134. const std::clock_t timestamp3 = std::clock();
  135. const std::clock_t time_fast = timestamp2 - timestamp1;
  136. const std::clock_t time_default = timestamp3 - timestamp2;
  137. std::printf("fastAsin Time %d clocks\n", static_cast<unsigned int>(time_fast));
  138. std::printf("asin Time %d clocks\n", static_cast<unsigned int>(time_default));
  139. return time_fast < time_default ? 0 : 1;
  140. }
  141. }//namespace fastAsin
  142. namespace fastAtan
  143. {
  144. int perf()
  145. {
  146. const float begin = -glm::pi<float>();
  147. const float end = glm::pi<float>();
  148. float result = 0.f;
  149. const std::clock_t timestamp1 = std::clock();
  150. for (float i = begin; i<end; i = glm::next_float(i, end))
  151. result = glm::fastAtan(i);
  152. const std::clock_t timestamp2 = std::clock();
  153. for (float i = begin; i<end; i = glm::next_float(i, end))
  154. result = glm::atan(i);
  155. const std::clock_t timestamp3 = std::clock();
  156. const std::clock_t time_fast = timestamp2 - timestamp1;
  157. const std::clock_t time_default = timestamp3 - timestamp2;
  158. std::printf("fastAtan Time %d clocks\n", static_cast<unsigned int>(time_fast));
  159. std::printf("atan Time %d clocks\n", static_cast<unsigned int>(time_default));
  160. return time_fast < time_default ? 0 : 1;
  161. }
  162. }//namespace fastAtan
  163. int main()
  164. {
  165. int Error(0);
  166. # ifdef NDEBUG
  167. Error += ::fastCos::perf();
  168. Error += ::fastSin::perf();
  169. Error += ::fastTan::perf();
  170. Error += ::fastAcos::perf();
  171. Error += ::fastAsin::perf();
  172. Error += ::fastAtan::perf();
  173. # endif//NDEBUG
  174. return Error;
  175. }